Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.35 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # (The MIT License)
  2. #
  3. # Copyright (c) 2011 Ledsworth Consulting LLC
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  6. # documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  7. # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
  8. # persons to whom the Software is furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  11. # Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. #
  18. # Call to algorithm (computer plays 'O', state array holds the current game board)
  19. state[best_comp_move] = 'O'
  20.  
  21. # Minmax Algorithm
  22. def best_comp_move(ply=0, alpha=-99999, beta=99999)
  23.   if is_won?  # Human won before comp move
  24.     reset_winner_state
  25.     return -9999+ply  # A Win closer to root is better than one deeper in the tree
  26.   end
  27.   return 0 if full_board?
  28.   value = -99999
  29.   best_move = 0
  30.   legal_moves.each do |sqr|
  31.     state[sqr] = 'O'
  32.     response = best_human_move(ply+1,alpha,beta)
  33.     state[sqr] = ' '
  34.     if response > value  
  35.       if ply==0 then puts "New best value #{response} from sqr #{sqr}" end
  36.       value = alpha = response
  37.       best_move = sqr
  38.     end
  39.     if beta <= alpha
  40.       break
  41.     end
  42.   end
  43.   return best_move if ply==0
  44.   value
  45.  end
  46.  
  47. def best_human_move(ply,alpha,beta)
  48.   if is_won?  # Comp won before human move
  49.     reset_winner_state
  50.     return 9999-ply  # A Win closer to root is better than one deeper in the tree
  51.   end
  52.   return 0 if full_board?
  53.   if ply >= 3
  54.     return score
  55.   end
  56.   value = 99999
  57.   legal_moves.each do |sqr|
  58.     state[sqr] = 'X'
  59.     response = best_comp_move(ply+1,alpha,beta)
  60.     state[sqr] = ' '
  61.     if response < value  
  62.       value = beta = response
  63.     end
  64.     if beta <= alpha
  65.       break
  66.     end
  67.   end
  68.   value
  69. end