slightly ok pruning based on max score of other branches of DFS and
modifying depth of branch by pieces taken
This commit is contained in:
parent
78afdda3f0
commit
da4ca314b8
66
checkers.rb
66
checkers.rb
|
@ -234,7 +234,9 @@ class Board
|
|||
return boards
|
||||
end
|
||||
|
||||
def score(team)
|
||||
def score()
|
||||
scores = {}
|
||||
[TEAM_1, TEAM_2].each {|team|
|
||||
score = 0
|
||||
score += @stats[team]['count']
|
||||
score += 12 - @stats[opposite_team(team)]['count']
|
||||
|
@ -243,60 +245,82 @@ class Board
|
|||
elsif @stats[team]['count'] == 0
|
||||
score -= 12
|
||||
end
|
||||
return score
|
||||
scores[team] = score
|
||||
}
|
||||
return scores
|
||||
end
|
||||
|
||||
def search(team)
|
||||
search_do(team, 0, 10, 100.0, 0.0, "R", {TEAM_1 => -100, TEAM_2 => -100})
|
||||
search_do(team, team, 0, 6, 100.0, 0.0, @stats.dup, [])
|
||||
end
|
||||
|
||||
# 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
|
||||
# maxs -
|
||||
def search_do(team, current_depth, max_depth, my_percent, done_percent, maxs)
|
||||
#puts location
|
||||
if depth == 0
|
||||
return score(team)
|
||||
# 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 @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|
|
||||
depth_mod = 0
|
||||
move = moves[i]['board']
|
||||
#puts "SEARCH"
|
||||
sub_score = move.search_do(opposite_team(team), depth -1, item_percent, last_percent+done, location + "." +i.to_s, maxs.dup )
|
||||
# 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.01
|
||||
last_percent += done;
|
||||
puts "%.5f" % last_percent + "% depth: " + depth.to_s + " max: " + maxs.to_s
|
||||
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 > maxs[team]
|
||||
maxs[team] = sub_score
|
||||
if max == nil or sub_score[team] > max[team]
|
||||
max = sub_score
|
||||
end
|
||||
end
|
||||
return maxs[team]
|
||||
return max
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue