def reset(): is_resumed = input( "Would you like to resume the game from a previous session? [y/n]\n>" ) if is_resumed.strip().lower()[0] == 'y': info['ID'] = input( "Enter the ID for the game you would like to play.\n>") # TODO: stub.doResumed -> # get the corresponding data from server # assign an AI with the same ID while True: try: info['ID'] = stub.GetID( play_pb2.State(status=True, ID=info['ID'])).ID break except: if info['ID'] == "": _ = input( "AI is unavailable at the moment. Press enter to try again.\n>" ) else: print("Unable to retrieve session with ID ", info['ID']) info['ID'] = "" continue print("You are assigned with ID ", info['ID']) color = input("Enter 'B' for Black, 'W' for White \n>").upper() if color == "B": info['color'] = 1 else: info['color'] = 2 _ = stub.SetPlayer(play_pb2.Player(color=info['color'], ID=info['ID'])) return
def exit(c): if type(c) == str and c.strip().lower() == "exit": print("Ending game...") _ = stub.SetExit(play_pb2.State(status=True, ID=info['ID'])) info['ID'] = "" reset() return True return False
def check_reset(reply): console.reset = stub.CheckExit( play_pb2.State(status=True, ID=console.ID)).status if console.reset: print("\n\n\nRestarting game...\n\n\n") reset() console.reset = False reply["a"] = console.actions["clear"] return True, reply return False, reply
def reset(): ID = stub.NewRoom(play_pb2.State(status=True)).ID console.ID = ID console.color = {'has_chosen': False, "client": 1, "AI": 2} console.prev_player = 0 print("Current AI's ID is ", console.ID) if not console.color["has_chosen"]: while not stub.HasChosen(play_pb2.State(status=True, ID=ID)).status: pass # AI_color = stub.GetAIPlayer(play_pb2.State(status = True)).color # human_color = AI_color % 2 + 1 console.color["AI"] = stub.GetAIPlayer( play_pb2.State(status=True, ID=ID)).color console.color["client"] = console.color["AI"] % 2 + 1 console.color["has_chosen"] = True console.res_arr = stub.GetResumed(play_pb2.State(status=True, ID=ID)).move console.res_len = len(console.res_arr) if console.res_len > 0 and console.res_arr[-1][0].upper() == "B": _ = stub.UpdateNext(play_pb2.State(status=True, ID=ID))
def human_actor(batch): # print("\n\n\nCheck human_actor\n\n\n") reply = dict(pi=None, a=None, V=0) ID = console.ID # console.reset = stub.CheckExit(play_pb2.State(status = True, ID = ID)).status # if console.reset: # print("\n\n\nRestarting game...\n\n\n") # reset() # console.reset = False # reply["a"] = console.actions["clear"] # return reply AI_color = console.color["AI"] human_color = console.color["client"] # is_resumed = stub.IsResumed(play_pb2.State(status = True)).status if console.res_len > 0: # print("\n\n\nCheck is_resumed = true\n\n\n") # print("\n\n\n", arr[-console.res_ind], "\n\n\n") reply["a"] = console.str2action(console.res_arr[-console.res_len]) console.res_len -= 1 return reply # print("\n\n\nCheck is_resumed = false\n\n\n") while True: if console.prev_player == 1: move = console.get_last_move(batch) x, y = move2xy(move) _ = stub.SetMove( play_pb2.Step(x=x, y=y, player=play_pb2.Player(color=AI_color, ID=ID))) _ = stub.UpdateNext(play_pb2.State(status=True, ID=ID)) if stub.IsNextPlayer(play_pb2.Player(color=AI_color, ID=ID)).status: reply["a"] = console.actions["skip"] console.prev_player = 1 return reply # else: while stub.IsNextPlayer(play_pb2.Player(color=human_color, ID=ID)).status: do_reset, reply = check_reset(reply) if do_reset: return reply pass human_xy = stub.GetMove(play_pb2.Player(color=human_color, ID=ID)) reply["a"] = console.move2action(xy2move(human_xy.x, human_xy.y)) console.prev_player = 2 return reply
def run(): address = addrs["game_server"] if address != "": channel = grpc.insecure_channel(address + ":50051") else: channel = grpc.insecure_channel('localhost:50051') stub = play_pb2_grpc.TurnStub(channel) # ID_state = "" # while True: # print("Waiting for AI to connect to server...") # ID_state = stub.GetID(play_pb2.State(status = True)) # if ID_state.status: # break # ID = ID_state.ID info = {'ID': "", 'color': 1} def reset(): is_resumed = input( "Would you like to resume the game from a previous session? [y/n]\n>" ) if is_resumed.strip().lower()[0] == 'y': info['ID'] = input( "Enter the ID for the game you would like to play.\n>") # TODO: stub.doResumed -> # get the corresponding data from server # assign an AI with the same ID while True: try: info['ID'] = stub.GetID( play_pb2.State(status=True, ID=info['ID'])).ID break except: if info['ID'] == "": _ = input( "AI is unavailable at the moment. Press enter to try again.\n>" ) else: print("Unable to retrieve session with ID ", info['ID']) info['ID'] = "" continue print("You are assigned with ID ", info['ID']) color = input("Enter 'B' for Black, 'W' for White \n>").upper() if color == "B": info['color'] = 1 else: info['color'] = 2 _ = stub.SetPlayer(play_pb2.Player(color=info['color'], ID=info['ID'])) return def exit(c): if type(c) == str and c.strip().lower() == "exit": print("Ending game...") _ = stub.SetExit(play_pb2.State(status=True, ID=info['ID'])) info['ID'] = "" reset() return True return False """x = -1 && y = -1 indicates passing this round""" # def get_move(): # x = input("Enter coordinate x \n>") # exit(x) # x = int(x) # y = input("Enter coordindate y \n>") # exit(y) # y = int(y) # return x, y # def get_move(): # x = input("Enter coordinate x \n>") # y = input("Enter coordinate y \n>") # return x, y reset() while True: if stub.IsNextPlayer( play_pb2.Player(color=info['color'], ID=info['ID'])).status: x, y = "", "" try: x = input("Enter coordinate x \n>") x = int(x) y = input("Enter coordinate y \n>") y = int(y) _ = stub.SetMove( play_pb2.Step(x=x, y=y, player=play_pb2.Player(color=info['color'], ID=info['ID']))) _ = stub.UpdateNext(play_pb2.State(status=True, ID=info['ID'])) human_move = stub.GetMove( play_pb2.Player(color=info['color'], ID=info['ID'])) print("You placed a stone at coordinates: ", human_move.x, ",", human_move.y) except: if exit(x) or exit(y): continue else: print("Invalid coordinates") else: print("It's AI's turn to play.") while stub.IsNextPlayer( play_pb2.Player(color=info['color'] % 2 + 1, ID=info['ID'])).status: # print("Waiting for AI to make a move...") pass ("Receiving next move from AI...") AI_move = stub.GetMove( play_pb2.Player(color=info['color'] % 2 + 1, ID=info['ID'])) if AI_move.x == -1 and AI_move.y == -1: print("AI chose to pass this round.") else: print("AI placed a stone at coordinates: ", AI_move.x, ",", AI_move.y)