c语言 - 如何使该程序的获胜语句递归?



这是程序:

/*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("%ct",board[0][n]);  
printf("n");//formatting
for(n=0;n<3;n++) //for statement to print row 1
printf("%ct",board[1][n]);  
printf("n");//formatting
for(n=0;n<3;n++)//for statement to print row 2
printf("%ct",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 spotn");
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 spotn");
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: %dn",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: %dn",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和对角线Check,并增加了调用draw的功能。问题是,我的老师希望行、列和对角线检查是递归的,而不是硬编码的值。我以为我已经做到了一定程度,但这还不够。我一片空白。

要有一个递归checkWin函数,请向它传递一个参数,说明要检查哪行或哪列。在您的实际检查代码实现中断条件后(下一列/行无效(。如果break条件不为true,则为下一列/行调用函数本身。

建议类似于以下内容:

#define MAX_COLUMN 3
#define WIN 1
#define NO_WIN 0
int rowCheck( int row, int player, int column )
{   
if( column == MAX_COLUMN )
{
return WIN;
}
if( board[ row ][ column ] != player )
{
return NO_WIN;
}
return rowCheck( row, player, column+1 );
}

此外,由于主文件中不需要(非主(文件中的任何内容,除了几个原型:建议您的头只包含以下内容:

#ifndef TIC_TAC_TOW_H
#define TIC_TAC_TOW_H
boardSet( void ); // sets the board up
playGame( void ); // starts the game
#endif // TIC_TAC_TOW_H

当前头文件中的所有其他语句都应该移到文件中:TicTacToe.c

最新更新