rewritten remaining unwraps

This commit is contained in:
JOLIMAITRE Matthieu 2022-09-03 02:52:21 +02:00
parent e41d6741f7
commit afecbf5af9
5 changed files with 25 additions and 8 deletions

View file

@ -64,13 +64,16 @@ pub struct Arguments {
fn main() -> Result<(), GameError> {
let arguments = Arguments::parse();
let game_rules = GameRules::default()
.size(arguments.size)
.spawn_per_turn(arguments.spawn);
let manager_rules = ManagerRules::default()
.clear_term(!arguments.no_clear)
.display_skips(arguments.display_skips)
.turn_duration(Duration::from_millis(arguments.delay));
let controller = match arguments.controller {
ControllerParam::Player => PlayerController::new().into_box(),
ControllerParam::Random => RandomController::new().into_box(),

View file

@ -1,6 +1,6 @@
[package]
name = "rs48_lib"
version = "1.1.0"
version = "1.1.1"
edition = "2021"
description = "components of rs48"
license = "MIT"

View file

@ -15,9 +15,11 @@ impl PlayerController {
impl Controller for PlayerController {
fn next_move(&mut self, _grid: &Grid) -> Result<Move, ControllerError> {
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() {
let movement = match c.unwrap() {
let movement = match c.expect("key should be readable") {
Key::Char('q') => return Err(ControllerError::ExitSignal),
Key::Left => Move::LEFT,
Key::Right => Move::RIGHT,

View file

@ -103,7 +103,12 @@ impl Game {
let mut potentials = vec![];
for x 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));
}
}
@ -119,7 +124,6 @@ impl Game {
Ok(())
}
// TODO: macro peut être ?
pub fn perform_move(&mut self, movement: Move) -> usize {
let mut move_score = 0;
match movement {
@ -160,7 +164,12 @@ impl Game {
direction: (isize, isize),
tile_pos: (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
} else {
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 {
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() {
match self.grid.get_val(next_pos) {
None => {

View file

@ -79,7 +79,7 @@ impl TileDisplayer {
.map(|frac| (frac as f64) / (u32::MAX as f64))
.collect::<Vec<_>>()
.try_into()
.unwrap();
.expect("iterator is constructed from array of two values");
let mut remaining = 255f64;
let r = Self::take_fraction(&mut remaining, frac_a, 150.) as u8;