-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.rb
195 lines (175 loc) · 4.33 KB
/
Player.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
class Player
def initialize color, board, pieces, king, gui = nil, old_boards = []
@color = color
@board = board
@pieces = pieces
@gui = gui
@old_boards = old_boards
@king = king
@boards = []
# @cmove = 0
end
def make_move piece, move
old_pos = piece.position?
change = piece.move move
pos = piece.position?
piece_type = piece.class.to_s
piece_color = piece.color.to_s
puts piece_color+ ' ' + piece_type+' moved from '+old_pos.to_s+' to '+ pos.to_s
move = old_pos.to_s+':'+pos.to_s
return move,change
end
def play
pieces = @pieces
board = @board
gui = @gui
old_boards = @old_boards
boards = @boards
# cmove = @cmove
moves = []
piece_type_prob = []
pieces.each_with_index do |piece,i|
moves[i] = piece.check_possible_moves
# puts i.to_s + ' ' + moves[i].to_s
if moves[i][0] != nil
p = picker moves[i]
piece_type_prob[i] = [i,p[0],p[1]]
end
# puts 'prob '+piece_type_prob[i].to_s
# puts moves[i].to_s
end
# puts pieces.map{|p| p.class if p.alive}.join(', ')
piece_type_prob = piece_type_prob.uniq
piece_type_prob.delete(nil)
# puts piece_type_prob.to_s
if piece_type_prob[0] == nil
puts 'Cannot move'
sleep(6)
cmove = cmove + 1
if cmove == 2
return
end
return
else
cmove = 0
end
# puts p.to_s
current_board = {board: Marshal.load(Marshal.dump(board)).dup, move: nil}
# sleep 2
# current_board = boards[-1]
cboard = compress_board current_board[:board]
gui.draw cboard if gui
# puts 'c '+cboard.join.to_s
# puts 'o '+old_boards.first.to_s
# cboard = old_boards.first.dup
# index = old_boards.index(cboard.join)
indeces = old_boards.each_index.select{|i| old_boards[i] == cboard.join}
# puts indeces.to_s
# TODO
checked = false
puts @color
checked = @king.under_check? @pieces.select{|b| b.alive}
king = @king
if checked
make_move king, checked
return
end
max = 0
best_index = nil
indeces.each do |index|
if @old_boards[index][@color] > max
max = @old_boards[index][@color]
best_index = index
end
end
if best_index
# puts 'f '+old_boards[index].to_s
# puts 'f '+@old_boards[index].to_s
best_board = @old_boards[best_index]
suggested_move = best_board[:move]
from = eval suggested_move.split(':')[0]
to = eval suggested_move.split(':')[1]
# puts from.to_s
# puts to.to_s
piece = board[from[0]][from[1]].piece
piece_index = pieces.index(piece)
if piece_index
# puts piece_index
to[2] = best_board[current_turn_color]
moves[piece_index] << to
p = picker moves[piece_index]
piece_type_prob[piece_index] = [piece_index,p[0],p[1]]
# puts current_turn_color + ' ' + to[2].to_s
end
# puts 'FOUND'
# puts index
# sleep 1
# exit
end
p = picker piece_type_prob
move = moves[p[2][0]][p[2][1]]
# puts move.to_s
# puts p[2][0].to_s
# puts pieces[p[2][0]].to_s
move, change = make_move pieces[p[2][0]], move
current_board[:move] = move
boards << current_board
# print_board board
# if turn == 0
# exit
# end
if change == true
statistics = []
if @color=='white'
winner = 'white'
loser = 'black'
else
winner = 'black'
loser = 'white'
end
# @mutex.synchronize do
# @old_boards = read_file @file
# puts @old_boards.to_s
boards.each do |board|
cboard = compress_board board[:board]
index = @old_boards.index(cboard.join)
indeces = []
@old_boards.each_index do |i|
if @old_boards[i][:board] == cboard and @old_boards[i][:move] == board[:move]
indeces << i
break
end
end
if indeces[0]
index = indeces[0]
@old_boards[index][winner] = @old_boards[index][winner] + 10
@old_boards[index][loser] = @old_boards[index][loser] - 5
# puts ({board: cboard, winner => 10, loser => 5,move: board[:move]}).to_s
# puts @old_boards[index]
# exit
else
statistics << {board: cboard, winner => 10, loser => 5,move: board[:move]}
end
end
statistics = @old_boards + statistics
@old_boards = statistics
# end
# puts 'Win'
puts @count
# puts king1.position?.to_s
gui.used = false if gui
return change
# exit
elsif change
pieces.delete_at p[2][0]
pieces << change
end
if change
puts 'Changed to '+change.class.to_s
end
# @cmove = cmove
end
def get_statistics
return @old_boards
end
end