-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.rb
128 lines (111 loc) · 2.69 KB
/
piece.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# encoding: utf-8
class Piece
attr_reader :color, :board
attr_accessor :pos, :king
def initialize(color, pos, board)
@color = color
@board = board
@king = false
@pos = pos
end
def direction
return [[-1, -1], [-1, 1], [1, 1], [1, -1]] if @king
return [[1, -1], [1, 1]] if color == :black
return [[-1, -1], [-1, 1]] if color == :white
end
def moves
returned_moves = []
direction.each do |delta|
pos = @pos.dup
pos[0] += delta[0]
pos[1] += delta[1]
returned_moves << pos
end
returned_moves.select! do |coord|
board.valid_pos?(coord) && !board.get_spot?(coord)
end
returned_moves
end
def jump
returned_moves = []
direction.each do |delta|
pos = @pos.dup
pos[0] += delta[0]
pos[1] += delta[1]
next if !board.valid_pos?(pos) || board.get_spot?(pos).nil? ||
board.get_spot?(pos).color == color
pos[0] += delta[0]
pos[1] += delta[1]
returned_moves << pos if board.valid_pos?(pos) && board.get_spot?(pos).nil?
end
returned_moves
end
def king?
@king = true if color == :black && pos[0] == 7
@king = true if color == :white && pos[0] == 0
end
def piece_between(pos1, pos2)
btw_0 = [pos1[0], pos2[0]].max - 1
btw_1 = [pos1[1], pos2[1]].max - 1
[btw_0, btw_1]
end
def perform_jump(pos)
if jump.include?(pos)
temp_spot = @pos.dup
board.move!(@pos, pos)
set_pos(pos)
board[piece_between(temp_spot, pos)] = nil
king?
else
raise "Invalid Move!"
end
end
def perform_slide(pos)
if moves.include?(pos)
board.move!(@pos, pos)
set_pos(pos)
king?
else
raise "Invalid Move!"
end
end
def perform_moves!(move_sequence)
begin
if move_sequence.count == 1
begin
perform_slide(move_sequence.first)
rescue
perform_jump(move_sequence.first)
end
else
until move_sequence.count == 0
perform_jump(move_sequence.shift)
end
end
rescue
raise "InvalidMoveError"
end
end
def perform_moves(move_sequence)
puts "in performs moves #{move_sequence}"
return perform_moves!(move_sequence.dup) if valid_move_seq?(move_sequence.dup)
raise "InvalidMoveError"
end
def valid_move_seq?(move_sequence)
dupped_board = board.dup
begin
dupped_board[@pos].perform_moves!(move_sequence)
return true
rescue
return false
end
end
def set_pos(new_pos)
@pos = new_pos
end
def inspect
return "♔" if color == :white && king == true
return "♚" if color == :black && king == true
color == :white ? "○" : "●"
end
end