C++ Tic Tac Toe Game [duplicate]

梦想与她 提交于 2019-12-13 08:49:01

问题


Possible Duplicate:
C++ Tic Tac Toe Game

I tried my best and this is what I have so far, please help me out. This is my code that I need to complete.

  1. Implement displayBoard to display Tic Tac Toe board.
  2. Prompt User for a box on the board to select, i.e. a number between 1 and 9 with 1 being the upper left corner.

    use cin.get(box) to get the box number and isdigit to verify it is a number; 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 If the box is available put the appropriate X or O in there and switch players, i.e. X becomes O and vice versa. If the box is NOT available warn the user and get another box until they select a valid open box.

  3. After all spots have been select Display "Game Over!";

  4. Write a main function to use the TicTacToe class and test all of the above functionality.

    #include <iostream>
    using namespace std;
    
    
    class TicTacToe {
    public:
    void displayBoard();
    void getMove();
    void playGame();
    private:
    char board[9];
    char player; // Switch after each move.
    };
    
    int main ()
    {
    TicTacToe ttt;
    
    // you need to do the following in a loop 9 times
    ttt.playGame();
    }
    
    void TicTacToe::playGame()
    {
    getMove();
    // Your implementation here...
    }
    
    void TicTacToe::displayBoard()
    {
    // Your implementation here...
    }
    
    void TicTacToe::getMove()
    {
    cout << "Enter Box: ";
    char c;
    cin.get(c);
    if (c > '9' || c < '0')
        // Error message here.
    
    int number = c - '0';
    
    cout << "your number is " << number;
    // Your implementation here...
    }
    

This is what I have so far.

#include <iostream> 

using namespace std;

void displayBoard() ;
char cSquare1('1');
char cSquare2('2');
char cSquare3('3');
char cSquare4('4');
char cSquare5('5');
char cSquare6('6');
char cSquare7('7');
char cSquare8('8');
char cSquare9('9');
int iPlayerTurn(1);
bool bGameOver(true);


do {
    // Display Board
    std::cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << std::endl;
    std::cout << "-+-+-"<< std::endl;
    std::cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << std::endl;
    std::cout << "-+-+-"<< std::endl;
    std::cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << std::endl;

    // Switch After Each Move
    char cPlayerMark;
    if (iPlayerTurn == 1) {
        cPlayerMark = 'X';
    } else {
        cPlayerMark = 'O';
    }

    // Play Game
    std::cout << "Player" << iPlayerTurn << "'s move Enter Box: " << std::endl;
    bool bValidMove;
    // Loop until the Move is Valid
    do {
        char cNextMove;
        std::cin >> cNextMove;
        bValidMove = true;

        // Check for a valid move
        if (cNextMove == '1' && cSquare1 == '1') {
            cSquare1 = cPlayerMark;
        } else if (cNextMove == '2' && cSquare2 == '2') {
            cSquare2 = cPlayerMark;
        } else if (cNextMove == '3' && cSquare3 == '3') {
            cSquare3 = cPlayerMark;
        } else if (cNextMove == '4' && cSquare4 == '4') {
            cSquare4 = cPlayerMark;
        } else if (cNextMove == '5' && cSquare5 == '5') {
            cSquare5 = cPlayerMark;
        } else if (cNextMove == '6' && cSquare6 == '6') {
            cSquare6 = cPlayerMark;
        } else if (cNextMove == '7' && cSquare7 == '7') {
            cSquare7 = cPlayerMark;
        } else if (cNextMove == '8' && cSquare8 == '8') {
            cSquare8 = cPlayerMark;
        } else if (cNextMove == '9' && cSquare9 == '9') {
            cSquare9 = cPlayerMark;
        } else if (cNextMove > '9' || cNextMove < '0')

            std::cout << "error!”." << std::endl;
        return;
                }

                int number = cNextMove - '0';
          cout << "your number is " << number;

    } while (!bValidMove);

    bGameOver       = false;
    bool bWinGame   = true;
    // Check for end of game 
    if (cSquare1 != '1') {
        if (cSquare2 == cSquare1 && cSquare3 == cSquare1) {
            bGameOver = true;
        }
        if (cSquare4 == cSquare1 && cSquare7 == cSquare1) {
            bGameOver = true;
        }
    }
    if (cSquare5 != '5') {
        if (cSquare1 == cSquare5 && cSquare9 == cSquare5) {
            bGameOver = true;
        }
        if (cSquare2 == cSquare5 && cSquare8 == cSquare5) {
            bGameOver = true;
        }
        if (cSquare4 == cSquare5 && cSquare6 == cSquare5) {
            bGameOver = true;
        }
        if (cSquare3 == cSquare5 && cSquare7 == cSquare5) {
            bGameOver = true;
        }
    }
    if (cSquare9 != '9') {
        if (cSquare3 == cSquare9 && cSquare6 == cSquare9) {
            bGameOver = true;
        }
        if (cSquare7 == cSquare9 && cSquare8 == cSquare9) {
            bGameOver = true;
        }
    }
    // Check For Tie Game
    if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' &&
        cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
        cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' &&        

!bGameOver)
    {
        bGameOver = true;
        bWinGame = false;
    }

    if (bGameOver) {
        if (bWinGame) {
            std::cout << "Player" << iPlayerTurn << " wins!" <<   
std::endl;
        }
        // Display Board
        std::cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 <<  
std::endl;
        std::cout << "-+-+-"<< std::endl;
        std::cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << 
std::endl;
        std::cout << "-+-+-"<< std::endl;
        std::cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 <<   
std::endl;

        std::cout << "Game Over!" << std::endl;

回答1:


I'm assuming this is an assignment or homework problem?

Notice that it's asking you to fill in the implementation of some empty methods. So you need to work the code you've written into the framework of the first listing. Note also that the TicTacToe class in that listing defines the board with a array of char.

So, for example:

void TicTacToe::displayBoard(){
    std::cout << board[1] << "|" << board[2]<< "|" << board[3]<< std::endl;
    std::cout << "-+-+-"<< std::endl;
    std::cout << board[4] << "|" << board[5]<< "|" << board[6]<< std::endl;
    std::cout << "-+-+-"<< std::endl;
    std::cout << board[7] << "|" << board[8]<< "|" << board[9]<< std::endl;
}


来源:https://stackoverflow.com/questions/6840388/c-tic-tac-toe-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!