【问题标题】:c++ bool returning true [closed]c ++ bool返回true [关闭]
【发布时间】:2014-11-12 17:25:35
【问题描述】:

我正在制作一个井字游戏控制台应用程序,并且我已经完成了大部分工作。我还要添加一些东西,但我遇到了一个大问题。我有一个 bool checkwin() 函数,它应该查看游戏是否赢了,但由于某种原因,无论我的参数是否满足,这个函数总是返回 true。这会导致程序在第一次移动完成后结束。为什么会这样,我该如何解决?

bool checkwin( Boardstatus& BSTAT)
{
    //Row 'A' checkwin
    if (BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("A2") && BSTAT.getsquarestatus("A2")==BSTAT.getsquarestatus("A3"))
    {
        return true;
    }
    //Row 'B' checkwin
    else if (BSTAT.getsquarestatus("B1")==BSTAT.getsquarestatus("B2") && BSTAT.getsquarestatus("B2")==BSTAT.getsquarestatus("B3"))
    {
        return true;
    }
    //Row 'C' checkwin
    else if (BSTAT.getsquarestatus("C1")==BSTAT.getsquarestatus("C2") && BSTAT.getsquarestatus("C2")==BSTAT.getsquarestatus("C3"))
    {
        return true;
    }
    //Column 1 checkwin
    else if (BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("B1") && BSTAT.getsquarestatus("B1")==BSTAT.getsquarestatus("C1"))
    {
        return true;
    }
    //Column 2 checkwin
    else if (BSTAT.getsquarestatus("A2")==BSTAT.getsquarestatus("B2") && BSTAT.getsquarestatus("B2")==BSTAT.getsquarestatus("C2"))
    {
        return true;
    }
    //Column 3 checkwin
    else if (BSTAT.getsquarestatus("A3")==BSTAT.getsquarestatus("B3") && BSTAT.getsquarestatus("B3")==BSTAT.getsquarestatus("C3"))
    {
        return true;
    }
    //Diagonal upper-left->bottom-right checkwin
    else if (BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("B2") && BSTAT.getsquarestatus("B2")==BSTAT.getsquarestatus("C3"))
    {
        return true;
    }
    //Diagonal lower-left->upper-right checkwin
    else if (BSTAT.getsquarestatus("C1")==BSTAT.getsquarestatus("B2") && BSTAT.getsquarestatus("B2")==BSTAT.getsquarestatus("A3"))
    {
        return true;
    }
    else
    {
        return false;
    }
}



int main()
{
//Start of initializing all squares as a blank
Boardstatus BSTAT;
BSTAT.setsquarestatus( "A1", ' ' );
BSTAT.setsquarestatus( "A2", ' ' );
BSTAT.setsquarestatus( "A3", ' ' );
BSTAT.setsquarestatus( "B1", ' ' );
BSTAT.setsquarestatus( "B2", ' ' );
BSTAT.setsquarestatus( "B3", ' ' );
BSTAT.setsquarestatus( "C1", ' ' );
BSTAT.setsquarestatus( "C2", ' ' );
BSTAT.setsquarestatus( "C3", ' ' );
//End of square initialization

    do
    {
    playerturn(BSTAT);
    } while (checkwin(BSTAT) == false);

    return 0;
}

【问题讨论】:

  • 当你发现数组时你会如释重负。
  • 可能哈哈。我做这件事的时间不长,我还有很多东西要学。我会尝试使用数组而不是我现在正在做的那些乱七八糟的东西。

标签: c++ boolean tic-tac-toe


【解决方案1】:

它返回真,因为' ' 等于' '——也就是说,在第一次移动之后,你保证连续三个方块仍然是空的,你只测试方块值是否为 相等而不是它们相等且不为空

解决此问题的一种可能方法:

if (BSTAT.getsquarestatus("A1")!=' ' &&
    BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("A2") &&
    BSTAT.getsquarestatus("A2")==BSTAT.getsquarestatus("A3"))

您需要将此... != ' ' 检查添加到每个条件,显然会更改您测试的特定方格。

这可能很明显,但您只需测试其中一个方格,因为如果第一个方格不为空而其他方格与它相等,那么显然其他方格也不为空。

【讨论】:

  • 啊当然。我不知道我怎么会错过。太感谢了。另外作为旁注,为什么人们对我的帖子投反对票?我是这个网站的新手,我做错了什么我需要知道的吗?
  • @UnclePutin 在寻求有关错误的帮助时,将您的代码简化为一个完整的、可编译的示例来演示该错误但没有任何不必要的代码被认为是礼貌的做到这一点。这有几个名称:SSCCE、MVCE 等。当大部分与问题无关时粘贴代码墙是不受欢迎的。
  • 哦,好的,谢谢您的信息。对于那个很抱歉。将来我会牢记这一点。我会继续减少这个。
【解决方案2】:

您必须添加其他测试, 例如 : if (BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("A2") && BSTAT.getsquarestatus("A2")==BSTAT.getsquarestatus("A3") && BSTAT.getsquarestatus("A1") != ' ' && BSTAT.getsquarestatus("A2") != ' ' && BSTAT.getsquarestatus("A3") != ' ')

对所有测试做同样的事情

但是还有其他更好的方法来实现这个算法

【讨论】:

  • 有什么更好的方法呢?我很想听。
【解决方案3】:

通过使用矩阵。 int TicTac[3][3] 初始化为 0 1 --> 玩家 1 2 --> 玩家 2 和支票获胜者将是 ` for(int i=0;i if((TicTac[i][0] == 2 && TicTac[i][1] == 2 && TicTac[i][2] == 2) || (TicTac[i][0] == 1 && TicTac[i][1] == 1 && TicTac[i][2] == 1)) {

            return TicTac[i][0];
        }
        if((TicTac[0][i] == 2 && TicTac[1][i]  == 2 && TicTac[2][i] == 2) ||
            (TicTac[0][i] == 1 && TicTac[1][i]  == 1 && TicTac[2][i] == 1)) {

            return TicTac[0][i];
        }
    }
    if((TicTac[0][0] == 1 && TicTac[1][1] == 1  && TicTac[2][2] == 1 )||
            TicTac[0][0] == 2 && TicTac[1][1] == 2  && TicTac[2][2] == 2) {

        return TicTac[0][0];
    }
    if(TicTac[0][2] == 1 && TicTac[1][1] == 1 && TicTac[2][0] == 1 ||
        TicTac[0][2] == 2 && TicTac[1][1] == 2 && TicTac[2][0] == 2 ) {

        return TicTac[0][2];}
}`

【讨论】:

    猜你喜欢
    • 2012-02-27
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    相关资源
    最近更新 更多