Question
Problem 2. Tic-Tac-Toe, Part 2 (20 points) Save a copy of your program from part 1 and modify it to include validMove() and checkWinner() functions.
Problem 2. Tic-Tac-Toe, Part 2 (20 points) Save a copy of your program from part 1 and modify it to include validMove() and checkWinner() functions. A valid move should be 1-3 for both rows and columns, and should not be a move that was previously entered. If the checkWinner() function finds a winner, the program should print a message and exit. Use the following function prototypes: int validMove(char board[][3], int row, int column); int checkWinner(char board[][3]);
current CODE IN C:
#include
//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;
printf(" Enter row and column you would like to fill:");
scanf("%d%d",&row,&col);
if(board[row-1][col-1]=='O'||board[row-1][col-1]=='X'){
printf(" This position is already filled.. enter another position...");
makeMove(board,player);
}
board[row-1][col-1]=player;
}
main(){
//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;i++){
if(i%2==0){
makeMove(board,'X');
displayBoard(board);
}
else{
makeMove(board,'O');
displayBoard(board);
}
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started