Checking for Win in Tic-Tac-Toe
-
29-04-2021 - |
문제
Hello I'm 17 and trying to learn how to code. 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.
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. What you really want to pass is grid
itself as others suggested. 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;
};
제휴하지 않습니다 StackOverflow