-
Notifications
You must be signed in to change notification settings - Fork 2
/
chess.rb
268 lines (208 loc) · 5.62 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
=begin
ruby_chess
The Chess Board game implemented in the metalanguage ruby
Using the mighty GTK2 library
Copyright (C) 2006 Sébastien Boisvert
http://sebhtml.blogspot.com/
Sebastien.Boisvert<AT>USherbrooke<DOT>CA
This software is released under the GPL version 2 (1991)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
An online plain text version is available here:
http://www.gnu.org/licenses/gpl.txt
=end
require_relative 'help_window'
require_relative 'man_selector'
require_relative 'board_selector'
require_relative 'player'
require_relative 'game_options'
require_relative 'graphical_board'
require_relative 'color'
require_relative 'artificial_intelligence'
require_relative 'human'
require_relative 'board'
require_relative 'chess_window'
class Chess
SELECT_FROM = 2
SELECT_TO = 3
def update_board move
target = board.content_at move.to
if target.class == King
@winner = @current_turn
end
piece = board.content_at move.from
new_board = @boards_list.last.do_move move
piece = board.content_at move.from
piece.add_move move
board.move_for_next_board = move
board.next_board = new_board
@boards_list << new_board
end
def white_intelligence= intelligence
@white_intelligence = intelligence
end
def black_intelligence= intelligence
@black_intelligence = intelligence
end
def show_move
@label.label += @current_turn.color+" "+
piece.letter+" "+
move.from.location + " " +
move.to.location+ "\n"
end
def initialize
@boards_list = Array.new
@current_from_button = nil
@current_to_button = nil
window = GameOptions.new self
window.show
Gtk.main
end
def run
@white_player = Player.new Color::WHITE, @white_intelligence
@black_player = Player.new Color::BLACK, @black_intelligence
@white_player.opponent = @black_player
@black_player.opponent = @white_player
help_button = Gtk::Button.new
label = Gtk::Label.new
label.label = "About"
help_button.add label
help_button.signal_connect 'clicked' do
help_window = HelpWindow.new
help_window.run
end
@graphical_board = GraphicalBoard.new self
board = Board.new @black_player, @white_player, @graphical_board
@boards_list << board
@run = true
chess_window = ChessWindow.new self
board_selector = BoardSelector.new
label = Gtk::Label.new
@label = label
hbox = Gtk::HBox.new
vbox = Gtk::VBox.new
hbox.add @graphical_board
vbox2 = Gtk::VBox.new
#vbox2.add board_selector
hbox.add vbox2
vbox2.show_all
vbox.add hbox
vbox.add help_button
chess_window.add vbox
hbox.show_all
vbox.show_all
chess_window.show_all
@current_turn = @white_player
@current_state = SELECT_FROM
@graphical_board.show @boards_list.last
if @current_turn.ai?
play_ai
end
end
def play_ai
Thread.new do
play_ai_in_thread
end
end
def do_move move
update_board move
@graphical_board.update @boards_list.last, move.from
@graphical_board.update @boards_list.last, move.to
switch_player
end
def play_ai_in_thread
if @winner == nil
moves_list = board.moves_for_player @current_turn
move = @current_turn.play board, moves_list
if move == nil
@winner = @current_turn.opponent
return
end
do_move move
if @current_turn.ai?
play_ai
end
else
@label.label += "
#{@winner} has won the game.
"
end
end
def declare_winner player
@winner = player
end
# interface for human player
def catch_click row, col
if @current_turn.ai?
return
end
moves_list=board.moves_for_player @current_turn
@current_turn.play board, moves_list
piece = board.board[row][col]
tile = Tile.new row, col
if @current_state == SELECT_FROM
if piece.nil?
return
end
if piece.player != @current_turn
return
end
all_moves = moves_list
correct = false
all_moves.each do |move|
if move.from == tile
correct = true
break
end
end
unless correct
return
end
@current_piece = piece
@from = tile
@current_state = SELECT_TO
elsif @current_state == SELECT_TO
@to = tile
move = Move.new @from, @to
possible_moves = moves_list
state = false
possible_moves.each do |move_p|
if move_p == move
state = true
break
end
end
unless state
@current_state = SELECT_FROM
return
end
do_move move
@current_state = SELECT_FROM
if @current_turn.ai?
play_ai
end
end
end
def switch_player
if @current_turn== @white_player
@current_turn = @black_player
else
@current_turn = @white_player
end
end
def board
@boards_list.last
end
def terminate
@run = false
Gtk.main_quit
end
end