-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.cpp
227 lines (190 loc) · 5.73 KB
/
grid.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
#include "grid.hpp"
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace Dominos;
Grid::Grid(int size) : size_(size), grid_((2 * size_) * (2 * size_), Ext) {
for (int y = 0; y < 2 * size_; y++) {
for (int x = start_x(y); x < end_x(y); x++) {
set_cell(x, y, None);
}
}
}
Grid::Grid(int size, const Grid &g)
: size_(size), grid_((2 * size_) * (2 * size_), Ext) {
assert(size >= g.size_);
const int size_diff = size_ - g.size_;
for (int y = 0; y < size_diff; y++) {
for (int x = start_x(y); x < end_x(y); x++) {
set_cell(x, y, None);
set_cell(x, 2 * size - y - 1, None);
}
}
for (int y = size_diff; y < 2 * size_ - size_diff; y++) {
const int start = start_x(y);
const int end = end_x(y);
for (int i = 0; i < size_diff; i++) {
set_cell(start + i, y, None);
set_cell(end - i - 1, y, None);
}
for (int x = start + size_diff; x < end - size_diff; x++) {
set_cell(x, y, g.cell(x - size_diff, y - size_diff));
}
}
}
GridElement Grid::cell(int x, int y) const { return grid_[linearise(x, y)]; }
GridElement Grid::safe_cell(int x, int y) const {
if (x < 0 || x >= 2 * size_ || y < 0 || y >= 2 * size_) return Ext;
return grid_[linearise(x, y)];
}
void Grid::set_cell(int x, int y, GridElement el) {
grid_[linearise(x, y)] = el;
}
int Grid::start_x(int y) const {
if (y >= size_) return y - size_;
return size_ - y - 1;
}
int Grid::end_x(int y) const { return 2 * size_ - start_x(y); }
bool Grid::is_horizontal(int x, int y) const {
return cell(x, y) == HL && cell(x + 1, y) == HR;
}
void Grid::set_horizontal(int x, int y) {
set_cell(x, y, HL);
set_cell(x + 1, y, HR);
}
bool Grid::is_vertical(int x, int y) const {
return cell(x, y) == VT && cell(x, y + 1) == VB;
}
void Grid::set_vertical(int x, int y) {
set_cell(x, y, VT);
set_cell(x, y + 1, VB);
}
void Grid::set_square_horizontal(int x, int y) {
set_horizontal(x, y);
set_horizontal(x, y + 1);
}
void Grid::set_square_vertical(int x, int y) {
set_vertical(x, y);
set_vertical(x + 1, y);
}
std::ostream &operator<<(std::ostream &os, const Grid &g) {
// os << "Taille: " << g.size_ << "\n";
for (int y = 0; y < 2 * g.size_; y++) {
for (int x = 0; x < 2 * g.size_; x++) {
os << g.grid_[g.linearise(x, y)];
}
os << "\n";
}
return os;
}
int Grid::size() const { return size_; }
int Grid::linearise(int x, int y) const { return 2 * y * size_ + x; }
std::ostream &operator<<(std::ostream &os, GridElement el) {
switch (el) {
case Ext:
return os << ' ';
case None:
return os << '.';
case VT:
return os << '^';
case VB:
return os << 'v';
case HL:
return os << '<';
case HR:
return os << '>';
default:
return os;
}
}
void update_grid_from_previous(Grid &g, int x, int y, const Grid &prev) {
if (prev.is_horizontal(x, y) && prev.is_horizontal(x, y + 1)) {
return;
} else if (prev.is_horizontal(x, y)) {
g.set_horizontal(x, y + 1);
} else if (prev.is_horizontal(x, y + 1)) {
g.set_horizontal(x, y);
} else if (prev.is_vertical(x, y) && prev.is_vertical(x + 1, y)) {
return;
} else if (prev.is_vertical(x, y)) {
g.set_vertical(x + 1, y);
} else if (prev.is_vertical(x + 1, y)) {
g.set_vertical(x, y);
} else if (rand() % 2 == 0) {
g.set_square_vertical(x, y);
} else {
g.set_square_horizontal(x, y);
}
}
typedef struct {
int x;
int y;
} SquarePos;
// std::vector<SquarePos> get_positions(int size, int s) {
// std::vector<SquarePos> pos(s * s);
// const int offset = size - s;
// for (int i = 0; i < s - 1; i++) {
// for (int j = 0; j <= i; j++) {
// pos.push_back({.x = size + 2 * j - i - 1, .y = offset + i});
// pos.push_back({.x = size + 2 * j - i - 1, .y = offset + 2 * s - i -
// 2});
// }
// }
// for (int j = 0; j < s; j++) {
// pos.push_back({.x = offset + 2 * j, .y = size - 1});
// }
// return pos;
// };
Grid Dominos::get_random_grid(int size, std::ostream *os) {
Grid g1 = Grid(size);
Grid g2 = Grid(size);
Grid &prev_g = g1;
Grid &new_g = g2;
for (int s = 1; s <= size; s++) {
Grid &temp = prev_g;
prev_g = new_g;
new_g = temp;
const int offset = size - s;
for (int i = 0; i < s - 1; i++) {
for (int j = 0; j <= i; j++) {
update_grid_from_previous(new_g, size + 2 * j - i - 1, offset + i,
prev_g);
update_grid_from_previous(new_g, size + 2 * j - i - 1,
offset + 2 * s - i - 2, prev_g);
}
}
for (int j = 0; j < s; j++) {
update_grid_from_previous(new_g, offset + 2 * j, size - 1, prev_g);
}
if (os != NULL) {
*os << s << "\n";
*os << new_g;
}
}
return new_g;
}
void Grid::to_svg(std::ostream &os) const {
const int SIZE = 20;
const int MARGIN = 0;
const int INSIDE_SIZE = SIZE - 2 * MARGIN;
os << "<svg viewBox=\"0 0 " << SIZE * 2 * size_ << " " << SIZE * 2 * size_
<< "\" xmlns=\"http://www.w3.org/2000/svg\">";
for (int x = 0; x < 2 * size_; x++) {
for (int y = 0; y < 2 * size_; y++) {
const GridElement c = cell(x, y);
if (c == VT) {
os << "<rect x=\"" << SIZE * x + MARGIN << "\" y=\""
<< SIZE * y + MARGIN << "\" width=\"" << INSIDE_SIZE
<< "\" height=\"" << INSIDE_SIZE + SIZE << "\" fill=\""
<< ((x + y) % 2 == 0 ? "#F00000" : "#FFD700") << "\"/>";
} else if (c == HL) {
os << "<rect x=\"" << SIZE * x + MARGIN << "\" y=\""
<< SIZE * y + MARGIN << "\" width=\"" << INSIDE_SIZE + SIZE
<< "\" height=\"" << INSIDE_SIZE << "\" fill=\""
<< ((x + y) % 2 == 0 ? "#0000CE" : "#00D1D1") << "\"/>";
}
}
}
os << "</svg>";
}