-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
240 lines (196 loc) · 5.78 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <iostream>
#include "board.hpp"
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
bool run = true;
int TAM_CELULA = 70;
int TAM_BORDA = 30;
int x_Dimension = 9;
int y_Dimension = 9;
int LARGURA_TELA = x_Dimension * TAM_CELULA + 2 * TAM_BORDA;
int ALTURA_TELA = y_Dimension * TAM_CELULA + 2 * TAM_BORDA;
ALLEGRO_DISPLAY *janela = NULL;
ALLEGRO_EVENT_QUEUE *fila_eventos = NULL;
ALLEGRO_FONT *fonte = NULL;
ALLEGRO_FONT *fonte2 = NULL;
void read_keyboard();
bool init_allegro();
void finish_allegro();
int number_errors = 0;
int ramdon_moves = 0;
void ramdon_search(Board board);
Board ramdon_search_aux(Board board);
void generate();
void generate_new_board();
int main(int argc, const char *argv[]){
if(argc < 2){
std::cout << "falta o nome do arquivo" << std::endl;
exit(1);
}
init_allegro();
Board board;
srand( (unsigned)time(NULL) );
switch(atoi(argv[1])){
case 1:
board.read_board(argv[2]);
board.init_potential();
board.put_the_single_potential();
board.show_board();
ramdon_search(board);
break;
case 2:
generate();
break;
}
finish_allegro();
return 0;
}
void generate(){
while(run){
generate_new_board();
}
}
void generate_new_board(){
Board board;
board.read_board("board_generate");
board.init_potential();
board.put_the_single_potential();
board.show_board();
Board resp = ramdon_search_aux(board);
if(resp.valid){
board = resp;
for(int i=0; i<100 && run; i++){
board.show_board();
}
}
}
void ramdon_search(Board board){
while(run){
Board resp = ramdon_search_aux(board);
if(resp.valid){
board = resp;
while(run){
board.show_board();
}
}
board.show_board();
}
}
Board ramdon_search_aux(Board board){
number_errors++;
ramdon_moves = 0;
for(int i=0; i<9*9; i++){
ramdon_moves++;
board.put_ramdon_element();
if(board.move_wrong()){
break;
}
board.show_board();
board.put_the_single_potential();
if(board.end_game()){
return board;
}
if(board.move_wrong()){
break;
}
board.show_board();
}
Board res_f;
res_f.valid = false;
return res_f;
}
/*ALLEGRO FUNCTIONS*/
void read_keyboard(){
while(!al_is_event_queue_empty(fila_eventos)){
ALLEGRO_EVENT evento;
al_wait_for_event(fila_eventos, &evento);
if(evento.type == ALLEGRO_EVENT_KEY_DOWN){
if(evento.keyboard.keycode == ALLEGRO_KEY_ESCAPE){
run = false;
}
}else if(evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
run = false;
}
}
}
bool init_allegro(){
if (!al_init()){
fprintf(stderr, "Falha ao inicializar Allegro.\n");
return false;
}
al_init_font_addon();
if (!al_init_ttf_addon()){
fprintf(stderr, "Falha ao inicializar allegro_ttf.\n");
return false;
}
if (!al_init_primitives_addon()){
fprintf(stderr, "Falha ao inicializar add-on allegro_primitives.\n");
return false;
}
if (!al_install_keyboard()){
fprintf(stderr, "Falha ao inicializar o teclado.\n");
return false;
}
janela = al_create_display(LARGURA_TELA, ALTURA_TELA);
if (!janela){
fprintf(stderr, "Falha ao criar janela.\n");
return false;
}
al_set_window_title(janela, "Sudoku - AI Solver");
fila_eventos = al_create_event_queue();
if (!fila_eventos){
fprintf(stderr, "Falha ao criar fila de eventos.\n");
al_destroy_display(janela);
return false;
}
fonte = al_load_font("Font.ttf", TAM_CELULA, 0);
if (!fonte)
{
fprintf(stderr, "Falha ao carregar fonte.\n");
al_destroy_event_queue(fila_eventos);
al_destroy_display(janela);
return false;
}
fonte2 = al_load_font("Font.ttf", TAM_CELULA*.5, 0);
if (!fonte2)
{
fprintf(stderr, "Falha ao carregar fonte2.\n");
al_destroy_event_queue(fila_eventos);
al_destroy_display(janela);
return false;
}
al_register_event_source(fila_eventos, al_get_keyboard_event_source());
al_register_event_source(fila_eventos, al_get_display_event_source(janela));
return true;
}
void finish_allegro(){
al_destroy_event_queue(fila_eventos);
al_destroy_display(janela);
}
/*BOARD FUNCTIONS THAT USE A GLOBAL VARIABLES*/
void Board::show_board(){
read_keyboard();
al_clear_to_color(al_map_rgb(200, 200, 200));
al_draw_textf(fonte2, al_map_rgb(0, 0, 0), TAM_BORDA , 0, 0, "try: %i | fitness: %i | RM: %i", number_errors, this->count_not_null(), ramdon_moves);
for(int i=1; i<9; i++){
al_draw_line(TAM_BORDA + 1, TAM_BORDA + (TAM_CELULA * i), TAM_BORDA + (TAM_CELULA * 9) - 1, TAM_BORDA + (TAM_CELULA * i), al_map_rgb(50, 50, 50), 1.0);
al_draw_line(TAM_BORDA + (TAM_CELULA * i), TAM_BORDA + 1, TAM_BORDA + (TAM_CELULA * i), TAM_BORDA + (TAM_CELULA * 9) - 1, al_map_rgb(50, 50, 50), 1.0);
}
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
al_draw_rectangle(TAM_BORDA + (TAM_CELULA * 3 * i), TAM_BORDA + (TAM_CELULA * 3 * j), TAM_BORDA + (TAM_CELULA * 3 * (i+1)), TAM_BORDA + (TAM_CELULA * 3 * (j+1)), al_map_rgb(50, 50, 50), 4.0);
}
}
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
if(this->board[j][i].second.second && this->original[j][i]){
al_draw_textf(fonte, al_map_rgb(0, 0, 100), TAM_BORDA + (TAM_CELULA * i) + TAM_CELULA/4, TAM_BORDA + (TAM_CELULA * j), 10, "%i", board[j][i].second.first);
}else if(this->board[j][i].second.second){
al_draw_textf(fonte, al_map_rgb(100, 0, 0), TAM_BORDA + (TAM_CELULA * i) + TAM_CELULA/4, TAM_BORDA + (TAM_CELULA * j), 10, "%i", board[j][i].second.first);
}
}
}
al_flip_display();
}