예제 #1
0
    def __init__(
        self,
        size=4,  # standard Threes board size
        nTiles=9,  # standard Threes number of starting tiles
        board=[],  # empty or previous board
        deck=TileDeck(),  # new tile deck, or previous deck
        history=[],  # empty, or previous history
        nextTile=0,
    ):  # no tile, or previous tile

        """Creating the Threes board

        If passing in an old board position, that game will be recreated
        The tile deck will can also be recreated
        """

        if not board:
            # Starting a new game; ignore previous history ... etc
            self.board = _create_board(size)
            self.deck = TileDeck([], 3)

            # Populating a new board with start up tiles
            self.board, self.deck = _populate_board(self.board, self.deck, nTiles)

            self.nextTile = self.deck.get_next_tile()

            # Set up empty history, then set initial condition
            self.history = []

            # history formate is: move, resulting board, next tile)
            self.history.append(("start", self.board, self.nextTile))
            self.highestTile = 3

        else:
            """To consider: store all information in history
                            eliminate the need for old boards, decks ...
                            initialize a previous game in form of
                            board = history[1]
                            nextTile = history[2]
                            ... etc
                            For next major version?
            """

            # Passing in the old game; size, ntiles are all ignored
            self.board = board
            self.deck = TileDeck(deck.deck)
            self.history = history
            self.highestTile = _get_highest(self.board)

            # If old game information was incomplete
            if nextTile == 0:
                self.nextTile = self.deck.get_next_tile(0)

            else:
                self.nextTile = nextTile
예제 #2
0
class ThreesBoard(object):
    """The board, tiles and state of the game in Threes"""

    def __init__(
        self,
        size=4,  # standard Threes board size
        nTiles=9,  # standard Threes number of starting tiles
        board=[],  # empty or previous board
        deck=TileDeck(),  # new tile deck, or previous deck
        history=[],  # empty, or previous history
        nextTile=0,
    ):  # no tile, or previous tile

        """Creating the Threes board

        If passing in an old board position, that game will be recreated
        The tile deck will can also be recreated
        """

        if not board:
            # Starting a new game; ignore previous history ... etc
            self.board = _create_board(size)
            self.deck = TileDeck([], 3)

            # Populating a new board with start up tiles
            self.board, self.deck = _populate_board(self.board, self.deck, nTiles)

            self.nextTile = self.deck.get_next_tile()

            # Set up empty history, then set initial condition
            self.history = []

            # history formate is: move, resulting board, next tile)
            self.history.append(("start", self.board, self.nextTile))
            self.highestTile = 3

        else:
            """To consider: store all information in history
                            eliminate the need for old boards, decks ...
                            initialize a previous game in form of
                            board = history[1]
                            nextTile = history[2]
                            ... etc
                            For next major version?
            """

            # Passing in the old game; size, ntiles are all ignored
            self.board = board
            self.deck = TileDeck(deck.deck)
            self.history = history
            self.highestTile = _get_highest(self.board)

            # If old game information was incomplete
            if nextTile == 0:
                self.nextTile = self.deck.get_next_tile(0)

            else:
                self.nextTile = nextTile

    def swipe(self, move):
        """Same function for different swipes

        This will make recording keeping easier
        """

        direction = {"left": _swipe_left, "right": _swipe_right, "up": _swipe_up, "down": _swipe_down}

        copy_board = deepcopy(self.board)

        try:
            copy_board = direction[move](copy_board, self.nextTile)

        except KeyError:
            raise InValidMoveError

        if self.board == copy_board:
            # raise NoMovementError
            pass

        else:
            self.board = copy_board
            self.highestTile = _get_highest(self.board)
            self.nextTile = self.deck.get_next_tile(self.highestTile)

            # Histoyr formate is: move, resulting board, next tile
            self.history.append((move, self.board, self.nextTile))

    def gameOver(self):
        new_board = deepcopy(self.board)

        if (
            _swipe_left(new_board, 0) == self.board
            and _swipe_right(new_board, 0) == self.board
            and _swipe_up(new_board, 0) == self.board
            and _swipe_down(new_board, 0) == self.board
        ):
            return True

        return False

    def __eq__(self, other):
        return self.board == other.board and self.deck == other.deck and self.nextTile == other.nextTile