Class: Pawn

Inherits:
Piece show all
Defined in:
lib/bangkok/piece.rb

Instance Attribute Summary collapse

Attributes inherited from Piece

#color, #piece, #square

Instance Method Summary collapse

Methods inherited from Piece

#clear_to?, create, #move_off_board, #to_s

Constructor Details

#initialize(board, listener, color, square) ⇒ Pawn

Returns a new instance of Pawn.



170
171
172
173
# File 'lib/bangkok/piece.rb', line 170

def initialize(board, listener, color, square)
  super(board, listener, color, :P, square)
  @moved = false
end

Instance Attribute Details

#movedObject (readonly)

Returns the value of attribute moved.



168
169
170
# File 'lib/bangkok/piece.rb', line 168

def moved
  @moved
end

Instance Method Details

#could_perform_move(move) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/bangkok/piece.rb', line 180

def could_perform_move(move)
  return false unless super

  square = move.square
  if @color == :white
    if square.file == @square.file && square.rank == @square.rank + 1
      # single step forwards
      return @board.empty_at?(square)
    elsif square.file + 1 == @square.file && square.rank == @square.rank + 1
      # take a piece queenside
      return !@board.empty_at?(square)
    elsif square.file == @square.file + 1 && square.rank == @square.rank + 1
      # take a piece kingside
      return !@board.empty_at?(square)
    elsif !@moved && square.file == @square.file &&
                     square.rank == @square.rank + 2
      # first move: 2 squares forward
      return @board.empty_at?(Square.new(@square.file, @square.rank + 1)) &&
        @board.empty_at?(square)

# TODO Implement en passant checking. The following code was wrong. 

#       elsif !@moved && square.file + 1 == @square.file &&
#                        square.rank == @square.rank + 2
#         # e.p. queenside
#         return [email protected]_at?(square)
#       elsif !@moved && square.file == @square.file + 1 &&
#                        square.rank == @square.rank + 2
#         # e.p. kingside
#         return [email protected]_at?(square)

    end
  else
    # black
    if square.file == @square.file && square.rank == @square.rank - 1
      # single step forwards
      return @board.empty_at?(square)
    elsif square.file + 1 == @square.file && square.rank == @square.rank - 1
      # take a piece queenside
      return !@board.empty_at?(square)
    elsif square.file == @square.file + 1 && square.rank == @square.rank - 1
      # take a piece kingside
      return !@board.empty_at?(square)
    elsif !@moved && square.file == @square.file &&
                     square.rank == @square.rank - 2
      # first move: 2 spaces forward
      return @board.empty_at?(Square.new(@square.file, @square.rank - 1)) &&
        @board.empty_at?(square)

# TODO Implement en passant checking. The following code was wrong. 

#       elsif !@moved && square.file + 1 == @square.file &&
#                        square.rank == @square.rank - 2
#         # en passant queenside
#         return [email protected]_at?(square)
#       elsif !@moved && square.file == @square.file + 1 &&
#                        square.rank == @square.rank - 2
#         # en passant kingside
#         return [email protected]_at?(square)

    end
  end
end

#move_to(square) ⇒ Object



175
176
177
178
# File 'lib/bangkok/piece.rb', line 175

def move_to(square)
  @moved = true
  return super
end