Skip to content

Instantly share code, notes, and snippets.

@jhamon
Last active December 23, 2015 21:19
Show Gist options
  • Save jhamon/6695683 to your computer and use it in GitHub Desktop.
Save jhamon/6695683 to your computer and use it in GitHub Desktop.
class Piece
attr_reader :color
attr_accessor :pos
def initialize(color, pos)
@color = color # :white or :black
@pos = pos # position = [x, y]
end
def dup
self.class.new(@color, @pos)
end
private
def new_position(old_pos, vector)
# Add a movement vector to a current position.
[old_pos[0] + vector[0], old_pos[1] + vector[1]]
end
def remove_self_killing_moves(moves, board)
# Take an array of potential moves and remove any moves that
# kill pieces of the same color as self
moves.select! do |move|
board.piece_at_position(move).nil? || board.piece_at_position(move).color != @color
end
moves
end
def move_on_board?(move)
(0...8).include?(move[0]) && (0...8).include?(move[1])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment