-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSolver.cpp
More file actions
136 lines (110 loc) · 2.95 KB
/
Copy pathCSolver.cpp
File metadata and controls
136 lines (110 loc) · 2.95 KB
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
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include "CSolver.h"
#include "common.h"
#define MAX_TEST 5000
static unsigned char modele[NB_BILLE] = {
255, 255, 0, 0, 0, 255, 255,
255, 0, 0, 0, 0, 0, 255,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
255, 0, 0, 0, 0, 0, 255,
255, 255, 0, 0, 0, 255, 255,
};
void CSolver::init(void) {
int x, y, idx, nbTest = 0;
srand(time(NULL));
for(y=idx=0;y<NB_LIGNE;y++) {
for(x=0;x<NB_COLONNE;x++,idx++) {
if(modele[idx] == VIDE) {
CPlateau *plateau = new CPlateau(modele, idx);
dout << "Ajout du plateau " << idx << " poids " << plateau->getPoids() << std::endl;
if(addPlateauIfNotExistst(plateaux, plateau)) {
if(++nbTest == MAX_TEST) {
return;
}
}
}
}
}
/*std::set<CPlateau *, SPlateauCmp>::iterator it;
int nb=0;
x=y=0;
initscr();
for(it=plateaux->begin();it!=plateaux->end();it++) {
(*it)->print(x, y);
if(nb < 10) {
nb++;
x+=12;
} else {
nb=x=0;
y+=12;
}
}
refresh();
getch();
endwin();*/
}
bool CSolver::addPlateauIfNotExistst(CPlateaux *plx, CPlateau *plateau) {
if(!plx->add(plateau)) {
delete plateau;
return false;
}
return true;
}
void CSolver::clearPlateaux(void) {
}
CSolver::CSolver(void) {
plateaux = new CPlateaux();
}
CSolver::~CSolver(void) {
delete plateaux;
}
int CSolver::getNbPlateaux(void) {
return plateaux->size();
}
void CSolver::process(void) {
bool fini = false;
int nbBille = 1;
init();
while(!fini) {
dout << "Nombre de bille: " << nbBille << ", nombre de plateau: " << plateaux->size() << std::endl;
nbBille++;
fini = nbBille == MAX_BILLE;
if(!fini) {
std::set<CPlateau *, SPlateauCmp>::iterator it;
CPlateaux *newPlateaux = new CPlateaux();
int nbTest = 0;
clear();
for(it=plateaux->begin();it!=plateaux->end();it++) {
CPlateau *pl = *it;
std::list<CCoup> nextCoups = pl->getNextCoups(rand() % DIFFRENT_COUP);
std::list<CCoup>::iterator itCoup;
for(itCoup=nextCoups.begin();itCoup!=nextCoups.end();itCoup++) {
if(nbTest < MAX_TEST && !(*itCoup).isNull()) {
CPlateau *plateau = new CPlateau(pl, *itCoup);
if(addPlateauIfNotExistst(newPlateaux, plateau)) {
nbTest++;
}
}
}
}
delete plateaux;
plateaux = newPlateaux;
}
}
if(plateaux->size() >= 1) {
CPlateau *plateau = *(plateaux->begin());
std::list<CCoup> coups = plateau->getCoups();
std::list<CCoup>::iterator itCoup;
plateau->printVide();
for(itCoup=coups.begin();itCoup!=coups.end();itCoup++) {
if(!(*itCoup).isNull()) {
(*itCoup).print();
}
}
}
}