I am attempting to write a text based Othello engine in C as a way of starting to learn C. I have already got this working in higher level languages so have decided to give it a go in C as the basic logic is correct and working.
I am trying to represent the board as an 8x8 array, which can be dynamically reset using a function.
The board should look like so:
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * w b * * *
* * * b w * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
I am trying to pass a pointer to the array storing the board into the resetBoard function so that I can reset the board at any time. How can I get the board to update the array with the corresponding characters?
This is what I am trying:
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");
}
}
And when I try to compile i get the following error message:
.\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] = "*";
I have tried just assigning the character to board[i] instead of board[i][j] however that provides this error:
.\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] = "*";
So I know I have multiple problems. I am completely new to C programming, or any low level programming for that matter so any help would be welcomed!
Many thanks!
board
, not the first row.board
arechar
, so character constants like'w'
should be used instead of string literals"w"
.