Class: TTT::AIMedium
- Defined in:
- lib/ttt/ai_medium.rb,
lib/ttt/ai_medium_back.rb
Instance Attribute Summary collapse
-
#best ⇒ Object
Returns the value of attribute best.
-
#max_ply ⇒ Object
Returns the value of attribute max_ply.
Attributes inherited from AI
Attributes inherited from Player
Instance Method Summary collapse
- #max_min_swapped?(max, min) ⇒ Boolean
- #minimax(max_player = true, ply = 0, min_score = 1000, max_score = -1000)) ⇒ Object
- #move(options) ⇒ Object
- #set_max_ply(moves) ⇒ Object
Methods inherited from AI
#available_moves, #no_gui?, #opposite_side, #prompt, #undo_move
Methods included from Minimax
#alpha_beta_swapped?, #base_case_satisfied?, #eval_score, #gen_score_game_tree, #mark_curr_player_side, #winning_score
Methods inherited from Player
Constructor Details
This class inherits a constructor from TTT::Player
Instance Attribute Details
#best ⇒ Object
Returns the value of attribute best.
4 5 6 |
# File 'lib/ttt/ai_medium_back.rb', line 4 def best @best end |
#max_ply ⇒ Object
Returns the value of attribute max_ply.
4 5 6 |
# File 'lib/ttt/ai_medium_back.rb', line 4 def max_ply @max_ply end |
Instance Method Details
#max_min_swapped?(max, min) ⇒ Boolean
54 55 56 |
# File 'lib/ttt/ai_medium_back.rb', line 54 def max_min_swapped?(max, min) max >= min end |
#minimax(max_player = true, ply = 0, min_score = 1000, max_score = -1000)) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ttt/ai_medium_back.rb', line 15 def minimax(max_player = true, ply = 0, min_score = 1000, max_score = -1000) if board.winner? return(max_player ? (-1000 + ply) : (1000 - ply)) elsif board.draw_game? return 0 end if ply >= max_ply return(max_player ? (max_score) : (min_score)) end best_move = 0 score = (max_player ? max_score : min_score ) available_moves.each do |index| board[][index] = ( max_player ? side : opposite_side(side) ) score = minimax(!max_player, ply + 1, min_score, max_score) undo_move(index) if max_player && score > max_score max_score = score best_move = index elsif !max_player && score < min_score min_score = score end break if max_min_swapped?(max_score, min_score) end return( ply == 0 ? best_move : ( max_player ? max_score : min_score ) ) end |
#move(options) ⇒ Object
5 6 7 8 9 |
# File 'lib/ttt/ai_medium.rb', line 5 def move() super self.max_ply = set_max_ply(available_moves.length) minimax end |
#set_max_ply(moves) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/ttt/ai_medium.rb', line 11 def set_max_ply(moves) if moves > 15 return 3 elsif moves > 5 return 5 else return 7 end end |