rewritten remaining unwraps
This commit is contained in:
parent
e41d6741f7
commit
afecbf5af9
5 changed files with 25 additions and 8 deletions
|
@ -64,13 +64,16 @@ pub struct Arguments {
|
||||||
|
|
||||||
fn main() -> Result<(), GameError> {
|
fn main() -> Result<(), GameError> {
|
||||||
let arguments = Arguments::parse();
|
let arguments = Arguments::parse();
|
||||||
|
|
||||||
let game_rules = GameRules::default()
|
let game_rules = GameRules::default()
|
||||||
.size(arguments.size)
|
.size(arguments.size)
|
||||||
.spawn_per_turn(arguments.spawn);
|
.spawn_per_turn(arguments.spawn);
|
||||||
|
|
||||||
let manager_rules = ManagerRules::default()
|
let manager_rules = ManagerRules::default()
|
||||||
.clear_term(!arguments.no_clear)
|
.clear_term(!arguments.no_clear)
|
||||||
.display_skips(arguments.display_skips)
|
.display_skips(arguments.display_skips)
|
||||||
.turn_duration(Duration::from_millis(arguments.delay));
|
.turn_duration(Duration::from_millis(arguments.delay));
|
||||||
|
|
||||||
let controller = match arguments.controller {
|
let controller = match arguments.controller {
|
||||||
ControllerParam::Player => PlayerController::new().into_box(),
|
ControllerParam::Player => PlayerController::new().into_box(),
|
||||||
ControllerParam::Random => RandomController::new().into_box(),
|
ControllerParam::Random => RandomController::new().into_box(),
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "rs48_lib"
|
name = "rs48_lib"
|
||||||
version = "1.1.0"
|
version = "1.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "components of rs48"
|
description = "components of rs48"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
@ -15,9 +15,11 @@ impl PlayerController {
|
||||||
impl Controller for PlayerController {
|
impl Controller for PlayerController {
|
||||||
fn next_move(&mut self, _grid: &Grid) -> Result<Move, ControllerError> {
|
fn next_move(&mut self, _grid: &Grid) -> Result<Move, ControllerError> {
|
||||||
let stdin = stdin();
|
let stdin = stdin();
|
||||||
let mut _stdout = stdout().into_raw_mode().unwrap();
|
let mut _stdout = stdout()
|
||||||
|
.into_raw_mode()
|
||||||
|
.expect("terminal needs to be set into raw mode");
|
||||||
for c in stdin.keys() {
|
for c in stdin.keys() {
|
||||||
let movement = match c.unwrap() {
|
let movement = match c.expect("key should be readable") {
|
||||||
Key::Char('q') => return Err(ControllerError::ExitSignal),
|
Key::Char('q') => return Err(ControllerError::ExitSignal),
|
||||||
Key::Left => Move::LEFT,
|
Key::Left => Move::LEFT,
|
||||||
Key::Right => Move::RIGHT,
|
Key::Right => Move::RIGHT,
|
||||||
|
|
|
@ -103,7 +103,12 @@ impl Game {
|
||||||
let mut potentials = vec![];
|
let mut potentials = vec![];
|
||||||
for x in 0..self.board.size() {
|
for x in 0..self.board.size() {
|
||||||
for y in 0..self.board.size() {
|
for y in 0..self.board.size() {
|
||||||
if self.board.get((x, y)).unwrap().is_empty() {
|
if self
|
||||||
|
.board
|
||||||
|
.get((x, y))
|
||||||
|
.expect("coordinates are valid")
|
||||||
|
.is_empty()
|
||||||
|
{
|
||||||
potentials.push((x, y));
|
potentials.push((x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,7 +124,6 @@ impl Game {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: macro peut être ?
|
|
||||||
pub fn perform_move(&mut self, movement: Move) -> usize {
|
pub fn perform_move(&mut self, movement: Move) -> usize {
|
||||||
let mut move_score = 0;
|
let mut move_score = 0;
|
||||||
match movement {
|
match movement {
|
||||||
|
@ -160,7 +164,12 @@ impl Game {
|
||||||
direction: (isize, isize),
|
direction: (isize, isize),
|
||||||
tile_pos: (usize, usize),
|
tile_pos: (usize, usize),
|
||||||
) -> usize {
|
) -> usize {
|
||||||
if self.board.get(tile_pos).unwrap().is_empty() {
|
if self
|
||||||
|
.board
|
||||||
|
.get(tile_pos)
|
||||||
|
.expect("function should only be called internally with known coordinates")
|
||||||
|
.is_empty()
|
||||||
|
{
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
let mut displacement = Displacement::new(&mut self.board, tile_pos, direction);
|
let mut displacement = Displacement::new(&mut self.board, tile_pos, direction);
|
||||||
|
@ -203,7 +212,10 @@ impl<'g> Displacement<'g> {
|
||||||
|
|
||||||
fn move_once(&mut self) -> bool {
|
fn move_once(&mut self) -> bool {
|
||||||
let current_pos = self.position;
|
let current_pos = self.position;
|
||||||
let current_value = self.grid.get_val(current_pos).unwrap();
|
let current_value = self
|
||||||
|
.grid
|
||||||
|
.get_val(current_pos)
|
||||||
|
.expect("last position should be valid");
|
||||||
if let Some(next_pos) = self.get_next_pos() {
|
if let Some(next_pos) = self.get_next_pos() {
|
||||||
match self.grid.get_val(next_pos) {
|
match self.grid.get_val(next_pos) {
|
||||||
None => {
|
None => {
|
||||||
|
|
|
@ -79,7 +79,7 @@ impl TileDisplayer {
|
||||||
.map(|frac| (frac as f64) / (u32::MAX as f64))
|
.map(|frac| (frac as f64) / (u32::MAX as f64))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.try_into()
|
.try_into()
|
||||||
.unwrap();
|
.expect("iterator is constructed from array of two values");
|
||||||
|
|
||||||
let mut remaining = 255f64;
|
let mut remaining = 255f64;
|
||||||
let r = Self::take_fraction(&mut remaining, frac_a, 150.) as u8;
|
let r = Self::take_fraction(&mut remaining, frac_a, 150.) as u8;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue