Skip to content

Instantly share code, notes, and snippets.

@brennovich
Created January 12, 2012 01:10
Show Gist options
  • Save brennovich/1597839 to your computer and use it in GitHub Desktop.
Save brennovich/1597839 to your computer and use it in GitHub Desktop.
Chess Class, with Knight valid moves
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'
@brennovich
Copy link
Author

There's an error when moves the Knight to out of the board. Ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment