Full search working, and horrifically slow even at depth 10

This commit is contained in:
Dan Ballard 2011-09-11 12:39:14 -07:00
parent a1f48682c0
commit 5617e7f82b
2 changed files with 64 additions and 30 deletions

13
README Normal file
View File

@ -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)

View File

@ -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,51 +234,71 @@ class Board
return boards
end
def search(team)
@top_depth = 100
search_do(team, @top_depth)
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_do(team, depth, percent)
def search(team)
@top_depth = 10
search_do(team, @top_depth, 100.0, 0.0, "R")
end
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
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]
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
#puts "Score: " + sub_score.to_s
if sub_score > max
max = sub_score
end
end
return max
end
end
end
end
end