Compare commits

..

1 Commits

Author SHA1 Message Date
Dan Ballard 280a38eb54 updated notes 2011-09-20 21:00:54 -07:00
2 changed files with 48 additions and 81 deletions

8
README
View File

@ -1,13 +1,17 @@
Checkers AI engine
For Ruby 1.9.1
Right now I'm working on the search.
To test
% load "checkers.rb"
% b = Board.new
% b.setup
% b.play
and then to look
also look at
% b.search(TEAM_1)
Right now entering jump moves or the AI playing jumps in the .play() interface may not work

View File

@ -19,7 +19,6 @@ 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
@ -227,8 +226,7 @@ class Board
moves.each { |move|
board = dup()
board.do_move(x, y, move, team(x, y))
boards.push( { "board" => board, "move" =>
{'from' => [x,y], 'to' => move}})
boards.push(board)
}
end
end
@ -236,9 +234,7 @@ class Board
return boards
end
def score()
scores = {}
[TEAM_1, TEAM_2].each {|team|
def score(team)
score = 0
score += @stats[team]['count']
score += 12 - @stats[opposite_team(team)]['count']
@ -247,80 +243,57 @@ class Board
elsif @stats[team]['count'] == 0
score -= 12
end
scores[team] = score
}
return scores
return score
end
def search(team)
return search_do(team, team, 0, 4, 100.0, 0.0, @stats.dup, [])
@top_depth = 10
search_do(team, @top_depth, 100.0, 0.0, "R")
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
# 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()
def search_do(team, depth, percent, last_percent, location)
#puts location
if depth == 0
#puts "DONE"
return score(team)
end
max = -100
#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()
return score(team)
elsif @stats[TEAM_2]['count'] == 0
puts "TEAM_1 WON!"
puts to_s
return score()
return score(team)
else
moves = gen_next_move_boards(team)
if moves == []
puts "NO MOVES AVAILABLE?"
return score()
return score(team)
else
jumps = moves.find_all { |item| item['move'][0].is_a?(Array)}
if jumps.length > 0
moves = jumps
end
done = 0.0
item_percent = my_percent/moves.length
max = nil
item_percent = percent/moves.length
for i in 0..moves.length-1 do
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)
#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 >= 10.0
done_percent += done;
puts "%.2f" % done_percent + "% depth: " + current_depth.to_s + "/" + max_depth.to_s + " max: " + maxs.to_s
if done >= 0.0001
last_percent += done;
puts "%.5f" % last_percent + "% depth: " + depth.to_s + " max: " + max.to_s
done = 0.0
end
#puts "Score: " + sub_score.to_s
if max == nil or sub_score[team] > max[team]
if sub_score > max
max = sub_score
max['move'] = moves[i]['move']
end
end
return max
@ -352,7 +325,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): "
@ -390,11 +363,8 @@ class Board
print "to: "
to = gets().strip!
from = parse_coords(from)
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
to = parse_coords(to)
puts to.to_s + " or " + from.to_s
if !to or !from
next
end
@ -409,13 +379,6 @@ 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