首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何在C中构造我正在尝试构造的Othello板数组?

我如何在C中构造我正在尝试构造的Othello板数组?
EN

Stack Overflow用户
提问于 2021-02-26 00:48:45
回答 2查看 103关注 0票数 0

我正在尝试用C编写一个基于文本的奥赛罗引擎,作为开始学习C的一种方式。我已经在高级语言中实现了这一点,所以我决定在C中尝试一下,因为基本的逻辑是正确的,并且可以工作。

我尝试将电路板表示为一个8x8数组,它可以使用一个函数动态重置。

板子应该是这样的:

代码语言:javascript
复制
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * w b * * *
* * * b w * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *

我尝试将一个指向存储电路板的数组的指针传递到resetBoard函数中,这样我就可以随时重置电路板。怎样才能让线路板用相应的字符更新数组?

这就是我正在尝试的:

代码语言:javascript
复制
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");
    }

}

当我尝试编译时,我得到了以下错误消息:

代码语言:javascript
复制
.\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] = "*";

我只是尝试将字符赋值给boardi,而不是boardi,但是它提供了这个错误:

代码语言:javascript
复制
.\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编程或任何低级编程都是完全陌生的,所以欢迎任何帮助!

非常感谢!

EN

回答 2

Stack Overflow用户

发布于 2021-02-26 00:52:12

  • 传递整个数组board (指向的第一个元素的指针),而不是第一行。
  • board的每个元素的元素都是char,因此应该使用字符常量,如'w',而不是字符串文字

代码语言:javascript
复制
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");
    }

}
票数 1
EN

Stack Overflow用户

发布于 2021-02-26 10:51:16

以下是建议的代码:

根据更改的原因,在每个更改的行上干净地为所需的functionality

  • eliminates‘compiles

  • performs’numbers

  • appropriately设置垂直间隔的'main()'

  • includes readability

  • commented。

  • 使用'logical‘OR而不是'bit wise’OR。

  • 为包含‘main()’

  • includes原型的头文件使用两个有效签名之一的printf()

(

  1. ) #include

现在,建议的代码如下:

代码语言:javascript
复制
// 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");
    }
}

运行建议的代码会产生以下结果:

代码语言:javascript
复制
 *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  * 
 *  *  *  *  w  b  *  * 
 *  *  *  *  b  w  *  * 
 *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  * 

注意:起始位置的偏移量1太低,1太右。这是因为在C中,数组从0开始,而不是1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66372854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档