-
Notifications
You must be signed in to change notification settings - Fork 5
/
go-game.c
89 lines (74 loc) · 2 KB
/
go-game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define BOARD_SIZE 19
typedef enum { EMPTY, BLACK, WHITE } player_t;
typedef struct {
player_t board[BOARD_SIZE][BOARD_SIZE];
} game_t;
void init_game(game_t *game) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
game->board[i][j] = EMPTY;
}
}
}
void print_board(game_t game) {
printf(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%2d ", i);
}
printf("\n");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%2d ", i);
for (int j = 0; j < BOARD_SIZE; j++) {
switch (game.board[i][j]) {
case BLACK:
printf(" B ");
break;
case WHITE:
printf(" W ");
break;
default:
printf(" . ");
break;
}
}
printf("%2d\n", i);
}
printf(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%2d ", i);
}
printf("\n");
}
bool is_valid_move(game_t game, int row, int col, player_t player) {
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
return false;
}
if (game.board[row][col] != EMPTY) {
return false;
}
return true;
}
void make_move(game_t *game, int row, int col, player_t player) {
game->board[row][col] = player;
}
int main() {
game_t game;
init_game(&game);
player_t current_player = BLACK;
while (true) {
print_board(game);
int row, col;
printf("%s's turn (row col): ", current_player == BLACK ? "Black" : "White");
scanf("%d %d", &row, &col);
if (!is_valid_move(game, row, col, current_player)) {
printf("Invalid move\n");
continue;
}
make_move(&game, row, col, current_player);
current_player = current_player == BLACK ? WHITE : BLACK;
}
return 0;
}