-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepper.rb
executable file
·49 lines (37 loc) · 903 Bytes
/
stepper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# encoding: utf-8
require_relative 'piece'
class Stepper < Piece
def moves
@vectors.map do |vector|
vector_add(@pos, vector)
end.select { |pos| @board.empty_or_capture?(pos, @color) }
end
end
class King < Stepper
def initialize(pos, color, board, has_moved = false)
super(pos, color, board)
@vectors = VECTOR_DIAGO + VECTOR_ORTHO
end
def to_s
return '♔' if @color == :white
return '♚' if @color == :black
end
def moves
if @has_moved
super
else
super.concat([[pos[0]-2,pos[1]],[pos[0]+2,pos[1]]])
end
end
end
class Knight < Stepper
VECTORS = [2,-2].product([1,-1]) + [1,-1].product([2,-2])
def initialize(pos, color, board, has_moved = false)
super(pos, color, board)
@vectors = VECTORS
end
def to_s
return '♘' if @color == :white
return '♞' if @color == :black
end
end