@@ -7,8 +7,9 @@ use std::cmp::{Ord, Eq, PartialOrd, Ordering};
77use std:: ops:: { Not , Add , Neg } ;
88
99extern 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]
594592fn 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
796810impl fmt:: Display for PieceType {
0 commit comments