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

时间:2018-11-09 22:32:06

标签: c recursion

这是程序:

/*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和对角线检查,并具有调用绘图的功能。问题是,我的老师希望行,列和对角线检查是递归的,而不是硬编码的值。我以为我已经做到了一定程度,但这还不够。我在画空白。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

建议类似以下内容:

#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