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};

/// Helper function to clear the terminal screen, not tested on Windows
fn clear_screen() {
	std::io::stdout().write_all("\x1b[2J\x1b[1;1H".as_bytes()).unwrap()
}

/// Move direction
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum Dir {
	Up,
	Right,
	Down,
	Left,
}

impl Dir {
	/// Converts u32 to `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`"),
		}
	}
}

/// Object representing things on the map.
/// You need to pick up a `Passenger` and deliver it to `Goal`, `Wall`s are potentially in the way.
#[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}")),
		}
	}
}

/// Calculates distance between two points
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 alias for the game-world
type World = [[Object; Game::WORLD_WIDTH]; Game::WORLD_HEIGHT];

/// The Game with accompanying state
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct Game {
	/// The current state of the game board, will change when you take actions
	world: World,
	/// Position of the player, defined as (row, column) coordinate on the world map
	position: (u32, u32),
	/// Position of the passenger, defined as (row, column) coordinate on the world map
	passenger: (u32, u32),
	/// Passenger has been picked up
	picked_up: bool,
	/// Position of the goal, defined as (row, column) coordinate on the world map
	goal: (u32, u32),
	/// How many moves the player has made
	moves: u32,
}

impl Game {
	const WORLD_WIDTH: usize = 11;
	const WORLD_HEIGHT: usize = 11;

	/// Initialize a new game state
	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
	}

	/// Returns size of the game world
	pub fn world_size(&self) -> (usize, usize) {
		(Self::WORLD_HEIGHT, Self::WORLD_WIDTH)
	}

	/// Returns true if player has won the game
	pub fn has_won(&self) -> bool {
		self.position == self.goal && self.picked_up
	}

	/// Passenger position
	pub fn passenger_position(&self) -> (u32, u32) {
		self.passenger
	}

	/// Goal position
	pub fn goal_position(&self) -> (u32, u32) {
		self.goal
	}

	/// Returns player position
	pub fn player_position(&self) -> (u32, u32) {
		self.position
	}

	/// Returns true if passenger has been picked up
	pub fn passenger_picked_up(&self) -> bool {
		self.picked_up
	}

	/// Returns a distance to a game goal
	pub fn distance_to_goal(&self) -> f64 {
		distance(self.goal, self.position)
	}

	/// Returns a distance to a passenger
	pub fn distance_to_passenger(&self) -> f64 {
		distance(self.passenger, self.position)
	}

	/// Enter move directly and optionally print the map
	/// Returns a bool signifying whether or not this move lead to winning the game
	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
		}
	}

	/// Makes a move and mutates the board, doesn't return anything
	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;
			}
		}
	}

	/// Print the map on screen
	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();
	}
}

/// The basic world that we're dealing with, giving a World, a position for the goal and a position for the passenger
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],
	];
	// Difficulty of the state space increases with the variation in goals and passengers
	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())
}