0

我正在尝试用 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 编程或任何低级编程完全陌生,因此欢迎任何帮助!

非常感谢!

4

2 回答 2

1
  • 传递(指向第一个元素的指针)整个数组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");
    }

}
于 2021-02-25T16:52:12.033 回答
0

以下建议的代码:

  1. 干净地编译
  2. 执行所需的功能
  3. 消除“魔术”数字
  4. 适当的垂直间隔以提高可读性
  5. 对每个更改的行评论更改的原因。
  6. 使用“逻辑”或而不是“位”或
  7. 使用“main()”的两个有效签名之一
  8. 包括(缺少)#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

于 2021-02-26T02:51:15.650 回答