C++ Tic Tac Toe project

C++ Tic Tac Toe project

本文关键字:project Toe Tac Tic C++      更新时间:2023-10-16

我现在正在做一个Tic-Tac-Toe项目。我遇到的问题是,每当我向控制台输入坐标时,例如大小为[15][15]的数组的[6][0](程序会将标记"X"或"O"放在那个位置),它会自动将标记"X"或"O"保存到另一个不在数组范围内的位置(在我的情况下是[5][15])。这是我的程序(P/S:我是越南人,所以忽略越南语中的部分):

int size = 15;
int inputAmount;
int inputX = 0;
int inputY = 0;
char board[15][15];
bool checkWin = false;
char mark = 'X';
// Interdisciplinary examination of scale loss
bool checkWinLose() {
    int max;
    int x,y;
    x = inputX;
    y = inputY;
    // Looking horizontally under investigation
    for (; max < 3; max++)
    {
        if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x - 1][y] && board[x - 1][y] == board[x - 2][y]))
        {
            cout << "Game over ngang!" << endl;
            return 1;
        }
        x++;
    }
    x = inputX;
    y = inputY;
    max = 0;
    // Interdisciplinary examination of vertical
    for (; max < 3; max++)
    {
        if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x][y - 1] && board[x][y - 1] == board[x][y - 2]))
        {
            cout << "Game over doc!" << endl;
            return 1;
        }
        y++;
    }
    x = inputX;
    y = inputY;
    max = 0;
    // Interdisciplinary examination of under the cliff cave from left to right
    for (; max < 3; max++)
    {
        if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x - 1][y - 1] && board[x - 1][y - 1] == board[x - 2][y - 2]))
        {
            cout << "Game over trai sang phai!" << endl;
            return 1;
        }
        x++;
        y++;
    }
    x = inputX;
    y = inputY;
    max = 0;
    // Interdisciplinary examination of under the cliff cave from right to left
    for (; max < 3; max++)
    {
        if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x + 1][y - 1] && board[x + 1][y - 1] == board[x + 2][y - 2]))
        {
            cout << "Game over phai sang trai!" << endl;
            return 1;
        }
        x--;
        y++;
    }
    // Flower test case
    if (inputAmount == 225)
    {
        cout << "Game over hoa!" << endl;
        return 1;
    }
}
// Lay-coordinate of the muon practice player list
void takeInput() {
    do {
        // Lay gia tri toa do x
        do {
            cout << "Please choose the horizontal (rightward) number (smaller than " << size + 1 << "): ";
            cin >> inputX;
        } while ((inputX > size) || (inputX <= 0));
        // Lay y coordinate values
        do {
            cout << "Please choose the vertical (downward) number (smaller than " << size + 1 << "): ";
            cin >> inputY;
        } while ((inputY > size) || (inputY <= 0));
        inputX--;
        inputY--;
        if (board[inputX][inputY] != '.')
            cout << "Already chosen!" << endl ;
    } while (board[inputX][inputY] != '.');
    board[inputX][inputY] = mark;
    if (mark == 'X')
        mark = 'O';
    else
        mark = 'X';
    cout << endl << endl << endl;
}
// Hien game board on the screen
void loadGameboard () {
    int x,y;
    ////TODO: check win or lose
    while (!checkWin) {
        for (; y < size ; y++)
        {
            for (; x < size ; x++)
            {
                cout << board[x][y] << "  ";
            }
            cout << endl;
            x = 0;
        }
        checkWin = checkWinLose();
        if (checkWin == true)
            return;
        x,y = 0;
        takeInput();
        inputAmount++;
    }
}
// At first preparation game board
void prepareGameboard () {
    int x,y;
    for (; y < size ; y++)
    {
        for (; x < size ; x++)
        {
            board[x][y] = '.' ;
        }
        x = 0;
    }
}
int main(array<System::String ^> ^args)
{
    char reset = 'y';
    do {
        prepareGameboard();
        loadGameboard();
        checkWin = 0;
        inputAmount = 0;
        cout << "Do you want to replay ? (y/n): ";
        cin >> reset;
        if ((reset == 'y') || (reset == 'Y'))
        {
            system("CLS");
        }
    } while ((reset == 'y') || (reset == 'Y'));
    return 0;
}

我会在粘贴的代码的第123行将x,y = 0更改为x = y = 0。并将return 0添加到函数checkWinLose()中。

您没有初始化其他for中的xy的值。

尝试使用:

void prepareGameboard () {
    int x,y;
    for (y = 0; y < size ; y++)
    {
        for (x = 0; x < size ; x++)
        {
            board[x][y] = '.' ;
        }
    }
}

和:

void loadGameboard () {
    int x,y;
    ////TODO: check win or lose
    while (!checkWin) {
        for (y = 0; y < size ; y++)
        {
            for (x = 0; x < size ; x++)
            {
                cout << board[x][y] << "  ";
            }
            cout << endl;
        }
        checkWin = checkWinLose();
        if (checkWin == true)
            return;
        takeInput();
        inputAmount++;
    }
}

此外,正如@Heinz所说,在checkWinLose函数末尾有一个return 0;

查看您的代码,我相信您假设int总是用0初始化,并且默认情况下checkWinLose将返回0。局部变量具有未定义的初始化值,当未调用显式返回时,函数只返回so"垃圾"值。

尝试始终将返回的值添加到函数并初始化变量(特别是计数器)。

更新

这就是checkWinLosereturn 0; 的功能

// Interdisciplinary examination of scale loss
bool checkWinLose() {
    int max;
    int x,y;
    x = inputX;
    y = inputY;
    // Looking horizontally under investigation
    for (; max < 3; max++)
    {
        if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x - 1][y] && board[x - 1][y] == board[x - 2][y]))
        {
            cout << "Game over ngang!" << endl;
            return 1;
        }
        x++;
    }
    x = inputX;
    y = inputY;
    max = 0;
    // Interdisciplinary examination of vertical
    for (; max < 3; max++)
    {
        if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x][y - 1] && board[x][y - 1] == board[x][y - 2]))
        {
            cout << "Game over doc!" << endl;
            return 1;
        }
        y++;
    }
    x = inputX;
    y = inputY;
    max = 0;
    // Interdisciplinary examination of under the cliff cave from left to right
    for (; max < 3; max++)
    {
        if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x - 1][y - 1] && board[x - 1][y - 1] == board[x - 2][y - 2]))
        {
            cout << "Game over trai sang phai!" << endl;
            return 1;
        }
        x++;
        y++;
    }
    x = inputX;
    y = inputY;
    max = 0;
    // Interdisciplinary examination of under the cliff cave from right to left
    for (; max < 3; max++)
    {
        if ((board[x][y] == 'X' || board[x][y] == 'O') && (board[x][y] == board[x + 1][y - 1] && board[x + 1][y - 1] == board[x + 2][y - 2]))
        {
            cout << "Game over phai sang trai!" << endl;
            return 1;
        }
        x--;
        y++;
    }
    // Flower test case
    if (inputAmount == 225)
    {
        cout << "Game over hoa!" << endl;
        return 1;
    }
    return 0; //<- Returning false if the all other tests failed
}