Question: Tic-Tac-Toe, Part 3. Save a copy of your program so far and modify it to include multiple game modes; the two-player mode youve written so

Tic-Tac-Toe, Part 3. Save a copy of your program so far and modify it to include multiple game modes; the two-player mode youve written so far and a single-player mode. The user should select which game mode they want to play from a menu at the start of the program. The single-player mode should control the computer player that assigns the letter O to the board. The computer should make moves by picking the most top left place among all the available places in the board. Use the following function prototypes: void playSinglePlayer(); void playTwoPlayer(); void makeComputerMove(char board[][3]);

Here is my current code. Please edit it to answer this question, thanks!

#include

int validMove(char board[][3], int row, int column) { int valid=1;

if((row<1 || row>3)|| (column<1|| column>3)) valid=0; else if(board[row-1][column-1]=='O'||board[row-1][column-1]=='X') valid=0; else if(board[row-1][column-1]!=' ') valid=0;

return valid;

} /** Function that takes 3x3 board and returns 1 if game won by any one of two players otherwise returns 1 */ int checkWinner(char board[][3]) { if(('X'==board[0][0]&&'X'==board[0][1]&&'X'==board[0][2]) || ('X'==board[1][0]&&'X'==board[1][1]&&'X'==board[1][2]) || ('X'==board[2][0]&&'X'==board[2][1]&&'X'==board[2][2]) || ('X'==board[0][0]&&'X'==board[1][1]&&'X'==board[2][2]) || ('X'==board[0][2]&&'X'==board[1][1]&&'X'==board[2][0]) || ('O'==board[0][0]&&'O'==board[1][0]&&'O'==board[2][0]) || ('O'==board[0][1]&&'O'==board[1][1]&&'O'==board[2][1]) || ('O'==board[0][2]&&'O'==board[1][2]&&'O'==board[2][2] || ('O'==board[0][0]&&'O'==board[1][1]&&'O'==board[2][2]) || ('O'==board[0][2]&&'O'==board[1][1]&&'O'==board[2][0]))) return 1; return 0; } //function body of initializeBoard function void initializeBoard(char board[][3]) { int i,j; for(i=0;i<3;i++){ for(j=0;j<3;j++){ board[i][j]=' '; } } }

//function body of displayBoard function void displayBoard(char board[][3]) { int i,j; printf(" \t %d\t %d\t %d",1,2,3); printf(" "); for(i=0;i<3;i++) { printf("%d\t",i+1); for(j=0;j<3;j++) { printf("[%c]\t",board[i][j]); } printf(" "); } }

//function body of makeMove function void makeMove(char board[][3],char player) { int row,col; //checking for valid move do { printf(" Enter row and column you would like to fill:"); scanf("%d%d",&row,&col); if(!validMove(board,row,col)) { printf(" Invalid Entry.. enter another position..."); }

}while(!(validMove(board,row,col)));

board[row-1][col-1]=player; }

int main() { int repeat=1; //declaring 2d char array board char board[3][3]; //calling initializeBoard() function initializeBoard(board); //calling displayBoard() function displayBoard(board);

int i;

//this is a for loop for 9 moves for(i=1;i<=9 && repeat;i++) { if(i%2==0) { makeMove(board,'X'); displayBoard(board); if(checkWinner(board)) { printf("X won the game!!!"); repeat=0; } } else{ makeMove(board,'O'); displayBoard(board); if(checkWinner(board)) { printf("O won the game!!!"); repeat=0; } } }

if(!checkWinner(board)) printf("It's a tie ");

//pause program output on console getchar(); return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!