Module: Pawn
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
Instance Method Summary collapse
- #illegal(board, x, y) ⇒ Object
- #jumped?(board, x, y) ⇒ Boolean
-
#move(board, x, y) ⇒ Object
attempt to move the piece to x,y on the board.
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
5 6 7 |
# File 'lib/pawn.rb', line 5 def direction @direction end |
Instance Method Details
#illegal(board, x, y) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pawn.rb', line 33 def illegal(board,x,y) possible_moves = [ [@x, @y+(1 * @direction)], ] possible_moves << [@x, @y+(2 * @direction)] if @start possible_moves = Board.board_safe(possible_moves) possible_attack_moves = Board.board_safe([ [@x-1, @y+(1 * @direction)], [@x+1, @y+(1 * @direction)] ]) en_passent = board.en_passent en_passent_coord = en_passent.to_coord en_passent_coord[1] &&= en_passent_coord[1] - en_passent.direction if board.at(x,y).friend_of?(self) error = "Your piece is blocking your path" elsif board.at(x,y).enemy_of?(self) unless possible_attack_moves.find {|move| move == [x,y]} error = "Cannot move this way!" end elsif en_passent_coord == [x,y] unless possible_attack_moves.find {|move| move == [x,y]} error = "Cannot move this way!" else en_passent.remove_from(board) end else unless possible_moves.find {|move| move == [x,y]} error = "Cannot move here!" else if jumped?(board,x,y) raise Game::IllegalMove, "Pawns cannot jump!" end end end raise Game::IllegalMove, error if error end |
#jumped?(board, x, y) ⇒ Boolean
74 75 76 77 |
# File 'lib/pawn.rb', line 74 def jumped?(board,x,y) return false unless (@y-y).abs == 2 !board.at(x,y-@direction).empty? end |
#move(board, x, y) ⇒ Object
attempt to move the piece to x,y on the board
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pawn.rb', line 8 def move(board,x,y) # check if it is an illegal move (raises Game::IllegalMove if it is) illegal(board, x, y) # make start false if it not already false !@start || @start = false # remove the piece from it's original position board.remove(self) # save the piece if it can be taken by en passent board.en_passent = (@y-y).abs == 2 ? self : NullSpace.new # update the coordinates @x, @y = x, y # replace with queen if on back row if @y == Board::BackRow[color] board.place(@queen.new(@x,@y)) else # or place on board place_on(board) end end |