Skip to content

Commit f036890

Browse files
committed
refactor check_puzzle into check_solution
1 parent b53b030 commit f036890

File tree

4 files changed

+41
-40
lines changed

4 files changed

+41
-40
lines changed

backend/src/bin/gol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ fn create_puzzles(output_dir: &PathBuf) -> Result<(), Box<dyn std::error::Error>
242242
println!("{puzzle:#}");
243243
println!("Created solution: {}", solution_path.display());
244244
println!("{solution:#}");
245-
let steps = solution.check_puzzle(&puzzle)?;
245+
let steps = puzzle.check_solution(&solution)?;
246246
println!("Verified solution: {steps} steps");
247247
println!();
248248
}
@@ -711,7 +711,7 @@ fn check_solution(
711711
let board: Board = bcs::from_bytes(&board_bytes)?;
712712

713713
// Check if board solves the puzzle
714-
match board.check_puzzle(&puzzle) {
714+
match puzzle.check_solution(&board) {
715715
Ok(steps) => {
716716
println!("✅ Solution is VALID!");
717717
println!(" Initial board passes all initial conditions");

backend/src/contract.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
mod state;
77

88
use async_graphql::ComplexObject;
9-
use gol_challenge::{GolChallengeAbi, Operation};
9+
use gol_challenge::{game::Puzzle, GolChallengeAbi, Operation};
1010
use linera_sdk::{
1111
linera_base_types::{AccountOwner, ChainId, DataBlobHash, Timestamp, WithContractAbi},
1212
views::{RootView, View},
@@ -75,8 +75,8 @@ impl Contract for GolChallengeContract {
7575
.expect("Operation must have an owner or be authenticated.")
7676
});
7777
let puzzle_bytes = self.runtime.read_data_blob(puzzle_id);
78-
let puzzle = bcs::from_bytes(&puzzle_bytes).expect("Deserialize puzzle");
79-
board.check_puzzle(&puzzle).expect("Invalid solution");
78+
let puzzle = bcs::from_bytes::<Puzzle>(&puzzle_bytes).expect("Deserialize puzzle");
79+
puzzle.check_solution(&board).expect("Invalid solution");
8080
let timestamp = self.runtime.system_time();
8181
let solution = Solution {
8282
board,

backend/src/game.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -823,71 +823,71 @@ impl Board {
823823

824824
conditions
825825
}
826+
}
826827

828+
impl Puzzle {
827829
/// Check that the board satisfies the given puzzle.
828-
pub fn check_puzzle(&self, puzzle: &Puzzle) -> Result<u16, InvalidSolution> {
829-
if puzzle.minimal_steps > puzzle.maximal_steps {
830+
pub fn check_solution(&self, board: &Board) -> Result<u16, InvalidSolution> {
831+
if self.minimal_steps > self.maximal_steps {
830832
return Err(InvalidSolution::InvalidStepRange {
831-
min_steps: puzzle.minimal_steps,
832-
max_steps: puzzle.maximal_steps,
833+
min_steps: self.minimal_steps,
834+
max_steps: self.maximal_steps,
833835
});
834836
}
835-
if self.size != puzzle.size {
837+
if board.size != self.size {
836838
return Err(InvalidSolution::SizeMismatch {
837-
board_size: self.size,
838-
puzzle_size: puzzle.size,
839+
board_size: board.size,
840+
puzzle_size: self.size,
839841
});
840842
}
841-
if let Err((condition_index, reason)) = self.check_conditions(&puzzle.initial_conditions) {
843+
if let Err((condition_index, reason)) = board.check_conditions(&self.initial_conditions) {
842844
return Err(InvalidSolution::InitialConditionFailed {
843845
condition_index,
844846
reason,
845847
});
846848
}
847-
if puzzle.is_strict {
848-
if puzzle.minimal_steps == 0 {
849+
if self.is_strict {
850+
if self.minimal_steps == 0 {
849851
return Err(InvalidSolution::InvalidStepRange {
850-
min_steps: puzzle.minimal_steps,
851-
max_steps: puzzle.maximal_steps,
852+
min_steps: self.minimal_steps,
853+
max_steps: self.maximal_steps,
852854
});
853855
}
854-
let board = self.advance(puzzle.minimal_steps - 1);
856+
let board = board.advance(self.minimal_steps - 1);
855857
match board.advance_until(
856-
&puzzle.final_conditions,
857-
puzzle.maximal_steps - puzzle.minimal_steps + 1,
858+
&self.final_conditions,
859+
self.maximal_steps - self.minimal_steps + 1,
858860
) {
859861
Ok((steps, _)) => {
860862
if steps == 0 {
861863
return Err(InvalidSolution::FinalConditionsMustFailAt {
862-
steps: puzzle.minimal_steps - 1,
864+
steps: self.minimal_steps - 1,
863865
});
864866
}
865-
Ok(puzzle.minimal_steps - 1 + steps)
867+
Ok(self.minimal_steps - 1 + steps)
866868
}
867869
Err((condition_index, reason)) => Err(InvalidSolution::FinalConditionFailed {
868870
condition_index,
869-
steps: puzzle.maximal_steps,
871+
steps: self.maximal_steps,
870872
reason,
871873
}),
872874
}
873875
} else {
874-
let board = self.advance(puzzle.minimal_steps);
876+
let board = board.advance(self.minimal_steps);
875877
match board.advance_until(
876-
&puzzle.final_conditions,
877-
puzzle.maximal_steps - puzzle.minimal_steps,
878+
&self.final_conditions,
879+
self.maximal_steps - self.minimal_steps,
878880
) {
879-
Ok((steps, _)) => Ok(puzzle.minimal_steps + steps),
881+
Ok((steps, _)) => Ok(self.minimal_steps + steps),
880882
Err((condition_index, reason)) => Err(InvalidSolution::FinalConditionFailed {
881883
condition_index,
882-
steps: puzzle.maximal_steps,
884+
steps: self.maximal_steps,
883885
reason,
884886
}),
885887
}
886888
}
887889
}
888-
}
889890

890-
impl Puzzle {
891891
/// Convert this puzzle to a DirectPuzzle representation for display.
892892
pub fn to_direct_puzzle(&self) -> DirectPuzzle {
893893
let mut initial_constraints =
@@ -1265,7 +1265,7 @@ mod tests {
12651265
};
12661266

12671267
assert_eq!(
1268-
board.check_puzzle(&puzzle),
1268+
puzzle.check_solution(&board),
12691269
Err(InvalidSolution::SizeMismatch {
12701270
board_size: 5,
12711271
puzzle_size: 10
@@ -1289,7 +1289,7 @@ mod tests {
12891289
};
12901290

12911291
assert_eq!(
1292-
board.check_puzzle(&puzzle),
1292+
puzzle.check_solution(&board),
12931293
Err(InvalidSolution::InvalidStepRange {
12941294
min_steps: 6,
12951295
max_steps: 4
@@ -1316,7 +1316,7 @@ mod tests {
13161316
};
13171317

13181318
assert_eq!(
1319-
board.check_puzzle(&puzzle),
1319+
puzzle.check_solution(&board),
13201320
Err(InvalidSolution::InitialConditionFailed {
13211321
condition_index: 0,
13221322
reason: ConditionFailureReason::PositionMismatch {
@@ -1378,7 +1378,7 @@ mod tests {
13781378
};
13791379

13801380
// Test that after 2 steps (full blinker cycle), we get back to the initial pattern.
1381-
assert_eq!(board.check_puzzle(&puzzle), Ok(2));
1381+
assert_eq!(puzzle.check_solution(&board), Ok(2));
13821382
}
13831383

13841384
#[test]
@@ -1450,7 +1450,7 @@ mod tests {
14501450
],
14511451
};
14521452

1453-
assert_eq!(board.check_puzzle(&puzzle), Ok(31));
1453+
assert_eq!(puzzle.check_solution(&board), Ok(31));
14541454
}
14551455

14561456
#[test]
@@ -1522,7 +1522,7 @@ mod tests {
15221522
};
15231523

15241524
assert_eq!(
1525-
board.check_puzzle(&puzzle),
1525+
puzzle.check_solution(&board),
15261526
Err(InvalidSolution::FinalConditionFailed {
15271527
condition_index: 0,
15281528
steps: 1,
@@ -1577,7 +1577,7 @@ mod tests {
15771577
};
15781578

15791579
// Test initial condition failure with detailed information.
1580-
match board.check_puzzle(&puzzle) {
1580+
match puzzle.check_solution(&board) {
15811581
Err(InvalidSolution::InitialConditionFailed {
15821582
condition_index,
15831583
reason,
@@ -1628,7 +1628,7 @@ mod tests {
16281628
final_conditions: vec![],
16291629
};
16301630

1631-
match board2.check_puzzle(&puzzle2) {
1631+
match puzzle2.check_solution(&board2) {
16321632
Err(InvalidSolution::InitialConditionFailed {
16331633
condition_index,
16341634
reason,

backend/src/service.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ impl GolChallengeState {
8181
.data::<Arc<ServiceRuntime<GolChallengeService>>>()
8282
.unwrap();
8383
let puzzle_bytes = runtime.read_data_blob(puzzle_id);
84-
let puzzle = bcs::from_bytes(&puzzle_bytes).expect("Failed to deserialize puzzle");
84+
let puzzle =
85+
bcs::from_bytes::<Puzzle>(&puzzle_bytes).expect("Failed to deserialize puzzle");
8586

86-
match board.check_puzzle(&puzzle) {
87+
match puzzle.check_solution(&board) {
8788
Ok(steps) => ValidationResult {
8889
is_valid_after_steps: Some(steps),
8990
error_message: None,

0 commit comments

Comments
 (0)