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

How would you modify this tictactoe program to check whether X or O wins the gam

ID: 3639792 • Letter: H

Question

How would you modify this tictactoe program to check whether X or O wins the game and then display the winner

#include <stdio.h>
int check(char [][3]);
void init_matrix(char[][3] );
int get_player_move(char,char[][3]);
void disp_matrix( char[][3]);
int main( )
{char p='X';
char matrix[3][3];
int done;
init_matrix(matrix);
disp_matrix(matrix);

disp_matrix(matrix);
do {done=get_player_move(p,matrix);
if(done==0)
{printf("after player move ");
if(p=='X')
p='O';
else
p='X';
disp_matrix(matrix);
done = check(matrix);
}
} while(done==0);
printf("game over");
getch();
return 0;
}

void init_matrix(char matrix[][3] )
{ int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++)
matrix[i][j] =' ';
}

int get_player_move(char p,char matrix[][3] )
{ int x, y,m;
int i,j;
do
{
printf("enter a row 1-3 for your move: ");
scanf("%d",&i);
while((i<-1||i>2))
{printf("Invalid input-try again ");
printf("enter a row 0-2 for your move: ");
scanf("%d",&i);
}
printf("enter a column 0-2 for your move: ");
scanf("%d",&j);
while((j<-1||j>2))
{printf("Invalid input-try again ");
printf("enter a column 0-2 for your move: ");
scanf("%d",&j);
}
if(i==-1&&j==-1)
return 1;

if(matrix[i][j]=='X'||matrix[i][j]=='O')
printf("Invalid move, try again. ");
}while(matrix[i][j]=='X'||matrix[i][j]=='O');
matrix[i][j] = p;
return 0;
}


void disp_matrix(char matrix[][3] )
{
int t;
for(t=0; t<3; t++) {
printf(" %2c | %2c | %2c",matrix[t][0], matrix[t][1], matrix [t][2]);
if(t!=2) printf(" ----|----|---- ");
}
printf(" ");
}

int check(char matrix[][3] )
{int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(matrix[i][j]==' ')
return 0;
return 1;
}

Explanation / Answer

please rate - thanks

#include <stdio.h>
char check(char [][3]);
void init_matrix(char[][3] );
int get_player_move(char,char[][3]);
void disp_matrix( char[][3]);
int main( )
{char p='X';
char matrix[3][3];
char done;
int moves=0;
init_matrix(matrix);
   disp_matrix(matrix);

do {

done=get_player_move(p,matrix);
     if(done==0&&moves!=9)
         {printf("after player move ");
         moves++;
         if(p=='X')
               p='O';
          else
                p='X';
          disp_matrix(matrix);
          done = check(matrix);
          }
} while(done==' '&&moves!=9);
if(moves==9)
       printf("tie game ");
else
      printf("game over %c wins ",done);
getch();
return 0;
}

void init_matrix(char matrix[][3] )
{ int i, j;
for(i=0; i<3; i++)
    for(j=0; j<3; j++)
       matrix[i][j] =' ';
}

int get_player_move(char p,char matrix[][3] )
{ int x, y,m;
int i,j;
do
{
printf("enter a row 1-3 for your move: ");
scanf("%d",&i);
while((i<1||i>3)&&i!=-1)
     {printf("Invalid input-try again ");
      printf("enter a row 1-3 for your move: ");
      scanf("%d",&i);
      }
printf("enter a column 1-3 for your move: ");
scanf("%d",&j);
while((j<1||j>3)&&i!=-1)
     {printf("Invalid input-try again ");
      printf("enter a column 1-3 for your move: ");
      scanf("%d",&j);
      }
   if(i==-1&&j==-1)
       return 1;  
   i--;
   j--;

if(matrix[i][j]=='X'||matrix[i][j]=='O')
       printf("Invalid move, try again. ");
}while(matrix[i][j]=='X'||matrix[i][j]=='O');
matrix[i][j] = p;
return 0;
}


void disp_matrix(char matrix[][3] )
{
int t;
for(t=0; t<3; t++) {
           printf(" %2c | %2c | %2c",matrix[t][0], matrix[t][1], matrix [t][2]);  
    if(t!=2) printf(" ----|----|---- ");
}
printf(" ");
}

char check(char matrix[][3] )
{int i;
for(i=0; i<3; i++) /* check rows */
    if(matrix[i][0]==matrix[i][1] &&
       matrix[i][0]==matrix[i][2]) return matrix[i][0];
for(i=0; i<3; i++) /* check columns */
    if(matrix[0][i]==matrix[1][i] &&
       matrix[0][i]==matrix[2][i]) return matrix[0][i];
/* test diagonals */
if(matrix[0][0]==matrix[1][1] &&
     matrix[1][1]==matrix[2][2])
       return matrix[0][0];
if(matrix[0][2]==matrix[1][1] &&
     matrix[1][1]==matrix[2][0])
       return matrix[0][2];
return ' ';
}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote