def test_game(self, enemy="opp", verbose=False, player1_starts=True): c4 = Game(self.height, self.width, on_move=1 if player1_starts else -1) while c4.winner is None: if player1_starts: move, q_max = c4.best_next_move(self.qnet) c4.play(move) print_cond(c4.field, "\nplayer 1 (qnet) played", move, cond=verbose) player1_starts = True if c4.winner is None: if enemy == "qnet": move, q_max = c4.best_next_move(self.qnet) elif enemy == "opp": move = self.ais[-1].next_move(c4) elif enemy == "rand": move = np.random.choice(c4.possible_moves()) elif enemy == "human": print("Current Field state:\n", c4.field) move = int(input("Your move: " + str(c4.possible_moves()))) print_cond("player -1", enemy, "played", move, cond=verbose) c4.play(move) return c4.winner
def next_move(self, c4: Game): if c4.lastMove is not None: vicinty = list( set(c4.possible_moves()).intersection( range(c4.lastMove - 1, c4.lastMove + 2))) if len(vicinty) > 0: return np.random.choice(vicinty) return np.random.choice(c4.possible_moves())
def next_exploring_move(self, c4: Game, epsilon=0.2): assert c4.on_move == self.color explore = np.random.rand() < epsilon if explore: move = np.random.choice(c4.possible_moves()) else: move = self.next_move(c4) return move, {"explore": explore}
def next_move(self, c4: Game): if c4.lastMove is not None and c4.lastMove in c4.possible_moves(): return c4.lastMove else: return np.random.choice(c4.possible_moves())
def next_move(self, c4: Game): return np.random.choice(c4.possible_moves())