-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.c
99 lines (83 loc) · 2.08 KB
/
logic.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
90
91
92
93
94
95
96
97
98
99
#include "./game.h"
#include "./logic.h"
void switch_player(game_t *game)
{
if (game->player == PLAYER_X) {
game->player = PLAYER_O;
} else if (game->player == PLAYER_O) {
game->player = PLAYER_X;
}
}
int check_player_won(game_t *game, int player)
{
int row_count = 0;
int column_count = 0;
int diag1_count = 0;
int diag2_count = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (game->board[i * N + j] == player) {
row_count++;
}
if (game->board[j * N + i] == player) {
column_count++;
}
}
if (row_count >= N || column_count >= N) {
return 1;
}
row_count = 0;
column_count = 0;
if (game->board[i * N + i] == player) {
diag1_count++;
}
if (game->board[i * N + N - i - 1] == player) {
diag2_count++;
}
}
return diag1_count >= N || diag2_count >= N;
}
int count_cells(const int *board, int cell)
{
int count = 0;
for (int i = 0; i < N * N; ++i) {
if (board[i] == cell) {
count++;
}
}
return count;
}
void game_over_condition(game_t *game)
{
if (check_player_won(game, PLAYER_X)) {
game->state = PLAYER_X_WON_STATE;
} else if (check_player_won(game, PLAYER_O)) {
game->state = PLAYER_O_WON_STATE;
} else if (count_cells(game->board, EMPTY) == 0) {
game->state = TIE_STATE;
}
}
void player_turn(game_t *game, int row, int column)
{
if (game->board[row * N + column] == EMPTY) {
game->board[row * N + column] = game->player;
switch_player(game);
game_over_condition(game);
}
}
void reset_game(game_t *game)
{
game->player = PLAYER_X;
game->state = RUNNING_STATE;
for (int i = 0; i < N * N; ++i) {
game->board[i] = EMPTY;
}
}
void click_on_cell(game_t *game, int row, int column)
{
if (game->state == RUNNING_STATE) {
player_turn(game, row, column);
} else {
reset_game(game);
}
}