Pr1.docx

  • Uploaded by: Ram Kumar
  • 0
  • 0
  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Pr1.docx as PDF for free.

More details

  • Words: 886
  • Pages: 11
PRACTICAL 1 Write a program to implement Tic-Tac-Toe game in C. THEORY: Tic-tac-toe (American English), noughts and crosses (British English) or Xs and Os, is a paper-and-pencil game for two players, Xand O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. The following example game is won by the first player, X:

Players soon discover that the best play from both parties leads to a draw. Hence, tic-tac-toe is most often played by young children. Because of the simplicity of tic-tac-toe, it is often used as a pedagogical tool for teaching the concepts of good and the branch of artificial intelligence that deals with the searching of game trees. It is straightforward to write a computer program to play tic-tac-toe perfectly or to enumerate the 765 essentially different positions or the 26,830 possible games upto rotations and reflections on this space. PROGRAM:#include<stdio.h> #include #include<stdlib.h> char data[9]={'1','2','3','4','5','6','7','8','9'}; char symbol[2]; void matrix() { printf("\n\n"); printf("\t\t\t %c | %c | %c \n", data[0], data[1], data[2]); printf("\t\t\t----+----+----\n"); printf("\t\t\t %c | %c | %c \n", data[3], data[4], data[5]); printf("\t\t\t----+----+---\n"); printf("\t\t\t %c | %c | %c \n", data[6], data[7], data[8]); } //function for player vs. player choice void pl_vs_pl(){ int count = 0,winner=0; int index, sign, player, flag, i,k; while (count < 9) { flag = 0; matrix();

if (count % 2 == 0) { sign = 'X'; player = 1; } else { sign = 'O'; player = 2; } printf("Move for player%d(1-9):", player); scanf("%d", &index); if (index < 1 || index > 9) { printf("Allowed index is 1 to 9!!\n"); continue; } if (data[index - 1] == 'X' || data[index - 1] == 'O') { printf("Position Already occupied!!\n"); sleep(1); continue; } data[index - 1] = sign; count++; for (i = 0; i < 9; i++) { if (i % 3 == 0) flag = 0; if (data[i] == sign) flag++; if (flag == 3) { winner = 1; goto win; } } flag = 0; for (i = 0; i < 3; i++) { for (k = i; k <= i + 6; k = k + 3) { if (data[k] == sign) flag++; } if (flag == 3) { winner = 1; goto win; } flag = 0; } if ((data[0] == sign && data[4] == sign && data[8] == sign) || (data[2] == sign && data[4] == sign && data[6] == sign)) { winner = 1; goto win;

} } win: printf("\n\n"); printf("\t\t\t %c | %c | %c \n", data[0], data[1], data[2]); printf("\t\t\t----+----+----\n"); printf("\t\t\t %c | %c | %c \n", data[3], data[4], data[5]); printf("\t\t\t----+----+---\n"); printf("\t\t\t %c | %c | %c \n\n", data[6], data[7], data[8]); if (winner) { printf("Player %d is the winner. Congrats!!\n", player); } else { printf("Match draw.. Best of luck for both\n"); } return 0; } //function for player vs. computer void pl_vs_co(){ char s[2]={'X','O'}; int i=-1,pos; int p[2]={1,2},player,flag=0; srand(time(NULL)); player=p[rand()%2]; printf("%d\n",player); if(player==1) { if(flag==0){ printf("\nPlayer Select X/O:",player); scanf("%c",&symbol[0]); } } else{ srand(time(NULL)); symbol[1]=s[rand()%2]; printf("%c\n",symbol[1]); flag=1; } if(symbol[1]=='X') symbol[0]='O'; else if(symbol[1]=='O') symbol[0]='X'; else if(symbol[0]=='X') symbol[1]='O'; else

