Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

-Create a two-player, human vs. human, tic tac toe game. (plz use c lunguage) -U

ID: 1846985 • Letter: #

Question

-Create a two-player, human vs. human, tic tac toe game. (plz use c lunguage)


-Use the source code file tictactoe.c for your solution. The file is included with this assignment. You are required to code all the functions listed. You may add any extra functions needed, but you must use all the functions described in the comments.


-Write a function that displays a start screen when the program begins running

Start Screen Example

Main menu function


b. Display a main menu, giving the user a choice to play a game or quit the program. This must be written as a separate function.


Main Menu Example



Display Board function


Create a display_board function. This displays the game board in its current state. An example game board at the start of a new game is shown below.




Display Board Example


In the example, the upper tic tac toe board is used to indicate the number of each board position. The empty board indicates a new game has started. Player X is prompted to enter a position on the game board. Suppose Player X enters a 4, then the updated game board would appear as follows:




Board updated after Player X enters 4


Other functions






Return 0 if game is won by XPLAYER

Return 1 if game is won by OPLAYER

Return 2 if game is a tie

Return 3 if game is still going on




You may write any other helper functions you need.



Preprocessor Macros

The following are defined in the given program shell. Use these macro definitions to make your code readable.


#define XPLAYER 0

#define OPLAYER 1

#define OPEN 32


#define TIEGAME 2

#define LIVEGAME 3



this is what i got so far,



#include <stdio.h>

#define XPLAYER 0

#define OPLAYER 1

#define OPEN 32


#define TIEGAME 2

#define LIVEGAME 3




int main(void)

{

int i = 0;

int player = 0;

int go = 0;

int row = 0;

int column = 0;

int line = 0;

int winner = 0;

char board[3][3];

  

  

for( i = 0; i<9 && winner==0; i++)

  

{

printf(" ");

printf(" %c | %c | %c ", board[0][0], board[0][1], board[0][2]);

printf("----------- ");

printf(" %c | %c | %c ", board[1][0], board[1][1], board[1][2]);

printf("----------- ");

printf(" %c | %c | %c ", board[2][0], board[2][1], board[2][2]);

  

player = i%2 + 1;

  

do

{

printf(" Player %d, please enter the number of the square "

   "where you want to place your %c: ", player,(player==1)?'X':'O');

scanf("%d", &go);

  

row = --go/3;

column = go%3;

}

while(go<0 || go>9 || board[row][column]>'9');

  

board[row][column] = (player == 1) ? 'X' : 'O';

  

if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||

   (board[0][2] == board[1][1] && board[0][2] == board[2][0]))

winner = player;

else

  

for(line = 0; line <= 2; line ++)

  

if((board[line][0] == board[line][1] && board[line][0] == board[line][2])||

   (board[0][line] == board[1][line] && board[0][line] == board[2][line]))

winner = player;

  

}

printf(" ");

printf(" %c | %c | %c ", board[0][0], board[0][1], board[0][2]);

printf("----------- ");

printf(" %c | %c | %c ", board[1][0], board[1][1], board[1][2]);

printf("----------- ");

printf(" %c | %c | %c ", board[2][0], board[2][1], board[2][2]);

  

if(winner == 0)

printf(" Draw ");

else

printf(" player %d, YOU WON! ", winner);


}

Explanation / Answer

#include <stdio.h>

#define XPLAYER 0

#define OPLAYER 1

#define OPEN 32


#define TIEGAME 2

#define LIVEGAME 3


char board[3][3];


void display_board()

{

printf("------------ ");

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

printf("| %c ",board[i][j]);

}

printf("| ");

printf("------------- ");

}

}


void initialize_board()

{

for(int i=0; i< 3; i++)

{

for(int j=0; j<3; j++)

{

board[i][j]= ((48 + i*3 + j )) ;

}

}

display_board();

}

int mainmenu()

{

int ch;

printf("Press 1 if you want to play Press 0 if you want to quit Enter your choice : ");

scanf("%d",&ch);

if(ch==0)

return 1;

else

initialize_board();

return 0;

}

int startscreen()

{

return mainmenu();

}




int get_player_move()

{

int move;

scanf("%d",&move);

return move;

}

int check_valid_move(int move)

{

if(move > 8 || move < 0)

return 0;

int y = move%3;

int x=move/3;

if((board[x][y]=='O') || (board[x][y]=='X'))

return 0;

return 1;

}

int check_win(char ch)

{

if(board[0][0] == ch && board[0][1] ==ch && board[0][2] == ch)

return 1;

if(board[0][0] == ch && board[1][1] ==ch && board[2][2] == ch)

return 1;

if(board[1][0] == ch && board[1][1] ==ch && board[1][2] == ch)

return 1;

if(board[2][0] == ch && board[2][1] ==ch && board[2][2] == ch)

return 1;

if(board[0][1] == ch && board[1][1] ==ch && board[2][1] == ch)

return 1;

if(board[0][2] == ch && board[1][2] ==ch && board[2][2] == ch)

return 1;

if(board[0][0] == ch && board[1][0] ==ch && board[2][0] == ch)

return 1;

if(board[0][2] == ch && board[1][1] ==ch && board[2][0] == ch)

return 1;

return 0;

}



int check_tied()

{

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

if(board[i][j]!='X' || board[i][j]!='O')

return 0;

}

}

return 1;

}

int check_winner()

{

if(check_win('X')== 1)

return XPLAYER;

if(check_win('O')== 1)

return OPLAYER;

if(check_tied() == 1)

return TIEGAME;

return LIVEGAME;

}




int main()

{   

while(1)

{

if(startscreen()==1)

break;

  

for(int i=0;i<9; i++)

{

if(i%2==0)

{

printf(" ");

printf("Player X Turn ");

int move = get_player_move();

while(!check_valid_move(move))

{printf("INVALID MOVE... PLEASE ENTER AGAIN ");

move=get_player_move();

}

int y=move%3;

int x=move/3;

board[x][y]='X';

display_board();

}

else

{

printf(" ");

printf("Player O Turn ");

int move = get_player_move();

while(!check_valid_move(move))

{

printf("INVALID MOVE... PLEASE ENTER AGAIN ");

move=get_player_move();

}

int y=move%3;

int x=move/3;

board[x][y]='O';

display_board();

}

if(check_winner()== XPLAYER)

{

printf("PLAYER X WON... ");

break;

}

if(check_winner()== OPLAYER)

{

printf("PLAYER O WON... ");

break;

}

if(check_winner()== TIEGAME)

{

printf("GAME TIED... ");

break;

}

if(check_winner()== LIVEGAME)

{

printf("GAME IS ON.... ");

}

}

}

}