简体   繁体   English

Tic Tac Toe计划帮助

[英]Tic Tac Toe Program help

I am not sure how to proceed from what I have already: I am having trouble getting my array to work and I am also having trouble with my chart reseting everytime i re-run the program. 我不知道如何从我已经拥有的东西开始:我无法让我的阵列工作,并且每次重新运行程序时我的图表重置也遇到了问题。 It does not save my X's and O's but restarts into the blank chart. 它不会保存我的X和O,而是重新开始进入空白图表。 Any hint will be helpful. 任何提示都会有所帮助。 Thanks 谢谢

/**
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 From your advice i was able to change it up. @Bunnit从你的建议我能够改变它。 Now my only problem is replacing each box with an X or O it doesnt seem like my if and else if are working. 现在我唯一的问题是用X或O替换每个盒子它看起来不像我的if和else如果正在工作。

Are you using the DisplayBoard function to display your board? 您是否使用DisplayBoard功能来显示您的主板?

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

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

    }

I'm not sure exactly what you are trying to achieve here but firstMove will always be true so your board will be getting reset everytime this is called.Also below this: 我不确定你到底想要实现什么,但是第一次移动将永远是真的,所以每次调用时你的电路板都会被重置。另外在这下面:

firstMove == false;

this line has no effect, to set firstMove to false use firstMove = false; 这行没有效果,将firstMove设置为false使用firstMove = false; though the if statement above will still be called each time as firstMove is set to true everytime the function is called, if you only want to set firstMove to true on the first call to DisplayBoard you could use a static variable: static firstMove == true; 虽然每次调用函数时firstMove设置为true时仍然会调用上面的if语句,如果你只想在第一次调用DisplayBoard时将firstMove设置为true,你可以使用一个静态变量: static firstMove == true; .

Edit 编辑

It also looks like the you have confused chars with ints, in DisplayBoard you set each board tile to be equal to it's corresponding number(the for statement above), but when you check this in the getMove function you use the character equivalents: if (c == '1' && board[0] == '1') . 它看起来像你有混乱的字符与int,在DisplayBoard中你设置每个板块等于它的相应数字(上面的for语句),但是当你在getMove函数中检查这个时你使用等价的字符: if (c == '1' && board[0] == '1') 1!='1'. 1!= '1'。 Since it is homework I'll leave it to you to look up an ascii chart to find out what it does equal. 既然是作业,我会留给你查找一个ascii图表来找出它的相同之处。

When a program ends, all of its memory is reclaimed by the operating system so that it can be used by other programs. 当程序结束时,操作系统将回收其所有内存,以便其他程序可以使用它。 All values in variables and arrays are lost when a program ends. 程序结束时,变量和数组中的所有值都将丢失。

If you want to maintain some persistent state until the next time the program is run, you need to save your variables/arrays to some storage medium like a file or a database. 如果要在下次运行程序时保持某种持久状态,则需要将变量/数组保存到某些存储介质(如文件或数据库)中。 When the program is later started, you load those variables/arrays back from the storage medium. 稍后启动程序时,将从存储介质中加载这些变量/数组。

If they didn't teach you how to read/write files yet, then I doubt they expect your game board to be restored to it's previous state every time your program starts. 如果他们没有教你如何读/写文件,那么我怀疑他们希望你的游戏板在每次程序启动时都恢复到之前的状态。

Rather than filling the board with numbers and then overwriting with X's and O,s you should just take an input in an integer format. 而不是用数字填充电路板然后用X和O覆盖,你应该只采用整数格式的输入。 That might help with the overwriting problem. 这可能有助于覆盖问题。 Also, I don't know if you've covered 2D Arrays or not, but it would help your programming and logical thought process a bit more if you used them. 另外,我不知道你是否已经覆盖了2D阵列,但如果你使用它们,它将有助于你的编程和逻辑思维过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM