better integrate ai and play function. it works for moves. not so much

with jumps
This commit is contained in:
Dan Ballard 2011-09-13 23:33:51 -07:00
parent 96b3abbfa1
commit d08af5722c
1 changed files with 24 additions and 11 deletions

View File

@ -19,6 +19,7 @@ class Board
end end
def setup() def setup()
initialize()
0.step(BOARD_SIZE) {|i| 0.step(BOARD_SIZE) {|i|
peice = (i < 3 ? TEAM_1_MAN : TEAM_2_MAN) peice = (i < 3 ? TEAM_1_MAN : TEAM_2_MAN)
if [0,2,6].include? i if [0,2,6].include? i
@ -226,7 +227,8 @@ class Board
moves.each { |move| moves.each { |move|
board = dup() board = dup()
board.do_move(x, y, move, team(x, y)) board.do_move(x, y, move, team(x, y))
boards.push( { "board" => board, "move" => move}) boards.push( { "board" => board, "move" =>
{'from' => [x,y], 'to' => move}})
} }
end end
end end
@ -251,7 +253,7 @@ class Board
end end
def search(team) def search(team)
search_do(team, team, 0, 6, 100.0, 0.0, @stats.dup, []) return search_do(team, team, 0, 4, 100.0, 0.0, @stats.dup, [])
end end
# the owner of the search: affects pruning bonuses # the owner of the search: affects pruning bonuses
@ -302,15 +304,15 @@ class Board
# modify depth on piece loss # modify depth on piece loss
if stats[team]['count'] > move.stats[team]['count'] if stats[team]['count'] > move.stats[team]['count']
# search less is search owner lost peices # search less is search owner lost peices
if owner == team #if owner == team
depth_mod = -4 # depth_mod = -4
else #else
depth_mod = 4 # depth_mod = 4
end #end
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) 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 done += item_percent
if done >= 0.01 if done >= 10.0
done_percent += done; done_percent += done;
puts "%.2f" % done_percent + "% depth: " + current_depth.to_s + "/" + max_depth.to_s + " max: " + maxs.to_s puts "%.2f" % done_percent + "% depth: " + current_depth.to_s + "/" + max_depth.to_s + " max: " + maxs.to_s
done = 0.0 done = 0.0
@ -318,6 +320,7 @@ class Board
#puts "Score: " + sub_score.to_s #puts "Score: " + sub_score.to_s
if max == nil or sub_score[team] > max[team] if max == nil or sub_score[team] > max[team]
max = sub_score max = sub_score
max['move'] = moves[i]['move']
end end
end end
return max return max
@ -349,7 +352,7 @@ class Board
# todo: deal with loss when no move avail # todo: deal with loss when no move avail
def play() def play()
setup() #setup()
color = '' color = ''
while color != 'w' and color != 'b' while color != 'w' and color != 'b'
print "Choose color ([W]hite or [B]lack): " print "Choose color ([W]hite or [B]lack): "
@ -387,8 +390,11 @@ class Board
print "to: " print "to: "
to = gets().strip! to = gets().strip!
from = parse_coords(from) from = parse_coords(from)
to = parse_coords(to) to = to.split(",").map {|c| parse_coords(c.strip)}
puts to.to_s + " or " + from.to_s if to.length == 1
to = to[0]
end
puts "from "+ from.to_s + " to " + to.to_s
if !to or !from if !to or !from
next next
end end
@ -403,6 +409,13 @@ class Board
do_move(from[0], from[1], to, team) do_move(from[0], from[1], to, team)
else else
puts "AI MOVE" 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 end
if @stats[TEAM_1]['count'] == 0 if @stats[TEAM_1]['count'] == 0