Compare commits

...

5 Commits

Author SHA1 Message Date
Dan Ballard d08af5722c better integrate ai and play function. it works for moves. not so much
with jumps
2011-09-13 23:33:51 -07:00
Dan Ballard 96b3abbfa1 Merge branch 'master' into pruning
Conflicts:
	checkers.rb
2011-09-13 21:55:41 -07:00
Dan Ballard da4ca314b8 slightly ok pruning based on max score of other branches of DFS and
modifying depth of branch by pieces taken
2011-09-12 22:51:43 -07:00
Dan Ballard 78afdda3f0 doc changes to search function 2011-09-12 18:08:23 -07:00
Dan Ballard 34bccf463f adding some max pruning... not working past depth 25? 2011-09-12 13:44:19 -07:00
1 changed files with 79 additions and 42 deletions

View File

@ -19,6 +19,7 @@ class Board
end
def setup()
initialize()
0.step(BOARD_SIZE) {|i|
peice = (i < 3 ? TEAM_1_MAN : TEAM_2_MAN)
if [0,2,6].include? i
@ -226,7 +227,8 @@ class Board
moves.each { |move|
board = dup()
board.do_move(x, y, move, team(x, y))
boards.push(board)
boards.push( { "board" => board, "move" =>
{'from' => [x,y], 'to' => move}})
}
end
end
@ -234,66 +236,91 @@ 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
def score()
scores = {}
[TEAM_1, TEAM_2].each {|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
scores[team] = score
}
return scores
end
def search(team)
@top_depth = 10
search_do(team, @top_depth, 100.0, 0.0, "R")
return search_do(team, team, 0, 4, 100.0, 0.0, @stats.dup, [])
end
def search_do(team, depth, percent, last_percent, location)
#puts location
if depth == 0
#puts "DONE"
return score(team)
end
max = -100
# the owner of the search: affects pruning bonuses
# team - current player this turn
# current depth - counting up to max_depth
# max_depth - when we should stop our search
# my_percent - the percent range this work covers
# done_percent - the percent of work already done
# stats - stats from 2 turns ago, ie before most recent opponent turn
# maxs - depth indexed array of max scores
def search_do(owner, team, current_depth, max_depth, my_percent, done_percent, stats, maxs)
#puts current_depth.to_s + '/' + max_depth.to_s + ' ' + done_percent.to_s + "%"
if current_depth < maxs.length and score()[team] < maxs[current_depth][team]
return score()
end
if current_depth >= maxs.length or score()[team] > maxs[current_depth][team]
maxs[current_depth] = score()
end
if current_depth >= max_depth
return score()
end
#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)
return score()
elsif @stats[TEAM_2]['count'] == 0
puts "TEAM_1 WON!"
return score(team)
puts to_s
return score()
else
moves = gen_next_move_boards(team)
if moves == []
puts "NO MOVES AVAILABLE?"
return score(team)
return score()
else
jumps = moves.find_all { |item| item['move'][0].is_a?(Array)}
if jumps.length > 0
moves = jumps
end
done = 0.0
item_percent = percent/moves.length
item_percent = my_percent/moves.length
max = nil
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 )
depth_mod = 0
move = moves[i]['board']
# modify depth on piece loss
if stats[team]['count'] > move.stats[team]['count']
# search less is search owner lost peices
#if owner == team
# depth_mod = -4
#else
# depth_mod = 4
#end
end
sub_score = move.search_do(owner, opposite_team(team), current_depth + 1, max_depth + depth_mod, item_percent, done_percent+done, @stats.dup, maxs)
done += item_percent
if done >= 0.0001
last_percent += done;
puts "%.5f" % last_percent + "% depth: " + depth.to_s + " max: " + max.to_s
if done >= 10.0
done_percent += done;
puts "%.2f" % done_percent + "% depth: " + current_depth.to_s + "/" + max_depth.to_s + " max: " + maxs.to_s
done = 0.0
end
#puts "Score: " + sub_score.to_s
if sub_score > max
if max == nil or sub_score[team] > max[team]
max = sub_score
max['move'] = moves[i]['move']
end
end
return max
@ -325,7 +352,7 @@ class Board
# todo: deal with loss when no move avail
def play()
setup()
#setup()
color = ''
while color != 'w' and color != 'b'
print "Choose color ([W]hite or [B]lack): "
@ -363,8 +390,11 @@ class Board
print "to: "
to = gets().strip!
from = parse_coords(from)
to = parse_coords(to)
puts to.to_s + " or " + from.to_s
to = to.split(",").map {|c| parse_coords(c.strip)}
if to.length == 1
to = to[0]
end
puts "from "+ from.to_s + " to " + to.to_s
if !to or !from
next
end
@ -379,6 +409,13 @@ class Board
do_move(from[0], from[1], to, team)
else
puts "AI MOVE"
info = search(team)
move = info['move']
if move == nil
puts "NO MOVE!!!"
else
do_move(move['from'][0], move['from'][1], move['to'], team(move['from'][0], move['from'][1]))
end
end
if @stats[TEAM_1]['count'] == 0