连接四个set token到右列c



我需要编程连接四个游戏。我已经有了布局从游戏板和打印。现在我想将X和O设置为玩家输入的字段(列)。你知道我该怎么做吗?它不应该像一百个if一样,我想过一个switch case或一个struct,但我真的没有考虑过struct。

如果你已经在多维数组中拥有了游戏表,那么你可以通过一些循环轻松地处理整个布局打印。下面是我编写的如何处理逻辑的快速演示:

#include <stdio.h>
#include <stdlib.h>
#define ROWS (6)
#define COLUMNS (7)
#define PLAYER_1 ('X')
#define PLAYER_2 ('O')
#define EMPTY ('#')
int main() {
// we have 6 rows, 7 colums each
char board[ROWS][COLUMNS];
for(int y = 0; y < ROWS; y++) {
for(int x = 0; x < COLUMNS; x++) {
board[y][x] = EMPTY;
}
}
// we receive some input from player 1
// NOTE: starting at 0 nad origin of table is top left corner
int row = 4, column = 2;
// we change the state of the field if not already set
if(board[row][column] != EMPTY) {
printf("field already occupiedn");
return 1;
}
// set directly to player 1 status
board[row][column] = PLAYER_1;
// print table
for(int y = 0; y < ROWS; y++) {
for(int x = 0; x < COLUMNS; x++) {
int value = board[y][x];
printf("%c ", value);
}
printf("n");
}
return 0;
}