This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solver.cpp
151 lines (123 loc) · 3.08 KB
/
Solver.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
#include "Solver.hpp"
void Solver::gen_all_pos_mov(Board& board) {
save_all_pos_mov(board);
print_all_pos_mov();
}
void Solver::gen_all_pos_mov_cut_if_game_over(Board& board) {
deb printf("GEN_ALL_POS_MOV_CUT_IF_GAME_OVER\n");
save_all_pos_mov_cut_if_game_over(board);
print_all_pos_mov();
}
void Solver::destroy() {
// Clear all memory
if(board_count == 0) return;
FOR(i, board_count) {
boards[i].destroy();
}
free(boards);
}
void Solver::save_all_pos_mov(Board& board) {
if(board.get_winner() != 0) {
board_count = 0;
return;
}
int n = board.empty_count;
save_deb {
printf("SAVING ALL MOVES\n");
printf("STARTING PLAYER: %d\n", board.player);
printf("POSSIBLE MOVES: %d\n", n);
printf("STARTING BOARD:\n");
board.print();
printf("\n");
}
if(n == 0) {
board_count = 0;
return;
}
boards = (Board*)malloc(sizeof(Board) * n);
board_count = n;
Board board_copy;
board_copy.copy_from(board);
int x, y;
bool b = false;
FOR(i, board_copy.n) {
FOR(j, board_copy.m) {
if(board_copy.fields[i][j].get_value() == 0) {
board_copy.set_field(i, j, board_copy.player);
x = i;
y = j;
b = true;
break;
}
}
if(b) break;
}
save_deb {
printf("k: 0\n");
printf("COORDS: 0 0 %d %d\n", x, y);
}
boards[0].copy_from(board_copy);
boards[0].next_player();
save_deb {
board_copy.print();
printf("\n");
}
int px = x, py = y;
FOR(k, n - 1) {
int i = k + 1;
board_copy.kth_zero(x, y, x, y, i);
save_deb {
printf("k: %d\n", i);
printf("COORDS: %d %d %d %d\n", px, py, x, y);
}
board_copy.swap_fields(px, py, x, y);
boards[i].copy_from(board_copy);
boards[i].next_player();
save_deb {
board_copy.print();
printf("\n");
}
px = x;
py = y;
}
board_copy.destroy();
}
void Solver::print_pos_mov(int k) {
deb printf("PRINTING MOVE NUMBER %d:\n", k);
get_pos_mov(k).print();
deb printf("\n");
}
Board Solver::get_pos_mov(int k) {
return boards[k];
}
void Solver::save_all_pos_mov_cut_if_game_over(Board& board) {
int winner = board.get_winner();
this->winner = winner;
if(winner != 0) {
board_count = 0;
return;
}
save_all_pos_mov(board);
int win_pos = -1;
FOR(i, board_count) {
if (boards[i].get_winner() != 0) {
win_pos = i;
break;
}
}
if (win_pos != -1) {
Board win_board;
win_board.copy_from(boards[win_pos]);
destroy();
board_count = 1;
boards = (Board*)malloc(sizeof(Board));
boards[0].copy_from(win_board);
win_board.destroy();
}
}
void Solver::print_all_pos_mov() {
printf("%d\n", board_count);
FOR(i, board_count) {
print_pos_mov(i);
}
}