gpt4 book ai didi

c - 如何使该程序的 win 语句递归?

转载 作者:行者123 更新时间:2023-11-30 21:43:13 25 4
gpt4 key购买 nike

这是程序:

/*MAIN*/
#include <stdio.h>
#include "ticTacToe.h"


int main(void) {
boardSet(); // sets the board up
playGame(); // starts the game
}





/*ticTacToe.c*/
//add diagonal check
#include <stdio.h>
#include "ticTacToe.h"

int x = 0;
void boardSet(){
for(n=0;n<3;n++){ // sets row 0 to x
board[0][n] = '#';
}
for(n=0;n<3;n++){ // sets row 1 to x
board[1][n] = '#';
}
for(n=0;n<3;n++){ // sets row 2 to x
board[2][n] = '#';
}
}

void playGame(){ //starts the game, include both players turns and then repeats
playerOne();
playerTwo();
playGame();
}

void display(){
printf("\n");//formatting
for(n=0;n<3;n++) // for statement to print row 0
printf("%c\t",board[0][n]);
printf("\n");//formatting
for(n=0;n<3;n++) //for statement to print row 1
printf("%c\t",board[1][n]);
printf("\n");//formatting
for(n=0;n<3;n++)//for statement to print row 2
printf("%c\t",board[2][n]);
printf("\n");//formatting
}

void playerOne(){
x = 0;
//ask for and gets playerOne's input
char input[BUFFER] = {0};
printf("Player one:");
fgets(input, BUFFER, stdin);

//if there's an open space put an o there
if(board[input[0]-97][input[1]-49] == '#'){
board[input[0]-97][input[1]-49] = 'o';
display(); //shows the board
}

//if there's no an open space try again
else if((board[input[0]-97][input[1]-49] != '#')){
printf("Please select an open spot\n");
playerOne();
}


checkWin(); //checks to see if, after that move, one of the players win
}

void playerTwo(){
x = 0;
//asks for and gets playerTwo's input
char input[BUFFER] = {0};
printf("Player two:");
fgets(input, BUFFER, stdin);

//if there's an open space put an x there
if(board[input[0]-97][input[1]-49] == '#'){
board[input[0]-97][input[1]-49] = 'x';
display();
}

//if there's not an open space try again
else if((board[input[0]-97][input[1]-49] != '#')){
printf("Please select an open spot\n");
playerTwo();
}

//display(); //shows the board
checkWin(); //checks to see if, after that move, one of the players win
}

void checkWin(){ // checks if one of the players win, checks rows, then the columns and then the diagonals
int continueGame = 0;
rowCheck();
x=0;
columnCheck();
diagonalCheck();
for(x=0;x<=2;x++){ // if all of the spaces have been taken up, call a draw
if(board[x][COUNT] == '#' || board[x][COUNT+1] == '#' || board[x][COUNT+2] == '#')
continueGame = 1;
}
if (continueGame == 0){
printf("Draw!");
exit(1);
}

}

void rowCheck(){ //checks rows, repeats going down the rows
//printf("x: %d\n",x); //for debugging
//row check, if row 1 is all o's, then player one wins and the program ends
if(board[x][COUNT] == 'o' && board[x][COUNT+1] == 'o' && board[x][COUNT+2] == 'o'){
printf("Player One wins!\n");
exit(1);
}
//same as above, but with player 2
else if(board[x][COUNT] == 'x' && board[x][COUNT+1] == 'x' && board[x][COUNT+2] == 'x'){
printf("Player Two wins!\n");
exit(1);
}
++x;
if(x > 3)
return;
rowCheck();
}

void columnCheck(){ //checks columns, repeats going across the columns
//printf("x: %d\n",x); //for debugging
//column check
if(board[COUNT][x] == 'o' && board[COUNT+1][x] == 'o' && board[COUNT+2][x] == 'o'){
printf("Player One wins!\n");
exit(1);
}
else if(board[COUNT][x] == 'x' && board[COUNT+1][x] == 'x' && board[COUNT+2][x] == 'x'){
printf("Player Two wins!\n");
exit(1);
}
++x;
if(x > 3)
return;
columnCheck();
}

void diagonalCheck(){
if(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o'){
printf("Player One wins!\n");
exit(1);
}
else if(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'){
printf("Player One wins!\n");
exit(1);
}

if(board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x'){
printf("Player Two wins!\n");
exit(1);
}
else if(board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == 'x'){
printf("Player Two wins!\n");
exit(1);
}

}




/*ticTacToe.h*/
#include <string.h>
#include <stdlib.h>
#define HEADER_H
#define BUFFER 256
#define COUNT 0

//prototypes
void boardSet();
void playGame();
void display();
void playerOne();
void playerTwo();
void checkWin();
void rowCheck();
void columnCheck();
void diagonalCheck();

//global variables
int board[3][3]; //column then row
int n;
int row[2];
int column[2];

您将在 tic-tac-toe.c 中看到我有一个 checkWin 函数,它本身调用 rowCheck、columnCheck 和对角线检查,并具有调用绘图的附加功能。问题是,我的老师希望行、列和对角线检查是递归的,而不是硬编码的值。我以为我已经做到了一定程度,但这还不够。我正在画一个空白。

最佳答案

要拥有递归 checkWin 函数,请向其传递一个参数,该参数表示要检查哪一行或哪一列。在您的实际检查代码实现中断条件之后(下一列/行无效)。如果中断条件不成立,则为下一列/行调用函数本身。

关于c - 如何使该程序的 win 语句递归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53234112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com