Skip to content

Commit 7916567

Browse files
committed
Handles correctly the end of the searched puzzles.
It was crashing when we hit the end of the puzzles array.
1 parent df04250 commit 7916567

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ pub struct OfflinePuzzles {
425425
squares: [button::State; 64],
426426
last_move_from: Option<PositionGUI>,
427427
last_move_to: Option<PositionGUI>,
428+
is_playing: bool,
428429

429430
theme_list: pick_list::State<TaticsThemes>,
430431
theme: TaticsThemes,
@@ -455,6 +456,7 @@ impl Default for OfflinePuzzles {
455456
squares: [button::State::default(); 64],
456457
last_move_from: None,
457458
last_move_to: None,
459+
is_playing: false,
458460

459461
theme_list: pick_list::State::default(),
460462
theme : TaticsThemes::default(),
@@ -563,7 +565,7 @@ impl Sandbox for OfflinePuzzles {
563565
self.current_puzzle_move += 1;
564566

565567
if self.current_puzzle_move == correct_moves.len() {
566-
if self.current_puzzle < self.puzzles.len() {
568+
if self.current_puzzle < self.puzzles.len() - 1 {
567569
// The previous puzzle ended, and we still have puzzles available,
568570
// so we prepare the next one.
569571
self.current_puzzle += 1;
@@ -589,6 +591,12 @@ impl Sandbox for OfflinePuzzles {
589591
} else {
590592
self.puzzle_status = String::from("Black to move!");
591593
}
594+
} else {
595+
self.board = Board::default();
596+
self.last_move_from = None;
597+
self.last_move_to = None;
598+
self.is_playing = false;
599+
self.puzzle_status = String::from("All puzzles done for this search!");
592600
}
593601
} else {
594602
movement = ChessMove::new(
@@ -660,11 +668,13 @@ impl Sandbox for OfflinePuzzles {
660668
} else {
661669
self.puzzle_status = String::from("Black to move!");
662670
}
671+
self.is_playing = true;
663672
} else {
664673
// Just putting the default position to make it obvious the search ended.
665674
self.board = Board::default();
666675
self.last_move_from = None;
667676
self.last_move_to = None;
677+
self.is_playing = false;
668678
self.puzzle_status = String::from("Sorry, no puzzle found");
669679
}
670680
} Err(_) => {

0 commit comments

Comments
 (0)