Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Tic Tac Toe编程帮助C++;_C++ - Fatal编程技术网

C++ Tic Tac Toe编程帮助C++;

C++ Tic Tac Toe编程帮助C++;,c++,C++,我的主要问题是,如何让代币(X和O)出现在游戏板上?我必须向用户询问行号和列号,令牌将放在那里。正如你可能知道的,我对编程是新手。我已经做了好几个小时了,我只想完成。在这里问是我最后的办法。如果任何代码可以以不同的方式或更有效地完成,或者没有必要,或者如果我做错了什么,请让我知道。我没有主意了。我只是想完成这个程序,所以你越详细越好。我非常感激。我很困惑,真的需要建议来完成这件事 到目前为止,我的代码是: #include <iostream> using namespace std

我的主要问题是,如何让代币(X和O)出现在游戏板上?我必须向用户询问行号和列号,令牌将放在那里。正如你可能知道的,我对编程是新手。我已经做了好几个小时了,我只想完成。在这里问是我最后的办法。如果任何代码可以以不同的方式或更有效地完成,或者没有必要,或者如果我做错了什么,请让我知道。我没有主意了。我只是想完成这个程序,所以你越详细越好。我非常感激。我很困惑,真的需要建议来完成这件事

到目前为止,我的代码是:

#include <iostream>
using namespace std;

void createBoard(int board[][3], char charBoard[][3]);
void printBoard(char charBoard[][3]);
int checkWinner (int board[][3], int player);
bool checkMove(int board[][3], int row, int column);
void resetBoard(int board[][3]);

int main ()
{
    int board[3][3];
    char charBoard[3][3];
    int player;
    int row, column;
    bool win = false;
    char again = 'y';

    while (win == false)
    {
        createBoard(board, charBoard);
        printBoard(charBoard);
        cout << "Player O's turn!" << endl;
        cout << "Enter row number: "; cin >> row;
        cout << "Enter column number: "; cin >> column;
        checkMove(board, row, column);
        //update board
        win = checkWinner(board, -1);
        if (checkWinner (board, -1)  == true)
        {
            cout << "Player O wins!" << endl;
            break;
        }
        //set flag to denote it's x's turn
        printBoard(charBoard);
        cout << "Player X's turn!" << endl;
        cout << "Enter row number: "; cin >> row;
        cout << "Enter column number: "; cin >> column;
        checkMove(board, row, column);
        //update board
        win = checkWinner (board, 1);
        if (checkWinner (board, 1) == true)
        {
            cout << "Player X wins!" << endl;
            break;
        }


        cout << endl << endl;
        cout << "Would you like to play again? (y/n): ";
        cin >> again;

        }
        cout << endl << endl;
        cout << "Good bye!" << endl;

        return 0;
}



void createBoard(int board[][3], char charBoard[][3])
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            // if game board has a -1, put an "O" in the character board
            if (board[i][j]== -1)
            {
                charBoard[i][j] = 'O';
            }
            // if game board has a 1, put an "X" in the character board
            else if (board[i][j]== 1)
            {
                charBoard[i][j] = 'X';
            }
            // if game board has a 0, put an " " (blank space) in the character board
            else
            {
                charBoard[i][j] = ' ';
            }
        }
    }
}

void printBoard(char charBoard[][3])
{
    cout << "       Tic Tac Toe!" << endl << endl;
    cout << "          Column\n";
    cout << "       0     1     2" << endl << endl;
    cout << "R 0    " << charBoard[0][0] << "  |  " << charBoard[0][1] << "  |  " << charBoard[0][2] << endl;
    cout << "    --------------------" << endl;
    cout << "O 1    " << charBoard[1][0] << "  |  " << charBoard[1][1] << "  |  " << charBoard[1][2] << endl;
    cout << "    --------------------" << endl;
    cout << "W 2    " << charBoard[2][0] << "  |  " << charBoard[2][1] << "  |  " << charBoard[2][2] << endl << endl;
}

int checkWinner (int board[][3], int player)
{
    int sum;

    // Check each column for a winner
    for (int i = 0; i < 3; i++)
    {
        sum = board[i][0] + board[i][1] + board[i][2];
        // if O is a winner
        if (sum == 3*player)
        {
            return true;
        }
    }

    // Check each row for a winner
    for (int j = 0; j < 3; j++)
    {
        sum = board[0][j] + board[1][j] + board[2][j];
        // if O is a winner
        if (sum == 3*player)
        {
            return true;
        }
    }
    // Check the back and front diagonal
    sum = board[0][0] + board[1][1] + board[2][2];
    if (sum == 3*player)
    {
        return true;
    }

    sum = board[0][2] + board[1][1] + board[2][0];
    if (sum == 3*player)
    {
        return true;
    }

    return false;
}

bool checkMove(int board[][3], int row, int column)
{
    if ((row < 0) || (row > 2))
    {
        return false;
    }
    else if ((column < 0) || (column > 2))
    {
        return false;
    }
    else if (board[row][column] == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void resetBoard(int board[][3])
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[i][j] = 0;
        }
    }
}
#包括
使用名称空间std;
void createBoard(int board[][3],char charBoard[][3]);
无效印刷板(字符字符板[][3]);
int checkWinner(int board[][3],int player);
bool checkMove(int board[][3],int行,int列);
无效复位板(int板[][3]);
int main()
{
国际板[3][3];
炭纤维板[3][3];
国际球员;
int行,列;
布尔温=假;
char再次='y';
while(win==false)
{
createBoard(board,charBoard);
印刷板(纸板);
cout柱;
选中移动(板、行、列);
//更新板
win=支票赢家(棋盘,-1);
如果(选中赢家(棋盘,-1)==真)
{

cout看起来您应该在“checkMove”函数中插入适当的值:

else if (board[row][column] == 0)
{
    board[row][column] = value;    // value -1 or 1
    return true;
}

其中“value”是-1还是1取决于您是插入X还是O。函数本身没有任何关于谁添加标记的信息,因此您可以向函数中添加另一个参数以传入“value”。

我正在考虑您的问题,但应该提到的是
if(checkWinner(board,1)==true)
是多余的,
if(checkWinner(board,1))
也会做同样的事情。我想你还有其他问题。似乎在玩家赢了之后,代码会退出,并且在每次回合后(在某人赢之前),他们都会被询问是否想再次玩。