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
extern crate rand;
use super::Game;
use self::rand::Rng;
pub struct Moves {
pub left: Vec<u64>,
pub right: Vec<u64>,
pub down: Vec<u64>,
pub up: Vec<u64>,
pub scores: Vec<u64>
}
#[derive(Clone, PartialEq, Debug)]
pub enum Direction {
Left,
Right,
Up,
Down,
None
}
lazy_static! { static ref DIRECTIONS: Vec<Direction> = vec![Direction::Left, Direction::Right,
Direction::Up, Direction::Down]; }
impl Direction {
pub fn sample() -> Direction {
match rand::thread_rng().gen_range(0, 4) {
0 => Direction::Left,
1 => Direction::Right,
2 => Direction::Down,
3 => Direction::Up,
_ => Direction::None
}
}
pub fn without(dirs: &Vec<Direction>) -> Vec<Direction> {
let mut filtered = DIRECTIONS.clone();
filtered.retain(|dir| dirs.iter().all(|tried| &dir != &tried));
filtered
}
pub fn sample_without(dirs: &Vec<Direction>) -> Direction {
let filtered = &Self::without(dirs);
let filtered_len = filtered.len();
if filtered_len == 0 { return Direction::None }
filtered[rand::thread_rng().gen_range(0, filtered.len())].clone()
}
}
impl Moves {
pub fn generate() -> Self {
let mut left_moves = vec![0; 65536];
let mut right_moves = vec![0; 65536];
let mut up_moves = vec![0; 65536];
let mut down_moves = vec![0; 65536];
let mut scores = vec![0; 65536];
for row in 0 .. 65536 {
let mut line = [
(row >> 0) & 0xF,
(row >> 4) & 0xF,
(row >> 8) & 0xF,
(row >> 12) & 0xF
];
let mut s = 0;
for i in 0 .. 4 {
if line[i] >= 2 { s += (line[i] - 1) * (1 << line[i]) }
}
scores[row as usize] = s;
let mut i = 0;
while i < 3 {
let mut j = i + 1;
while j < 4 {
if line[j] != 0 { break };
j += 1;
};
if j == 4 { break };
if line[i] == 0 {
line[i] = line[j];
line[j] = 0;
continue;
} else if line[i] == line[j] {
if line[i] != 0xF { line[i] += 1 };
line[j] = 0;
}
i += 1;
}
let result = (line[0] << 0) |
(line[1] << 4) |
(line[2] << 8) |
(line[3] << 12);
let rev_row = (row >> 12) & 0x000F | (row >> 4) & 0x00F0 | (row << 4) & 0x0F00 | (row << 12) & 0xF000;
let rev_res = (result >> 12) & 0x000F | (result >> 4) & 0x00F0 | (result << 4) & 0x0F00 | (result << 12) & 0xF000;
let row_idx = row as usize;
let rev_idx = rev_row as usize;
right_moves[row_idx] = row ^ result;
left_moves[rev_idx] = rev_row ^ rev_res;
up_moves[rev_idx] = Game::column_from(rev_row) ^ Game::column_from(rev_res);
down_moves[row_idx] = Game::column_from(row) ^ Game::column_from(result);
};
Moves { left: left_moves, right: right_moves, down: down_moves, up: up_moves, scores: scores }
}
}