Ejemplo 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
Ejemplo n.º 2
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
Ejemplo n.º 3
0
 def handle_fixed_handicap(self, args):
     try:
         number_of_stones = gtp_engine.interpret_int(args[0])
     except IndexError:
         gtp_engine.report_bad_arguments()
     if not self.board.is_empty():
         raise GtpError("board not empty")
     try:
         points = handicap_layout.handicap_points(number_of_stones,
                                                  self.board_size)
     except ValueError:
         raise GtpError("invalid number of stones")
     for row, col in points:
         self.board.play(row, col, 'b')
     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 points)
Ejemplo n.º 4
0
 def handle_fixed_handicap(self, args):
     try:
         number_of_stones = gtp_engine.interpret_int(args[0])
     except IndexError:
         gtp_engine.report_bad_arguments()
     if not self.board.is_empty():
         raise GtpError("board not empty")
     try:
         points = handicap_layout.handicap_points(
             number_of_stones, self.board_size)
     except ValueError:
         raise GtpError("invalid number of stones")
     for row, col in points:
         self.board.play(row, col, 'b')
     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 points)
Ejemplo n.º 5
0
             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)
 else:
     # May propagate ValueError
     points = handicap_layout.handicap_points(handicap, self.board_size)
     for colour in "b", "w":
         vertices = self.send_command(colour, "fixed_handicap",
                                      str(handicap))
         try:
             seen_points = [
                 move_from_vertex(vt, self.board_size)
                 for vt in vertices.split(" ")
             ]
             if set(seen_points) != set(points):
                 raise ValueError
         except ValueError:
             raise BadGtpResponse(
                 "bad response from fixed_handicap command "
                 "to %s: %s" % (self.players[colour], vertices))
 self.board.apply_setup(points, [], [])
Ejemplo n.º 6
0
 def _choose_free_handicap_moves(self, number_of_stones):
     i = min(number_of_stones,
             handicap_layout.max_fixed_handicap_for_board_size(
                 self.board_size))
     return handicap_layout.handicap_points(i, self.board_size)
Ejemplo n.º 7
0
     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)
 else:
     # May propagate ValueError
     points = handicap_layout.handicap_points(handicap, self.board_size)
     for colour in "b", "w":
         vertices = self.send_command(
             colour, "fixed_handicap", str(handicap))
         try:
             seen_points = [move_from_vertex(vt, self.board_size)
                            for vt in vertices.split(" ")]
             if set(seen_points) != set(points):
                 raise ValueError
         except ValueError:
             raise BadGtpResponse(
                 "bad response from fixed_handicap command "
                 "to %s: %s" % (self.players[colour], vertices))
 self.board.apply_setup(points, [], [])
 self.handicap = handicap
 self.additional_sgf_props.append(('HA', handicap))