Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import random
- maxsize = 1000
- choices = range(1, 30)
- class Node(object):
- def __init__(self, depth, player, value=0):
- self.depth = depth
- self.value = value
- self.children = []
- self.player = player
- self.createChildren()
- def createChildren(self):
- if self.depth < 0:
- return
- for i in choices:
- val = [-10, int(random() * 25)][self.depth < 1]
- self.children.append(
- Node(self.depth - 1, self.player, val)
- )
- def minimax(node, depth, maximizingPlayer):
- if depth == 0 or len(node.children) < 1:
- return node.value
- if maximizingPlayer:
- bestValue = -maxsize
- for child in node.children:
- val = minimax(child, depth - 1, False)
- bestValue = max(bestValue, val)
- # print "WH: val{}, bval{}".format(val, bestValue)
- return bestValue
- else:
- bestValue = +maxsize
- for child in node.children:
- val = minimax(child, depth - 1, True)
- bestValue = min(bestValue, val)
- # print "BL: val{}, bval{}".format(val, bestValue)
- return bestValue
- def get_player(player_turn):
- if player_turn == "WHITE":
- return 1
- else:
- return -1
- def start_game(player_turn="WHITE"):
- sticks = 7
- depth = 4
- player_1_sticks = 0
- player_2_sticks = 0
- node = Node(depth, player_turn)
- while sticks > 0:
- def print_children(n, index):
- for x in n.children:
- print_children(x, index + 1)
- if index == depth:
- print x.value,
- # print "index: {}, val: {}".format(index, x.value)
- #print_children(node, 0)
- #print ""
- cur_player = get_player(player_turn)
- bestValue = -cur_player * maxsize
- val = minimax(node, depth + 1, cur_player)
- print "Current_val: {}".format(val)
- print "[i={}] {} ".format("xx", bestValue)
- #for i in range(len(node.children)):
- #n_child = node.children[i]
- #val = minimax(n_child, depth, cur_player)
- #if (
- #abs(cur_player * maxsize - val) <=
- #abs(cur_player * maxsize - bestValue)
- #):
- #bestValue = val
- #bestChoice = i + 1
- #print "Current_val: {}".format(val)
- #print "[i={}] {} ".format(bestChoice, bestValue)
- print "\n\n"
- #cur_player = -1 * get_player(player_turn)
- #for i in range(len(node.children)):
- #n_child = node.children[i]
- #val = minimax(n_child, depth, cur_player)
- #if (
- #abs(cur_player * maxsize - val) <=
- #abs(cur_player * maxsize - bestValue)
- #):
- #bestValue = val
- #bestChoice = i + 1
- #print "Current_val: {}".format(val)
- #print "[i={}] {} ".format(bestChoice, bestValue)
- if player_turn == "WHITE" and sticks > 0:
- p_sticks = raw_input("choose 1, 2 or 3, 4 stick to pick up: ")
- player_1_sticks += int(p_sticks)
- sticks -= int(p_sticks)
- if player_turn == "BLACK" and sticks > 0:
- p_sticks = val
- player_2_sticks += int(p_sticks)
- sticks -= int(p_sticks)
- print "Bot picked up {} sticks".format(p_sticks)
- if player_turn == "WHITE":
- player_turn = "BLACK"
- else:
- player_turn = "WHITE"
- print "\n- You have {}, Bot have {}, Left {}".format(
- player_1_sticks, player_2_sticks, sticks
- )
- if __name__ == "__main__":
- start_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement