【问题标题】:How can I construct the Othello board array I am attempting to construct in C?如何构建我试图用 C 构建的奥赛罗棋盘阵列?
【发布时间】:2021-05-28 01:39:23
【问题描述】:

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

非常感谢!

【问题讨论】:

  • board是一个二维数组,因此int resetBoard(char *board)应该是int resetBoard(char** board)
  • @RobertHarvey 这是不正确的。 char ** 表示指向 char * 的指针
  • @RobertHarvey:二维数组不是“一维数组指针的一维数组”。遗憾的是,在 C 语言中,两种情况下的访问使用相同的语法(例如,x = array[y][z];` 可以像 x = *(array + y*8 + z); 或像 temp = array[y]; x = temp[z];,这取决于 array 是什么),这会造成很多混乱。

标签: arrays c pointers othello


【解决方案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");
    }

}

【讨论】:

  • 谢谢,我想我现在明白了...我对将数组传递给函数感到困惑 - 假设您必须主动将指针传递给数组而不仅仅是数组.. . 所以你把数组的变量名传给函数,但是函数必须接受这样的数组?
  • 投了反对票,因为代码中仍有几个错误
  • @user3629249:Stack Overflow 的章程不包括修复某人代码中的所有错误。
【解决方案2】:

以下建议的代码:

  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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-04
    • 2020-12-22
    • 2017-05-18
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多