-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
157 lines (132 loc) · 3.18 KB
/
game.cpp
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
152
153
154
155
156
157
#include "minimax.h"
int check_lig(ivec_t state){
for (int i = 0; i < NBC*NBL; i+=3)
{
if (state[i] == PLAYER_ONE &&
state[i+1] == PLAYER_ONE &&
state[i+2] == PLAYER_ONE)
{
return PLAYER_ONE;
}
else if (state[i] == PLAYER_TWO &&
state[i+1] == PLAYER_TWO &&
state[i+2] == PLAYER_TWO)
{
return PLAYER_TWO;
}
}
return NO_PLAYER;
}
int check_col(ivec_t state){
for (int i = 0; i < NBL; i++)
{
if (state[i] == PLAYER_ONE &&
state[i+3] == PLAYER_ONE &&
state[i+6] == PLAYER_ONE)
{
return PLAYER_ONE;
}
else if (state[i] == PLAYER_TWO &&
state[i+3] == PLAYER_TWO &&
state[i+6] == PLAYER_TWO)
{
return PLAYER_TWO;
}
}
return NO_PLAYER;
}
int check_diag(ivec_t state){
int const pos[]={0,8,2,6};
for (int i = 0; i < 4; i+=2)
{
if (state[4] == PLAYER_ONE &&
state[pos[i]] == PLAYER_ONE &&
state[pos[i+1]] == PLAYER_ONE)
{
return PLAYER_ONE;
}
else if (state[4] == PLAYER_TWO &&
state[pos[i]] == PLAYER_TWO &&
state[pos[i+1]] == PLAYER_TWO)
{
return PLAYER_TWO;
}
}
return NO_PLAYER;
}
bool check_full(ivec_t state){
for (int i = 0; i < NBL*NBC; i++)
{
if (state[i] == 0){
return false;
}
}
return true;
}
int is_terminal(ivec_t state){
int c = 0;
if ((c = check_col(state)) != 0){
return c;
}
if ((c = check_lig(state)) != 0){
return c;
}
if ((c = check_diag(state)) != 0){
return c;
}
return c;
}
int get_winner(ivec_t state){
return is_terminal(state);
}
void print_vec(ivec_t v){
}
ivec_t get_state(ivec_t s, int m, int p){
ivec_t sp = s;
ivec_t::iterator i = sp.begin()+m;
if (sp[m] == 0){
(*i) = p;
}
return sp;
}
int get_next_player(int current_player){
if (current_player == PLAYER_ONE)
return PLAYER_TWO;
return PLAYER_ONE;
}
board_t get_next_board(board_t b, int m){
board_t t;
t.state = get_state(b.state, m, b.next_player);
t.next_player = get_next_player(b.next_player);
t.move = m;
t.winner = get_winner(t.state);
t.full = check_full(t.state);
return t;
}
ivec_t get_next_moves(ivec_t state){
ivec_t nexts;
int n = 0;
ivec_t::iterator i = state.begin();
for(; i != state.end(); ++i, ++n){
if ((*i) == 0){
nexts.push_back(n);
}
}
return nexts;
}
void print_board(board_t board){
cout << "---------------------------" << endl;
cout << "next_player: " << board.next_player << endl;
cout << "winner: " << board.winner << endl;
cout << "full: " << ((board.full == true)?"true":"false") << endl;
int v;
for(int i = 0; i < NBL; ++i)
{
for (int j = 0; j < NBC; ++j){
v = board.state[i * NBC + j];
printf("%c",((v) == 0) ? '.' : ((v) == 1)?'X':'O');
}
printf("\n");
}
cout << endl;
}