symbol[1]='X'; do{ player=((player%2)==1)?1:2; if(player==1){ printf("Player Enter Position (1 to 9): "); scanf("%d",&pos); if(pos < 1 || pos > 9){ printf("Plz choose from 1 to 9 only...\n"); continue; } } else{ printf("Computer's Turn\n"); pos=Condition(); } if(data[pos-1]=='X' || data[pos-1]=='O') { printf("Already Filled\n"); continue; } data[pos-1]=symbol[player-1]; matrix(); i=Win(); if(i==1){ if(player==1) printf("\n Player Win the game\n"); else printf("\n Computer Win the game\n"); break; } else if(i==0){ printf("\n==> Game Draw<==\n"); break; } player++; }while(counti()==0); } int Condition(){ int p1[4]={1,3,7,9},p2[2]={1,3},comp,i,pos; if(data[4]=='5' ) return 5; else{ pos=check(&symbol[1]); if(pos!=-1) return pos; else{

pos=check(&symbol[0]); if(pos!=-1) return pos; else{ if(data[0]=='1'|| data[2]=='3'|| data[6]=='7' || data[8]=='9'){ do{ srand(time(NULL)); i=rand()%4; comp=p1[i]; }while(data[comp-1]=='X' || data[comp-1]=='O'); return comp; } else{ for(i=0;i<=8;i++){ if(data[i]=='X' || data[i]=='O'){ return i+1; break; } } } } } } } int check(char s[0]) { if(((data[0]==s[0] && data[1]==s[0]) || (data[5]==s[0] && data[8]==s[0]) || (data[4]==s[0] &&data[6]==s[0] ) )&& data[2]=='3') return 3; else if(((data[0]==s[0] && data[2]==s[0]) || (data[4]==s[0] && data[7]==s[0])) &&data[1]=='2') return 2; else if(((data[1]==s[0] && data[2]==s[0] )|| (data[3]==s[0] && data[6]==s[0]) || (data[4]==s[0] &&data[8]==s[0]))&& data[0]=='1') return 1; else if(((data[3]==s[0] && data[4]==s[0]) || (data[2]==s[0] && data[8]==s[0])) &&data[5]=='6') return 6; else if(((data[4]==s[0] && data[5]==s[0]) || (data[0]==s[0] && data[6]==s[0])) &&data[3]=='4') return 4; else if(((data[3]==s[0] && data[5]==s[0]) || (data[1]==s[0] && data[7]==s[0] )|| (data[0]==s[0] &&data[8]==s[0]) ||(data[6]==s[0]&&data[2]))&& data[4]=='5') return 5; else if(((data[6]==s[0] && data[7]==s[0]) || (data[5]==s[0] && data[2]==s[0]) || (data[0]==s[0] &&data[4]==s[0]))&& data[8]=='9') return 9; else if(((data[7]==s[0] && data[8]==s[0]) || (data[3]==s[0] && data[0]==s[0]) || (data[4]==s[0] &&data[2]==s[0]))&& data[6]=='7')

return 7; else if(((data[6]==s[0] && data[8]==s[0]) || (data[1]==s[0] && data[4]==s[0]))&& data[7]=='8') return 8; else return -1; } int counti(){ if(data[0]=='1'|| data[1]=='2' || data[2]=='3'|| data[3]=='4'|| data[4]=='5'|| data[5]=='6'||data[6]=='7'|| data[7]=='8'|| data[8]=='9' ) return 0; else return 1; } int Win(){ if((data[0]==data[1] && data[1]==data[2])|| (data[3]==data[4] && data[4]==data[5])|| (data[6]==data[7] && data[7]==data[8])|| (data[0]==data[4] && data[4]==data[8])|| (data[2]==data[4] && data[4]==data[6])|| (data[0]==data[3] && data[3]==data[6])|| (data[2]==data[5] && data[5]==data[8])|| (data[1]==data[4] && data[4]==data[7])) return 1; else if(data[0]!='1'&& data[1]!='2' && data[2]!='3'&& data[3]!='4'&& data[4]!='5'&& data[5]! ='6'&& data[6]!='7'&& data[7]!='8'&& data[8]!='9' ) return 0; else return -1; } int main() { int ch; matrix(); printf("1.Player vs Player \n2.Player vs Computer"); printf("\n Select the game:"); scanf("%d",&ch); if(ch==1){ printf("\nStart the game\n"); pl_vs_pl(); } else if(ch==2){ pl_vs_co(); } else{ printf("invalid choice");

} return 0; }

OUTPUT:-

1) Player vs player: draw situation:

2) Player vs player: win situation:

3) Player vs computer: win situation:

4) Player vs computer : draw situation:

More Documents from "Ram Kumar"

2013-04-05-dt.pdf
December 2019 3
Dsp Unit 3
October 2019 7
Edunit1&2(1).pdf
December 2019 3
458.pdf
May 2020 4
Pr1.docx
April 2020 2
14
May 2020 21