如果用户选择了游戏板的大小,我该如何制作一个功能来检查井字游戏中的获胜动作



我已经将Tic-Tac-Toe程序从使用普通的3x3网格更改为用户选择的网格大小(在3到9之间(。我使用全局常量"SIZE"来保存用户选择的值。我的问题是调整我的winMove((;功能,以便根据用户选择的当前棋盘尺寸(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 << "nnNumber 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   ;
}

我将把垂直胜利留给你。

最新更新