def work_JSON(self, obj): print("working with socket") if obj[0] == REGISTER: output = self.register() elif obj[0] == RECEIVE: if obj[1] == BLACK_STONE: stone_e = StoneEnum.BLACK elif obj[1] == WHITE_STONE: stone_e = StoneEnum.WHITE else: print("RC: Invalid stone type.") raise Exception() self.receive_stone(stone_e) output = None return output elif obj[0] == MOVE: boards_obj = parse_boards(obj[1]) print("trying") output = self.make_a_move(boards_obj) print("found one") if isinstance(output, tuple): output = get_raw(output) elif obj[0] == GAME_OVER: output = GAME_OVER_RESPONSE else: print("RC: Invalid JSON input.") raise Exception() output = "\"" + output + "\"" return output
def execute_input(player, arr): if arr[0] == REGISTER: return player.register() elif arr[0] == RECEIVE: stone = parse_stone(arr[1]) player.receive_stone(stone.get_type()) elif arr[0] == MOVE: boards = parse_boards(arr[1]) output = player.choose_move(boards) if isinstance(output, str): return output return get_raw(output) else: raise Exception("Invalid name given to execute input")
def work_JSON(self, obj): if obj[0] == REGISTER: output = self.register() elif obj[0] == RECEIVE: if obj[1] == BLACK_STONE: stone_e = StoneEnum.BLACK elif obj[1] == WHITE_STONE: stone_e = StoneEnum.WHITE else: raise Exception("Invalid stone type.") self.receive_stone(stone_e) output = None elif obj[0] == MOVE: boards_obj = parse_boards(obj[1]) output = self.make_a_move(boards_obj) if isinstance(output, tuple): output = get_raw(output) elif obj[0] == GAME_OVER: output = GAME_OVER_RESPONSE else: raise Exception("Invalid JSON input.") return output
def execute_input(player, arr): try: if arr[0] == REGISTER: return player.register() elif arr[0] == RECEIVE: stone = parse_stone(arr[1]) return player.receive_stone(stone.get_type()) elif arr[0] == MOVE: if (len(arr) != 2): raise GoCrazyException("bad input") boards = parse_boards(arr[1]) output = player.choose_move(boards) if isinstance(output, str): return output return get_raw(output) elif arr[0] == END_GAME: output = player.end_game() if output != "OK": raise GoCrazyException("player did not return OK") return output else: raise GoCrazyException("bad procedure name") except GoCrazyException: return GO_CRAZY
def referee_game(self): # Play game after registration complete while not self.game_over: print(self.players[self.current_player].name + "'s turn:") p = self.players[self.current_player].choose_move(self.board_history) if p == PASS: print("{} ({}) makes move {}".format(self.players[self.current_player].name, make_stone(self.current_player).get_raw(), p)) self.execute_move(PASS) else: print("{} ({}) makes move {}".format(self.players[self.current_player].name, make_stone(self.current_player).get_raw(), get_raw(p))) self.execute_move(Point(p[0], p[1])) print(format_pretty_json(format_one_board(self.board_history[0])))
def format_points(pts_set): return sorted([get_raw(tupl) for tupl in list(pts_set)])
def test_get_raw(self): point = str_to_point("3-1") self.assertEqual("3-1", get_raw((point.x, point.y)))