def __init__(self): ''' ''' self.outerlines_heurestic = [[7, 6, 5, 4], [6, 4, 3, 3], [5, 3, 2, 2], [4, 3, 2, 1]] self.snake_heurestic = [[16, 15, 14, 13], [9, 10, 11, 12], [8, 7, 6, 5], [1, 2, 3, 4]] self.gradient_heurestic = [[7, 6, 5, 4], [6, 5, 4, 3], [5, 4, 3, 2], [4, 3, 2, 1]] self.directions = {1: "RIGHT", 2: "LEFT", 3: "UP", 4: "DOWN"} self.logic = Gamelogic() self.neighbours = list() heapq.heapify(self.neighbours)
def move(): rnd = random.randrange(1000) if rnd == 999: print("You found a Chest with a chance of 0.01%! But this feature is not implemented yet, so it will do nothing. haha^^") elif rnd >= 900 and Player.isLocked == False: print("You get to a locked door. You'll have to use the key to open it!") Gamelogic.key() elif rnd >= 500 and Player.isLocked == False: print("You move deeper into the dungeon!") Player.room += 1 elif Player.isLocked == True and Player.hasKey == True: Gamelogic.key() elif rnd >= 0: print("An enemy attacks you!") Actions.fight()
def __init__(self): ''' ''' self.outerlines_heurestic = [[7,6,5,4],[6,4,3,3],[5,3,2,2],[4,3,2,1]] self.snake_heurestic = [[16,15,14,13],[9,10,11,12],[8,7,6,5],[1,2,3,4]] self.gradient_heurestic = [[7,6,5,4],[6,5,4,3],[5,4,3,2],[4,3,2,1]] self.directions = {1: "RIGHT", 2: "LEFT", 3: "UP", 4: "DOWN"} self.logic = Gamelogic() self.neighbours = list() heapq.heapify(self.neighbours)
class Solver(): ''' ''' def __init__(self): ''' ''' self.outerlines_heurestic = [[7, 6, 5, 4], [6, 4, 3, 3], [5, 3, 2, 2], [4, 3, 2, 1]] self.snake_heurestic = [[16, 15, 14, 13], [9, 10, 11, 12], [8, 7, 6, 5], [1, 2, 3, 4]] self.gradient_heurestic = [[7, 6, 5, 4], [6, 5, 4, 3], [5, 4, 3, 2], [4, 3, 2, 1]] self.directions = {1: "RIGHT", 2: "LEFT", 3: "UP", 4: "DOWN"} self.logic = Gamelogic() self.neighbours = list() heapq.heapify(self.neighbours) def move(self, direction, board): ''' ''' return self.logic.move(direction, board) def solve(self, board): ''' ''' if board != None: self.neighbours = list() heapq.heapify(self.neighbours) for neighbour in self.get_neighbours(board): heapq.heappush( self.neighbours, (self.expectimax(neighbour, 3, False), neighbour)) board = heapq.heappop(self.neighbours)[1] board = self.logic.append_random_number(board) return board def get_neighbours(self, board): ''' ''' neighbours = list() for move in self.directions: neighbour = self.logic.check_move(self.directions[move], copy.deepcopy(board)) if neighbour != None: neighbours.append(neighbour) return neighbours def heuristic(self, board): ''' ''' h = 9000 #number of zeros for row in board: h -= row.count(0) #gradient ''' for rotations in range(4): gradient_board_score = numpy.array(board) * numpy.rot90(self.outerlines_heurestic, rotations) gradient_board_score = numpy.array(board) * numpy.array(self.snake_heurestic) best_h = h for row in gradient_board_score: for value in row: h -= value if h < best_h: best_h = h ''' return h def random_permutations(self, board): ''' ''' permutations = self.logic.append_all_random_numbers(board) return permutations def probability_of_reaching_node(self, key, board): ''' ''' zero_count = 0 for row in board: zero_count += row.count(0) if key == 2: return 0.9 / zero_count elif key == 4: return 0.1 / zero_count def expectimax(self, board, depth, maximizing_player): ''' ''' if depth == 0: h = self.heuristic(board) return h if maximizing_player: #Return value of maximum-valued child node alpha = float("-inf") for neighbour in self.get_neighbours(board): alpha = max(alpha, self.expectimax(neighbour, depth - 1, False)) elif maximizing_player == False: # Return weighted average of all child nodes' values alpha = 0 neighbours = self.logic.append_all_random_numbers(board) for key in neighbours.keys(): for neighbour in neighbours[key]: alpha += (self.probability_of_reaching_node(key, board) * self.expectimax(neighbour, depth - 1, True)) #print("alpha") return alpha
''' ''' if depth == 0: h = self.heuristic(board) return h if maximizing_player: #Return value of maximum-valued child node alpha = float("-inf") for neighbour in self.get_neighbours(board): alpha = max(alpha, self.expectimax(neighbour, depth - 1, False)) elif maximizing_player == False: # Return weighted average of all child nodes' values alpha = 0 neighbours = self.logic.append_all_random_numbers(board) for key in neighbours.keys(): for neighbour in neighbours[key]: alpha += (self.probability_of_reaching_node(key, board) * self.expectimax(neighbour, depth - 1, True)) #print("alpha") return alpha if __name__ == "__main__": solver = Solver() logic = Gamelogic() print("-----------") board = [[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] neighbours = solver.get_neighbours(board) #print(neighbours)
def __init__(self): gamedata.board = Board() self.board_surf = DrawBoardData() self.gamelg = Gamelogic() self.selected_token = [] self.selected_joker = False
class GamePlay(): def __init__(self): gamedata.board = Board() self.board_surf = DrawBoardData() self.gamelg = Gamelogic() self.selected_token = [] self.selected_joker = False def update(self): #update board and set button self.board_surf.update(gamedata.board,gamedata.players) self.bnoble = [Button(noble[0],noble[1]) for noble in self.board_surf.nobles] self.bdeck1 = Button(self.board_surf.deck1[0],self.board_surf.deck1[1]) self.btier1 = [Button(card[0],card[1]) for card in self.board_surf.tier1] self.bdeck2 = Button(self.board_surf.deck2[0],self.board_surf.deck2[1]) self.btier2 = [Button(card[0],card[1]) for card in self.board_surf.tier2] self.bdeck3 = Button(self.board_surf.deck3[0],self.board_surf.deck3[1]) self.btier3 = [Button(card[0],card[1]) for card in self.board_surf.tier3] self.btokenpool = [Button(token[0],token[1]) for token in self.board_surf.tokenpool] self.bjoker = Button(self.board_surf.joker[0],self.board_surf.joker[1]) self.bplayercard = [[Button(card[0],card[1]) for card in player[0].holdcards] for player in self.board_surf.players] self.bplayertoken = [[Button(token[0][0],token[0][1]) for token in player[0].token] for player in self.board_surf.players] self.bplayerjoker = [Button(player[0].jokericon[0],player[0].jokericon[1]) for player in self.board_surf.players] for player in range(len(gamedata.players)): if gamedata.players[player].isplaying: self.player = gamedata.players[player] self.playernum = player #click and hover function for card in range(len(self.bplayercard[self.playernum])): if self.bplayercard[self.playernum][card].isclicked(): self.gamelg.player_buy_hold_card(self.player,card) for token in range(len(self.bplayertoken[self.playernum])): if self.bplayertoken[self.playernum][token].isclicked(): self.gamelg.player_convert_joker(self.player,token) if self.bplayerjoker[self.playernum].isclicked(): self.gamelg.player_reset_joker(self.player) for bnoble in self.bnoble: if bnoble.isclicked(): pass if self.bdeck1.ishovered(): print("deck1 is hovered") for card in range(len(self.btier1)): if self.btier1[card].isclicked(): print('tier1 '+str(card)) if self.selected_joker: self.gamelg.player_take_card(gamedata.board,gamedata.board.tier1,gamedata.board.deck1,card,self.player) self.selected_joker = False else: self.gamelg.player_buy_card(self.player,gamedata.board.deck1,gamedata.board.tier1,card) if self.bdeck2.ishovered(): print("deck2 is hovered") for card in range(len(self.btier2)): if self.btier2[card].isclicked(): print('tier2 '+str(card)) if self.selected_joker: self.gamelg.player_take_card(gamedata.board,gamedata.board.tier2,gamedata.board.deck2,card,self.player) self.selected_joker = False else: self.gamelg.player_buy_card(self.player,gamedata.board.deck2,gamedata.board.tier2,card) if self.bdeck3.ishovered(): print("deck3 is hovered") for card in range(len(self.btier3)): if self.btier3[card].isclicked(): print('tier3 '+str(card)) if self.selected_joker: self.gamelg.player_take_card(gamedata.board,gamedata.board.tier3,gamedata.board.deck3,card,self.player) self.selected_joker = False else: self.gamelg.player_buy_card(self.player,gamedata.board.deck3,gamedata.board.tier3,card) #click token for btoken in range(len(self.btokenpool)): if self.btokenpool[btoken].isclicked(): if not(len(self.selected_token) == 2 and btoken in self.selected_token) and not(self.selected_joker): if gamedata.board.tokenpool.asList()[btoken] > 0: self.selected_token.append(btoken) if self.selected_token.count(btoken) == 2 and len(self.selected_token) == 2: if gamedata.board.tokenpool.asList()[btoken] > 3: self.gamelg.player_take_two(self.selected_token,self.player) self.selected_token = [] else: self.selected_token.pop() if len(self.selected_token) == 3: self.gamelg.player_take_three(self.selected_token,self.player) self.selected_token = [] if self.btokenpool[btoken].isRclicked(): if btoken in self.selected_token: self.selected_token.remove(btoken) #click joker if self.bjoker.isclicked(): if self.selected_token == []: self.selected_joker = True if self.bjoker.isRclicked(): self.selected_joker = False #render gamedata.listRender.append((self.board_surf.boardbg,(0,0))) gamedata.listRender.append(self.board_surf.deck1) gamedata.listRender.append(self.board_surf.deck2) gamedata.listRender.append(self.board_surf.deck3) for player in self.board_surf.players: gamedata.listRender.append(player[0].bg) for token in player[0].token: for surf in token: gamedata.listRender.append(surf) for holdcard in player[0].holdcards: gamedata.listRender.append(holdcard) gamedata.listRender.append(player[0].jokericon) gamedata.listRender.append(player[0].jokerscore) gamedata.listRender.append(player[0].pvicon) gamedata.listRender.append(player[0].pvscore) for bnoble in self.bnoble: bnoble.render() for btier1 in self.btier1: btier1.render() for btier2 in self.btier2: btier2.render() for btier3 in self.btier3: btier3.render() for btoken in range(len(self.btokenpool)): if btoken in self.selected_token: self.btokenpool[btoken].renderselected() else: self.btokenpool[btoken].render() if self.selected_joker: self.bjoker.renderselected() else: self.bjoker.render()
class Solver(): ''' ''' def __init__(self): ''' ''' self.outerlines_heurestic = [[7,6,5,4],[6,4,3,3],[5,3,2,2],[4,3,2,1]] self.snake_heurestic = [[16,15,14,13],[9,10,11,12],[8,7,6,5],[1,2,3,4]] self.gradient_heurestic = [[7,6,5,4],[6,5,4,3],[5,4,3,2],[4,3,2,1]] self.directions = {1: "RIGHT", 2: "LEFT", 3: "UP", 4: "DOWN"} self.logic = Gamelogic() self.neighbours = list() heapq.heapify(self.neighbours) def move(self, direction, board): ''' ''' return self.logic.move(direction, board) def solve(self, board): ''' ''' if board != None: self.neighbours = list() heapq.heapify(self.neighbours) for neighbour in self.get_neighbours(board): heapq.heappush(self.neighbours, (self.expectimax(neighbour, 3, False), neighbour)) board = heapq.heappop(self.neighbours)[1] board = self.logic.append_random_number(board) return board def get_neighbours(self, board): ''' ''' neighbours = list() for move in self.directions: neighbour = self.logic.check_move(self.directions[move], copy.deepcopy(board)) if neighbour != None: neighbours.append(neighbour) return neighbours def heuristic(self, board): ''' ''' h = 9000 #number of zeros for row in board: h -= row.count(0) #gradient ''' for rotations in range(4): gradient_board_score = numpy.array(board) * numpy.rot90(self.outerlines_heurestic, rotations) gradient_board_score = numpy.array(board) * numpy.array(self.snake_heurestic) best_h = h for row in gradient_board_score: for value in row: h -= value if h < best_h: best_h = h ''' return h def random_permutations(self, board): ''' ''' permutations = self.logic.append_all_random_numbers(board) return permutations def probability_of_reaching_node(self, key, board): ''' ''' zero_count = 0 for row in board: zero_count += row.count(0) if key == 2: return 0.9 / zero_count elif key == 4: return 0.1 / zero_count def expectimax(self, board, depth, maximizing_player): ''' ''' if depth == 0: h = self.heuristic(board) return h if maximizing_player: #Return value of maximum-valued child node alpha = float("-inf") for neighbour in self.get_neighbours(board): alpha = max(alpha, self.expectimax(neighbour, depth - 1, False)) elif maximizing_player == False: # Return weighted average of all child nodes' values alpha = 0 neighbours = self.logic.append_all_random_numbers(board) for key in neighbours.keys(): for neighbour in neighbours[key]: alpha += (self.probability_of_reaching_node(key, board) * self.expectimax(neighbour, depth - 1, True)) #print("alpha") return alpha