Skip to content

Commit

Permalink
refactor: 'secondary_values' function return a 'SecondaryValues' struct
Browse files Browse the repository at this point in the history
The 'secondary_values' function in the Actions struct has been refactored to return a 'SecondaryValues' struct instead of a tuple.
  • Loading branch information
ShenMian committed Apr 19, 2024
1 parent e5b5482 commit 85ce8e4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/actions.rs
Expand Up @@ -10,6 +10,18 @@ use nalgebra::Vector2;

use crate::{action::Action, error::ParseActionError, run_length::rle_decode};

/// Secondary statistics for a sequence of actions.
pub struct SecondaryValues {
/// Straight line box pushes.
pub box_lines: i32,
/// Changing focus from one box to another.
pub box_changes: i32,
/// Changing from moving the line to pushing the boxes.
pub pushing_sessions: i32,
/// Straight line player moves.
pub player_lines: i32,
}

/// A sequence of actions.
#[derive(Clone, Eq, PartialEq, Hash, Debug, Default)]
pub struct Actions(pub Vec<Action>);
Expand All @@ -26,7 +38,7 @@ impl Actions {
}

/// Returns the secondary values.
pub fn secondary_values(&self) -> (i32, i32, i32, i32) {
pub fn secondary_values(&self) -> SecondaryValues {
let mut player_lines = 0;
let mut box_lines = 0;
let mut box_changes = 0;
Expand Down Expand Up @@ -62,7 +74,12 @@ impl Actions {
if prev_pushed_box_position.is_some() {
box_changes += 1;
}
(box_lines, box_changes, pushing_sessions, player_lines)
SecondaryValues {
box_lines,
box_changes,
pushing_sessions,
player_lines,
}
}
}

Expand Down
23 changes: 19 additions & 4 deletions tests/actions.rs
@@ -1,6 +1,6 @@
use std::str::FromStr;

use soukoban::{Actions, ParseActionError};
use soukoban::{Actions, ParseActionError, SecondaryValues};

#[test]
fn parse_actions_error() {
Expand Down Expand Up @@ -31,7 +31,12 @@ fn scoring_metrics() {
let empty_actions = Actions::from_str("").unwrap();
assert_eq!(empty_actions.moves(), 0);
assert_eq!(empty_actions.pushes(), 0);
let (box_lines, box_changes, pushing_sessions, player_lines) = empty_actions.secondary_values();
let SecondaryValues {
box_lines,
box_changes,
pushing_sessions,
player_lines,
} = empty_actions.secondary_values();
assert_eq!(box_lines, 0);
assert_eq!(box_changes, 0);
assert_eq!(pushing_sessions, 0);
Expand All @@ -49,7 +54,12 @@ fn scoring_metrics() {
let actions = Actions::from_str("ruuLLLLrrrrddlUruLLLulDrddllluuRRDrdLuuurDD").unwrap();
assert_eq!(actions.moves(), 43);
assert_eq!(actions.pushes(), 15);
let (box_lines, box_changes, pushing_sessions, player_lines) = actions.secondary_values();
let SecondaryValues {
box_lines,
box_changes,
pushing_sessions,
player_lines,
} = actions.secondary_values();
assert_eq!(box_lines, 8);
assert_eq!(box_changes, 5);
assert_eq!(pushing_sessions, 7);
Expand All @@ -65,7 +75,12 @@ fn scoring_metrics() {
// box lines : 6
// pushing sessions: 6
let actions = Actions::from_str("ullDullddrRuLurrrdLLrrddlUruL").unwrap();
let (box_lines, box_changes, pushing_sessions, player_lines) = actions.secondary_values();
let SecondaryValues {
box_lines,
box_changes,
pushing_sessions,
player_lines,
} = actions.secondary_values();
assert_eq!(box_lines, 6);
assert_eq!(box_changes, 4);
assert_eq!(pushing_sessions, 6);
Expand Down

0 comments on commit 85ce8e4

Please sign in to comment.