[solved]-Would Write C Function Allows User Find Next Move Next Play Player X O Turn Play Winning M Q39029620
How would you write a C function that allows the user to find ifany next move or next play for any player X or O who has the turnto play is winning move? The function should print the cellnumber/ID that if the player places an X or O in it he/she will winthe game. Also, if there is more than one cell where the playercould place his symbol in and win the game, the function shouldprint all the winning cells for that player.
The function prototype is void ListWinningCells(int m, int n, charboard[][n])
Output example:
so far, this is how my code is
#include <stdio.h>
#include <stdlib.h>
void InitializeBoard(int m,int n,char board[][n]){
int c=1,i,j;
for (i=0;i<m;i++){
for(j=0;j<n;j++){
board[i][j]=c+’0′;
c++;
}
}
}
void PrintBoard(int m,int n,char board[][n]){
int i,j;
for (i=0;i<m;i++){
printf(” | | n”);
for(j=0;j<n;j++){
printf(” %c “,board[i][j]);
if(j==n-1){
printf(“n”);
}
else{
printf(“|”);
}
}
if(i!=m-1){
printf(“___|___|___n”);
}
else{
printf(” | | n”);
}
}
}
void CreateBoard(int m,int n,char board[][n]){
int choice=1,cell=0;
char input=’X’;
while(choice){
PrintBoard(m,n,board);
printf(“Enter the cell you want to enter X or O or enter -1 toexit:n”);
scanf(” %d”,&cell);
if(cell==-1){
return;
}
while(cell>=10 || cell<=0)
{
printf(“Enter a valid input (1-9):n”);
scanf(“%d”,&cell);
}
printf(“Enter X or O:n”);
scanf(” %c”,&input);
if(input==’x’){
input = ‘X’;
}
if(input==’o’){
input = ‘O’;
}
while(!(input==’X’ || input==’O’ || input==’x’ ||input==’o’)){
printf(“Enter a valid input (X or O):n”);
scanf(” %c”,&input);
if(input==’x’){
input = ‘X’;
}
if(input==’o’){
input = ‘O’;
}
}
if(cell==1)
board[0][0]=input;
if(cell==2)
board[0][1]=input;
if(cell==3)
board[0][2]=input;
if(cell==4)
board[1][0]=input;
if(cell==5)
board[1][1]=input;
if(cell==6)
board[1][2]=input;
if(cell==7)
board[2][0]=input;
if(cell==8)
board[2][1]=input;
if(cell==9)
board[2][2]=input;
}
}
int IsValidBoard(int m,int n,char board[][n]){
int countx=0,counto=0,i,j,diff;
for (i=0;i<m;i++){
for(j=0;j<n;j++){
if(board[i][j]==’X’){
countx+=1;
}
if(board[i][j]==’O’){
counto+=1;
}
}
}
diff=countx-counto;
if(abs(diff)==0 || abs(diff)==1)
return 1;
else
return 0;
}
void ListWinningCells(int m,int n,char board[][n]){
//how would I code this program
}
int main(){
int m=3,n=3,test;
char board[m][n],choice=’a’;
InitializeBoard(m,n,board);
while(choice!=’e’){
printf(“press ‘p’ to print the tic-tac-toe boardn”);
printf(“press ‘c’ to create a tic-tac-toe board with some X and Ocellsn”);
printf(“press ‘t’ to test if a tic-tac-toe board is valid orinvalid boardn”);
printf(“press ‘w’ to predict winning cell for player X orOn”);
printf(“press ‘e’ to exitn”);
scanf(” %c”,&choice);
switch(choice){
case ‘p’: PrintBoard(m,n,board);
break;
case ‘c’: CreateBoard(m,n,board);
break;
case ‘t’: test = IsValidBoard(m,n,board);
if(test)
printf(“Valid board!!n”);
else
printf(“Invalid board!!n”);
break;
case ‘w’: ListWinningCells(m,n,board);
break;
case ‘e’:
break;
default: printf(“Enter a valid input: “);
}
printf(“n”);
}
}
2 X 0 X 0 x X I 2 0 2 3 1 4 5 6 X 0 C 5 6 4 X |0 7 X 7 8 9 7 0 7 8 9 Cell # 5 is a winning cell for player X No winning cells for player X or O Cell 9 is winning cell Cell #5 and #6 and #2 are winning cells for player X for Player O Cell 9 is winning cell for Player X NOTE: If the board is invalid then you can not predict winning cells for this board C LO Show transcribed image text 2 X 0 X 0 x X I 2 0 2 3 1 4 5 6 X 0 C 5 6 4 X |0 7 X 7 8 9 7 0 7 8 9 Cell # 5 is a winning cell for player X No winning cells for player X or O Cell 9 is winning cell Cell #5 and #6 and #2 are winning cells for player X for Player O Cell 9 is winning cell for Player X NOTE: If the board is invalid then you can not predict winning cells for this board C LO
Expert Answer
Answer to How would you write a C function that allows the user to find if any next move or next play for any player X or O who ha… . . .
OR

