2011-03-27 3 views
-1
#include <iostream> 
using namespace std; 

void CreateCharBoard(int Board[][3], char CharBoard[][3]); 
void PrintBoard(char CharBoard[][3]); 
bool CheckWinner(int Board[][3], int Player); 
void ResetBoard(int Board[][3]); 
bool CheckMove(int Board[][3], int Row, int Column); 

int main(){ 
    // delcare and initialize (if necessary) all required variables here 
    int Board[3][3]; 
    char CharBoard[3][3]; 
    int Player, Row, Column; 
    bool Win = false; 
    char Again = 'y'; 
    // Print lab header and if desired, statement of what this lab is doing 
    //this line of code will pause the program until the user hits any key to continue 
    system("pause"); 

    while (Again == 'y'){ 
     ResetBoard(Board); 
     system("clear"); 
     while (Win == false){ 
      CreateCharBoard(Board, CharBoard); 
      PrintBoard(CharBoard); 
      cout << "Player O's turn!" << endl; 
      cout << "Enter row number: "; cin >> Row; 
      cout << "Enter column number: "; cin >> Column; 
      CheckMove(Board, Row, Column); 
      //update board 
      Win = CheckWinner(Board, -1); 
      if (CheckWinner (Board, -1) == true){ 
       cout << "Player O wins!" << endl; 
       break; 
      } 
      //set flag to denote it's x's turn 
      PrintBoard(CharBoard); 
      cout << "Player X's turn!" << endl; 
      cout << "Enter row number: "; cin >> Row; 
      cout << "Enter column number: "; cin >> Column; 
      CheckMove(Board, Row, Column); 
      //update board 
      Win = CheckWinner (Board, 1); 
      if (CheckWinner (Board, 1) == true){ 
       cout << "Player X wins!" << endl; 
       break; 
      } 
      } 

      cout << endl << endl; 
      cout << "Would you like to play again? (y/n): "; 
      cin >> Again; 
      } 

      cout << endl << endl; 
      cout << "Good bye!" << endl; 

      return 0; 
      } 

      // function to take game board and create a board with corresponding characters 
      void CreateCharBoard(int Board[][3], char CharBoard[][3]) 
      { 
       for (int i = 0; i < 3; i++){ 
        for (int j = 0; j < 3; j++){ 
         // if game board has a -1, put an "O" in the character board 
         if (Board[i][j]== -1){ 
          CharBoard[i][j] = 'O'; 
         } 
         // if game board has a 1, put an "X" in the character board 
         else if (Board[i][j]== 1){ 
          CharBoard[i][j] = 'X'; 
         } 
         // if game board has a 0, put an " " (blank space) in the character board 
         else{ 
          CharBoard[i][j] = ' '; 
         } 
        } 
       } 
      } 
      // print the character representation of the game board 
      void PrintBoard(char CharBoard[][3]){ 
       cout << " Tic Tac Toe!" << endl << endl; 
       cout << " 0  1  2" << endl << endl; 
       cout << "0 " << CharBoard[0][0] << " | " << CharBoard[0][1] << " | " << CharBoard[0][2] << endl; 
       cout << " -----------------" << endl; 
       cout << "1 " << CharBoard[1][0] << " | " << CharBoard[1][1] << " | " << CharBoard[1][2] << endl; 
       cout << " -----------------" << endl; 
       cout << "2 " << CharBoard[2][0] << " | " << CharBoard[2][1] << " | " << CharBoard[2][2] << endl << endl; 
      } 
      // function to check if a player, either "X" or "O" has won the game 
      bool CheckWinner(int Board[][3], int Player){ 
       int Sum; 
       // check each column 
       for (int i = 0; i < 3; i++){ 
        Sum = Board[i][0] + Board[i][1] + Board[i][2]; 
        // if O is a winner 
        if (Sum == 3*Player){ 
         return true; 
        }  
       } 
       // check each row 
       for (int j = 0; j < 3; j++){ 
        Sum = Board[0][j] + Board[1][j] + Board[2][j]; 
        // if O is a winner 
        if (Sum == 3*Player){ 
         return true; 
        } 
       } 
       // check diagonal 
       Sum = Board[0][0] + Board[1][1] + Board[2][2]; 
       if (Sum == 3*Player){ 
        return true; 
       } 
       Sum = Board[0][2] + Board[1][1] + Board[2][0]; 
       if (Sum == 3*Player){ 
        return true; 
       } 
       // if none of the rows, columns, or diagonals = 3*player, then return false 
       return false; 
      } 
      // function to reset all elements of the game board back to 0 
      void ResetBoard(int Board[][3]){ 
       for (int i = 0; i < 3; i++){ 
        for (int j = 0; j < 3; j++){ 
         Board[i][j] = 0; 
        } 
       } 
      } 
      // function to see if player's move is valid, if 0 (or not played) return true, otherwise false 
      bool CheckMove(int Board[][3], int Row, int Column){ 
       if ((Row < 0) || (Row > 2)){ 
        return false; 
       } 
       else if ((Column < 0) || (Column > 2)){ 
        return false; 
       } 
       else if (Board[Row][Column] == 0){ 
        return true; 
       } 
       else{ 
        return false; 
       } 
      } 
+0

'while (Win == false)'등 나쁜 스타일입니다. 대신'while (! Win)'을 사용하십시오. –

답변

1

귀하의 Board을 최신으로 업데이트합니다.

Board에 무엇이든 쓸 수있는 유일한 행은 으로 모든 값을 다시 설정하는 ResetBoard()입니다. 게임 루프에서

은 두 줄 // update board이다 - 나는 당신이 그의 기능을 구현하는 것을 잊었다 생각 ...

기타 결함 :

  • 당신은 두 번 CheckWinner 전화 - 한 번 Win을 설정 수표를 한 번, 사용자가 이겼다면.

  • CheckMove의 반환은 사용되지 않습니다. 플레이어가 유효한 데이터를 입력 할 때까지 입력을 읽는 루프가 있어야합니다.

  • 한 플레이어가 승리하면 while -loop을 종료합니다. 수표 while (Win == false)은 결코 사용되지 않으며 누구에게나 표시하려면 while(true)이어야합니다.

관련 문제