-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.rb
175 lines (139 loc) · 3.74 KB
/
pieces.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require 'byebug'
class Pieces
RENDER_HASH_BLACK = {:pawn=>"\u265f",
:knight=>"\u265e",
:king=>"\u265a",
:queen=>"\u265b",
:rook=>"\u265c",
:bishop=>"\u265d"}
RENDER_HASH_WHITE = {:pawn=>"\u2659",
:knight=>"\u2658",
:king=>"\u2654",
:queen=>"\u2655",
:rook=>"\u2656",
:bishop=>"\u2657"}
attr_accessor :position, :color, :board, :rank
def initialize(position, color, board, rank)
@position = position
@color = color
@board = board
@rank = rank
end
def moves
"will return all able moves"
end
def move(pos)
@board[@position.first][@position.last]=nil
@board[pos.first][pos.last] = self
@position = pos
end
def valid_moves(all_moves)
valid_moves = []
all_moves.each do |(row, col)|
if @board[row][col].nil?
valid_moves << [row,col]
next
end
unless @board[row][col].color == @color
valid_moves << [row, col]
end
end
valid_moves
end
def remove_invalid(pos)
pos -= [position]
pos.select{ |(row, col)| row.between?(0,7) && col.between?(0,7)}
end
def render
d={:white=>RENDER_HASH_WHITE,:black=>RENDER_HASH_BLACK}
d[color][self.rank]
end
end
class Sliding_Pieces < Pieces
DIAGONAL_MOTION=[[1,1],[-1,1],[-1,-1],[1,-1]]
STRAIGHT_MOTION = [[-1,0],[1,0],[0,1],[0,-1]]
DIC = { :rook => STRAIGHT_MOTION, :bishop => DIAGONAL_MOTION, :queen => DIAGONAL_MOTION+STRAIGHT_MOTION}
def moves
pos = []
DIC[self.rank].each do |(row,col)|
(1...8).to_a.each do |idx|
pos << [position.first + row*idx , position.last + col*idx]
break if remove_invalid([pos[-1]]) == []
last_obj = @board[pos.last.first][pos.last.last]
break unless last_obj.nil?
end
end
all_moves = remove_invalid(pos)
valid_moves(all_moves)
end
end
class Stepping_Pieces < Pieces
KNIGHT_MOTION= [ [1,2],[2,1],[1,-2],[-2,1],[-1,2],[2,-1],[-1,-2],[-2,-1]]
KING_MOTION=[[0,1],[0,-1],[1,0],[1,1],[1,-1],[-1,-1],[-1,0],[-1,1]]
DIC= {:king=>KING_MOTION,:knight=>KNIGHT_MOTION}
# def move(pos)
# if self.rank == :knight
# return "ERROR" if !knight_moves.include?(pos)
# else
# return "ERROR" if !king_moves.include?(pos)
# end
# super(pos)
# end
def moves
pos=[]
DIC[self.rank].each do |(row, col)|
pos << [position.first + row, position.last + col]
end
all_moves = remove_invalid(pos)
valid_moves(all_moves)
end
end
class Pawns < Pieces
attr_accessor :pawn_motion
def initialize(position, color, board, rank)
super(position, color, board, rank)
@moved = false
set_moves
end
def set_moves
if color == :black
@pawn_motion = [[1,0], [1,1], [1,-1], [2,0]]
else
@pawn_motion = [[-1,0], [-1,1], [-1,-1], [-2,0]]
end
end
def move(pos)
super(pos)
@moved = true
end
def moves
@pawn_motion = pawn_motion.take(3) if @moved
pos = []
@pawn_motion.each do |(row,col)|
pos << [position.first + row, position.last + col]
end
all_moves = remove_invalid(pos)
valid_moves(all_moves)
end
def valid_moves(all_moves)
valid_moves = []
f_moves=[]
all_moves.each do |(row,col)|
if col == self.position.last
f_moves << [row,col]
else
if !@board[row][col].nil? && @board[row][col].color != self.color
valid_moves << [row,col]
end
end
end
f_moves.each do |(row,col)|
if @board[row][col].nil?
valid_moves << [row,col]
else
break
end
end
valid_moves
end
end