Module: Knight

Extended by:
Knight
Includes:
Moves
Included in:
BlackKnight, Knight, WhiteKnight
Defined in:
lib/knight.rb

Instance Method Summary collapse

Methods included from Moves

#diagonal, #horizontal, #move, #vertical

Instance Method Details

#illegal(board, x, y) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/knight.rb', line 6

def illegal(board,x,y)
  possible_moves = [
    [@x+2, @y-1],
    [@x+2, @y+1],
    [@x-2, @y+1],
    [@x-2, @y-1],
    [@x+1, @y-2],
    [@x-1, @y-2],
    [@x+1, @y+2],
    [@x-1, @y+2],
  ]

  if not(possible_moves.any? {|move| move == [x,y]})
    raise Game::IllegalMove, "#{x},#{y} is not a possible move" 
  elsif board.at(x,y).friend_of?(self)
    raise Game::IllegalMove, "#{x},#{y} is occupied by a friend"
  else
    :legal_move
  end
end