我正在尝试用 C 编写一个基于文本的 Othello 引擎,作为开始学习 C 的一种方式。我已经在更高级别的语言中使用它,所以决定在 C 中尝试一下,因为基本逻辑是正确的并且有效。
我试图将板表示为 8x8 数组,可以使用函数动态重置。
董事会应该是这样的:
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * w b * * *
* * * b w * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
我正在尝试将指向存储板的数组的指针传递到 resetBoard 函数中,以便我可以随时重置板。我怎样才能让板子用相应的字符更新数组?
这就是我正在尝试的:
int resetBoard(char *board) {
int i;
int j;
for (i = 0; i<8; i++) {
for (j = 0; j<8; j++) {
if ((i == 4 && j == 4) | (i == 5 && j == 5)) {
board[i][j] = "w";
} else if ((i == 5 && j == 4) | (i == 4 && j == 5)) {
board[i][j] = "b";
} else {
board[i][j] = "*";
}
}
}
return 0;
}
void main() {
char board[8][8];
resetBoard(*board);
for (int i = 0; i<8; i++) {
for (int j = 0; j<8; j++) {
char x = board[i][j];
printf(" %c ", x);
}
printf("\n");
}
}
当我尝试编译时,我收到以下错误消息:
.\Othello.c: In function 'resetBoard':
.\Othello.c:10:25: error: subscripted value is neither array nor pointer nor vector
board[i][j] = "w";
^
.\Othello.c:12:25: error: subscripted value is neither array nor pointer nor vector
board[i][j] = "b";
^
.\Othello.c:14:25: error: subscripted value is neither array nor pointer nor vector
board[i][j] = "*";
我尝试将字符分配给 board[i] 而不是 board[i][j] ,但这会提供此错误:
.\Othello.c: In function 'resetBoard':
.\Othello.c:10:26: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
board[i] = "w";
^
.\Othello.c:12:26: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
board[i] = "b";
^
.\Othello.c:14:26: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
board[i] = "*";
所以我知道我有很多问题。我对 C 编程或任何低级编程完全陌生,因此欢迎任何帮助!
非常感谢!