无效检查板功能无效。这是一个tic tac toe游戏。因此,如果有人赢了比赛或者是平局,那么每次移动后该功能都会进行检查。在有人获胜或有平局后,董事会将被重置并且游戏重新开始。我只是不能让void checkboard功能工作。请帮我解决这个问题。
checkboard应该检查每个用户输入以查看该游戏是否赢了。因此,checkboard必须检查playerOne是否在一条线或对角线上有Xs,或者如果playerTwo有这样的方式,或者是否有平局。如果玩家获胜或有抽奖,它会告诉谁赢了或者有一个平局,然后询问用户是否要再次玩或退出该计划。根据他们的回复,董事会被重置或游戏退出。
我认为问题在于bGameOver无论如何都会保持真实。我把游戏之前的代码放在bGameOver = false之后,程序就像它想象的那样运行。所以在if语句中的某些地方,它会在1次移动后继续使bGameOver =为真。
解决之后,我想了解如何在问题解决后改进它的建议。
#include <iostream>
#include <string>
void checkboard(char** CurrentPlayer, char (&squares)[3][3], char &playerMarker, bool bGameOver,char playerOne[]);
using namespace std;
void main()
{
char board[3][3];
board[0][0] = ' ';
board[0][1] = ' ';
board[0][2] = ' ';
board[1][0] = ' ';
board[1][1] = ' ';
board[1][2] = ' ';
board[2][0] = ' ';
board[2][1] = ' ';
board[2][2] = ' ';
bool bGameOver(true);
char playerMarker;
// Main game loop
do
{
showboard(board);
boardMarker(playerTurn, playerOne, playerTwo, playerMarker, ¤tPlayer);
playerMove(currentPlayer, board, playerMarker);
checkboard(¤tPlayer, board, playerMarker, bGameOver, playerOne);
alternatePlayers(playerTurn);
}
while (!bGameOver);
}
void showboard(char (&board)[3][3])
{
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout << board[i][j];
if(j < 2) cout << "|";
}
cout << "\n";
if(i < 2) cout << "-+-+-" << endl;;
}
}
void playerMove(char* CurrentPlayer, char (&board)[3][3], char &playerMarker)
{
bool bValidMove;
int x,y;
// Prompt the player for a move
cout << "Player " << CurrentPlayer << "'s move:" << endl;
// Loop until we get a valid move
do
{
cout << "Please enter the row number for the place you wish to mark: " << endl;
cin >> x;
cout << "Please enter the column number for the place you wish to mark" << endl;
cin >> y;
bValidMove = true;
// Check for a valid move
if (x == 1 && y == 1)
{
board[0][0] = playerMarker;
}
else if (x == 1 && y == 2)
{
board[0][1] = playerMarker;
}
else if (x == 1 && y == 3)
{
board[0][2] = playerMarker;
}
else if (x == 2 && y == 1)
{
board[1][0] = playerMarker;
}
else if (x == 2 && y == 2)
{
board[1][1] = playerMarker;
}
else if (x == 2 && y == 3)
{
board[1][2] = playerMarker;
}
else if (x == 3 && y == 1)
{
board[2][0] = playerMarker;
}
else if (x == 3 && y == 2)
{
board[2][1] = playerMarker;
}
else if (x == 3 && y == 3)
{
board[2][2] = playerMarker;
}
else
{
cout << "Invalid Move. Try again." << endl;
bValidMove = false;
}
}
while (!bValidMove);
}
void checkboard(char** CurrentPlayer, char (&board)[3][3], char &playerMarker, bool bGameOver, char playerOne[100])
{
bGameOver = false;
bool bWinGame = true;
// Check for end of game conditions
do
{
if (board[0][0] = playerMarker)
{
if (board[0][1] == board[0][0] && board[0][2] == board[0][0]) {
bGameOver = true;
}
if (board[1][0] == board[0][0] && board[2][0] == board[0][0]) {
bGameOver = true;
}
}
if (board[1][1] != playerMarker)
{
if (board[0][0] == board[1][1] && board[2][2] == board[1][1])
{
bGameOver = true;
}
if (board[0][1] == board[1][1] && board[2][1] == board[1][1])
{
bGameOver = true;
}
if (board[1][0] == board[1][1] && board[1][2] == board[1][1])
{
bGameOver = true;
}
if (board[0][2] == board[1][1] && board[2][0] == board[1][1])
{
bGameOver = true;
}
}
if (board[2][2] != playerMarker)
{
if (board[0][2] == board[2][2] && board[1][2] == board[2][2])
{
bGameOver = true;
}
if (board[2][0] == board[2][2] && board[2][1] == board[2][2])
{
bGameOver = true;
}
}
// Need to check the board full (no-win condition)
if (board[0][0] != playerMarker && board[0][1] != playerMarker && board[0][2] != playerMarker &&
board[1][0] != playerMarker && board[1][1] != playerMarker && board[1][2] != playerMarker &&
board[2][0] != playerMarker && board[2][1] != playerMarker && board[2][2] != playerMarker && !bGameOver)
{
bGameOver = true;
bWinGame = false;
}
system("cls");
if (bGameOver)
{
if (bWinGame)
{
cout << "Player " << CurrentPlayer << " wins!" << endl;
}
// Print ending board
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout << board[i][j];
if(j < 2) cout << "|";
}
cout << "\n";
if(i < 2) cout << "-+-+-" << endl;;
}
cout << "Game Over!" << endl;
cout << "Play again (y/n)?" << endl;
char cPlayAgain;
cin >> cPlayAgain;
if (cPlayAgain == 'y')
{
bGameOver = false;
// Clear the board
board[0][0] = ' ';
board[0][1] = ' ';
board[0][2] = ' ';
board[1][0] = ' ';
board[1][1] = ' ';
board[1][2] = ' ';
board[2][0] = ' ';
board[2][1] = ' ';
board[2][2] = ' ';
}
*CurrentPlayer = playerOne;
}
} while (!bGameOver);
}
答案 0 :(得分:0)
如果使用签名怎么样:
void checkboard(char** CurrentPlayer, char** squares, char &playerMarker, bool bGameOver,char playerOne[]);
虽然我认为这不是你应该改变的唯一内容,但我建议你多读一些关于数组和指针的内容,并用较小的程序来练习。
我不知道你是如何在不注意它没有按预期工作的情况下编写所有代码的。
我也不知道是否有人会花时间为你修改你的程序。这是作业。
答案 1 :(得分:0)
if (board[0][0] = playerMarker)
这将始终将playerMarker分配到棋盘[0] [0]。你必须习惯&#34; =&#34;之间的区别。 (作业)和&#34; ==&#34; (问题&#34;这些是平等的吗?&#34;),参见Yoda Conditions虽然这对你没有帮助。