我通过Xcode(Mac)开始连接4游戏。我将值扫描到电路板尺寸的全局阵列(2个位置),并打印出具有这些尺寸的游戏板。在那部分之后,全局数组中的值返回为{0,0},因为我初始化它们,而它们应该保持扫描值。我尝试通过Windows上的代码块运行程序,它没有这个问题。我该如何解决?
这是初始化和打印主板中的板尺寸值部分:
#define BOARD_MAX_SIDE 25
int main()
{
int board_size[2]={0}, player1=1;
char board[BOARD_MAX_SIDE][BOARD_MAX_SIDE]={0}, color=0;
print_initial(board, board_size, color);
printf("SET: %d By %d\n", board_size[0], board_size[1]);
这是print_initial函数,包含打开消息和电路板打印:
void print_initial(char board[BOARD_MAX_SIDE][BOARD_MAX_SIDE],
int board_size[], char color)
{
printf("got size: %d %d\n", board_size[0], board_size[1]);
printf("Welcome to 4-in-a-row game!");
do {
printf("Please choose starting color (Y)ellow or (R)ed:");
scanf("%s", &color);
} while (color!='R' && color!='Y');
do{
printf("Please enter number of rows:");
scanf("%d", board_size);
} while (board_size[0]<=0 || board_size[0]>25);
do{
printf("Please enter number of columns:");
scanf("%d", board_size+1);
} while (board_size[1]<=0 || board_size[1]>25);
print_board(board, board_size);
}
这是打印电路板的功能:
#define IS_IN_RANGEI(VAL, MIN, MAX) (((VAL)>=(MIN)) && ((VAL)<=(MAX)))
enum {
BOARD_VERT_SEP=(int)'|',
BOARD_LEFT_ANG=(int)'\\',
BOARD_RIGHT_ANG=(int)'/',
BOARD_BOTTOM=(int)'-',
BOARD_BOTTOM_SEP=(int)'-'
};
void print_board(char board[][BOARD_MAX_SIDE], int board_side[])
{
assert(IS_IN_RANGEI(board_side[0], 0, BOARD_MAX_SIDE)
&& IS_IN_RANGEI(board_side[1], 0, BOARD_MAX_SIDE));
for (int row=0; row<board_side[0]; ++row)
{
printf("\n%c", (char)BOARD_VERT_SEP);
for (int col = 0; col < board_side[1]; ++col)
printf("%c%c", board[row][col], (char)BOARD_VERT_SEP);
}
printf("\n%c", (char)BOARD_LEFT_ANG);
for (int ii=0; ii<board_side[1]-1; ++ii)
printf("%c%c", (char)BOARD_BOTTOM, (char)BOARD_BOTTOM_SEP);
printf("%c%c\n", (char)BOARD_BOTTOM, (char)BOARD_RIGHT_ANG);
}
这就是程序打印的内容:
Welcome to 4-in-a-row game!
Please choose starting color (Y)ellow or (R)ed: R
Please enter number of rows: 5
Please enter number of columns: 4
|||||
|||||
|||||
|||||
|||||
\-------/
SET: 0 By 0
谢谢你!