From 99f2af0233b62fea7b2482b435e068399b351af3 Mon Sep 17 00:00:00 2001 From: Admiral Adama Date: Fri, 21 Sep 2018 00:57:27 -0700 Subject: [PATCH] SPEED - convert north/south type strings to ints --- models/ChessRulebook.php | 89 ++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/models/ChessRulebook.php b/models/ChessRulebook.php index b39d871..41c1e0b 100644 --- a/models/ChessRulebook.php +++ b/models/ChessRulebook.php @@ -1,6 +1,15 @@ array(2,1), @@ -13,53 +22,53 @@ class ChessRulebook { 11 => array(2,-1) ); const DIRECTION_OFFSETS = array( - 'north' => array(1,0), - 'south' => array(-1,0), - 'east' => array(0,1), - 'west' => array(0,-1), - 'northeast' => array(1,1), - 'northwest' => array(1,-1), - 'southeast' => array(-1,1), - 'southwest' => array(-1,-1) + self::NORTH => array(1,0), + self::SOUTH => array(-1,0), + self::EAST => array(0,1), + self::WEST => array(0,-1), + self::NORTHEAST => array(1,1), + self::NORTHWEST => array(1,-1), + self::SOUTHEAST => array(-1,1), + self::SOUTHWEST => array(-1,-1) ); const BISHOP_DIRECTIONS = array( - 'northwest', - 'northeast', - 'southwest', - 'southeast' + self::NORTHWEST, + self::NORTHEAST, + self::SOUTHWEST, + self::SOUTHEAST ); const ROOK_DIRECTIONS = array( - 'north', - 'south', - 'east', - 'west' + self::NORTH, + self::SOUTH, + self::EAST, + self::WEST ); const QUEEN_DIRECTIONS = array( - 'north', - 'south', - 'east', - 'west', - 'northwest', - 'northeast', - 'southwest', - 'southeast' + self::NORTH, + self::SOUTH, + self::EAST, + self::WEST, + self::NORTHWEST, + self::NORTHEAST, + self::SOUTHWEST, + self::SOUTHEAST ); const KING_DIRECTIONS = array( - 'north', - 'south', - 'east', - 'west', - 'northwest', - 'northeast', - 'southwest', - 'southeast' + self::NORTH, + self::SOUTH, + self::EAST, + self::WEST, + self::NORTHWEST, + self::NORTHEAST, + self::SOUTHWEST, + self::SOUTHEAST ); const KNIGHT_DIRECTIONS = array(1, 2, 4, 5, 7, 8, 10, 11); - const BLACK_PAWN_CAPTURE_DIRECTIONS = array('southeast', 'southwest'); - const BLACK_PAWN_MOVEMENT_DIRECTIONS = array('south'); - const WHITE_PAWN_CAPTURE_DIRECTIONS = array('northeast', 'northwest'); - const WHITE_PAWN_MOVEMENT_DIRECTIONS = array('north'); + const BLACK_PAWN_CAPTURE_DIRECTIONS = array(self::SOUTHEAST, self::SOUTHWEST); + const BLACK_PAWN_MOVEMENT_DIRECTIONS = array(self::SOUTH); + const WHITE_PAWN_CAPTURE_DIRECTIONS = array(self::NORTHEAST, self::NORTHWEST); + const WHITE_PAWN_MOVEMENT_DIRECTIONS = array(self::NORTH); const PROMOTION_PIECES = array( ChessPiece::QUEEN, @@ -428,12 +437,12 @@ static function add_en_passant_moves_to_moves_list($piece, $board, $moves, $stor // It was actually slower! I still had to use variables to make the code readable, plus // it was a two level array. Boo. if ( $piece->color == ChessPiece::WHITE ) { - $capture_directions_from_starting_square = array('northeast', 'northwest'); - $enemy_pawn_direction_from_ending_square = array('south'); + $capture_directions_from_starting_square = array(self::NORTHEAST, self::NORTHWEST); + $enemy_pawn_direction_from_ending_square = array(self::SOUTH); $en_passant_rank = 5; } elseif ( $piece->color == ChessPiece::BLACK ) { - $capture_directions_from_starting_square = array('southeast', 'southwest'); - $enemy_pawn_direction_from_ending_square = array('north'); + $capture_directions_from_starting_square = array(self::SOUTHEAST, self::SOUTHWEST); + $enemy_pawn_direction_from_ending_square = array(self::NORTH); $en_passant_rank = 4; }