Created
January 12, 2012 01:10
-
-
Save brennovich/1597839 to your computer and use it in GitHub Desktop.
Chess Class, with Knight valid moves
This file contains hidden or 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 Chess | |
def self.board | |
board = [] | |
('a'..'h').map do |w| | |
line = [] | |
('1'..'8').each do |h| | |
line.push w + h | |
end | |
board.push line | |
end | |
board | |
end | |
@@board = self.board | |
def knight_valid_moves position | |
search = @@board.detect{|aa| aa.include?(position)} | |
valid_moves = [] | |
[[2, 1], [1, 2]].each do |row, column| | |
[-1, 1].each do |row_sign| | |
[-1, 1].each do |column_sign| | |
valid_moves.push @@board[@@board.index(search)-"#{row * row_sign}".to_i][search.index(position)+"#{column * column_sign}".to_i] | |
end | |
end | |
end | |
valid_moves | |
end | |
end | |
### | |
xalingo = Chess.new | |
p xalingo.knight_valid_moves 'e4' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's an error when moves the Knight to out of the board. Ideas?