-1

重複の可能性:
C++ Tic Tac Toe Game

私は最善を尽くしましたが、これが私がこれまでに持っているものです。助けてください。これは、完了する必要がある私のコードです。

  1. Tic Tac Toe ボードを表示するための displayBoard を実装します。
  2. ボード上のボックスを選択するようにユーザーに促します。つまり、1 が左上隅である 1 から 9 までの数字です。

    cin.get(box) を使用してボックス番号を取得し、isdigit を使用してそれが数字であることを確認します。1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 ボックスが利用可能な場合は、適切な X または O をそこに入れ、プレーヤーを切り替えます。つまり、X は O になり、その逆も同様です。ボックスが利用できない場合は、ユーザーに警告し、有効なオープン ボックスを選択するまで別のボックスを取得します。

  3. すべてのスポットを選択したら、「ゲーム オーバー!」を表示します。

  4. TicTacToe クラスを使用するメイン関数を作成し、上記のすべての機能をテストします。

    #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...
    }
    

これは私がこれまでに持っているものです。

#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;
4

1 に答える 1

0

これは課題か宿題の問題だと思いますか?

いくつかの空のメソッドの実装を埋めるように求めていることに注意してください。したがって、作成したコードを最初のリストのフレームワークに組み込む必要があります。そのリストの TicTacToe クラスは、char の配列でボードを定義することにも注意してください。

たとえば、次のようになります。

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;
}
于 2011-07-27T07:00:08.780 に答える