我正试图在C中编写一个基于文本的奥赛罗引擎,作为开始学习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编程,或任何低级编程的问题,所以任何帮助将是受欢迎的!
多谢!
- 传递(指向第一个元素的指针)整个数组
board
,而不是第一行 board
的每个元素的元素都是char
,所以应该使用像'w'
这样的字符常量来代替字符串字面量"w"
。
int resetBoard(char board[][8]) { /* change argument type to accept the whole array */
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'; /* use character constant */
} else if ((i == 5 && j == 4) | (i == 4 && j == 5)) {
board[i][j] = 'b'; /* use character constant */
} else {
board[i][j] = '*'; /* use character constant */
}
}
}
return 0;
}
void main() {
char board[8][8];
resetBoard(board); /* pass the whole array */
for (int i = 0; i<8; i++) {
for (int j = 0; j<8; j++) {
char x = board[i][j];
printf(" %c ", x);
}
printf("n");
}
}
建议代码如下:
- 干净地编制
- 执行所需的功能
- 消除'魔术'数字
- 垂直间距适当,便于阅读
- 对每一行的修改进行注释,说明修改的原因。
- 使用'逻辑'或而不是'位明智'或
- 为'main()' 使用两个有效签名之一
- 包含(缺失的)
#include
,用于包含printf()
原型的头文件
现在,建议代码:
// note vertical spacing for readability
#include <stdio.h> // added for 'printf()'
// to give 'magic' numbers meaningful names
#define ROWS 8
#define COLS 8
// return type modified as nothing uses the returned value
void resetBoard(char board[ROWS][COLS]) // valid parameter, which compiler needs
{
int i;
int j;
for (i = 0; i< ROWS; i++) // ROWS rather than 'magic' number
{
for (j = 0; j< COLS; j++) // COLS rather than 'magic' number
{
if ((i == 4 && j == 4) || (i == 5 && j == 5)) // || for logical OR
{
board[i][j] = 'w'; // 'w' for a single char rather than array
}
else if ((i == 5 && j == 4) || (i == 4 && j == 5)) // || for logical OR
{
board[i][j] = 'b'; // 'b' for a single char rather than an array
}
else // these lines modified to follow axiom:
// only one statement per line and (at most) one variable declaration per statement
{
board[i][j] = '*'; // '*' for a single char rather than an array
}
}
}
}
int main( void ) // valid C signature for 'main()'
{
char board[ROWS][COLS]; // rather than 'magic' numbers
resetBoard(board); // since 'board[][]' is array, bare reference degrades to address of first byte of array
for (int i = 0; i<ROWS; i++) // use meaningful name rather than 'magic' number
{
for (int j = 0; j<COLS; j++) // use meaningful name rather than 'magic' number
{
char x = board[i][j];
printf(" %c ", x);
}
printf("n");
}
}
建议代码的运行结果是:
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * w b * *
* * * * b w * *
* * * * * * * *
* * * * * * * *
注意:起始位置是向下偏移1,向右偏移1。这是因为在C语言中,数组从0开始,而不是1