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 # base case: no jumps avail, return path
# jumps avail: do all and merge and return # 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 = [] jumps = []
for xm in x_mod for xm in x_mod
new_x = x + xm new_x = x + xm
for ym in y_mod for ym in y_mod
new_y = y + ym 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_x = new_x + xm
final_y = new_y + ym final_y = new_y + ym
if (valid_coords(final_x, final_y) and empty?(final_x, final_y)) 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 end
end end
@ -233,49 +234,69 @@ class Board
return boards return boards
end 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) def search(team)
@top_depth = 100 @top_depth = 10
search_do(team, @top_depth) search_do(team, @top_depth, 100.0, 0.0, "R")
end end
def search_do(team, depth, percent) def search_do(team, depth, percent, last_percent, location)
#puts location
if depth == 0 if depth == 0
puts "DONE" #puts "DONE"
return {TEAM_1 => @stats[TEAM_1]['count'], TEAM_2 => @stats[TEAM_2]['count']} return score(team)
end end
max = -100
max = @stats[team]['count'] #if team == TEAM_1
min = 0 # puts depth.to_s + ": TEAM_1's turn:"
#else
if team == TEAM_1 # puts depth.to_s + ": TEAM_2's turn:"
puts depth.to_s + ": TEAM_1's turn:" #end
else #puts to_s #moves[0].to_s
puts depth.to_s + ": TEAM_2's turn:"
end
puts to_s #moves[0].to_s
if @stats[TEAM_1]['count'] == 0 if @stats[TEAM_1]['count'] == 0
puts "TEAM_2 WON!" puts "TEAM_2 WON!"
return score(team)
elsif @stats[TEAM_2]['count'] == 0 elsif @stats[TEAM_2]['count'] == 0
puts "TEAM_1 WON!" puts "TEAM_1 WON!"
return score(team)
else else
moves = gen_next_move_boards(team) moves = gen_next_move_boards(team)
if moves == [] if moves == []
puts "NO MOVES AVAILABLE?" puts "NO MOVES AVAILABLE?"
return score(team)
else else
done = 0.0
item_percent = percent/moves.length item_percent = percent/moves.length
results = moves[0].search_do(opposite_team(team), depth -1, item_percent)
if item_percent >= 0.1 for i in 0..moves.length-1 do
# UM WE NEED PERCENT SO FAR #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 end
if results[opposite_team(team)] == 0 #puts "Score: " + sub_score.to_s
return {team => max, opposite_team(team) => 0} if sub_score > max
elsif results[team] == max max = sub_score
return {team => max, opposite_team(team) => results[opposite_team(team)]}
else
if results[team] > min
min = results[team]
end end
end end
return max
end end
end end
end end