-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.c
152 lines (116 loc) · 2.87 KB
/
Board.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "Board.h"
int winning_combos_len = 8;
int winning_combos[] = {
448, 56, 7, // Rows b111000000 b000111000 b000000111
292, 146, 73, // Cols b100100100 b010010010 b001001001
273, 84 // Diags b100010001 b001010100
};
Board*
b_init(void)
{
Board* new_board = malloc(sizeof(Board));
if (new_board == NULL)
{
perror(__func__);
exit(EXIT_FAILURE);
}
new_board->x_pieces = 0; // b 000 000 000
new_board->o_pieces = 0; // b 000 000 000
new_board->empty = 511; // b 111 111 111
new_board->x_to_move = true;
return new_board;
}
Board*
b_clone(Board* board)
{
Board* clone = malloc(sizeof(Board));
if (clone == NULL)
{
perror(__func__);
exit(EXIT_FAILURE);
}
clone->x_pieces = board->x_pieces;
clone->o_pieces = board->o_pieces;
clone->empty = board->empty;
clone->x_to_move = board->x_to_move;
return clone;
}
void
b_move(Board* board, int move)
{
if (move < 0 || move >= 9)
{
fprintf(stderr, "Error: move must be an integer between 0 and 8"
": recieved %d.\n", move);
exit(EXIT_FAILURE);
}
if (board->x_to_move)
board->x_pieces |= (1 << move);
else
board->o_pieces |= (1 << move);
board->empty &= (~(1 << move));
board->x_to_move = !(board->x_to_move);
}
bool
b_move_validate(Board* board, int move)
{
return ((board->empty & (1 << move)) != 0);
}
void
b_print(Board* board)
{
char pcs[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
for (int i = 0; i < 9; ++i)
{
if ((board->x_pieces & (1 << i)) != 0)
pcs[i] = 'X';
else if ((board->o_pieces & (1 << i)) != 0)
pcs[i] = 'O';
}
printf("\n");
printf("+-+-+-+\n");
printf("|%c|%c|%c|\n", pcs[0], pcs[1], pcs[2]);
printf("+-+-+-+\n");
printf("|%c|%c|%c|\n", pcs[3], pcs[4], pcs[5]);
printf("+-+-+-+\n");
printf("|%c|%c|%c|\n", pcs[6], pcs[7], pcs[8]);
printf("+-+-+-+\n");
printf("\n");
}
Result
b_result(Board* board)
{
for (int i = 0; i < winning_combos_len; ++i)
{
int combo = winning_combos[i];
if ((board->x_pieces & combo) == combo)
return X_WIN;
if ((board->o_pieces & combo) == combo)
return O_WIN;
}
return (board->empty == 0 ? DRAW : NIL);
}
char*
b_result_str(Result res)
{
char* res_str = NULL;
switch (res)
{
case X_WIN :
res_str = strdup("The winner is X!");
break;
case O_WIN :
res_str = strdup("The winner is O!");
break;
case DRAW :
res_str = strdup("The game is a draw.");
break;
default :
break;
}
return res_str;
}