def choose_move(self, state): mv = None # Do not return until Move is valid while not mv or not state.validMove(mv): if state._stage == 0 or state._stage == 2: # Prompt for pass or flip if both are options if state._prev_move._action != 'flip' and \ state._num_flips[state._player] < const.MAX_FLIPS: msg = 'Flip or Pass [f or p]:' mapping = {'f': 'flip', 'p': 'none'} # Automatically pass if only option is a none Move else: mv = Move('none', state._player) break # Prompt for column if in 2nd stage of turn elif state._stage == 1: msg = 'Place [1-%d]:' % (const.NUM_COLS) mapping = { str(n): 'place' for n in range(1, const.NUM_COLS + 1) } # Get key pressed by user and create a corresponding Move object sys.stdout.flush() key = prompt(msg).lower() if key in mapping: if key.isnumeric(): mv = Move(mapping[key], state._player, int(key) - 1) else: mv = Move(mapping[key], state._player) return mv
def choose_move(self, state): self.createQ(state) _action = self.choose_action(state) if state._stage == 0 or state._stage == 2: mv = Move(_action, state._player) else: mv = Move('place', state._player, _action) self.learn(state, mv, _action) return mv
def test(): const.DEBUG = True ai1 = Human('P1') ai2 = Human('P2') player_pair = (ai1, ai2) game = Game(player_pair) f = open('fail.txt') lf = [l.strip() for l in f] last = lf[-1] seq = map(lambda s: s.strip('()').split(', '), last.split('),(')) mv_list = list(map(lambda t: Move(t[0].strip("'"), int(t[1]), int(t[2])), seq)) game.drawScreen() for mv in mv_list: game.update(mv) game.drawScreen() print(game.checkWin())
def place(game, player, col): game.update(Move('none', player)) game.update(Move('place', player, col)) game.update(Move('none', player))