Skip to content

Commit 2ec3223

Browse files
committed
[DEAD END] This makes rustc crash
(this branch is only for reporting the bug)
1 parent c4b35e1 commit 2ec3223

2 files changed

Lines changed: 61 additions & 33 deletions

File tree

‎src/game.rs‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use std::cmp::{Ord, Eq, PartialOrd, Ordering};
77
use std::ops::{Not, Add, Neg};
88

99
extern crate bit_vec;
10+
extern crate test;
1011

11-
const KING_MOVES: [PositionDelta; 8] = [
12+
const KING_DELTAS: &'static [PositionDelta; 8] = &[
1213
PositionDelta { x: 1, y: 1},
1314
PositionDelta { x: 1, y: 0},
1415
PositionDelta { x: 1, y: -1},
@@ -18,7 +19,7 @@ const KING_MOVES: [PositionDelta; 8] = [
1819
PositionDelta { x: 0, y: 1},
1920
PositionDelta { x: 0, y: -1},
2021
];
21-
const KNIGHT_MOVES: [PositionDelta; 8] = [
22+
const KNIGHT_DELTAS: &'static [PositionDelta; 8] = &[
2223
PositionDelta { x: 1, y: 2},
2324
PositionDelta { x: -1, y: 2},
2425
PositionDelta { x: 1, y: -2},
@@ -29,7 +30,7 @@ const KNIGHT_MOVES: [PositionDelta; 8] = [
2930
PositionDelta { x: -2, y: -1},
3031
];
3132

32-
const QUEEN_DIRS: [Direction; 8] = [
33+
const QUEEN_DIRS: &'static [Direction; 8] = &[
3334
Up,
3435
Down,
3536
Right,
@@ -40,14 +41,14 @@ const QUEEN_DIRS: [Direction; 8] = [
4041
DownLeft,
4142
];
4243

43-
const ROOK_DIRS: [Direction; 4] = [
44+
const ROOK_DIRS: &'static [Direction; 4] = &[
4445
Up,
4546
Down,
4647
Right,
4748
Left,
4849
];
4950

50-
const BISHOP_DIRS: [Direction; 4] = [
51+
const BISHOP_DIRS: &'static [Direction; 4] = &[
5152
UpRight,
5253
UpLeft,
5354
DownRight,
@@ -449,8 +450,8 @@ impl Game {
449450
_ => return moves,
450451
};
451452
match piece {
452-
piece!(color, King) => {
453-
for delta_pos in KING_MOVES.iter() {
453+
piece!(color, pt @ King) | piece!(color, pt @ Knight) => {
454+
for delta_pos in pt.get_posible_deltas().iter() {
454455
let to_pos = from_pos + *delta_pos;
455456
if let Some(to_square) = self.get_raw_square(to_pos) {
456457
if ! to_square.has_color(color) {
@@ -513,16 +514,6 @@ impl Game {
513514
}
514515
}
515516
},
516-
piece!(color, Knight) => {
517-
for delta_pos in KNIGHT_MOVES.iter() {
518-
let to_pos = from_pos + *delta_pos;
519-
if let Some(to_square) = self.get_raw_square(to_pos) {
520-
if ! to_square.has_color(color) {
521-
moves.push_back(ValuedMove::new(from_pos, to_pos, MoveType::Normal));
522-
}
523-
}
524-
}
525-
},
526517
piece!(color, Pawn) => {
527518
let (promotion_y, long_move_y, foward_dir) = match color {
528519
White => (Position::ch2y('7'), Position::ch2y('2'), Up),
@@ -590,6 +581,13 @@ impl fmt::Display for Game {
590581
}
591582
}
592583

584+
#[bench]
585+
fn bench_is_valid(b: &mut test::Bencher) {
586+
let mut game: Game = Game::new();
587+
// b.iter(|| game.make_move(&Move::safe_from_string("e2e4")));
588+
b.iter(|| game.make_move(&Move::safe_from_string("a1e4")));
589+
}
590+
593591
#[test]
594592
fn test_helper_functions() {
595593
let mut game = Game::new();
@@ -791,6 +789,22 @@ impl PieceType {
791789
Pawn => 1,
792790
}
793791
}
792+
793+
pub fn get_posible_dirs(&self) -> &'static [Direction] {
794+
match *self {
795+
Queen => QUEEN_DIRS,
796+
Rook => ROOK_DIRS,
797+
Bishop => BISHOP_DIRS,
798+
_ => unreachable!("Asked posible directions of a piece that doesn't have posible directions"),
799+
}
800+
}
801+
pub fn get_posible_deltas(&self) -> &'static [PositionDelta] {
802+
match *self {
803+
King => KING_DELTAS,
804+
Knight => KNIGHT_DELTAS,
805+
_ => unreachable!("Asked posible positions of a piece that doesn't have defined positions"),
806+
}
807+
}
794808
}
795809

796810
impl fmt::Display for PieceType {

‎src/lurri.rs‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ extern crate rand;
77
extern crate test;
88

99
pub fn get_move(game: &Game) -> Move {
10-
get_best_move(game, 1, 1)
10+
get_best_move(game)
1111
}
1212

13-
pub fn get_best_move(game: &Game, final_depth: u32, current_depth: u32) -> Move {
13+
pub fn get_best_move(game: &Game) -> Move {
1414
let mut moves = game.get_all_valid_moves();
1515
game.evaluate_moves(&mut moves);
1616
let mov: ValuedMove = match game.turn {
@@ -23,28 +23,26 @@ pub fn get_best_move(game: &Game, final_depth: u32, current_depth: u32) -> Move
2323

2424
impl Game {
2525
pub fn evaluate(&self) -> i32 {
26-
Position::all().fold(0, |acc, val|
27-
match self.get_piece(val) {
26+
Position::all().fold(0, |acc, pos|
27+
match self.get_piece(pos) {
2828
None => acc,
2929
Some(piece) =>
30-
acc + piece.get_value() * 10 + piece.color.get_sign() * (self.get_valid_moves(val).len() as i32),
30+
acc + piece.get_value() * 10
31+
+ piece.color.get_sign() * (self.get_valid_moves(pos).len() as i32)
32+
,
3133
}
3234
)
3335
}
3436
pub fn evaluate_move(&self, mov: &Move) -> i32 {
3537
let mut aux_game = (*self).clone();
3638
if let Err(e) = aux_game.make_move(mov) {
37-
println!("ERROR mov: {}, reason: {}", mov, e)
39+
println!("ERROR (engine generated an invalid move) mov: {}, reason: {}", mov, e)
3840
}
3941
aux_game.evaluate()
4042
}
41-
pub fn evaluate_moves<'a>(&'a self, moves: &'a mut LinkedList<ValuedMove>) -> &'a mut LinkedList<ValuedMove> {
43+
pub fn evaluate_moves<'a>(&self, moves: &'a mut LinkedList<ValuedMove>) -> &'a mut LinkedList<ValuedMove> {
4244
for mov in moves.iter_mut() {
43-
if mov.value.is_none() {
44-
mov.value = Some(self.evaluate_move(&mov.mov))
45-
} else {
46-
panic!("tried to value an already valued move")
47-
}
45+
mov.value = Some(self.evaluate_move(&mov.mov))
4846
}
4947
moves
5048
}
@@ -57,9 +55,25 @@ fn test_evaluate() {
5755
}
5856

5957
#[bench]
60-
fn bench_speed(b: &mut test::Bencher) {
61-
let mut game: Game = Game::new();
62-
b.iter(|| get_move(&game));
63-
game.make_move(&Move::safe_from_string("e2e4"));
58+
fn bench_evaluate(b: &mut test::Bencher) {
59+
let game: Game = Game::new();
60+
b.iter(|| game.evaluate());
61+
}
62+
#[bench]
63+
fn bench_get_all_valid_moves(b: &mut test::Bencher) {
64+
let game: Game = Game::new();
65+
b.iter(|| game.get_all_valid_moves());
66+
}
67+
#[bench]
68+
fn bench_evaluate_linkedlist(b: &mut test::Bencher) {
69+
let game: Game = Game::new();
70+
b.iter(|| {
71+
let mut moves = game.get_all_valid_moves();
72+
game.evaluate_moves(&mut moves);
73+
});
74+
}
75+
#[bench]
76+
fn bench_get_move(b: &mut test::Bencher) {
77+
let game: Game = Game::new();
6478
b.iter(|| get_move(&game));
6579
}

0 commit comments

Comments
 (0)