简体   繁体   English

c++ 2D 数组在 If 语句逻辑失败井字游戏

[英]c++ 2D Array in If statement logic failing tic-tac-toe

TLDR: I am making a tic tac toe game in c++. TLDR:我正在 c++ 中制作井字游戏。 My win condition checking if statements are failing and I don't know why:(我的获胜条件检查语句是否失败,我不知道为什么:(

The board state is maintained by a global 2D board array板子 state 由全局二维板阵列维护

int board[3][3]{ {0,0,0}, {0,0,0}, {0,0,0}};

As the game plays on, a 1 or 2 is inserted to represent an X or O. Periodically I check for a winner with a function that uses 8 if statements to check for win conditions (3 horizontals, 3 verticals, or 2 diagonals).随着游戏的进行,插入 1 或 2 来代表 X 或 O。我会定期使用 function 检查获胜者,该 function 使用 8 个if语句来检查获胜条件(3 个水平、3 个垂直或 2 个对角线)。

int winCheck()
{
    int winner = 0;
    
    // check horizontal 1
    if ((board[0][0] == board[0][1] == board[0][2]) && (board[0][2] > 0))
    {
        winner = board[0][0];
    }

    // check horizontal 2

    ...

    return winner;
}

Any ideas?有任何想法吗? I think the logic is fine but my syntax is off.我认为逻辑很好,但我的语法不正确。

Sigh.叹。 After pulling my brain out for 4 hours.把我的大脑拉出来4个小时后。 It appears that you can't logically compare 3 things in an if statement ie:看来您无法在 if 语句中从逻辑上比较 3 件事,即:

if (A == B == C)

You must instead do 2 comparisons...您必须改为进行 2 次比较...

if (A == B && B == C)

Maybe this will help someone someday...也许有一天这会对某人有所帮助......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM