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
extern crate ansi_term;
extern crate rand;
use rand::Rng;
use std::fmt;
use std::io::Write;
use ansi_term::Colour::{White, Black, Yellow, Green, Cyan};
fn clear_screen() {
std::io::stdout().write_all("\x1b[2J\x1b[1;1H".as_bytes()).unwrap()
}
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum Dir {
Up,
Right,
Down,
Left,
}
impl Dir {
pub fn from_u32(int: u32) -> Result<Dir, &'static str> {
match int {
0 => Ok(Dir::Up),
1 => Ok(Dir::Right),
2 => Ok(Dir::Down),
3 => Ok(Dir::Left),
_ => Err("Cannot convert u32 to `Dir`"),
}
}
}
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
enum Object {
Wall,
Passenger,
Goal,
Empty,
}
impl fmt::Display for Object {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Object::Wall => write!(f, "{}", White.paint("\u{2588}")),
Object::Passenger => write!(f, "{}", Yellow.paint("\u{2588}")),
Object::Goal => write!(f, "{}", Green.paint("\u{2588}")),
Object::Empty => write!(f, "{}", Black.paint("\u{2588}")),
}
}
}
pub fn distance(p0: (u32, u32), p1: (u32, u32)) -> f64 {
((((p0.0 as i64 - p1.0 as i64).pow(2) + (p0.1 as i64 - p1.1 as i64).pow(2)) as f64).sqrt())
}
type World = [[Object; Game::WORLD_WIDTH]; Game::WORLD_HEIGHT];
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct Game {
world: World,
position: (u32, u32),
passenger: (u32, u32),
picked_up: bool,
goal: (u32, u32),
moves: u32,
}
impl Game {
const WORLD_WIDTH: usize = 11;
const WORLD_HEIGHT: usize = 11;
pub fn new(print: bool) -> Game {
let (w, p, g) = simple_world();
let mut game = Game {
world: w,
position: (1, 1),
passenger: p,
picked_up: false,
goal: g,
moves: 0,
};
game.world[p.0 as usize][p.1 as usize] = Object::Passenger;
game.world[g.0 as usize][g.1 as usize] = Object::Goal;
if print {
game.print_map()
};
game
}
pub fn world_size(&self) -> (usize, usize) {
(Self::WORLD_HEIGHT, Self::WORLD_WIDTH)
}
pub fn has_won(&self) -> bool {
self.position == self.goal && self.picked_up
}
pub fn passenger_position(&self) -> (u32, u32) {
self.passenger
}
pub fn goal_position(&self) -> (u32, u32) {
self.goal
}
pub fn player_position(&self) -> (u32, u32) {
self.position
}
pub fn passenger_picked_up(&self) -> bool {
self.picked_up
}
pub fn distance_to_goal(&self) -> f64 {
distance(self.goal, self.position)
}
pub fn distance_to_passenger(&self) -> f64 {
distance(self.passenger, self.position)
}
pub fn enter_move(&mut self, dir: Dir, print: bool) -> bool {
self.make_move(dir);
if print {
self.print_map();
};
if self.has_won() {
true
} else {
false
}
}
pub fn make_move(&mut self, dir: Dir) {
self.moves += 1;
let target = match dir {
Dir::Up => (self.position.0 - 1, self.position.1),
Dir::Right => (self.position.0, self.position.1 + 1),
Dir::Down => (self.position.0 + 1, self.position.1),
Dir::Left => (self.position.0, self.position.1 - 1)
};
match self.world[target.0 as usize][target.1 as usize] {
Object::Wall => (),
Object::Goal => self.position = target,
Object::Passenger => {
self.picked_up = true;
self.world[target.0 as usize][target.1 as usize] = Object::Empty;
self.position = target;
}
Object::Empty => {
self.position = target;
}
}
}
pub fn print_map(&self) {
clear_screen();
for r in 0..Game::WORLD_HEIGHT {
for c in 0..Game::WORLD_WIDTH {
let pos = (r as u32, c as u32);
if pos == self.position {
print!("{}", Cyan.paint("\u{2588}"));
} else {
print!("{}", self.world[r][c]);
}
}
print!("\n\r");
}
print!("\n\rMoves: {}", self.moves);
print!("\n\r\n\rPress <ESC> to exit game.\n\r");
std::io::stdout().flush().unwrap();
}
}
fn simple_world() -> (World, (u32, u32), (u32, u32)) {
let mut rng = rand::thread_rng();
let w = [
[Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall],
[Object::Wall, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Wall],
[Object::Wall, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Wall],
[Object::Wall, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Wall],
[Object::Wall, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Wall],
[Object::Wall, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Wall],
[Object::Wall, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Wall],
[Object::Wall, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Wall],
[Object::Wall, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Wall],
[Object::Wall, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Empty, Object::Wall],
[Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall, Object::Wall],
];
let goals = vec![(8,8), (1,2), (1,8), (8,1)];
let passengers = vec![(3,4), (4,8), (6,1), (6,8)];
(w, rng.choose(&passengers).unwrap().clone(), rng.choose(&goals).unwrap().clone())
}