我正在尝试用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] = "*";我只是尝试将字符赋值给boardi,而不是boardi,但是它提供了这个错误:
.\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编程或任何低级编程都是完全陌生的,所以欢迎任何帮助!
非常感谢!
发布于 2021-02-26 00:52:12
board (指向的第一个元素的指针),而不是第一行。board的每个元素的元素都是char,因此应该使用字符常量,如'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-26 10:51:16
以下是建议的代码:
根据更改的原因,在每个更改的行上干净地为所需的functionality
printf()(
#include现在,建议的代码如下:
// 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
https://stackoverflow.com/questions/66372854
复制相似问题