-
Notifications
You must be signed in to change notification settings - Fork 1
/
chess_move.rb
55 lines (41 loc) · 1.07 KB
/
chess_move.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
50
51
52
53
54
55
def bishop(position, move)
px, py = position
mx, my = move
(mx - px).abs == (my - py).abs
end
def rook(position, move)
px, py = position
mx, my = move
px == mx || py == my
end
def queen(position, move)
bishop(position, move) || rook(position, move)
end
def knight(position, move)
px, py = position
mx, my = move
(px - mx).abs == 1 && (py - my).abs == 2 ||
(px - mx).abs == 2 && (py - my).abs == 1
end
def king(position, move)
px, py = position
mx, my = move
[0,1].include?((mx-px).abs) && [0,1].include?((my-py).abs)
end
def pawn(position, move)
true
end
RULES = {'rook' => lambda{|x,y| rook(x,y)},
'knight' => lambda{|x,y| knight(x,y)},
'bishop' => lambda{|x,y| bishop(x,y)},
'queen' => lambda{|x,y| queen(x,y)},
'king' => lambda{|x,y| king(x,y)},
'pawn' => lambda{|x,y| pawn(x,y)}}
def chess_move(desk, selected, move_to)
position = [selected.x, selected.y]
x, y = move_to
predicate = RULES[selected.type.type]
if predicate.call(position, move_to)
selected.update(x: x, y: y)
end
end