def randomPlay(mat,player,actionVisitCounter,actionEndCounter,actionWinCounter,State): tempPlayer = player firstActionChoice = -1 MCnet = Nnet1.net#load_checkpoint('Winner0_40.pth') while (False == terminalTest(mat)): possibilities = getPossibilities(mat,tempPlayer) probs = [0,0,0,0] # Get here probability distribution p from NN # pNN= MCnet(State)[1] #print(sum(pNN)) #pNN = normalize(pNN) #pNN = [0,0,0,0] numberOfPossibilites = len(possibilities) for i in range (0, numberOfPossibilites): pNN[possibilities[i]] = 1/numberOfPossibilites # The sum of the entries of p should be 1 for i in range (0,len(possibilities)): actionIndex = possibilities[i] probs[actionIndex] = pNN[actionIndex] probs = normalize(probs) rn = random.random() cnt=0.0 for i in range(len(probs)): cnt+=probs[i] if rn <= cnt: randomAction=i break #randomAction = np.random.choice([0,1,2,3],1,p=probs)[0] #print(randomAction) if (firstActionChoice == -1): firstActionChoice = randomAction actionVisitCounter[randomAction] = actionVisitCounter[randomAction] + 1 mat = result(mat,tempPlayer,randomAction) tempPlayer = (tempPlayer+1)%2 mat = logic1.add_two(mat,tempPlayer) actionEndCounter[firstActionChoice] = actionEndCounter[firstActionChoice] + 1 if ("draw" == logic1.game_state(mat)): return actionVisitCounter, actionEndCounter, actionWinCounter elif (player == logic1.game_state(mat)-1): actionWinCounter[firstActionChoice] = actionWinCounter[firstActionChoice] + 1 return actionVisitCounter, actionEndCounter, actionWinCounter else: #Oppenent has won return actionVisitCounter, actionEndCounter, actionWinCounter
#print(key) #Move_list =[logic1.up,logic1.down,logic1.left,logic1.right] matrix, done = commands[key](matrix,current_player)#self.commands[repr(event.char)](self.matrix,self.current_player) if current_player<1: #replace 1 by number of players -1 current_player=current_player+1 else: current_player=0 if done: matrix = logic1.add_two(matrix,current_player) # record last move history_matrixs.append(matrix) done = False if logic1.game_state(matrix) != 'not over': if logic1.game_state(matrix) != "draw": print(current_player,'win') else: print('Draw') game_start=str(logic1.game_state(matrix)) """ Getting a maximum value from matrix """ print(z)
def terminalTest(mat): return (logic1.game_state(mat) != 'not over')
int2dir = ['up', 'down', 'left', 'right'] score = 0 lose = False score = [] step = [] maxnum = [] for i in range(100): s = 0 # init matrix = [[0 for i in range(4)] for j in range(4)] add_block(matrix) add_block(matrix) while True: #printgrid(matrix) if game_state(matrix) == 'lose': # print ('lose') # print ('score: ', cal_score(matrix)) break direction = expectimax_direction(matrix) #print(direction) new_matrix = MERGE_FUNCTIONS[direction](matrix) if (matrix == new_matrix): continue matrix = new_matrix add_block(matrix) s += 1 print(cal_score(matrix), matrix) score.append(cal_score(matrix)) step.append(s) maxnum.append(max(matrix))
def key_down(self, event): """Once the key is pressed the key_down function runs till the game state gives winner. """ game_start = "not over" while game_start == "not over": # AI - Action key = AlphaZero_with_pytorch.alphaZeroSearch( self.matrix, self.current_player, 10, 0.5, 1., True) # AI - Action print(key) if key == c.KEY_BACK and len(self.history_matrixs) > 1: self.matrix = self.history_matrixs.pop() self.update_grid_cells() print('back on step total step:', len(self.history_matrixs)) elif key in self.commands: self.matrix, done = self.commands[key]( self.matrix, self.current_player ) #self.commands[repr(event.char)](self.matrix,self.current_player) print(self.commands[key]) if self.current_player < 1: #replace 1 by number of players -1 self.current_player = self.current_player + 1 else: self.current_player = 0 if done: self.matrix = logic1.add_two(self.matrix, self.current_player) # record last move self.history_matrixs.append(self.matrix) self.update_grid_cells() done = False if logic1.game_state(self.matrix) != 'not over': if logic1.game_state(self.matrix) != "draw": root = tkinter.Tk() root.title("Win") Label(root, text="Player {} Wins!".format( logic1.game_state(self.matrix)), bg=c.BACKGROUND_COLOR_GAME, font=c.FONT).pack() root.mainloop() """ self.grid_cells[1][1].configure( text="P5layer {}".format(logic1.game_state(self.matrix)), bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Wins!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) """ else: root = tkinter.Tk() root.title("Draw") Label(root, text="Draw", bg=c.BACKGROUND_COLOR_GAME, font=c.FONT).pack() """ self.grid_cells[1][1].configure( text="Draw", bg=c.BACKGROUND_COLOR_CELL_EMPTY) """ game_start = str(logic1.game_state(self.matrix))