值不保存在数组c ++中

时间:2018-10-13 02:36:00

标签: c++

我一直试图做一个tictactoe游戏,但是在我接受输入之后,数组内的值没有改变。一切看起来都很好,任何人都可以帮助我。我已经多次重写了代码,还检查了很多次,但是没有运气。

#include <iostream>
using namespace std;

char gameArray[3][3] =
{ 
    '1', '2', '3', 
    '4', '5', '6', 
    '7', '8', '9' 
}; //making the board while also initializing it

char player = 'X'; //setting a default player
void switchPlayer() //Switches the player
{
    if (player == 'X')
    {
        player = 'O';
    }
    else
    {
        player = 'X';
    }
}
void input() //taking input for the game
{
    int userInput;
    cin >> userInput;
    if (userInput == 1)
    {
        gameArray[0][0] == player;
    }
    else if (userInput == 2)
    {
        gameArray[0][1] == player;
    }
}

void draw() //display the board
{
    cout << "\t  " << gameArray[0][0] << "|  " 
                   << gameArray[0][1] << "|  "
                   << gameArray[0][2] << endl;
    cout << "\t __|___|____\n";
    cout << "\t  " << gameArray[1][0] << "|  " 
                   << gameArray[1][1] << "|  "
                   << gameArray[1][2] << endl;
    cout << "\t __|___|____\n";
    cout << "\t  " << gameArray[2][0] << "|  " 
                   << gameArray[2][1] << "|  "
                   << gameArray[2][2] << endl;
}

int main()
{
    cout << "Tic-Tac-Toe Game\n\n";
    draw();
    cout << "Player " << player << " turn. Enter the number to input: ";
    while (1)
    {
        input();
        draw();
        switchPlayer();
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您没有分配值

您必须更改此行代码

    gameArray[0][0] == player;
    gameArray[0][1] == player;

替换为该行

    gameArray[0][0] = player;
    gameArray[0][1] = player;