def start_pve_game(self, player1, player2): game = Connect4(player1, player2, data_collection=False, print_boards=True) winner, data = game.start() print(f"The winner was Player {winner}!")
def start_self_play(self, player1, player2, rounds=1000, to_plot=True): # no checkpoint usage or advancing generations self_play_winners = {1: 0, -1: 0, "DRAW": 0} now = time.time() for i in range(rounds): game = Connect4(player1, player2, data_collection=True, print_boards=False, plot=to_plot) winner, data = game.start() self.buffer.extend(data) self_play_winners[winner] += 1 if (i + 1) % 1 == 0: try: self.train_from_data(player1) self.train_from_data(player2) except ValueError as e: print(e) later = time.time() total = later - now print(f"Player being trained: Player 1") print(f"Games won by Player 1: {self_play_winners[1]}!") print(f"Games won by Player -1: {self_play_winners[-1]}!") print(f"Draws: {self_play_winners['DRAW']}!") print(f"Time during {rounds} matches: {total:.2f}s")
def start_arena_play(self, player1, player2, rounds=1000): # automatic movement to next generation self_play_winners = {1: 0, -1: 0, "DRAW": 0} now = time.time() for i in range(rounds): game = Connect4(player1, player2, data_collection=True, print_boards=False, verbose=False) winner, data = game.start() self.buffer.extend(data) self_play_winners[winner] += 1 if (i + 1) % 100 == 0: self.train_from_data(player1) print("Training done") if (self_play_winners[1] / rounds) > 0.5: print("Moving to next generation") player1.save_checkpoint() later = time.time() total = later - now print(f"Player being trained: Player 1") print(f"Games won by Player 1: {self_play_winners[1]}!") print(f"Games won by Player -1: {self_play_winners[-1]}!") print(f"Draws: {self_play_winners['DRAW']}!") print(f"Time during {rounds} matches: {total:.2f}s")
def start_pvp_game(self, player1, player2): game = Connect4(player1, player2, data_collection=False, print_boards=True) winner, data = game.start() print(f"The winner was Player {winner}!") print(f"Data collected during match: {data}")
def start_new_game(self): self.player_1 = self.temp1 self.player_2 = self.temp2 self.curr_player = self.player_1 self.game = Connect4(7, 6) self.canvas.delete(ALL) self.master.update() self.draw_grid() self.drawButtons() self.game_over = False if not self.player_1.human: self.player_1.take_turn(self.game.board)
def __init__(self, master): self.master = master self.game_over = False self.player_1 = Human() self.player_2 = Human() self.temp1 = Human() self.temp2 = Human() self.curr_player = Human() self.game = Connect4(7, 6) master.title('Connect Four') label = Label(master, text="Connect Four") label.grid(row=0) self.canvas = Canvas(master, width=500, height=500) self.canvas.grid(row=2) self.draw_grid() self.canvas.bind('<Button-1>', self.canvas_click) self.drawButtons()
def on_post(self, req, resp): data = urllib.parse.parse_qs(req.stream.read().decode('utf-8')) board_id = str(uuid.uuid4()) player1 = data['user_id'][0] player2 = data['text'][0].split('|')[0].split('@')[-1] # TODO: validate this is a user (at least the format) # TODO: Make sure you are not playing with yourself theme = 'classic' current_game = Connect4(player1, player2, theme=theme) r_connect4.set(board_id, pickle.dumps(current_game)) header_message = f"Connect4 game between Player 1: <@{player1}> & Player 2: <@{player2}>" default_message_blocks[0]['text']['text'] = header_message default_message_blocks[1]['image_url'] = f"{BASE_URL}/slack/connect4/assets/{theme}/player1.png" default_message_blocks[2]['image_url'] = f"{BASE_URL}/slack/connect4/assets/{theme}/player2.png" board_url = current_game.render_board(board_id) default_message_blocks[3]['image_url'] = board_url default_message_blocks[0]['block_id'] = board_id default_message_blocks[-2]['text']['text'] = f"<@{current_game.turn}>'s Turn" resp.media = {'response_type': 'in_channel', 'blocks': default_message_blocks}
def new_game(): game = Connect4(frank, human, print_boards=True) game.start() return "Game made"
@app.route('/newgame') def new_game(): game = Connect4(frank, human, print_boards=True) game.start() return "Game made" @app.route('/choice') def make_choice(): choice = 1 human.choice = choice return "Recorded choice" @app.route('/board') def get_board(): print(game.board.get_board_state_normal(1)) return "Printed board" if __name__ == '__main__': frank = IntelligentWebAgent(1, trained=True) human = WebAgent(-1) game = Connect4(frank, human, print_boards=True) t = threading.Thread(target=game.start) t.daemon = True t.start() app.run(debug=True, threaded=True, use_reloader=False ) # host='0.0.0.0' keyword to access on another machine