Exemplo n.º 1
0
    def set_handicap(self, handicap, is_free):
        """Arrange for the game to be played at a handicap.

        handicap -- int (number of stones)
        is_free  -- bool

        Raises ValueError if the number of stones isn't valid (see GTP spec).

        Propagates any exceptions from the handicap-related backend methods:
          get_free_handicap()
          notify_free_handicap()
          notify_fixed_handicap()

        """
        if self._state != 1:
            raise GameRunnerStateError
        if is_free:
            max_points = handicap_layout.max_free_handicap_for_board_size(
                self.board_size)
            if not 2 <= handicap <= max_points:
                raise ValueError
            self._state = 2
            points = self.backend.get_free_handicap(handicap)
            self.backend.notify_free_handicap(points)
        else:
            # May propagate ValueError
            points = handicap_layout.handicap_points(handicap, self.board_size)
            self._state = 2
            for colour in "b", "w":
                self.backend.notify_fixed_handicap(colour, handicap, points)
        self.additional_sgf_props.append(('HA', handicap))
        self.handicap_stones = points
Exemplo n.º 2
0
    def set_handicap(self, handicap, is_free):
        """Initialise the board position for a handicap.

        Raises ValueError if the number of stones isn't valid (see GTP spec).

        Raises BadGtpResponse if there's an invalid respone to
        place_free_handicap or fixed_handicap.

        """
        if is_free:
            max_points = handicap_layout.max_free_handicap_for_board_size(
                self.board_size)
            if not 2 <= handicap <= max_points:
                raise ValueError
            vertices = self.send_command("b", "place_free_handicap",
                                         str(handicap))
            try:
                points = [
                    move_from_vertex(vt, self.board_size)
                    for vt in vertices.split(" ")
                ]
                if None in points:
                    raise ValueError("response included 'pass'")
                if len(set(points)) < len(points):
                    raise ValueError("duplicate point")
            except ValueError, e:
                raise BadGtpResponse(
                    "invalid response from place_free_handicap command "
                    "to %s: %s" % (self.players["b"], e))
            vertices = [format_vertex(point) for point in points]
            self.send_command("w", "set_free_handicap", *vertices)
Exemplo n.º 3
0
 def handle_place_free_handicap(self, args):
     try:
         number_of_stones = gtp_engine.interpret_int(args[0])
     except IndexError:
         gtp_engine.report_bad_arguments()
     max_points = handicap_layout.max_free_handicap_for_board_size(
         self.board_size)
     if not 2 <= number_of_stones <= max_points:
         raise GtpError("invalid number of stones")
     if not self.board.is_empty():
         raise GtpError("board not empty")
     if number_of_stones == max_points:
         number_of_stones = max_points - 1
     moves = self._choose_free_handicap_moves(number_of_stones)
     try:
         try:
             if len(moves) > number_of_stones:
                 raise ValueError
             for row, col in moves:
                 self.board.play(row, col, 'b')
         except (ValueError, TypeError):
             raise GtpError("invalid result from move generator: %s"
                            % format_vertex_list(moves))
     except Exception:
         self.reset()
         raise
     self.simple_ko_point = None
     self.handicap = number_of_stones
     self.set_history_base(self.board.copy())
     return " ".join(format_vertex((row, col))
                     for (row, col) in moves)
Exemplo n.º 4
0
    def set_handicap(self, handicap, is_free):
        """Arrange for the game to be played at a handicap.

        handicap -- int (number of stones)
        is_free  -- bool

        Raises ValueError if the number of stones isn't valid (see GTP spec).

        Propagates any exceptions from the handicap-related backend methods:
          get_free_handicap()
          notify_free_handicap()
          notify_fixed_handicap()

        """
        if self._state != 1:
            raise GameRunnerStateError
        if is_free:
            max_points = handicap_layout.max_free_handicap_for_board_size(
                self.board_size)
            if not 2 <= handicap <= max_points:
                raise ValueError
            self._state = 2
            points = self.backend.get_free_handicap(handicap)
            self.backend.notify_free_handicap(points)
        else:
            # May propagate ValueError
            points = handicap_layout.handicap_points(handicap, self.board_size)
            self._state = 2
            for colour in "b", "w":
                self.backend.notify_fixed_handicap(colour, handicap, points)
        self.additional_sgf_props.append(('HA', handicap))
        self.handicap_stones = points
Exemplo n.º 5
0
    def set_handicap(self, handicap, is_free):
        """Initialise the board position for a handicap.

        Raises ValueError if the number of stones isn't valid (see GTP spec).

        Raises BadGtpResponse if there's an invalid respone to
        place_free_handicap or fixed_handicap.

        """
        if is_free:
            max_points = handicap_layout.max_free_handicap_for_board_size(
                self.board_size)
            if not 2 <= handicap <= max_points:
                raise ValueError
            vertices = self.send_command(
                "b", "place_free_handicap", str(handicap))
            try:
                points = [move_from_vertex(vt, self.board_size)
                          for vt in vertices.split(" ")]
                if None in points:
                    raise ValueError("response included 'pass'")
                if len(set(points)) < len(points):
                    raise ValueError("duplicate point")
            except ValueError, e:
                raise BadGtpResponse(
                    "invalid response from place_free_handicap command "
                    "to %s: %s" % (self.players["b"], e))
            vertices = [format_vertex(point) for point in points]
            self.send_command("w", "set_free_handicap", *vertices)
Exemplo n.º 6
0
def validate_handicap(handicap, handicap_style, board_size):
    """Check whether a handicap is allowed.

    handicap       -- int or None
    handicap_style -- 'free' or 'fixed'
    board_size     -- int

    Raises ControlFileError with a description if it isn't.

    """
    if handicap is None:
        return True
    if handicap < 2:
        raise ControlFileError("handicap too small")
    if handicap_style == 'fixed':
        limit = handicap_layout.max_fixed_handicap_for_board_size(board_size)
    else:
        limit = handicap_layout.max_free_handicap_for_board_size(board_size)
    if handicap > limit:
        raise ControlFileError("%s handicap out of range for board size %d" %
                               (handicap_style, board_size))
Exemplo n.º 7
0
def validate_handicap(handicap, handicap_style, board_size):
    """Check whether a handicap is allowed.

    handicap       -- int or None
    handicap_style -- 'free' or 'fixed'
    board_size     -- int

    Raises ControlFileError with a description if it isn't.

    """
    if handicap is None:
        return True
    if handicap < 2:
        raise ControlFileError("handicap too small")
    if handicap_style == 'fixed':
        limit = handicap_layout.max_fixed_handicap_for_board_size(board_size)
    else:
        limit = handicap_layout.max_free_handicap_for_board_size(board_size)
    if handicap > limit:
        raise ControlFileError(
            "%s handicap out of range for board size %d" %
            (handicap_style, board_size))
Exemplo n.º 8
0
 def handle_set_free_handicap(self, args):
     max_points = handicap_layout.max_free_handicap_for_board_size(
         self.board_size)
     if not 2 <= len(args) <= max_points:
         raise GtpError("invalid number of stones")
     if not self.board.is_empty():
         raise GtpError("board not empty")
     try:
         for vertex_s in args:
             move = gtp_engine.interpret_vertex(vertex_s, self.board_size)
             if move is None:
                 raise GtpError("'pass' not permitted")
             row, col = move
             try:
                 self.board.play(row, col, 'b')
             except ValueError:
                 raise GtpError("engine error: %s is occupied" % vertex_s)
     except Exception:
         self.reset()
         raise
     self.set_history_base(self.board.copy())
     self.handicap = len(args)
     self.simple_ko_point = None