나는 tic-tac-toe 프로그램을 만들고 있는데 동점이 있는지 정확하게 확인하기 위해 게임을 시작할 수 없습니다. 보드의 모든 숫자가 'X'또는 'O'로 채워질 때 넥타이를 선언해야합니다. & 승자가 없습니다.동점 게임 초보자를 확인하는 방법 C++
코드를 사용하면 프로그램을 실행할 때마다 매듭이 있다고 선언합니다. 함수를 잘못 두었습니까? 뭔가 잘못된 것 같아요 tieGame()
불리 안의 내부.
using namespace std;
char board[9] { //array of characters with number placeholders for chars X and O
'1', '2', '3', '4', '5', '6', '7', '8', '9'
};
bool checkWinner(void) {
bool winner = false;
if // Check for possible winning solutions for X
((board[0] == 'X' && board[1] == 'X' && board[2] == 'X')
||
(board[3] == 'X' && board[4] == 'X' && board[5] == 'X')
||
(board[6] == 'X' && board[7] == 'X' && board[8] == 'X')
||
(board[0] == 'X' && board[4] == 'X' && board[8] == 'X')
||
(board[2] == 'X' && board[4] == 'X' && board[6] == 'X')
||
(board[0] == 'X' && board[3] == 'X' && board[6] == 'X')
||
(board[1] == 'X' && board[4] == 'X' && board[7] == 'X')
||
(board[2] == 'X' && board[5] == 'X' && board[8] == 'X'))
{
winner = 1; // Winner is true if conditions are met
cout << "Player 1 Wins!" << endl;
}
else if // Check for possible winning solutions for O
((board[0] == 'O' && board[1] == 'O' && board[2] == 'O')
||
(board[3] == 'O' && board[4] == 'O' && board[5] == 'O')
||
(board[6] == 'O' && board[7] == 'O' && board[8] == 'O')
||
(board[0] == 'O' && board[4] == 'O' && board[8] == 'O')
||
(board[2] == 'O' && board[4] == 'O' && board[6] == 'O')
||
(board[0] == 'O' && board[3] == 'O' && board[6] == 'O')
||
(board[1] == 'O' && board[4] == 'O' && board[7] == 'O')
||
(board[2] == 'O' && board[5] == 'O' && board[8] == 'O'))
{
winner = 1; // winner is True if conditions are met
cout << "Player 2 Wins!" << endl;
}
return winner; // Is there a winner?
}
bool tieGame() {
bool tiegame = false;
if // check for tie
((board[0] == 'X' || 'O') && (board[1] == 'X' || 'O') && (board[2] == 'X' || 'O')
&&
(board[3] == 'X' || 'O') && (board[4] == 'X' || 'O') && (board[5] == 'X' || 'O')
&&
(board[6] == 'X' || 'O') && (board[7] == 'X' || 'O') && (board[8] == 'X' || 'O'))
{
tiegame = 1;
cout << "The game is a tie! Play again!" << endl;
}
else {
tiegame = 0;
}
return tiegame; // Is the game a tie?
}
void displayBoard(void) { //Displays the game board
int index; // used to access the array
index = 0;
cout << endl;
cout << board[index] << "|" << board[index+1] << "|" << board[index+2] << endl;
cout << "-----" << endl;
cout << board[index+3] << "|" << board[index+4] << "|" << board[index+5] << endl;
cout << "-----" << endl;
cout << board[index+6] << "|" << board[index+7] << "|" << board[index+8] << endl;
}
void tictactoe(void) { //Main function; displays board and inputs player moves
int movePosition; // used to track user input and replace array indexes with the user input
cout << "Player 1 is X, player 2 is O" << endl;
for (int i=0; i < 5; i++) {
if (tieGame()) {
cout << "Tie game!" << endl;
return;
}
displayBoard(); // Display game board with updated characters
if (checkWinner()) //if winner is TRUE, return "Winner" and exit game.
{
cout << "Good Game!" << endl;
return;
}
cout << "Player 1, Enter the space number where you would like to place X" << endl;
cin >> movePosition; // Retrieve user input & call it 'movePosition'
while ((board[movePosition - 1] == 'X' || board[movePosition - 1] == 'O')) { //Check to make sure a user has not
cout << "This space is already taken. Please choose an open space." << endl; // attempted to enter a
cin >> movePosition; // value that has already been entered
}
board[movePosition - 1] = 'X';
displayBoard(); // Display game board with updated characters
if (checkWinner()) //if winner is TRUE, return "Winner" and exit game.
{
cout << "Good Game!" << endl;
return;
}
cout << "Player 2, Enter the space number where you would like to place O" << endl;
cin >> movePosition;
while ((board[movePosition - 1] == 'X' || board[movePosition - 1] == 'O')) {
cout << "This space is already taken. Please choose an open space." << endl;
cin >> movePosition;
}
board[movePosition - 1] = 'O';
}
}
int main (int argc, char *argv[]) {
tictactoe();
}
코드 벽. 첫 번째 생각 : 모든 가능한 고유 한 라인을 확인하는 이유 - 루프에 대해 들어 본 적이 없습니까? – usr2564301