Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. from random import random
  2.  
  3. maxsize = 1000
  4. choices = range(1, 30)
  5.  
  6.  
  7. class Node(object):
  8.     def __init__(self, depth, player, value=0):
  9.         self.depth = depth
  10.         self.value = value
  11.         self.children = []
  12.         self.player = player
  13.         self.createChildren()
  14.  
  15.     def createChildren(self):
  16.         if self.depth < 0:
  17.             return
  18.  
  19.         for i in choices:
  20.             val = [-10, int(random() * 25)][self.depth < 1]
  21.             self.children.append(
  22.                 Node(self.depth - 1, self.player, val)
  23.             )
  24.  
  25.  
  26. def minimax(node, depth, maximizingPlayer):
  27.     if depth == 0 or len(node.children) < 1:
  28.         return node.value
  29.  
  30.     if maximizingPlayer:
  31.         bestValue = -maxsize
  32.         for child in node.children:
  33.             val = minimax(child, depth - 1, False)
  34.             bestValue = max(bestValue, val)
  35.             # print "WH: val{}, bval{}".format(val, bestValue)
  36.         return bestValue
  37.  
  38.     else:
  39.         bestValue = +maxsize
  40.         for child in node.children:
  41.             val = minimax(child, depth - 1, True)
  42.             bestValue = min(bestValue, val)
  43.             # print "BL: val{}, bval{}".format(val, bestValue)
  44.         return bestValue
  45.  
  46.  
  47. def get_player(player_turn):
  48.     if player_turn == "WHITE":
  49.         return 1
  50.     else:
  51.         return -1
  52.  
  53.  
  54. def start_game(player_turn="WHITE"):
  55.     sticks = 7
  56.     depth = 4
  57.  
  58.     player_1_sticks = 0
  59.     player_2_sticks = 0
  60.  
  61.     node = Node(depth, player_turn)
  62.  
  63.     while sticks > 0:
  64.  
  65.         def print_children(n, index):
  66.             for x in n.children:
  67.                 print_children(x, index + 1)
  68.                 if index == depth:
  69.                     print x.value,
  70.                     # print "index: {}, val: {}".format(index, x.value)
  71.  
  72.         #print_children(node, 0)
  73.         #print ""
  74.  
  75.         cur_player = get_player(player_turn)
  76.         bestValue = -cur_player * maxsize
  77.         val = minimax(node, depth + 1, cur_player)
  78.  
  79.         print "Current_val: {}".format(val)
  80.         print "[i={}] {} ".format("xx", bestValue)
  81.  
  82.         #for i in range(len(node.children)):
  83.             #n_child = node.children[i]
  84.             #val = minimax(n_child, depth, cur_player)
  85.             #if (
  86.                     #abs(cur_player * maxsize - val) <=
  87.                     #abs(cur_player * maxsize - bestValue)
  88.             #):
  89.                 #bestValue = val
  90.                 #bestChoice = i + 1
  91.  
  92.             #print "Current_val: {}".format(val)
  93.             #print "[i={}] {} ".format(bestChoice, bestValue)
  94.  
  95.         print "\n\n"
  96.  
  97.         #cur_player = -1 * get_player(player_turn)
  98.         #for i in range(len(node.children)):
  99.             #n_child = node.children[i]
  100.             #val = minimax(n_child, depth, cur_player)
  101.             #if (
  102.                     #abs(cur_player * maxsize - val) <=
  103.                     #abs(cur_player * maxsize - bestValue)
  104.             #):
  105.                 #bestValue = val
  106.                 #bestChoice = i + 1
  107.  
  108.             #print "Current_val: {}".format(val)
  109.             #print "[i={}] {} ".format(bestChoice, bestValue)
  110.  
  111.         if player_turn == "WHITE" and sticks > 0:
  112.             p_sticks = raw_input("choose 1, 2 or 3, 4 stick to pick up: ")
  113.             player_1_sticks += int(p_sticks)
  114.             sticks -= int(p_sticks)
  115.  
  116.         if player_turn == "BLACK" and sticks > 0:
  117.             p_sticks = val
  118.             player_2_sticks += int(p_sticks)
  119.             sticks -= int(p_sticks)
  120.             print "Bot picked up {} sticks".format(p_sticks)
  121.  
  122.         if player_turn == "WHITE":
  123.             player_turn = "BLACK"
  124.         else:
  125.             player_turn = "WHITE"
  126.             print "\n- You have {}, Bot have {}, Left {}".format(
  127.                 player_1_sticks, player_2_sticks, sticks
  128.             )
  129.  
  130.  
  131. if __name__ == "__main__":
  132.     start_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement