void checkboard函數不起作用。這是一個tic tac腳趾遊戲。因此,如果有人贏得比賽或者是平局,那麼每次移動後該功能都會被檢查。在有人贏了或者有平局之後,董事會將被重新設置,並且遊戲將會開始。我無法讓無效的checkboard函數工作。請幫我解決這個問題。使用IF語句檢查數組輸入的問題
檢查板應檢查每個用戶的輸入,看看該比賽是否贏了。所以checkboard必須檢查playerOne是否有一行或對角線的X,或者如果playerTwo有這種方式,或者有一條平手。如果玩家獲勝或者有抽籤,它會告訴誰贏了或者有一個平局,然後詢問用戶他們是否想再玩一次或退出該程序。從他們的迴應中,棋盤重置或遊戲退出。
我認爲問題在於無論如何,bGameOver一直保持真實。在gameover之前,我把代碼放在bGameOver = false之後,並且程序按照它的設想運行。所以一些if語句中的一些地方,在1次移動後,bGameOver = true。
解決問題後,我想提出一些建議,只有在問題得到解決後才能改進。
#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);
}
由同一用戶關閉http://stackoverflow.com/questions/3369478/checking-an-array-for-results-using-a-function-關閉。 – 2010-07-30 16:59:11
這個問題被關閉了,所以我想我必須用他們想要的信息開始一個新的問題 – Renge 2010-07-30 17:05:29
我沒有做任何適當的分析(它有點意大利麪條),但是「if(board [0] [0] ='1'){「可能是錯誤的s/= /!=/ – 2010-07-30 18:45:33