C++;提克托问题 我正在努力学习C++,我已经做了一些简单的游戏,但有些事情不对。我试着让优胜者的职业既空虚又乏味。但是,当我输入一个坐标时,它将执行类。为了简单起见,只有当上面的3个是O时,你才能赢。怎么了? 所以,如果我输入:0,它表示赢家

C++;提克托问题 我正在努力学习C++,我已经做了一些简单的游戏,但有些事情不对。我试着让优胜者的职业既空虚又乏味。但是,当我输入一个坐标时,它将执行类。为了简单起见,只有当上面的3个是O时,你才能赢。怎么了? 所以,如果我输入:0,它表示赢家,c++,boolean,void,C++,Boolean,Void,代码如下: #include <iostream> const int rows = 3; const int elements = 3; const char Ochar = 'O'; char board[rows][elements]; void Clear() { for (int i = 0; i < rows; i++) { for (int j = 0; j < elements; j++) {

代码如下:

#include <iostream>

const int rows = 3;
const int elements = 3;

const char Ochar = 'O';

char board[rows][elements];

void Clear()
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < elements; j++)
        {
            board[i][j] = 0;
        }
    }
}

void Show()
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < elements; j++)
        {
            std::cout << " " << board[i][j] << " |";
        }
        std::cout << std::endl;
        std::cout << "------------" << std::endl;
    }
}

bool PlayerAttack(int x, int y)
{
    if (board[x][y] == 0)
    {
        board[x][y] = Ochar;
        return true;
    }
    return false;
}

void Winner()
{
    if (board[0][0], board[0][1], board[0][2] = 'O')
    {
        std::cout << "Winner";
    }
}

int main()
{
    Clear();
    Show();
    int pos1 = 0;
    int pos2 = 0;
    while (1)
    {
        std::cout << "Please input a coordinate: "; std::cin >> pos1 >> pos2; std::cout <<     std::endl;
        PlayerAttack(pos1, pos2);
        Show();
        Winner();
    }
}
#包括
const int rows=3;
常量int元素=3;
常量char Ochar='O';
字符板[行][元素];
无效清除()
{
对于(int i=0;istd::cout这条线并不像你想象的那样

if (board[0][0], board[0][1], board[0][2] = 'O')
你必须这么做

if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
使用
Winner
函数中断循环

bool Winner()
{
    // You'll obviously have to check more than just this row
    if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
    {
        std::cout << "Winner";
        return true;
    }
    return false;
}

这条线并不像你想象的那样

if (board[0][0], board[0][1], board[0][2] = 'O')
你必须这么做

if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
使用
Winner
函数中断循环

bool Winner()
{
    // You'll obviously have to check more than just this row
    if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
    {
        std::cout << "Winner";
        return true;
    }
    return false;
}

它们被称为函数,而不是类(Cube,Stand,…)。如果(板[ 0 ] [ 0 ],板[ 0 ] [1 ],板[0 ] [2 ] =“O”)< /代码>这是一个公然的误解。读关于C++和运算符。它们被称为函数,而不是类(清除,显示,…)。
这是一个明显的误解。阅读C++和运算符。但是当我获胜时,我想从循环中解开我该怎么做?那个代码会评估表达式板[0 ] [0 ],并且板[0 ] [1 ]和板[0 ] [2 ]顺序地,忽略第一个2的值(逗号运算符),并且将“O”分配给板[0 ] [2 ]。(=是赋值运算符,比较运算符应该是==)。逗号很少使用,不能像这样使用。然后它返回赋值“O”。因为“O”(即一些ASCII字符代码)不是零,conditional为true,它进入块并打印“Winner”。但当我获胜时,我想从循环中跳出来。我该怎么做?该代码将依次计算表达式board[0][0],board[0][1]和board[0][2],忽略前2的值(逗号运算符),并将“O”赋值给board[0][2]。(=是赋值运算符,比较运算符将为=).Comma很少使用,不能像这样使用。然后它返回赋值“O”。因为“O”(也称为某种ASCII字符代码)不是零,所以conditional为true,它进入块并打印“Winner”。