From d08af5722c6221076e03a51fab966781dfaebd04 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 13 Sep 2011 23:33:51 -0700 Subject: [PATCH] better integrate ai and play function. it works for moves. not so much with jumps --- checkers.rb | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/checkers.rb b/checkers.rb index 4530ffe..2851368 100644 --- a/checkers.rb +++ b/checkers.rb @@ -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" => board, "move" => move}) + boards.push( { "board" => board, "move" => + {'from' => [x,y], 'to' => move}}) } end end @@ -251,7 +253,7 @@ class Board end 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 # the owner of the search: affects pruning bonuses @@ -302,15 +304,15 @@ class 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 + #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 + 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 @@ -318,6 +320,7 @@ class Board #puts "Score: " + sub_score.to_s if max == nil or sub_score[team] > max[team] max = sub_score + max['move'] = moves[i]['move'] end end return max @@ -349,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): " @@ -387,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 @@ -403,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