-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.rb
254 lines (227 loc) · 5.4 KB
/
chess.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
class Chess
class Board < Chess
def initialize
rows = {}
row = [0,1,0,1,0,1,0,1]
[1,3,5,7].each{|i| rows[i] = row; rows[i + 1] = row.reverse}
@board = rows
@light_dead = []
@dark_dead = []
@player1 = 'light'
@player2 = 'dark'
@turn = @player1
@a_king_is_dead = false
end
def a_king_is_dead?
@a_king_is_dead
end
def current_player
@turn
end
def dead_dark
@dark_dead
end
def dead_light
@light_dead
end
def kill(piece)
if piece.team == "light"
@light_dead << piece
else
@dark_dead << piece
end
@a_king_is_dead = true if piece.is_a?(Chess::Piece::King)
end
def dead_opponents(player)
if player == 'light'
dead_dark
else
dead_light
end
end
def change_player
@turn = @turn == 'light' ? 'dark' : 'light'
end
def layout
@board
end
def row
@board
end
def square(coords)
r = coords[0].to_i
s = (coords[1].to_i) -1
row[r][s]
end
def place_piece(piece, coords)
r,s = coords
@board[r][s] = piece
end
def pieces
nobles = [Piece::Rooke.new,Piece::Knight.new,Piece::Bishop.new,Piece::Queen.new,Piece::King.new,Piece::Bishop.new,Piece::Knight.new, Piece::Rooke.new]
dark_nobles = [Piece::Rooke.new('dark'),Piece::Knight.new('dark'),Piece::Bishop.new('dark'),Piece::Queen.new('dark'),Piece::King.new('dark'),Piece::Bishop.new('dark'),Piece::Knight.new('dark'), Piece::Rooke.new('dark')]
peasants = [Piece::Pawn.new,Piece::Pawn.new,Piece::Pawn.new,Piece::Pawn.new,Piece::Pawn.new,Piece::Pawn.new,Piece::Pawn.new,Piece::Pawn.new]
dark_peasants = [Piece::Pawn.new('dark'),Piece::Pawn.new('dark'),Piece::Pawn.new('dark'),Piece::Pawn.new('dark'),Piece::Pawn.new('dark'),Piece::Pawn.new('dark'),Piece::Pawn.new('dark'),Piece::Pawn.new('dark')]
row[1] = nobles
row[2] = peasants
row[8] = dark_nobles.reverse
row[7] = dark_peasants
end
def is_white?(coords)
r, s = coords
s -= 1
if r.even?
s.even? ? false : true
else
s.even? ? true : false
end
end
def make_default_color!(coords)
r,s = coords
s -= 1
color = is_white?(coords) ? 0 : 1
@board[r][s] = color
end
end
class Piece
def move!(board, before, after)
# return false if team != board.current_player
return false unless board.square(before).is_a?(Piece)
# return false if board.square(after).is_a?(Piece)
piece = board.square(before)
board.place_piece(piece, after)
board.make_default_color!(before)
end
class Rooke < Piece
def initialize(color='light'); @team=color;end
def team; @team;end
def cant_move_there?(before, after)
r,s = before
ar, as = after
return true if r != ar && s != as
return true if r == ar && s == as
false
end
end
class Knight < Piece
def initialize(color='light'); @team=color;end
def team; @team;end
def cant_move_there?(before, after)
r,s = before
ar, as = after
if ar == r - 1 || ar == r + 1
return true if as == s - 2 || as == s + 2
end
if as == s - 1 || as == s + 1
return true if ar == r - 2 || ar == r + 2
end
false
end
end
class Bishop < Piece
def initialize(color='light'); @team=color;end
def team; @team;end
def cant_move_there?(before, after)
r,s = before
ar, as = after
return true unless (r - ar).abs == (s - as)
false
end
end
class Queen < Piece
def initialize(color='light'); @team=color;end
def team; @team;end
def cant_move_there?(before, after)
r,s = before
ar, as = after
if (r - ar).abs != (s - as)
return true if r != ar && s != as
return true if r == ar && s == as
false
end
false
end
end
class King < Piece
def initialize(color='light'); @team=color;end
def team; @team;end
def cant_move_there?(before, after)
r,s = before
ar, as = after
return false if (r+s) == (ar+as+1) || (r+s) == (ar+as-1)
true
end
end
class Pawn < Piece
def initialize(color='light'); @team=color;end
def team; @team;end
def cant_move_there?(before, after)
r,s = before
ar, as = after
return true if s != as
if team == 'light'
return true if r != ar -1
false
else
return true if r != ar + 1
false
end
end
end
end
end
class String
def to_coords
strip.split(',').collect{|k| k.to_i}
end
end
board = Chess::Board.new
board.pieces
until board.a_king_is_dead? do
player = board.current_player
p "#{player} it's your turn, which peice would you like to move?"
before = STDIN.gets.to_coords
puts before.class
case
when before == [0]
p board.row
redo
when before.min < 1
p "The values must be between 1 and 8"
redo
when before.max > 8
p "The values must be between 1 and 8"
redo
end
piece = board.square(before)
case
when !piece.is_a?(Chess::Piece)
p "There is not a piece in that square"
redo
when piece.team != player
p "That is not your piece"
redo
end
p "Where would you like to move your #{piece.class}?"
after = STDIN.gets.to_coords
case
when piece.cant_move_there?(before, after) == true
p "That isn't a move for #{piece.class}"
redo
end
landing = board.square(after)
if landing.is_a?(Chess::Piece)
if landing.team == player
p "You can't kill your teammate"
redo
else
p "You killed your opponents #{landing.class}"
board.kill(landing)
p "You've killed #{board.dead_opponents(player)}."
end
end
board.square(before).move!(board, before, after)
board.change_player
p board.layout
end
p "You win!"