def chooseRandomTile(self): possible_tiles = [0,1,2,3,4,5,6,11,12,13,14,15,16,22,23,24,25,26,33,34,35,36,44,45,46,55,56,66] for index in possible_tiles: actDict = { 'type': messages.ACTION_REPLY, 'action': { 'ac': 'play', 'tile': possible_tiles[index], 'right': True } } if self.gameState.play(Action(actDict['action'], self.gameState.turn), False): return possible_tiles[index], actDict['action']['right'] actDict['action']['right'] = False if self.gameState.play(Action(actDict['action'], self.gameState.turn), False): return possible_tiles[index], actDict['action']['right']
def __init__(self, state): self.lost = state.winner != 0 if not self.lost: self.wins = 0 self.n = 0 self.children = [] self.to_try = [] for i in range(N): for j in range(N): a = Action(i,j) if state.legal_move(a): self.to_try.append(a) random.shuffle(self.to_try)
def test_z(self): """ ## ## #####..### """ for j in range(5): self.board.table[self.row_num - 1][j] = 1 for j in range(7, self.col_num): self.board.table[self.row_num - 1][j] = 1 piece = self.piece_dict["Z"][0] self.board.proceed(Action(4, piece)) self.assertEqual(self.board.resolve(), 1, str(self.board))
def playTile(self, tile, bool): #Update the board with the last play actDict = { 'type': messages.ACTION_REPLY, 'action': { 'ac': 'play', 'tile': tile, 'right': bool } } if not self.gameState.play(Action(actDict['action'],self.gameState.turn)): return False print(self.gameState.board) return
def human_game(): player_names = ['player1', 'player2'] rules = SplendorGameRules(len(player_names)) game = SplendorGameState(player_names, rules) while not game.check_win(): for player_name in player_names: print(game) while True: # check for invalid action inputs action_str = input(player_name + ' move: ') try: game.action(Action.from_str(action_str)) break except AttributeError as err: print('Invalid action {}: {}'.format(action_str, str(err))) print('best player:', game.best_player())
def self_play(agent): s = State() states = [] actions = [] while s.winner == 0: batch, valid, _ = state_tensor([s]) idx = agent.act(batch, valid) a = Action(idx // N, idx % N) states.append(s.copy()) actions.append(a) s.make_move(a) for i in range(len(states)): states[i].winner = s.winner return states, actions
def test_i(self): """ # # # # #########. #########. #########. #########. """ for i in range(4): for j in range(self.col_num - 1): self.board.table[self.row_num - i - 1][j] = 1 piece = self.piece_dict["I"][1] self.board.proceed(Action(self.col_num - 1, piece)) self.assertEqual(self.board.resolve(), 4, str(self.board))
def get_action(self, board: Board, piece_set: List[Piece]) -> Action: best_action: Action min_cost = sys.maxsize for piece in piece_set: for x in range(board.col_num - piece.width + 1): action = Action(x, piece) before_board, after_board = copy.deepcopy( board), copy.deepcopy(board) can_put = after_board.proceed(action) if can_put: tmp_cost = self.calc(before_board, after_board) else: tmp_cost = sys.maxsize - 1 if tmp_cost < min_cost: min_cost = tmp_cost best_action = action assert min_cost < sys.maxsize return best_action
def duel(*players, show=False): s = State() player = 0 while s.winner == 0: batch, valid, _ = state_tensor([s]) while True: agent = players[player] idx = agent(batch, valid) a = Action(idx // N, idx % N) if s.legal_move(a): break if show: s.show_board() s.make_move(a) player = (player + 1) % len(players) return s.winner
def test_vertical_z(self): """ # ## # ####..#### ####.##### """ for i in [0, 1]: for j in range(4): self.board.table[self.row_num - i - 1][j] = 1 for j in range(5, self.col_num): self.board.table[self.row_num - 1][j] = 1 for j in range(6, self.col_num): self.board.table[self.row_num - 2][j] = 1 piece = self.piece_dict["Z"][1] self.board.proceed(Action(4, piece)) self.assertEqual(self.board.resolve(), 2, str(self.board))
def get_action(self, game_state): player_name = game_state.players[game_state.player_to_move].name action_str = input(player_name + ' move: ') return Action.from_str(action_str)
def get_actions(state): return [ Action('move', 'left') ]
def test_once(): env = GameEnv() # cards = [[3, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0], # [0, 2, 2, 2, 2, 2, 2, 2, 3, 0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 4, 4, 4, 0, 0]] # env.load(np.array(cards)) # history = [355, 352, 352] # history = [355, 352, 352, 337, 350, 351, 109, 124, 0, 14] history = [] games = [Game(env, 0), Game(env, 1), Game(env, 2)] print(games[0].period()) return for action in history: games[0].move(action) games[1].move(action) games[2].move(action) env.move(action) # print(games[0]) # print(games[0].curr_player()) # print(games[1]) # print(games[1].curr_player()) # print(games[2]) # print(games[2].curr_player()) # print(env) print('=================') # env.load(np.array(cards, dtype=np.int32)) # print(games[0], games[1], games[2]) print(env.bottom()) while not env.game_over(): period = env.period() curr_player = env.curr_player() print('-----------------------------------') print('period:', period) print('curr_player:', curr_player) if period == 3: # bottom print('response:', env.response(0), env.response(1), env.response(2)) print(games[0].action_list()) print('remaining cards:') print(games[0].remaining_cards()) print(games[1].remaining_cards()) print(games[2].remaining_cards()) games[0].move(env.response(0)) games[1].move(env.response(1)) games[2].move(env.response(2)) print('bottom:') print(games[0].bottom()) print(games[1].bottom()) print(games[2].bottom()) env.move(-1) print(env.hand_cards_num()) model_input = games[0].to_model_input() # for channel in range(18): # print(channel) # print(model_input[channel]) else: action_list = games[curr_player].action_list() action = np.random.choice(action_list) print('action_list:') print(games[0].action_list()) print(games[1].action_list()) print(games[2].action_list()) action_c = Action(action) print('action:', action) print('', Action(action).to_array()) games[0].move(action) games[1].move(action) games[2].move(action) env.move(action) print(env.hand_cards_num()) # if action_c.need_attach() or action_c.is_attach() or action_c.is_bottom() or action_c.is_bid(): # model_input = games[0].to_model_input() # for channel in range(18): # print(channel) # print(model_input[channel]) print(env) print(games[0].eval()) print(games[1].eval()) print(games[2].eval())
def match(AIs): def read(popen): while True: output = popen.stdout.readline() if output is not None: return output.decode('utf-8') def write(popen, obj): if popen == AIs[args.p]: return popen.stdin.write((str(obj) + '\n').encode('utf-8')) popen.stdin.flush() (name0, simulation0), (name1, simulation1), (name2, simulation2) = players_attr write(AIs[0], name0) write(AIs[0], simulation0) write(AIs[1], name1) write(AIs[1], simulation1) write(AIs[2], name2) write(AIs[2], simulation2) env = GameEnv() hand_cards = env.hand_cards() data.append(env.hand_cards()) game = Game(env, args.p) for player in range(3): write(AIs[player], list(hand_cards.flatten())) slot = [] ig = False print('Filename:', filename) print('Your position:', args.p) print('Your hand cards:', to_chars(hand_cards[args.p])) while not env.game_over(): period = env.period() player = env.curr_player() if period == 1 and player == args.p: print('Your position:', args.p) print('Your hand cards:', to_chars(env.hand_cards()[args.p])) print('Hand cards num of 3 players:', env.hand_cards_num()) print('-----') if period == 1 or period == 2: if player == args.p: if period == 1: _game = game.copy() print('Your turn:', end=' ') chars = input() try: if chars == 'pass': actions = [0] else: chars = chars.upper().rstrip().replace( ',', '').split(' ') while '' in chars: chars.remove('') # for char in chars: # if char not in card_dict: # raise KeyError('input error') actions = to_action(chars) for action in actions: if action in _game.action_list(): _game.move(action) else: raise RuntimeError('couldn\'t move') except (RuntimeError, KeyError): print('Invalid action! Please retry.') print('=====') continue game = _game for action in actions: env.move(action) for target in range(3): write(AIs[target], action) print('=====') if period == 2: print('Your bet:', end=' ') action = int(input()) + 352 if action not in game.action_list(): print('Invalid action! Please retry.') continue env.move(action) game.move(action) for target in range(3): write(AIs[target], action) else: action = int(read(AIs[player])) # print(action) env.move(action) game.move(action) for target in range(3): if target != player: write(AIs[target], action) if period == 1: slot += to_chars(Action(action).to_array()).replace( '[', '').replace(']', '').replace(',', '').split(' ') if env.curr_player() != player: if action == 0: print('Player%d pass' % player) else: print('Player%d\'s turn:' % player, str(slot).replace('\'', '')) print('=====') slot = [] if period == 2: print('Player%d\'s bet: %d' % (player, action - 352)) if period == 2 and env.period() == 3: print('Landlord is Player%d' % env.lord_player()) print('Bottom cards:', to_chars(env.bottom())) print('===== Game Start =====') if period == 3: for player in range(3): write(AIs[player], env.response(player)) game.move(env.response(args.p)) env.move(-1) evals = [None, None, None] for player in range(3): if player != args.p: evals[player] = (float(read(AIs[player])) + 1) / 2 AIs[player].terminate() # print(evals) data.append(game.policy()) with open('human/%s.pkl' % filename, 'wb') as f: pickle.dump(data, f) if args.p == env.lord_player(): is_winner = env.hand_cards_num()[args.p] == 0 else: is_winner = env.hand_cards_num()[env.lord_player()] != 0 if game.lord_player() == -1: print('<<<<<<<<<<<<<<< DRAW >>>>>>>>>>>>>>>') elif is_winner: print('<<<<<<<<<<<<<<< YOU WIN >>>>>>>>>>>>>>>') else: print('<<<<<<<<<<<<<<< YOU LOSE >>>>>>>>>>>>>>>')
def to_action(_cards): cards = np.zeros(15, dtype=np.int) # print(_cards) for card in _cards: cards[card_dict[card]] += 1 # print(cards) if check_kicker(cards): for action in range(ACTION_NUM): if Action(action).is_attach() or Action(action).need_attach(): continue if (Action(action).to_array() == cards).all(): return [action] else: suit = -1 suit_num = 0 for action in range(ACTION_NUM): action_consumed = Action(action).num( ) + Action(action).attach_num() * Action(action).attach_type() if Action(action).need_attach() and action_consumed == cards.sum(): # print(Action(action).to_array()) if ((cards - Action(action).to_array()) < 0).any(): continue if Action(action).to_array().sum() > suit_num: suit_num = Action(action).to_array().sum() suit = action if suit != -1: ret = [suit] cards -= Action(suit).to_array() while cards.sum() > 0: flag = False for action in range(309, 337): if Action(action).num() == Action(suit).attach_type(): if ((cards - Action(action).to_array()) >= 0).all(): ret.append(action) cards -= Action(action).to_array() flag = True break if flag is False: break if cards.sum() == 0: return ret raise RuntimeError('WTF?')
def test_proceed(self): for piece_set in self.pieces: for piece in piece_set: for x in range(self.board.col_num - piece.width + 1): board = copy.deepcopy(self.board) self.assertTrue(board.proceed(Action(x, piece)))
def get_actions(state): currentPosition = [state.unit.x, state.unit.y] print(currentPosition) rnd = randint(0, 3) directions = ['left', 'right', 'up', 'down'] #possibleNextPositions = #print('floor = ', state.floor_map) #print('empty = ', state.empty_map) dirWalk = directions[rnd] dirAttack = 'left' foundFoe = False needToWalk = False manh = 1000 # Idéer # - om man inte kan gå åt det hållet man vill, gå åt ett annat håll, ofta finns det ju två håll som är "rätt håll". # - låt alla units gå mot en och samma fiende och mosa denna. # - # Slå direkt for foe in state.foes: if (foe.health == 0): continue foeDist = dist(state.unit, foe) manh = manhattan(foeDist) #print('foe: ', foe.x, ' ', foe.y, ' (dist: ', foeDist, ' manh: ', manh, ')') if (manh == 1): #print('found foe') dirAttack = directFromDist(foeDist) foundFoe = True break # First weakest non-dead enemy closestFoe = state.foes[0] minstrength = 1000 for foe in state.foes: if (foe.health == 0): continue if foe.power + foe.health < minstrength: minstrength = foe.power + foe.health closestFoe = foe print('minstrength = ', minstrength) standStill = False attackThenWalk = False # Gå ett steg, slå sen if not foundFoe: print('go towards closeby') foeDist = dist(state.unit, closestFoe) dirWalk = directFromDist(foeDist) #if (manh == 1): # foundFoe = True #if (manh == 2): # standStill = True currentPt = Pt(state.unit.x, state.unit.y) currentPt.go(dirWalk) foeDist = dist(currentPt, foe) dirAttack = directFromDist(foeDist) needToWalk = True else: foeDist = dist(state.unit, closestFoe) dirWalk = directFromDist(foeDist) attackThenWalk = True # Move our weakest unit south east unitPowerList = [] thereIsSomeoneWeaker = False for unit in state.units: unitPowerList.append((unit, unit.power + unit.health)) #if (unit.health == 0): # continue #if unit.power+unit.health > state.unit.power + state.unit.health: # thereIsSomeoneWeaker = True sortedUnitPowerList = sorted(unitPowerList, key=lambda x: x[1]) weakestUnit = sortedUnitPowerList[0][0] secondWeakestUnit = sortedUnitPowerList[1][0] print('---', weakestUnit.id, state.unit.id) if state.unit.id == weakestUnit.id and weakestUnit.power + weakestUnit.health < secondWeakestUnit.power + secondWeakestUnit.health: print('reverse') dirWalk = reverse(dirWalk) moveAction = Action('move', dirWalk) attackAction = Action('attack', dirAttack) if standStill: print("don't walk") actions = [] elif (needToWalk and foundFoe): actions = [moveAction, attackAction] elif (attackThenWalk): actions = [attackAction, moveAction] elif (foundFoe): actions = [attackAction] else: actions = [moveAction] return actions