I'm trying to make a tic-tac-toe game and I'm using a separate method that would update the board after each turn. However, I can't seem to be able to access the 2D array board, which was initialised in main().
I've tried making it public, like you would in Java, but it doesn't seem to work.
Any help would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#define public
#define static
#define private static
int main(void) {
public char board[5][8] = { " | | ", "--+--+--", " | | ", "--+--+--", " | | " };
int turn = 0;
public static int base = 0;
for (size_t row = 0; row < sizeof(board) / sizeof(board[0]); row++) {
for (size_t col = 0; col < sizeof(board[0]) / sizeof(board[0][0]); col++)
printf("%c", board[row][col]);
printf("\n");
}
printf("Player1's turn\n ");
fflush(stdout);
scanf("%i", &turn);
updateBoard(&turn);
}
void updateBoard(int a){
switch (a) {
case 1:
board[0][0] = 'X';
break;
case 2:
board[0][4] = 'X';
break;
case 3:
board[0][6] = 'X';
break;
case 4:
board[2][0] = 'X';
break;
case 5:
board[2][4] = 'X';
break;
case 6:
board[2][6] = 'X';
break;
case 7:
board[4][0] = 'X';
break;
case 8:
board[4][4] = 'X';
break;
case 9:
board[4][6] = 'X';
break;
default:
printf("Please input a correct value (1-9)");
}
for (size_t row = 0; row < sizeof(board) / sizeof(board[0]); row++) {
for (size_t col = 0; col < sizeof(board[0]) / sizeof(board[0][0]); col++)
printf("%c", board[row][col]);
printf("\n");
}
base = 1;
}
#define private staticseems bad.statichas multiple meanings in c. It can either mean "scope to this translation unit (eg *.c file) only" if it is on a declaration outside the scope of a function, and inside a function scope it means (simplifying) "retain the value of this variable on repeated calls of this function." Also by doing#define static, you could be breaking a lot of things without even knowing it, eg if the header files havestaticin them anywhere.