C PROGRAMME#5:BUILD A TIC TAC TOE GAME

  • #include<stdio.h>
  • #include<stdio.h>
  • char square[10]={0,1,2,3,4,5,6,7,8,9};
  • int choice, player;
  • int checkforwin();
  • void gameboard(char mark);
  • void displayboard();
  • void main()
  • {
  • int player;char mark;
  • for(i=1;i<-2;i–)
  • {
  • player=(player%2)?1;2;
  • printf(“Player%d enter a number:”,player);
  • scanf(“%d”,&choice);
  • mark=(player==1)?X;O;
  • gameboard(mark);
  • player++;
  • }
  • }
  • int checkforwin()
  • {
  • if (square[1]==square[2]&&sqaure[2]==square[3])
  • {
  • return 1;
  • }
  • else if(square[4]==square[5]&&sqaure[5]==sqaure[6]
  • return 1;
  • else if(square[7]==sqaure[8]&&square[8]==sqaure[9])
  • return 1;
  • else if(square[2]==square[5]&&square[5]==square==[8])
  • return 1:
  • else if(square[1]==square[4]&&square[4]==square[7])
  • return 1;
  • else if(square[3]==square[6]&&square[6]==square[9])
  • return 1;
  • else if(square[1]==square[5]&&square[5]==square[8])
  • return 1;
  • else if(square[3]==square[5]&&square[5]==square[7])
  • return 1;
  • else if (square[1]!=’1’&&square[2]!=’2’&&square[3]!=’3’&&square[4]!=’4’&&square[5]!=’5’&&square[6]!=’6’&&square[7]!=’7’&&square[8]!=’8’&&square[9]!=’9′)
  • return 0;
  • else
  • return -1;
  • }
  • system(“cls”); //please see if you are running on compilers like turbo c use clrscr()in this function to clear output important for getting only the output we want.//
  • void displayboard()
  • {
  • printf(“Player 1(X) PLAYER2(O)”);
  • printf(” %c|%c|%c \n”,square[1],square[2],square[3];
  • printf(“%c|%c|%c|\n”square[4],square[5],square[6];
  • printf(“%c|%c|%c|\n”square[7],square[8],square[9];
  • }
  • void gameboard(char mark)
  • {
  • if(choice==1)
  • square[1]==mark;
  • else if(choice==2)
  • square{2]=mark;
  • else if(choice==3)
  • square[3]=mark;
  • else if(choice==4)
  • square[4]=mark;
  • else if(choice==5)
  • square[5]=mark
  • else if(choice==6)
  • square[6]=mark;
  • else if(choice==7)
  • square[7]=mark;
  • else if(choice==8)
  • square[8]=mark;
  • else if(choice==9)
  • square[9]=mark;
  • else
  • player- -;
  • getch();
  • }