Class: TakaTicTacToe::ImpossibleAi
- Inherits:
-
Computer
- Object
- Computer
- TakaTicTacToe::ImpossibleAi
show all
- Defined in:
- lib/taka_tic_tac_toe/ai_difficulty/impossible_ai.rb
Constant Summary
collapse
- Negative_Infinity =
-1.0/0
Instance Attribute Summary
Attributes inherited from Computer
#difficulty, #mark, #opponent_mark
Instance Method Summary
collapse
Methods inherited from Computer
#initialize, #set_difficulty
Instance Method Details
#check_game_state(board, mark) ⇒ Object
36
37
38
39
40
|
# File 'lib/taka_tic_tac_toe/ai_difficulty/impossible_ai.rb', line 36
def check_game_state(board, mark)
return 0 if board.is_tie
return 1 if board.winner == mark
-1
end
|
#make_move(board) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/taka_tic_tac_toe/ai_difficulty/impossible_ai.rb', line 6
def make_move(board)
best_score = Negative_Infinity
best_index = 0
board.empty_slots.each do |slots|
board.set_move(@mark, slots)
score = -negamax_score(board, -1)
board.undo_move(slots)
if score > best_score
best_score = score
best_index = slots
end
end
return best_index
end
|
#negamax_score(board, color) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/taka_tic_tac_toe/ai_difficulty/impossible_ai.rb', line 21
def negamax_score(board, color)
score = Negative_Infinity
mark = color == 1 ? @mark : @opponent_mark
return check_game_state(board, (mark)) if board.has_winner || board.is_tie
board.empty_slots.each do |slots|
board.set_move(mark, slots)
score = [score, -negamax_score(board, -color)].max
board.undo_move(slots)
end
return score
end
|