繁体   English   中英

如果用户选择游戏板的大小,我如何制作 function 以检查井字游戏中的获胜动作?

[英]How do I make a function to check for the winning move in tic-tac-toe if the user chooses the size of the game board?

我已将井字游戏程序从使用普通的 3x3 网格更改为用户选择的网格大小(3 到 9 之间)。 我正在使用全局常量“SIZE”来保存用户选择的值。 我的问题是调整我的 winMove(); function 以便根据用户选择的当前棋盘尺寸 (SIZE) 进行调整以检查获胜棋步。 我尝试了不同的循环,但无法让它工作。 现在它只适用于 3x3 板。

我仍在学习 c++ 并且我已经坚持了几天,所以我希望友好的人可以提供帮助。 这是我到目前为止的完整程序,希望它不会太乱!

#include <iostream>
using namespace std;
 
struct TicTacToe
{
  char **board;  
};

void makeBoard(TicTacToe&);
void deallocBoard(TicTacToe&);
void printBoard(TicTacToe);
bool isDraw(TicTacToe);
void convertInput(char, char, int&, int&);
char winMove(TicTacToe, int, int);
bool validateSIZE(int);
int SIZE = 1;

int main(){
  while(SIZE < 3 || SIZE > 9)
  {
    cout << "Enter number between 3 and 9 for the length of board.\n";
    cout << "Example: 4 will make a board with 4 rows and 4 columns: ";
    cin >> SIZE;
    validateSIZE(SIZE);
  }

  TicTacToe game;
  makeBoard(game);
  char winner = 0;               
  char turn = 'X';               
  char rowIn, colIn;  
  int row, column; 

  while(!winner && !isDraw(game))
  {
    printBoard(game);
    cout << "\nPlayer " << turn << "'s move (input format: a1): ";
    cin >> rowIn >> colIn;
    convertInput(rowIn, colIn, row, column); 

    if (game.board[row][column]==' ')           
    {                   
      game.board[row][column] = turn;
      if (turn == 'X')                           
        {turn = 'O';}
      else                
        {turn = 'X';}
      winner = winMove(game, row, column);  
    }                               
      else
          cout << "Taken, try again!\n";     
  } 
  printBoard(game);

  if (winner == 'X' || winner == 'O')
    {cout << " Congrats, the winner is " << winner << '.' << endl;}
  else
    {cout << " Game ends in a draw." << endl;}

  cout << endl << "Game Over!" << endl << endl;
  deallocBoard(game);
  
  return 0;
}

// Need help with this function.
char winMove(TicTacToe gameIn, int i, int j) 
{
  //row win
  if (gameIn.board[i][0]==gameIn.board[i][1] &&
      gameIn.board[i][0]==gameIn.board[i][2]) 
  {
    return gameIn.board[i][j];   
  }
  //column win
  if (gameIn.board[0][j]==gameIn.board[1][j] &&
      gameIn.board[0][j]==gameIn.board[2][j])  
  {
    return gameIn.board[i][j]; 
  }
  //left diagonal win
  if (gameIn.board[0][0] != ' ' && 
      gameIn.board[0][0] == gameIn.board[1][1] && 
      gameIn.board[0][0] == gameIn.board[2][2])
  {
    return gameIn.board[i][j]; 
  }
  //right diagonal win
  if (gameIn.board[0][2] != ' ' && 
      gameIn.board[0][2] == gameIn.board[1][1] && 
      gameIn.board[0][2] == gameIn.board[2][0])
  {
    return gameIn.board[i][j]; 
  }
  return 0;
}

bool validateSIZE(int SIZE)
{
  if(SIZE < 3 || SIZE > 9)
  {
    cout << "\n\nNumber must be between 3 and 9!\nTry again!\nPlease ";
    return false;
  }
  return true;
};

void makeBoard(TicTacToe& gameIn) 
{                               
  gameIn.board = new char*[SIZE];    
  for(int i = 0; i < SIZE; i++)
    {gameIn.board[i] = new char[SIZE];}
                            
  for(int j =0; j < SIZE; j++)      
    for(int k = 0; k < SIZE; k++)  
      {gameIn.board[j][k] = ' ';}
} 

void deallocBoard(TicTacToe& gameIn)
{
  for(int i = 0; i < SIZE; i++)  
    delete [] gameIn.board[i];    
  delete [] gameIn.board;          
  gameIn.board = NULL;             
} 

void printBoard(TicTacToe gameIn) 
{
  int temp = 1;
  cout << "  ";
  
  while(temp < SIZE + 1)
  {
    cout << temp << " "; 
    temp++;
  }
  temp = 1;
  cout << endl;

  for(int i = 0; i < SIZE; i++)
    {
      cout << char(i + 'a') << '|';      
        for(int j = 0; j < SIZE; j++)
        {
            cout << gameIn.board[i][j] << '|';  
        }
      cout << endl;
    }
} 

bool isDraw(TicTacToe gameIn) 
{
  bool full = true;                    
  for(int i = 0; full && i < SIZE; i++)
    for(int j = 0; full && j < SIZE; j++)
      full = gameIn.board[i][j] != ' ';   
  return full;
} 

void convertInput(char rowIn, char colIn, int& row, int& column)
{
  row = toupper(rowIn) - 'A';  
  column = colIn - '1';    
} 

您可以轻松地使用 2 个 for 循环来迭代每个起点及其方向以进行胜利检查。 看起来像这样的东西:

 //           hor ver diagonals
 //    dx[4] = {1, 0, 1, 1};
 //    dy[4] = {0, 1, 1, -1};

// check horizontal win
    for(int i = 0; i < SIZE; ++i)
    {
      bool win = true;
      for(int j = 1; j < SIZE; ++j)
      {
         if(gameIn.board[j][i] != gameIn.board[j - 1][i])
         {
             win = false;
             break;
         }
      }
      if(win == true){return  ;}
    }
// check vertical win
.
.
.
// check diagonal 1 win
   for(int i = 1; i < SIZE; ++i)
   {
      if(gameIn.board[i][i] != gameIn.board[i - 1][i - 1])
          break;
      if(i == SIZE - 1)return   ;
   }
// check diagonal 2 win
   for(int i = 1; i < SIZE; ++i)
   {
      if(gameIn.board[i][SIZE - i - 1] != gameIn.board[i - 1][SIZE - i])
          break;
      if(i == SIZE - 1)return   ;
   }

我会把垂直胜利留给你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM