Tic-Tac-Toe使用C++和多维数组

Tic Tac Toe using C++ and multidimensional arrays

本文关键字:数组 使用 C++ Tic-Tac-Toe      更新时间:2023-10-16

感谢大家的帮助,我是编程新手,技术不太熟练,所以我很感谢大家的耐心。我开始工作了!代码中有几个问题,从用于检查"游戏结束"的for循环到用于设置棋盘的计数器。但最终的代码似乎没有bug。它在控制台上打印出一个带有标记方块的网格,允许两个人一起玩。现在很开心。

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool over = false;
void print_board(char board[3][3])
{
    cout<<"n------------------n";
    cout<<"n  "<<board[0][0]<<"  |  "<<board[0][1]<< "  |  "<<board[0][2];
    cout<<"n------------------n";
    cout<<"n  "<<board[1][0]<<"  |  "<<board[1][1]<< "  |  "<<board[1][2];
    cout<<"n------------------n";
    cout<<"n  "<<board[2][0]<<"  |  "<<board[2][1]<< "  |  "<<board[2][2];
    cout<<"n------------------n";
}
bool checkForCorrectMove (char board[3][3], int choice, bool player1Turn)
{
    int xCor;
    int yCor;
    xCor = ((choice + 2) / 3) - 1; 
    yCor = (choice - 1) % 3;
    if (((board[xCor][yCor]) != 'X') && ((board[xCor][yCor]) != 'O'))
    {
        if (player1Turn)
            board[xCor][yCor] = 'X';
        if (!player1Turn)
            board[xCor][yCor] = 'O';
        return true;
    }
    else 
        return false;
}
bool checkRow(char board[3][3])
{
    for (int i = 0; i<3; i++)
    {
        if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]))
        {
            return true;
        }
    }
    return false;
}
bool checkColumn (char board[3][3])
{
    for (int i = 0; i<3; i++)
    {
        if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]))
        {
            return true;
        }
    }
    return false;
}
bool checkForWin (char board[3][3])
{
    bool win = false;
    bool line_win = false;
    if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
    {
        win = true;
        return win;
    }
    if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2]))
    {
        win = true;
        return win;
    }
    if (checkColumn(board) || checkRow (board))
    {
        win = true;
        return win;
    }
    return win;
}
int main()
{
    cout << "Welcome to Tic Tac Toe!n";
    int choice;
    int boardcounter = 1;
    bool isOkayToMove = true;
    bool player1Turn = true;
    char board[3][3];
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[i][j] = boardcounter + '0';
            boardcounter++;
        }
    }
    print_board(board);
    do {
        if (player1Turn)
        {
            cout <<"nnPlayer 1, please enter a number that corresponds to an open space: ";
        }
        else
        {
            cout <<"nnPlayer 2, please enter a number that corresponds to an open space: ";
        }
        cin>>choice;
        isOkayToMove = checkForCorrectMove(board, choice, player1Turn);
        if (isOkayToMove)
        {
            over = checkForWin(board);
            print_board(board);
            player1Turn = !player1Turn;
        }
        else
        {
            cout <<"nnYou have attempted to move into a space that is already occupied, please try again.";
            print_board(board);
        }
    }
    while (over == false);
    cout <<"nnCongratulations!";
    player1Turn = !player1Turn;
    if (player1Turn)
        cout<<" Player 1 has won!";
    else
        cout<<" Player 2 has won!";
}

我试图用C++创建一个井字游戏,但我很难。我知道以前有人问过这个问题,但通过看别人的例子,很难弄清楚我的特定代码(我正在学习C++)出了什么问题。这是代码:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool over = false;
void print_board(char board[3][3])
{
    cout<<"n------------------n";
    cout<<"n  "<<board[0][0]<<"  |  "<<board[0][1]<< "  |  "<<board[0][2];
    cout<<"n------------------n";
    cout<<"n  "<<board[1][0]<<"  |  "<<board[1][1]<< "  |  "<<board[1][2];
    cout<<"n------------------n";
    cout<<"n  "<<board[2][0]<<"  |  "<<board[2][1]<< "  |  "<<board[2][2];
    cout<<"n------------------n";
}
bool checkForCorrectMove (char board[][3], int choice, bool player1Turn)
{
    int xCor;
    int yCor;
    switch(choice)
    {
        case 1: xCor = 0, yCor = 0;
        case 2: xCor = 0, yCor = 1;
        case 3: xCor = 0, yCor = 2;
        case 4: xCor = 1, yCor = 0;
        case 5: xCor = 1, yCor = 1;
        case 6: xCor = 1, yCor = 2;
        case 7: xCor = 2, yCor = 0;
        case 8: xCor = 2, yCor = 1;
        case 9: xCor = 2, yCor = 2;
    }
    if (((board[xCor][yCor]) != 'X') && ((board[xCor][yCor]) != 'O'))
    {
        if (player1Turn)
            board[xCor][yCor] = 'X';
        if (!player1Turn)
            board[xCor][yCor] = 'O';
        return true;
    }
    else 
        return false;
}
bool checkForWin (char board[][3])
{
    bool win = false;
    if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
    {
        win = true;
    }
    else if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2]))
    {
        win = true;
    }
    for (int i = 0; i<3; i++)
    {
        for (int j = 0; i < 3; j++) 
        {
            if (board[i][j] != board [i][0]);
            win = false;
        }
    }
    for (int i = 0; i<3; i++)
    {
        for (int j = 0; i < 3; j++) 
        {
            if (board[i][j] != board [0][j]);
            win = false;
        }
    }
    return win;
}
int main()
{
    cout << "Welcome to Tic Tac Toe!n";
    int choice;
    int boardcounter = 1;
    bool isOkayToMove = true;
    bool player1Turn = true;
    char board[3][3];
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[i][j] = (char) boardcounter;
            boardcounter++;
        }
    }
    print_board(board);
    do {
        if (player1Turn)
        {
            cout <<"nnPlayer 1, please enter a number that corresponds to an open space: ";
        }
        else
        {
            cout <<"nnPlayer 2, please enter a number that corresponds to an open space: ";
        }
        cin>>choice;
        switch(choice);
        isOkayToMove = checkForCorrectMove(board, choice, player1Turn);
        if (isOkayToMove)
        {
            print_board(board);
            over = checkForWin(board);
            player1Turn = !player1Turn;
        }
        else
        {
            cout <<"nnYou have attempted to move into a space that is already occupied, please try again.";
            print_board(board);
        }
    }
    while (over == false);
    cout <<"nnCongratulations!";
    if (player1Turn)
        cout<<" Player 1 has won!";
    else
        cout<<" Player 2 has won!";
}

我发现了一个问题:

    int boardcounter = 1;
//...
        for (int j = 0; j < 3; j++)
        {
            board[i][j] = (char) boardcounter;
            boardcounter++;
        }

问题是强制转换(char)不会将int变量转换为文本表示。强制转换实际上将整数转换为较小大小的整数变量。

有许多方法可以将数字转换为字符,例如snprintftostringostringstream

由于你有一个有限的范围,即数字0-9,你可以逃脱:

board[row][column] = '0' + boardcounter;

我建议您查看您的代码,找到其他将整数转换为char的地方,并相应地进行更改。

顺便说一句,大多数Tic-Tac-Toe游戏都使用"、"X"answers"O"作为值。