简体   繁体   English

在井字游戏中检查胜利

[英]Checking for Win in Tic-Tac-Toe

Hello I'm 17 and trying to learn how to code. 您好,我17岁,正在尝试学习编码。 I am having trouble debugging this section of code and would appreciate some help. 我在调试此部分代码时遇到麻烦,希望能提供一些帮助。

bool checkifwin(char grid[3][3], char player)
{
    if (checkverticle(char grid[3][3], char player) 
        || checkhorizontal(char grid[3][3], char player)
        || checkdiagonal( grid[3][3], player) 
       return true;
    else
       return false;
};

It says expected primary-expression before char. 它说预期的主要表达在字符之前。

checkverticle() is a call to a function not a declaration so you don't need the "char"s. checkverticle()是对函数的调用,而不是对声明的调用,因此您不需要“ char”。

bool checkifwin(char grid[3][3], char player)
{
    if ( checkverticle(grid, player) || 
         checkhorizontal(grid, player) || 
         checkdiagonal( grid, player) return true;
    else return false;
};

Just some coding advice. 只是一些编码建议。 In my opinion: 在我看来:

bool func()
{
    if (expr)
        return true;
    else
        return false;
}

Is not great style. 风格不是很好。 I'd suggest refactoring it to: 我建议将其重构为:

bool func()
{
    bool result = false; // This just makes it clear the expected default.
    // You could even go as far as
    // bool result = expr  or event return expr; - although I suggest sticking with a
    // variable in case you need to debug.
    result = expr; // e.g. result = checkVert() || checkHoriz() || checkDiag();
    return result;
}

There is no need to give type name while calling a function. 调用函数时无需提供类型名称。

if (checkverticle(char grid[3][3], char player) // Remove char here and in other cases

Regarding the error further you get - 关于错误,您进一步得到-

checkverticle( grid[3][3] ... )

Here grid[3][3] gives you the character at that index. 在这里grid[3][3]为您提供该索引处的字符。 What you really want to pass is grid itself as others suggested. 您真正想要传递的是grid本身,如其他人所建议的那样。 And ofcourse, be wary of the valid array indexes. 当然,要警惕有效的数组索引。

You're trying to pass type declarations as parameters. 您正在尝试将类型声明作为参数传递。 Try this instead: 尝试以下方法:

bool checkifwin(char grid[3][3], char player)
{
  return checkverticle(grid], player) || 
    checkhorizontal(grid, player) || 
    checkdiagonal(grid, player;

};

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

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