コード例 #1
0
    def __init__(self, is_p1_turn: bool) -> None:
        """
        initialize a new subtractsquare game

        #A docstring example is not needed for any functions or methods that
        use input() or random.
        """
        self.current_state = SubtractSquareState(is_p1_turn)
コード例 #2
0
ファイル: limits.py プロジェクト: TianyuDu/csc148
def count_states_mem(s1: SubtractSquareState, seen: dict) -> int:
    """
    Return the number of game states reachable from here *quickly*.
    """
    moves = s1.get_possible_moves()
    states = [s1.make_move(m) for m in moves]
    if s1.__repr__() not in seen:
        seen[s1.__repr__()] = 1 + sum([count_states_mem(x, seen) for x in states])
    return seen[s1.__repr__()]
コード例 #3
0
    def __init__(self, p1_starts):
        """
        Initialize this Game, using p1_starts to find who the first player is.

        :param p1_starts: A boolean representing whether Player 1 is the first
                          to make a move.
        :type p1_starts: bool
        """
        count = int(input("Enter the number to subtract from: "))
        self.current_state = SubtractSquareState(p1_starts, count)
コード例 #4
0
class SubtractSquareGame(Game):
    """
    Abstract class for a game to be played with two players.
    """

    def __init__(self, p1_starts):
        """
        Initialize this Game, using p1_starts to find who the first player is.

        :param p1_starts: A boolean representing whether Player 1 is the first
                          to make a move.
        :type p1_starts: bool
        """
        num = int(input("Enter a number: "))
        self.current_state = SubtractSquareState(p1_starts, num)

    def get_instructions(self):
        """
        Return the instructions for this Game.

        :return: The instructions for this Game.
        :rtype: str
        """
        return "A non-negative whole number is chosen as the starting \n" \
               "valueby some neutral entity. In our case, a player will \n" \
               "choose it (i.e. through the use of input. The player whose \n" \
               "turn it is chooses some square of a positive whole number (\n" \
               "such as 1, 4, 9, 16, . . . ) to subtract from the \n" \
               "value, provided the chosen square is not larger. After \n" \
               "subtracting, we have a new value and the next player \n" \
               "chooses a square to ubtract from it. Play continues\n" \
               " to alternate between the two players until no moves are\n" \
               " possible. Whoever is about to play at that point loses!"

    def is_over(self, state):
        """
        Return whether or not this game is over.

        :return: True if the game is over, False otherwise.
        :rtype: bool
        """
        return state.current_total == 0

    def is_winner(self, player):
        """
        Return whether player has won the game.
        """
        return (self.current_state.get_current_player_name() != player
                and self.is_over(self.current_state))

    def str_to_move(self, string):
        """
        Return the move that string represents. If string is not a move,
        return an invalid move.
        """
        if not string.strip().isdigit():
            return -1

        return int(string.strip())
コード例 #5
0
    def __init__(self, is_p1_turn: bool) -> None:
        """Initialize a new subtract sqaure game

        Extends Game.__init__

        ===New Attributes===
        current_value - the current value of a subtract square game
        current_state - an instance of SubtractSquareState class

        Assume starting_value is a non-negative whole number
        """
        super().__init__(is_p1_turn)
        self.current_value = int(input("Type a non-negative whole number " +
                                       "as the starting value of the game: "))
        self.current_state = SubtractSquareState(self.current_player,
                                                 self.current_value)
コード例 #6
0
class SubtractSquare:
    """
    class game SubtractSquare
    """
    current_state: SubtractSquareState

    def __init__(self, is_p1_turn: bool) -> None:
        """
        initialize a new subtractsquare game

        #A docstring example is not needed for any functions or methods that
        use input() or random.
        """
        self.current_state = SubtractSquareState(is_p1_turn)

    def __eq__(self, other: Any) -> bool:
        """
        return whether self is equivalent to other

        # A docstring example is not needed for any functions or methods that
        use input() or random.
        """
        return (type(self) == type(other) and self.current_state.is_game_over
                == other.current_state.is_game_over
                and self.current_state.starting_number
                == other.current_state.starting_number
                and self.current_state.is_p1_turn
                == other.current_state.is_p1_turn)

    def __str__(self) -> str:
        """
        return a str representation of self

        # A docstring example is not needed for any functions or methods that
        use input() or random.
        """
        return "The current player is: {}, and the current value is: " \
               "{}".format(self.current_state.get_current_player_name(),
                           self.current_state.starting_number)

    def get_instructions(self) -> str:
        """
        reutrn instruction of game for players

        # A docstring example is not needed for any functions or methods that
        use input() or random.
        """
        instruction = 'Players take turns subtracting square numbers ' \
                      'from the starting number. The winner is the person ' \
                      'who subtracts to 0'
        return instruction

    def is_over(self, current_state: SubtractSquareState) -> bool:
        """
        return whether this game is over

        # A docstring example is not needed for any functions or methods that
        use input() or random.
        """
        return current_state.is_game_over

    def str_to_move(self, move: str) -> int:
        """
        a method whcih converts a string into a move

        # A docstring example is not needed for any functions or methods that
        use input() or random.
        """
        return int(move)

    def is_winner(self, player: str) -> bool:
        """
        return which player is the winner

        # A docstring example is not needed for any functions or methods that
        use input() or random.
        """
        if not self.current_state.is_game_over:
            return False
        else:
            if self.current_state.is_p1_turn:
                if player == 'p1':
                    return True
                return False
            else:
                if player == 'p2':
                    return True
                return False
コード例 #7
0
ファイル: limits.py プロジェクト: TianyuDu/csc148
def count_states(s1: SubtractSquareState) -> int:
    """ Return the number of game states reachable from here.
    """
    moves = s1.get_possible_moves()
    states = [s1.make_move(m) for m in moves]
    return 1 + sum([count_states(x) for x in states])
コード例 #8
0
class SubtractSquareGame(Game):
    """
    Abstract class for a game to be played with two players.
    """
    def __init__(self, p1_starts):
        """
        Initialize this Game, using p1_starts to find who the first player is.

        :param p1_starts: A boolean representing whether Player 1 is the first
                          to make a move.
        :type p1_starts: bool
        """
        count = int(input("Enter the number to subtract from: "))
        self.current_state = SubtractSquareState(p1_starts, count)

    def get_instructions(self):
        """
        Return the instructions for this Game.

        :return: The instructions for this Game.
        :rtype: str
        """
        instructions = "Players take turns subtracting square numbers from" + \
            " the starting number. The winner is the person who subtracts to 0."
        return instructions

    def is_over(self, state):
        """
        Return whether or not this game is over.

        :return: True if the game is over, False otherwise.
        :rtype: bool
        """
        return state.current_total == 0

    def is_winner(self, player):
        """
        Return whether player has won the game.

        Precondition: player is 'p1' or 'p2'.

        :param player: The player to check.
        :type player: str
        :return: Whether player has won or not.
        :rtype: bool
        """
        return (self.current_state.get_current_player_name() != player
                and self.is_over(self.current_state))

    def str_to_move(self, string):
        """
        Return the move that string represents. If string is not a move,
        return an invalid move.

        :param string:
        :type string:
        :return:
        :rtype:
        """
        if not string.strip().isdigit():
            return -1

        return int(string.strip())