refact(perf): less board clones with new function unmove_checker()
This commit is contained in:
parent
6beaa56202
commit
a239c02937
2 changed files with 21 additions and 5 deletions
|
|
@ -604,6 +604,20 @@ impl Board {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reverse a previously applied `move_checker`. No validation: assumes the move was valid.
|
||||||
|
pub fn unmove_checker(&mut self, color: &Color, cmove: CheckerMove) {
|
||||||
|
let unit = match color {
|
||||||
|
Color::White => 1,
|
||||||
|
Color::Black => -1,
|
||||||
|
};
|
||||||
|
if cmove.from != 0 {
|
||||||
|
self.positions[cmove.from - 1] += unit;
|
||||||
|
}
|
||||||
|
if cmove.to != 0 {
|
||||||
|
self.positions[cmove.to - 1] -= unit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn remove_checker(&mut self, color: &Color, field: Field) -> Result<(), Error> {
|
pub fn remove_checker(&mut self, color: &Color, field: Field) -> Result<(), Error> {
|
||||||
if field == 0 {
|
if field == 0 {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
|
||||||
|
|
@ -534,14 +534,16 @@ impl MoveRules {
|
||||||
let mut moves_seqs = Vec::new();
|
let mut moves_seqs = Vec::new();
|
||||||
let color = &Color::White;
|
let color = &Color::White;
|
||||||
let ignored_rules = vec![TricTracRule::Exit, TricTracRule::MustFillQuarter];
|
let ignored_rules = vec![TricTracRule::Exit, TricTracRule::MustFillQuarter];
|
||||||
|
let mut board = self.board.clone();
|
||||||
for moves in self.get_possible_moves_sequences(true, ignored_rules) {
|
for moves in self.get_possible_moves_sequences(true, ignored_rules) {
|
||||||
let mut board = self.board.clone();
|
|
||||||
board.move_checker(color, moves.0).unwrap();
|
board.move_checker(color, moves.0).unwrap();
|
||||||
board.move_checker(color, moves.1).unwrap();
|
board.move_checker(color, moves.1).unwrap();
|
||||||
// println!("get_quarter_filling_moves_sequences board : {:?}", board);
|
// println!("get_quarter_filling_moves_sequences board : {:?}", board);
|
||||||
if board.any_quarter_filled(*color) && !moves_seqs.contains(&moves) {
|
if board.any_quarter_filled(*color) && !moves_seqs.contains(&moves) {
|
||||||
moves_seqs.push(moves);
|
moves_seqs.push(moves);
|
||||||
}
|
}
|
||||||
|
board.unmove_checker(color, moves.1);
|
||||||
|
board.unmove_checker(color, moves.0);
|
||||||
}
|
}
|
||||||
moves_seqs
|
moves_seqs
|
||||||
}
|
}
|
||||||
|
|
@ -558,13 +560,13 @@ impl MoveRules {
|
||||||
let mut moves_seqs = Vec::new();
|
let mut moves_seqs = Vec::new();
|
||||||
let color = &Color::White;
|
let color = &Color::White;
|
||||||
let forbid_exits = self.has_checkers_outside_last_quarter();
|
let forbid_exits = self.has_checkers_outside_last_quarter();
|
||||||
|
let mut board = self.board.clone();
|
||||||
// println!("==== First");
|
// println!("==== First");
|
||||||
for first_move in
|
for first_move in
|
||||||
self.board
|
self.board
|
||||||
.get_possible_moves(*color, dice1, with_excedents, false, forbid_exits)
|
.get_possible_moves(*color, dice1, with_excedents, false, forbid_exits)
|
||||||
{
|
{
|
||||||
let mut board2 = self.board.clone();
|
if board.move_checker(color, first_move).is_err() {
|
||||||
if board2.move_checker(color, first_move).is_err() {
|
|
||||||
println!("err move");
|
println!("err move");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -574,7 +576,7 @@ impl MoveRules {
|
||||||
let mut has_second_dice_move = false;
|
let mut has_second_dice_move = false;
|
||||||
// println!(" ==== Second");
|
// println!(" ==== Second");
|
||||||
for second_move in
|
for second_move in
|
||||||
board2.get_possible_moves(*color, dice2, with_excedents, true, forbid_exits)
|
board.get_possible_moves(*color, dice2, with_excedents, true, forbid_exits)
|
||||||
{
|
{
|
||||||
if self
|
if self
|
||||||
.check_corner_rules(&(first_move, second_move))
|
.check_corner_rules(&(first_move, second_move))
|
||||||
|
|
@ -637,7 +639,7 @@ impl MoveRules {
|
||||||
// empty move
|
// empty move
|
||||||
moves_seqs.push((first_move, EMPTY_MOVE));
|
moves_seqs.push((first_move, EMPTY_MOVE));
|
||||||
}
|
}
|
||||||
//if board2.get_color_fields(*color).is_empty() {
|
board.unmove_checker(color, first_move);
|
||||||
}
|
}
|
||||||
moves_seqs
|
moves_seqs
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue