示例#1
0
   def execute_move(self, move):
      if (not self.game_over):
         old_history = deepcopy(self.board_history)
         if (move == PASS):
            add_board = old_history[0]
            self.update_history(add_board)

            # Check for both players consecutive passes 
            if len(self.board_history) == 3:
               if self.board_history[0].equal(self.board_history[1]) and self.board_history[1].equal(self.board_history[2]):
                  self.game_over = True

         elif isinstance(move, Point):
            if (self.move_ref.valid_move(self.current_player, move, self.board_history, self.board_history[0])):
               add_board = self.make_move(self.current_player, move)
               self.update_history(add_board)
            else:
               self.game_over = True
               self.broke_rules = self.current_player
               self.winner = get_other_type(self.current_player)
         
         else:
            raise Exception("GO REF: Not a valid move.")
         self.current_player = get_other_type(self.current_player)
         return old_history
示例#2
0
    def run_game(self):

        #Set Player 2
        client_socket = self.create_server(self.IP, self.port)
        self.remote_player = RemoteContractProxy(connection=client_socket)
        player2_name = self.remote_player.register()
        self.remote_player.receive_stone(StoneEnum.WHITE)

        #Set Player 1
        player1_name = self.local_player.register()
        self.local_player.receive_stone(StoneEnum.BLACK)

        # Pass to Referee to start game
        go_ref = GoReferee(player1=self.local_player,
                           player2=self.remote_player)
        connected = True
        valid_response = True

        # Play game
        while not go_ref.game_over and connected and valid_response:
            try:
                go_ref.referee_game()
            except socket_error:
                go_ref.game_over = True
                print("{} disconnected.".format(
                    go_ref.players[go_ref.current_player].name))
                connected = False
                go_ref.winner = get_other_type(go_ref.current_player)
                break
            except PointException:
                go_ref.game_over = True
                valid_response = False
                print("{} played an invalid move.".format(
                    go_ref.players[go_ref.current_player].name))
                go_ref.winner = get_other_type(go_ref.current_player)
                break

        if go_ref.broke_rules:
            print("{} broke the rules.".format(
                go_ref.players[go_ref.broke_rules].name))

        if connected:
            winner = go_ref.get_winners()
        elif not connected:
            winner = [self.local_player.name]
        else:
            raise Exception("GO ADMIN: Game ended unexpectedly.")

        return winner
示例#3
0
 def place_and_update(self, stone_type, point):
     self.place_stone(stone_type, point)
     neighbors = self.get_neighbors(point)
     other_type = get_other_type(stone_type)
     for other_point in self._filter_neighbors_by_type(
             neighbors, other_type):
         if not self.reachable(other_point, None):
             self._remove_component(other_type, other_point)
     if not self.reachable(point, None):
         self._remove_component(stone_type, point)
     return self.board
示例#4
0
 def _choose_move_recur(self, hist, curr, n):
    if n is 1:
       return self._choose_move_base(hist, curr)
    for x, y in sorted(list(self._get_valid_moves(hist, curr))):
       next1 = deepcopy(curr)
       next1.place_and_update(self.stone_type, Point(x, y))
       for x2, y2 in sorted(list(self._get_valid_moves(hist, curr))):
          next2 = deepcopy(next1)
          next2.place_and_update(get_other_type(self.stone_type), Point(x2, y2))
          ret = self._choose_move_recur(hist, next2, n - 1)
          if ret:
             return (x, y)
    return None
示例#5
0
 def _valid_step(self, curr_board, mover, next_board):
     othr_mvr = get_other_type(mover)
     curr_mvr_pts, next_mvr_pts = curr_board.get_points(
         mover), next_board.get_points(mover)
     curr_othr_pts, next_othr_pts = curr_board.get_points(
         othr_mvr), next_board.get_points(othr_mvr)
     move = next_mvr_pts.difference(curr_mvr_pts)
     if len(move) is 0:
         return curr_board.equal(next_board)
     elif len(move) is 1:
         mv_x, mv_y = list(move)[0]
         test_board = deepcopy(curr_board)
         test_board.place_and_update(mover, Point(mv_x, mv_y))
         if not test_board.equal(next_board):
             return False
     else:
         return False
     return True
 def test_get_other_type(self):
     self.assertEqual(StoneEnum.BLACK, get_other_type(StoneEnum.WHITE))
     self.assertEqual(StoneEnum.WHITE, get_other_type(StoneEnum.BLACK))
示例#7
0
 def _get_history_mover(self, i, stone_type):
     if i % 2:
         return get_other_type(stone_type)
     return stone_type
示例#8
0
 def _move_captures(self, board, point):
     test_board = deepcopy(board)
     other = get_other_type(self.stone_type)
     before = len(test_board.get_points(other))
     test_board.place_and_update(self.stone_type, point)
     return len(test_board.get_points(other)) < before
示例#9
0
    def run_game(self, player1, player2):
        go_ref = GoReferee(player1=player1, player2=player2)
        connected = True
        valid_response = True
        cheater = None

        # Distribute Stones Before Game
        player1_received = False
        try:
            player1.receive_stone(StoneEnum.BLACK)
            player1_received = True
        except:
            go_ref.winner = StoneEnum.WHITE
            cheater = player1.name
            connected = False
            cheater = player1.name
            print("Unsuccessful receive stone for {}.".format(cheater))
            go_ref.winner = StoneEnum.WHITE
        if player1_received:
            try:
                player2.receive_stone(StoneEnum.WHITE)
            except:
                go_ref.winner = StoneEnum.BLACK
                cheater = player2.name
                connected = False
                cheater = player2.name
                print("Unsuccessful receive stone for {}.".format(cheater))
                go_ref.winner = StoneEnum.BLACK

        # Referee game and check for cheating condition
        # - Game over via breaking the rules.
        # - Invalid responses during game.
        # - Disconnecting during game.
        while not go_ref.game_over and connected and valid_response:
            try:
                go_ref.referee_game()
            except OSError:
                go_ref.game_over = True
                connected = False
                cheater = go_ref.players[go_ref.current_player].name
                print("Player {} disconnected.".format(cheater))
                go_ref.winner = get_other_type(go_ref.current_player)
                break
            except PointException:
                go_ref.game_over = True
                valid_response = False
                cheater = go_ref.players[go_ref.current_player].name
                print("Invalid response from player {}.".format(cheater))
                go_ref.winner = get_other_type(go_ref.current_player)
                break

        if go_ref.broke_rules:
            cheater = go_ref.players[go_ref.broke_rules].name
            print("Player {} broke the rules.".format(cheater))

        # Validate Game Over for both players
        if connected:  #(go_ref.game_over and connected and valid_response) or not valid_response:
            if not player1.game_over([GAME_OVER]):
                cheater = player1.name
                print("Did not receive game_over from Player {}".format(
                    player1.name))
                go_ref.winner = StoneEnum.WHITE
            elif not player2.game_over([GAME_OVER]):
                cheater = player2.name
                print("Did not receive game_over from Player {}".format(
                    player2.name))
                go_ref.winner = StoneEnum.BLACK

        winner = go_ref.get_winners()

        # Randomly break ties if two winners
        if len(winner) == 1:
            return winner[0], cheater
        else:
            rand_idx = random.randint(0, 1)
            return winner[rand_idx], cheater