Class: Piece
- Inherits:
-
Object
- Object
- Piece
- Defined in:
- lib/bangkok/piece.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#piece ⇒ Object
readonly
Returns the value of attribute piece.
-
#square ⇒ Object
readonly
Returns the value of attribute square.
Class Method Summary collapse
-
.create(board, listener, color, piece_sym, square) ⇒ Object
Factory method that creates a piece of the proper subclass.
Instance Method Summary collapse
-
#clear_to?(square) ⇒ Boolean
Checks diagonals and straight horizontal/vertical lines.
-
#could_perform_move(move) ⇒ Object
Make sure this piece can perform
move
. -
#initialize(board, listener, color, piece, square) ⇒ Piece
constructor
A new instance of Piece.
- #move_off_board ⇒ Object
- #move_to(square) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(board, listener, color, piece, square) ⇒ Piece
Returns a new instance of Piece.
24 25 26 27 |
# File 'lib/bangkok/piece.rb', line 24 def initialize(board, listener, color, piece, square) @board, @listener, @color, @piece, @square = board, listener, color, piece, square end |
Instance Attribute Details
#color ⇒ Object (readonly)
Returns the value of attribute color.
4 5 6 |
# File 'lib/bangkok/piece.rb', line 4 def color @color end |
#piece ⇒ Object (readonly)
Returns the value of attribute piece.
4 5 6 |
# File 'lib/bangkok/piece.rb', line 4 def piece @piece end |
#square ⇒ Object (readonly)
Returns the value of attribute square.
4 5 6 |
# File 'lib/bangkok/piece.rb', line 4 def square @square end |
Class Method Details
.create(board, listener, color, piece_sym, square) ⇒ Object
Factory method that creates a piece of the proper subclass
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/bangkok/piece.rb', line 7 def Piece.create(board, listener, color, piece_sym, square) return case piece_sym when :K King.new(board, listener, color, square) when :Q Queen.new(board, listener, color, square) when :B Bishop.new(board, listener, color, square) when :N Knight.new(board, listener, color, square) when :R Rook.new(board, listener, color, square) when :P Pawn.new(board, listener, color, square) end end |
Instance Method Details
#clear_to?(square) ⇒ Boolean
Checks diagonals and straight horizontal/vertical lines. Won’t work correctly for anything else.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bangkok/piece.rb', line 50 def clear_to?(square) curr_file = @square.file end_file = square.file file_delta = @square.file < square.file ? 1 : (@square.file == square.file ? 0 : -1) curr_file += file_delta unless curr_file == end_file # Skip current loc curr_rank = @square.rank end_rank = square.rank rank_delta = @square.rank < square.rank ? 1 : (@square.rank == square.rank ? 0 : -1) curr_rank += rank_delta unless curr_rank == end_rank # Skip current loc if file_delta == 0 && rank_delta == 0 raise "error: trying to move to same space #{file}#{rank}" end while curr_file != end_file || curr_rank != end_rank return false unless @board.empty_at?(Square.at(curr_file, curr_rank)) curr_file += file_delta curr_rank += rank_delta end return true end |
#could_perform_move(move) ⇒ Object
Make sure this piece can perform move
. This implementation checks the basics (the color and type of this piece and the emptiness or color of the piece at the destination square); subclasses add further checks.
42 43 44 45 46 |
# File 'lib/bangkok/piece.rb', line 42 def could_perform_move(move) p = @board.at(move.square) return @color == move.color && piece == move.piece && (p.nil? || p.color != @color) end |
#move_off_board ⇒ Object
35 36 37 |
# File 'lib/bangkok/piece.rb', line 35 def move_off_board return move_to(Square::OFF_BOARD) end |
#move_to(square) ⇒ Object
29 30 31 32 33 |
# File 'lib/bangkok/piece.rb', line 29 def move_to(square) puts "#{self} moving to #{square}" if $verbose @listener.move(self, @square, square) @square = square end |
#to_s ⇒ Object
76 77 78 79 80 81 |
# File 'lib/bangkok/piece.rb', line 76 def to_s str = "#{@color.to_s.capitalize} #@piece " str << "at " if @square.on_board? str << @square.to_s str end |