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,51 +234,71 @@ class Board
return boards return boards
end end
def search(team) def score(team)
@top_depth = 100 score = 0
search_do(team, @top_depth) 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 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 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|
end move = moves[i]
if results[opposite_team(team)] == 0 #puts "SEARCH"
return {team => max, opposite_team(team) => 0} sub_score = move.search_do(opposite_team(team), depth -1, item_percent, last_percent+done, location + "." +i.to_s )
elsif results[team] == max done += item_percent
return {team => max, opposite_team(team) => results[opposite_team(team)]} if done >= 0.0001
else last_percent += done;
if results[team] > min puts "%.5f" % last_percent + "% depth: " + depth.to_s + " max: " + max.to_s
min = results[team] done = 0.0
end
#puts "Score: " + sub_score.to_s
if sub_score > max
max = sub_score
end end
end end
return max
end end
end end
end end
end end