Full search working, and horrifically slow even at depth 10
This commit is contained in:
parent
a1f48682c0
commit
5617e7f82b
|
@ -0,0 +1,13 @@
|
|||
Checkers AI engine
|
||||
|
||||
Right now I'm working on the search.
|
||||
|
||||
To test
|
||||
|
||||
% load "checkers.rb"
|
||||
% b = Board.new
|
||||
% b.setup
|
||||
|
||||
and then to look
|
||||
|
||||
% b.search(TEAM_1)
|
75
checkers.rb
75
checkers.rb
|
@ -101,17 +101,18 @@ class Board
|
|||
#
|
||||
# base case: no jumps avail, return path
|
||||
# jumps avail: do all and merge and return
|
||||
def gen_jumps(path, x, y, team, x_mod, y_mod)
|
||||
def gen_jumps(path, x, y, team, x_mod, y_mod, taken=[])
|
||||
jumps = []
|
||||
for xm in x_mod
|
||||
new_x = x + xm
|
||||
for ym in y_mod
|
||||
new_y = y + ym
|
||||
if valid_coords(new_x, new_y) and piece?(new_x, new_y) and team != team(new_x, new_y)
|
||||
if valid_coords(new_x, new_y) and piece?(new_x, new_y) and !taken.include?([new_x, new_y]) and team != team(new_x, new_y)
|
||||
taken.push([new_x, new_y])
|
||||
final_x = new_x + xm
|
||||
final_y = new_y + ym
|
||||
if (valid_coords(final_x, final_y) and empty?(final_x, final_y))
|
||||
jumps += gen_jumps(path + [[final_x, final_y]], final_x, final_y, team, x_mod, y_mod)
|
||||
jumps += gen_jumps(path + [[final_x, final_y]], final_x, final_y, team, x_mod, y_mod, taken)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -233,49 +234,69 @@ class Board
|
|||
return boards
|
||||
end
|
||||
|
||||
def score(team)
|
||||
score = 0
|
||||
score += @stats[team]['count']
|
||||
score += 12 - @stats[opposite_team(team)]['count']
|
||||
if @stats[opposite_team(team)]['count'] == 0
|
||||
score += 12
|
||||
elsif @stats[team]['count'] == 0
|
||||
score -= 12
|
||||
end
|
||||
return score
|
||||
end
|
||||
|
||||
def search(team)
|
||||
@top_depth = 100
|
||||
search_do(team, @top_depth)
|
||||
@top_depth = 10
|
||||
search_do(team, @top_depth, 100.0, 0.0, "R")
|
||||
end
|
||||
|
||||
def search_do(team, depth, percent)
|
||||
def search_do(team, depth, percent, last_percent, location)
|
||||
#puts location
|
||||
if depth == 0
|
||||
puts "DONE"
|
||||
return {TEAM_1 => @stats[TEAM_1]['count'], TEAM_2 => @stats[TEAM_2]['count']}
|
||||
#puts "DONE"
|
||||
return score(team)
|
||||
end
|
||||
max = -100
|
||||
|
||||
max = @stats[team]['count']
|
||||
min = 0
|
||||
|
||||
if team == TEAM_1
|
||||
puts depth.to_s + ": TEAM_1's turn:"
|
||||
else
|
||||
puts depth.to_s + ": TEAM_2's turn:"
|
||||
end
|
||||
puts to_s #moves[0].to_s
|
||||
#if team == TEAM_1
|
||||
# puts depth.to_s + ": TEAM_1's turn:"
|
||||
#else
|
||||
# puts depth.to_s + ": TEAM_2's turn:"
|
||||
#end
|
||||
#puts to_s #moves[0].to_s
|
||||
if @stats[TEAM_1]['count'] == 0
|
||||
puts "TEAM_2 WON!"
|
||||
return score(team)
|
||||
elsif @stats[TEAM_2]['count'] == 0
|
||||
puts "TEAM_1 WON!"
|
||||
return score(team)
|
||||
else
|
||||
moves = gen_next_move_boards(team)
|
||||
if moves == []
|
||||
puts "NO MOVES AVAILABLE?"
|
||||
return score(team)
|
||||
else
|
||||
done = 0.0
|
||||
item_percent = percent/moves.length
|
||||
results = moves[0].search_do(opposite_team(team), depth -1, item_percent)
|
||||
if item_percent >= 0.1
|
||||
# UM WE NEED PERCENT SO FAR
|
||||
|
||||
for i in 0..moves.length-1 do
|
||||
#moves.each { |move|
|
||||
move = moves[i]
|
||||
#puts "SEARCH"
|
||||
sub_score = move.search_do(opposite_team(team), depth -1, item_percent, last_percent+done, location + "." +i.to_s )
|
||||
done += item_percent
|
||||
if done >= 0.0001
|
||||
last_percent += done;
|
||||
puts "%.5f" % last_percent + "% depth: " + depth.to_s + " max: " + max.to_s
|
||||
done = 0.0
|
||||
end
|
||||
if results[opposite_team(team)] == 0
|
||||
return {team => max, opposite_team(team) => 0}
|
||||
elsif results[team] == max
|
||||
return {team => max, opposite_team(team) => results[opposite_team(team)]}
|
||||
else
|
||||
if results[team] > min
|
||||
min = results[team]
|
||||
#puts "Score: " + sub_score.to_s
|
||||
if sub_score > max
|
||||
max = sub_score
|
||||
end
|
||||
end
|
||||
return max
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue