Skip to content

Commit

Permalink
add scoring + choice when 1 move left to minimmax
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDCarroll committed Jul 5, 2017
1 parent 65fd0cd commit 4a97691
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
10 changes: 8 additions & 2 deletions lib/ai/minimax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ def choose #first available to validate integration
end

def optimum_choice(spaces = @spaces, depth = @depth, choices = {})
p @judge.report({board: spaces})
if @judge.report({board: spaces}) == @const.draw then return 0 end
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

@board.available_spaces.each { |space| choices = space }
choices

end

def available_spaces
Expand Down
68 changes: 63 additions & 5 deletions spec/ai/minimax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,71 @@
end

describe "Minimax optimum_choice" do
context 'when the method can be called' do
context 'when the board is drawn' do
Given(:board) { Board.new }
Given(:minimax) { Minimax.new(board) }
When {
board.mark(0, "X")
}
Then { nil == minimax.optimum_choice }
When { board.mark(8, "X")
board.mark(0, "O")
board.mark(1, "X")
board.mark(2, "O")
board.mark(4, "X")
board.mark(3, "O")
board.mark(5, "X")
board.mark(7, "O")
board.mark(6, "X") }
Then { 0 == minimax.optimum_choice }
end

context 'when the board is lost at the last move' do
Given(:board) { Board.new }
Given(:minimax) { Minimax.new(board) }
When { board.mark(1, "X")
board.mark(4, "O")
board.mark(2, "X")
board.mark(5, "O")
board.mark(3, "X")
board.mark(7, "O")
board.mark(6, "X")
board.mark(8, "O")
board.mark(0, "X") }
Then { -1 == minimax.optimum_choice }
end

context 'when the board is lost early' do
Given(:board) { Board.new }
Given(:minimax) { Minimax.new(board) }
When { board.mark(0, "X")
board.mark(8, "O")
board.mark(1, "X")
board.mark(7, "O")
board.mark(2, "X") }
Then { -1 == minimax.optimum_choice }
end

context 'when the board is won early' do
Given(:board) { Board.new }
Given(:minimax) { Minimax.new(board) }
When { board.mark(0, "X")
board.mark(8, "O")
board.mark(1, "X")
board.mark(7, "O")
board.mark(4, "X")
board.mark(6, "O") }
Then { 1 == minimax.optimum_choice }
end

context 'when there is only one move left' do
Given(:board) { Board.new }
Given(:minimax) { Minimax.new(board) }
When { board.mark(8, "X")
board.mark(0, "O")
board.mark(1, "X")
board.mark(2, "O")
board.mark(4, "X")
board.mark(3, "O")
board.mark(5, "X")
board.mark(7, "O") }
Then { 6 == minimax.optimum_choice }
end
end

Expand Down

0 comments on commit 4a97691

Please sign in to comment.