Class: Chess::Pieces::Pawn
Overview
Pawn
Instance Attribute Summary
Attributes inherited from Piece
Instance Method Summary collapse
-
#attack_en_passant(board, file, rank) ⇒ Array
moves for attacking an pawn which is a Board#possible_en_passant_target.
-
#cross_side_attack(board, file, rank) ⇒ Array
cross attack moves of pawn.
-
#first_move?(rank) ⇒ Boolean
checks if it’s first move of pawn.
-
#north_attack(board, file, rank) ⇒ Array
helper method for #cross_side_attack.
-
#north_two_step(board, file, rank) ⇒ Array
helper method for #two_step_forward.
-
#one_step_forward(board, file, rank) ⇒ Array
one step move of pawn.
-
#pawn_moves_by_color(board, file, rank) ⇒ Array
creates all the possible first moves.
-
#possible_moves(board) ⇒ Array
possible_moves of the pawn.
-
#south_attack(board, file, rank) ⇒ Array
helper method for #cross_side_attack.
-
#south_two_step(board, file, rank) ⇒ Array
helper method for #two_step_forward.
-
#two_step_forward(board, file, rank) ⇒ Array
two step move of pawn.
Methods inherited from Piece
Constructor Details
This class inherits a constructor from Chess::Pieces::Piece
Instance Method Details
#attack_en_passant(board, file, rank) ⇒ Array
moves for attacking an pawn which is a Board#possible_en_passant_target
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/chess/pieces/pawn.rb', line 146 def attack_en_passant(board, file, rank) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength moves = [] if white? north_east = board.north_east_pos(file, rank) moves << north_east if (north_east.first + north_east.last.to_s) == board.possible_en_passant_target north_west = board.north_west_pos(file, rank) moves << north_west if (north_west.first + north_west.last.to_s) == board.possible_en_passant_target else south_east = board.south_east_pos(file, rank) moves << south_east if (south_east.first + south_east.last.to_s) == board.possible_en_passant_target south_west = board.south_west_pos(file, rank) moves << south_west if (south_west.first + south_west.last.to_s) == board.possible_en_passant_target end moves end |
#cross_side_attack(board, file, rank) ⇒ Array
cross attack moves of pawn
104 105 106 107 108 109 110 111 112 |
# File 'lib/chess/pieces/pawn.rb', line 104 def cross_side_attack(board, file, rank) moves = [] moves += if white? north_attack(board, file, rank) else south_attack(board, file, rank) end moves end |
#first_move?(rank) ⇒ Boolean
checks if it’s first move of pawn
22 23 24 |
# File 'lib/chess/pieces/pawn.rb', line 22 def first_move?(rank) (rank == 1 && white?) || (rank == 6 && black?) end |
#north_attack(board, file, rank) ⇒ Array
helper method for #cross_side_attack
117 118 119 120 121 122 123 124 125 |
# File 'lib/chess/pieces/pawn.rb', line 117 def north_attack(board, file, rank) moves = [] north_east = board.north_east_pos(file, rank) north_west = board.north_west_pos(file, rank) [north_east, north_west].each do |pos| moves << pos if !(board.empty_at?(*pos) || board.pos_nil?(pos)) && board.piece_at(*pos).black? end moves end |
#north_two_step(board, file, rank) ⇒ Array
helper method for #two_step_forward
76 77 78 79 80 81 82 |
# File 'lib/chess/pieces/pawn.rb', line 76 def north_two_step(board, file, rank) north_one_step = board.north_pos(file, rank) north_two_step = board.north_pos(*north_one_step) return [] unless board.empty_at?(*north_one_step) && board.empty_at?(*north_two_step) north_two_step end |
#one_step_forward(board, file, rank) ⇒ Array
one step move of pawn
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/chess/pieces/pawn.rb', line 46 def one_step_forward(board, file, rank) moves = [] if white? north = board.north_pos(file, rank) moves << north if board.empty_at?(*north) else south = board.south_pos(file, rank) moves << south if board.empty_at?(*south) && board.pos_in_range?(south) end moves end |
#pawn_moves_by_color(board, file, rank) ⇒ Array
creates all the possible first moves
31 32 33 34 35 36 37 38 39 |
# File 'lib/chess/pieces/pawn.rb', line 31 def pawn_moves_by_color(board, file, rank) moves = [] moves += one_step_forward(board, file, rank) moves += two_step_forward(board, file, rank) if first_move?(rank) moves += attack_en_passant(board, file, rank) unless first_move?(rank) moves += cross_side_attack(board, file, rank) moves.reject!(&:empty?) moves end |
#possible_moves(board) ⇒ Array
possible_moves of the pawn
12 13 14 15 16 17 18 |
# File 'lib/chess/pieces/pawn.rb', line 12 def possible_moves(board) moves = [] file = @pos[0] rank = @pos[1] moves += pawn_moves_by_color(board, file, rank) moves end |
#south_attack(board, file, rank) ⇒ Array
helper method for #cross_side_attack
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/chess/pieces/pawn.rb', line 130 def south_attack(board, file, rank) moves = [] south_east = board.south_east_pos(file, rank) south_west = board.south_west_pos(file, rank) [south_east, south_west].each do |pos| moves << pos if !(board.empty_at?(*pos) || board.pos_nil?(pos)) && board.piece_at(*pos).white? && board.pos_in_range?(pos) end moves end |
#south_two_step(board, file, rank) ⇒ Array
helper method for #two_step_forward
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/chess/pieces/pawn.rb', line 87 def south_two_step(board, file, rank) south_one_step = board.south_pos(file, rank) south_two_step = board.south_pos(*south_one_step) unless board.empty_at?(*south_one_step) && board.empty_at?(*south_two_step) && board.pos_in_range?(south_one_step) && board.pos_in_range?(south_two_step) return [] end south_two_step end |
#two_step_forward(board, file, rank) ⇒ Array
two step move of pawn
63 64 65 66 67 68 69 70 71 |
# File 'lib/chess/pieces/pawn.rb', line 63 def two_step_forward(board, file, rank) moves = [] moves << if white? north_two_step(board, file, rank) else south_two_step(board, file, rank) end moves end |