Menu

[solved]-Would Make Void Listwinningcells Recursion Code Function Suppose Allow User Find Next Move Q38997181

How would you make the ‘void ListWinningCells’ into a recursioncode?
The function is suppose to allow the user to find if any next moveor next play for any player X or O who has the turn to play iswinning move. The function will print the cell number or ID that ifthe player places an X or O in it he/she will win the game. Ifthere is more than one cell where the player could place his symbolin and win the game the function should print all the winning cellsfor that player.

Example output:
0 | XIO Il X 2 X 1 | 2 | 3 IX | 210 4 15 16 SOLSIOXIXTO 0 | 5 | 6 4 1 x 10 Il 4 | XI0 _ _ _ 7 X 19 II 7 | | X 7 | 8 | 9 17 18

Code
#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);
while(!(input==’X’ || input==’O’ || input==’x’ ||input==’o’)){
printf(“Enter a valid input (X or O):n”);
scanf(” %c”,&input);
}
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]){
if(board[0][0]==board[0][1] && board[0][2]==’3′)
printf(“%c %cn”,board[0][1],board[0][2]);
if(board[1][0]==board[1][1] && board[1][2]==’6′)
printf(“%c %cn”,board[1][1],board[1][2]);
if(board[2][0]==board[2][1] && board[2][2]==’9′)
printf(“%c %cn”,board[2][1],board[2][2]);

if(board[0][2]==board[0][1] && board[0][0]==’1′)
printf(“%c %cn”,board[2][1],board[2][2]);
if(board[1][2]==board[1][1] && board[1][0]==’4′)
printf(“%c %cn”,board[2][1],board[2][2]);
if(board[2][2]==board[2][1] && board[2][0]==’7′)
printf(“%c %cn”,board[2][1],board[2][2]);

if(board[0][0]==board[0][2] && board[0][1]==’2′)
printf(“%c %cn”,board[0][2],board[0][1]);
if(board[1][0]==board[1][2] && board[1][1]==’5′)
printf(“%c %cn”,board[1][2],board[1][1]);
if(board[2][0]==board[2][2] && board[2][1]==’8′)
printf(“%c %cn”,board[2][2],board[2][1]);

if(board[0][0]==board[1][0] && board[2][0]==’7′)
printf(“%c %cn”,board[1][0],board[2][0]);
if(board[0][1]==board[1][1] && board[2][1]==’8′)
printf(“%c %cn”,board[1][1],board[2][1]);
if(board[0][2]==board[1][2] && board[2][2]==’9′)
printf(“%c %cn”,board[1][2],board[2][2]);

if(board[0][0]==board[2][0] && board[1][0]==’5′)
printf(“%c %cn”,board[2][0],board[1][0]);
if(board[0][1]==board[2][1] && board[1][1]==’6′)
printf(“%c %cn”,board[2][1],board[1][1]);
if(board[0][2]==board[2][2] && board[1][2]==’7′)
printf(“%c %cn”,board[2][2],board[1][2]);

if(board[1][0]==board[2][0] && board[0][0]==’1′)
printf(“%c %cn”,board[1][0],board[0][0]);
if(board[1][1]==board[2][1] && board[0][1]==’2′)
printf(“%c %cn”,board[1][1],board[0][1]);
if(board[1][2]==board[2][2] && board[0][2]==’3′)
printf(“%c %cn”,board[1][2],board[0][2]);

if(board[0][0]==board[1][1] && board[2][2]==’9′)
printf(“%c %cn”,board[1][1],board[2][2]);
if(board[2][0]==board[1][1] && board[0][2]==’3′)
printf(“%c %cn”,board[1][1],board[0][2]);

if(board[2][2]==board[1][1] && board[0][0]==’1′)
printf(“%c %cn”,board[1][1],board[0][0]);
if(board[0][2]==board[1][1] && board[2][0]==’7′)
printf(“%c %cn”,board[1][1],board[2][0]);

if(board[0][0]==board[2][2] && board[1][1]==’5′)
printf(“%c %cn”,board[2][2],board[1][1]);
if(board[2][0]==board[0][2] && board[1][1]==’5′)
printf(“%c %cn”,board[0][2],board[1][1]);
}

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”);
}
}

0 | XIO Il X 2 X 1 | 2 | 3 IX | 210 4 15 16 SOLSIOXIXTO 0 | 5 | 6 4 1 x 10 Il 4 | XI0 _ _ _ 7 X 19 II 7 | | X 7 | 8 | 9 17 18 19 Cell # 5 is a winning Cell # 5 and #6 and No winning cells for Cell 9 is winning cell cell for player X #2 are winning cells player X or o for Player O for player X Cell 9 is winning cell for Player X NOTE: If the board is invalid then you can not predict winning cells for this board Show transcribed image text 0 | XIO Il X 2 X 1 | 2 | 3 IX | 210 4 15 16 SOLSIOXIXTO 0 | 5 | 6 4 1 x 10 Il 4 | XI0 _ _ _ 7 X 19 II 7 | | X 7 | 8 | 9 17 18 19 Cell # 5 is a winning Cell # 5 and #6 and No winning cells for Cell 9 is winning cell cell for player X #2 are winning cells player X or o for Player O for player X Cell 9 is winning cell for Player X NOTE: If the board is invalid then you can not predict winning cells for this board

Expert Answer


Answer to How would you make the ‘void ListWinningCells’ into a recursion code? The function is suppose to allow the user to find … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *