Skip to content

Commit

Permalink
change optimal_score to be theoretically correct
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDCarroll committed Jul 10, 2017
1 parent 3dadcb8 commit 4896584
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 102 deletions.
37 changes: 19 additions & 18 deletions lib/ai/minimax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,43 @@ def choose #first available to validate integration
def optimum_choice(spaces = @spaces, depth = 0, choices = {})
report = @judge.report({board: spaces})
if report == @const.draw then return 0
elsif report == @const.winner(@const.players[0]) then return -1
elsif report == @const.winner(@const.players[1]) then return 1 end
elsif report then return -1 end

p "enter " + depth.to_s
p @board.available_spaces
@board.available_spaces.each do |space|
p spaces
p "evaluate space " + space.to_s
@board.mark(space, appropriate_player(depth))
choices[space] = optimum_choice(@spaces, depth + 1, {})
p spaces
p choices
@board.mark(space, appropriate_player)
choices[space] = -1 * optimum_choice(@spaces, depth + 1, {})
@board.mark(space, nil)
end
p "exit " + depth.to_s

@choices = choices
p @choices
optimal_space = choices.max_by { |space, score| score }[0]
depth == 0 ? optimal_space : optimal_score(choices, depth)
end

attr_reader :choices

def readout(depth, choices, min_or_max)
p appropriate_player + " : " + choices.to_s + " : " + min_or_max
end

def optimal_score(choices, depth)
min = choices.min_by { |space, score| score }[1]
max = choices.max_by { |space, score| score }[1]
only = choices.first[1]
if depth == 1 then return min
elsif depth == 2 then return min
elsif depth == 3 then return max
elsif depth == 4 then return only
elsif depth == 5 then return only
if depth == 1 then p readout(depth, choices, 'min1') ; return min
elsif depth == 2 then p readout(depth, choices, 'max2'); return max
elsif depth == 3 then p readout(depth, choices, 'only3'); return only
#elsif depth == 4 then p readout(depth, 'max4'); return max
#elsif depth == 5 then p readout(depth, 'only5'); return only
else raise StandardError, "exceeded recursion depth " + depth.to_s end
end

def appropriate_player(depth)
depth.odd? ? @const.players[0] : @const.players[1]
def appropriate_player # this needs to go in the board class
# or maybe in the rules class
# if rules, then rules needs to inherit board
# and rules needs to be passed into AI & Minimax
spaces_taken = @board.spaces.length - @board.available_spaces.length
spaces_taken.odd? ? @const.players[1] : @const.players[0]
end
end
Loading

0 comments on commit 4896584

Please sign in to comment.