2014-05-08 1 views
-6

나는 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(); 
} 
+3

코드 벽. 첫 번째 생각 : 모든 가능한 고유 한 라인을 확인하는 이유 - 루프에 대해 들어 본 적이 없습니까? – usr2564301

답변

2

을 확인 물론, 항상 진실입니다. 따라서 모든 분야와 보드 전체에서 항상 성공합니다.

당신은이 같은 두 개의 비교에 명시 적으로 작성해야합니다 :

bool tieGame() 
{ 
    for (int i = 0; i < 9; i++) { 
     if (board[i] != 'X' && board[i] != 'O') { 
      // Some field is empty, not a tie 
      return false; 
     } 
    } 
    // All fields are either 'X' or 'O' 
    cout << "The game is a tie! Play again!" << endl; 
    return true; 
} 
: 미래를위한

(board[0] == 'X' || board[0] == 'O') 

조건의 무리보다 더 나은 솔루션은, 예를 들어 루프가 될 것입니다

그리고 더 나은 점은 Nenad이 답을 썼습니다. 남은 공백 수 (또는 사용 된 필드)를 계산하십시오. 매번 전체 보드를 통과하는 대신 하나의 변수 비교 일뿐입니다.

+0

대단히 감사합니다 -이 프로젝트의 카운터를 사용하기로 결정했습니다. 간단하고 효과적입니다. 나는 모든 도움을 주셔서 감사합니다! – Ephexx

0

당신이 때마다 플레이어를 감소시킵니다

int freeSpaces = 9; 

보드에 빈 슬롯을 채우는 카운터를 가짐으로써 동점 인 경우에 당신은 확인할 수 있습니다.

(board[0] == 'X') || ('O' != 0) 

번째 부분 :

때문에 C++ 연산자 우선 순위 평가 규칙
(board[0] == 'X' || 'O') 

컴파일러로 이해 : 그런 다음, 다음과 같은 조건이 잘못

if (freeSpaces == 0 && !winner) tieGame = true; 
else tieGame = false; 
0

(board[0] == 'X' || 'O')의 표현식은 'O'이 0이 아닌 값 (정확히는 79)이므로 항상 true으로 평가됩니다. 따라서 tieGame에 대한 모든 검사가 사실입니다. 원하는 것은 (board[0] == 'X' || board[0] == 'O')입니다.

관련 문제