0

我不确定如何从已有的内容着手:我无法让我的阵列正常工作,而且每次重新运行程序时我的图表重置也有问题。它不会保存我的 X 和 O,而是重新启动到空白图表中。任何提示都会有所帮助。谢谢

/**
Lab 4 - Due 7/22/2010.

Convert Lab 3 to a class

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>

#include <limits>

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;

    for (int i = 0; i < 9; i++) {
        ttt.playGame();
    }
}

void TicTacToe::playGame()
{
    displayBoard();


    getMove();    

    // Your implementation here...
}

void TicTacToe::displayBoard()
{
    // Your implementation here...

    bool firstMove = true;
    if (firstMove  == true)
    {

        for (int i = 0; i < 9; i++) {
            board[i] = i + 1;
        }

    }

    firstMove == false;
    for (int i = 0; i < 9; i++) 
    {
        if ( (i+1) % 3 == 0 )
        {
            cout << board[i] << endl;
        }
        else 
        {

            cout << board[i] << " | ";
        }
    }
}

void TicTacToe::getMove()
{
    if (player == 'X') { 
        player = 'O';
    }
    else {
        player = 'X';
    }

    cout << player << " ";
    cout << "Enter Box: ";
    char c;


    bool move;
    move = true;

    do {
        cin.get(c);
        cin.ignore(numeric_limits<int>::max(), '\n');
        if (c > '9' || c < '0')
            // error message
            cout << "please enter a number 1-9" << endl;

        int number = c - '0';

        cout << "your number is " << number << endl;
        // Your implementation here...

        if (c == '1' && board[0] == '1') {
            board[0] = player;

            move = false;
        }
        else if(c == '2' && board[1] == '2')
        {
            board[1] = player;
            move = false;
        }
        else if(c == '3' && board[2] == '3')
        {
            board[2] = player;
            move = false;
        }
        else if(c == '4' && board[3] == '4')
        {
            board[3] = player;
            move = false;
        }
        else if(c == '5' && board[4] == '5')
        {
            board[4] = player;
            move = false;
        }
        else if(c == '6' && board[5] == '6')
        {
            board[5] = player;
            move = false;
        }
        else if(c == '7' && board[6] == '7')
        {
            board[6] = player;
            move = false;
        }
        else if(c == '8' && board[7] == '8')
        {
            board[7] = player;
            move = false;
        }
        else if(c == '9' && board[8] == '9')
        {
            board[8] = player;
            move = false;
        }

    } while (!move);     
}

@Bunnit 根据您的建议,我能够对其进行更改。现在我唯一的问题是用 X 或 O 替换每个框,这似乎不像我的 if 和 else if 工作。

4

3 回答 3

2

您是否使用 DisplayBoard 功能来显示您的电路板?

    bool firstMove = true;
    if (firstMove  == true)
    {

        for (int i = 0; i < 9; i++) {
            board[i] = i + 1;
        }

    }

我不确定您要在这里实现什么,但 firstMove 将始终为真,因此每次调用时您的电路板都会被重置。也在下面:

firstMove == false;

此行没有任何效果,将 firstMove 设置为 false 使用firstMove = false;虽然上面的 if 语句仍将每次调用,因为每次调用函数时 firstMove 都设置为 true,如果您只想在第一次调用 DisplayBoard 时将 firstMove 设置为 true您可以使用静态变量: static firstMove == true;.

编辑

看起来您将字符与整数混淆了,在 DisplayBoard 中,您将每个板图块设置为等于其对应的数字(上面的 for 语句),但是当您在 getMove 函数中检查时,您使用字符等价物:if (c == '1' && board[0] == '1')。1!='1'。由于这是家庭作业,我将把它留给您查找 ascii 图表以找出它的作用。

于 2011-07-29T05:42:59.823 回答
0

而不是用数字填充板,然后用 X 和 O 覆盖,您应该只以整数格式输入。这可能有助于解决覆盖问题。另外,我不知道您是否涵盖了 2D 数组,但如果您使用它们,它将有助于您的编程和逻辑思维过程。

于 2011-07-29T18:31:24.010 回答
0

当一个程序结束时,它的所有内存都被操作系统回收,以便其他程序可以使用它。程序结束时,变量和数组中的所有值都会丢失。

如果您想在下次程序运行之前保持一些持久状态,您需要将变量/数组保存到文件或数据库等存储介质中。当程序稍后启动时,您从存储介质中加载这些变量/数组。

如果他们还没有教您如何读/写文件,那么我怀疑他们是否希望您的游戏板在每次程序启动时都恢复到以前的状态。

于 2011-07-29T05:48:15.807 回答