def play(colours_players, logfilename, out): out.section("game start") game = Chexers(logfilename=logfilename) # Let's get all the players ready! for colour, player in colours_players: player.init(colour) _colours, players = zip(*colours_players) # Repeat the following until the game ends # (starting with Red as the current player, then alternating): curr_player, next_player, prev_player = players while not game.over(): out.section(f"{curr_player.player_str}'s turn") # Ask the current player for their next action (calling their .action() # method). action = curr_player.action() # Validate this action (or pass) and apply it to the game if it is # allowed. Display the resulting game state. game.update(curr_player.colour, action) # Notify all three players (including the current player) of the action # (or pass) (using their .update() methods). for player in players: player.update(curr_player.colour, action) # Next player's turn! curr_player, next_player, prev_player = next_player, prev_player, curr_player # After that loop, the game has ended! return game.end()
def play(players, options, out): # Set up a new Chexers game and initialise a Red, Green and Blue player # (constructing three Player classes including running their .__init__() # methods). game = Chexers(logfilename=options.logfile, debugboard=options.verbosity > 2) out.section("initialising players") for player, colour in zip(players, ['red', 'green', 'blue']): # NOTE: `player` here is actually a player wrapper. Your program should # still implement a method called `__init__()`, not one called `init()`. player.init(colour) # Display the initial state of the game. out.section("game start") out.comment("displaying game info:") out.comments(game, pad=1) # Repeat the following until the game ends # (starting with Red as the current player, then alternating): curr_player, next_player, prev_player = players while not game.over(): time.sleep(options.delay) out.section(f"{curr_player.name}'s turn") # Ask the current player for their next action (calling their .action() # method). action = curr_player.action() # Validate this action (or pass) and apply it to the game if it is # allowed. Display the resulting game state. game.update(curr_player.colour, action) out.comment("displaying game info:") out.comments(game, pad=1) # Notify all three players (including the current player) of the action # (or pass) (using their .update() methods). for player in players: player.update(curr_player.colour, action) # Next player's turn! curr_player, next_player, prev_player = next_player, prev_player, curr_player # After that loop, the game has ended (one way or another!) # Display the final result of the game to the user. result = game.end() out.section("game over!") out.print(result) with open('result2.txt', 'a') as the_file: the_file.writelines(result + "\n")
def play(p_R, p_G, p_B, options): # Set up a new Chexers game and initialise a Red, Green and Blue player # (constructing three Player classes including running their .__init__() # methods). game = Chexers(options.logfile) info("initialising players", options) all_players = p_R, p_G, p_B for player in all_players: # NOTE: `player` here is actually a player wrapper. Your program should # still implement a method called `__init__()`, not one called `init()`. player.init() # Display the initial state of the game. info("game start", options) display(game, options) # Repeat the following until the game ends # (starting with Red as the current player, then alternating): curr_player, next_player, prev_player = p_R, p_G, p_B result = None while not game.over(): time.sleep(options.delay) info(f"{curr_player.colour} player's turn", options) # Ask the current player for their next action (calling their .action() # method). action = curr_player.action() # Validate this action (or pass) and apply it to the game if it is # allowed. Display the resulting game state. game.update(curr_player.colour, action) display(game, options) # Notify all three players (including the current player) of the action # (or pass) (using their .update() methods). for player in all_players: player.update(curr_player.colour, action) # Next player's turn! curr_player,next_player,prev_player=next_player,prev_player,curr_player # After that loop, the game has ended (one way or another!) # Display the final result of the game to the user. result = game.end() info("game over!", options) say(result)
def connect_and_play(player, options, out): # SET UP SERVER CONNECTION out.section("connecting to battleground") # attempt to connect to the server... out.comment("attempting to connect to the server...") server = Server.from_address(options.host, options.port) out.comment("connection established!") # FIND A GAME # we would like to play a game! if options.channel: channel_str = f"channel '{options.channel}'" else: channel_str = "open channel" out.comment( f"submitting game request as '{options.name}' in {channel_str}...") server.send(M.PLAY, name=options.name, channel=options.channel) server.recv(M.OKAY) out.comment("game request submitted.") # wait for the server to find a game for us... out.comment(f"waiting for opponents in {channel_str}...") out.comment("(press ^C to stop waiting)") # (wait through some OKAY-OKAY msg exchanges until a GAME message comes--- # the server is asking if we are still here waiting, or have disconnected) gamemsg = server.recv(M.OKAY | M.GAME) while gamemsg['mtype'] is not M.GAME: server.send(M.OKAY) gamemsg = server.recv(M.OKAY | M.GAME) # when we get a game message, it's time to play! out.comment("opponents found!") out.comment("red player: ", gamemsg['red']) out.comment("green player:", gamemsg['green']) out.comment("blue player: ", gamemsg['blue']) # PLAY THE GAME # Set up a new Chexers game and initialise our player. game = Chexers(logfilename=options.logfile, debugboard=options.verbosity > 2) out.section("initialising player") out.comment("waiting for colour assignment...") initmsg = server.recv(M.INIT | M.ERRO) if initmsg['mtype'] is M.ERRO: erromsg = initmsg out.section("connection error") out.print(erromsg['reason']) return out.comment("playing as", initmsg['colour'], pad=1) out.comment("initialising your player class...") player.init(initmsg['colour']) out.comment("ready to play!") server.send(M.OKAY) players = format_players(gamemsg, player.colour) # Display the initial state of the game. out.section("game start", clear=True) out.comment("displaying game info:") out.comments(players, pad=1) out.comments(game, pad=1) # Now wait for messages from the sever and respond accordingly: while True: msg = server.recv(M.TURN | M.UPD8 | M.OVER | M.ERRO) if msg['mtype'] is M.TURN: # it's our turn! out.section("your turn!", clear=True) out.comment("displaying game info:") out.comments(players, pad=1) out.comments(game, pad=1) # decide on action and submit it to server action = player.action() server.send(M.ACTN, action=action) elif msg['mtype'] is M.UPD8: # someone made a move! colour = msg['colour'] action = msg['action'] # update our local state, out.section("receiving update", clear=True) game.update(colour, action) out.comment("displaying game info:") out.comments(players, pad=1) out.comments(game, pad=1) player.update(colour, action) # then notify server we are ready to continue: server.send(M.OKAY) elif msg['mtype'] is M.OVER: # the game ended! either legitmately or through some # game error (e.g. non-allowed move by us or opponent) out.section("game over!") out.print(msg['result']) break elif msg['mtype'] is M.ERRO: out.section("connection error") out.print(msg['reason']) break