From 29b6987759f104df9620d1770458875a72f359e2 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 8 Sep 2011 23:11:19 -0700 Subject: [PATCH] start real DFS --- checkers.rb | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/checkers.rb b/checkers.rb index 50bc3f2..41c898d 100644 --- a/checkers.rb +++ b/checkers.rb @@ -233,11 +233,24 @@ class Board return boards end - def run_1st_path(team) + def search(team) + @top_depth = 100 + search_do(team, @top_depth) + end + + def search_do(team, depth) + if depth == 0 + puts "DONE" + return {TEAM_1 => @stats[TEAM_1]['count'], TEAM_2 => @stats[TEAM_2]['count']} + end + + max = @stats[team]['count'] + min = 0 + if team == TEAM_1 - puts "TEAM_1's turn:" + puts depth.to_s + ": TEAM_1's turn:" else - puts "TEAM_2'2 turn:" + puts depth.to_s + ": TEAM_2's turn:" end puts to_s #moves[0].to_s if @stats[TEAM_1]['count'] == 0 @@ -249,7 +262,16 @@ class Board if moves == [] puts "NO MOVES AVAILABLE?" else - moves[0].run_1st_path(opposite_team(team)) + results = moves[0].search_do(opposite_team(team), depth -1) + if results[opposite_team(team)] == 0 + return {team => max, opposite_team(team) => 0} + elsif results[team] == max + return {team => max, opposite_team(team) => results[opposite_team(team)]} + else + if results[team] > min + min = results[team] + end + end end end end