Last active
December 23, 2015 21:19
-
-
Save jhamon/6695683 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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