def test_isBunchEmpty_BunchIsEmpty_ReturnsTrue(self) :
        # Arrange
        tiles = []
        bunch = Bunch(tiles)

        # Act
        bunch.DealFromBunch(144)
        result = bunch.IsBunchEmpty()

        # Assert
        self.assertTrue(result) 
    def test_IsBunchEmpty_BunchNotEmpty_ReturnsFalse(self) :
        # Arrange
        tiles = []
        tileA = Tile('A', 1, 1, 1)
        tileB = Tile('B', 2, 2, 3)
        tiles.append(tileA)
        tiles.append(tileB)
        bunch = Bunch(tiles)

        # Act
        result = bunch.IsBunchEmpty()

        # Assert
        self.assertFalse(result)
Exemple #3
0
class Game:
    def __init__(self, heuristic):
        self.bunch = Bunch()
        handTiles = self.bunch.DealFromBunch(15)
        self.hand = Hand("BRI", handTiles)
        self.bunch.DealFromBunch(15)
        self.board = Board()
        self.board.PrintBoard()

        self.concurrentExceptions = 0
        self.bri = BRI(heuristic)
        self.time = 1000
        self.timer = self.time  #nanoseconds

    def IsGoalState(self):
        print("Goal State:", self.bunch.IsBunchEmpty(),
              self.hand.IsHandEmpty())
        return self.bunch.IsBunchEmpty() and self.hand.IsHandEmpty()

    def IsTimeOut(self):
        print("Time out:", self.timer)
        return self.timer <= 0

    def IsEndState(self):
        return self.IsGoalState() or self.IsTimeOut(
        ) or self.concurrentExceptions > 5

    # also should consider timer for BRI i.e. cap time spent calculating a move
    # though maybe we won't need that actually
    def Play(self):
        timeStart = time.time()
        playedWords = []
        for t in self.hand.PeekHand():
            print(t.GetLetter(), end=" ")
        print()
        while not self.IsEndState():
            try:
                word, anchor, anchorIndex, direction = self.bri.FindBestMove(
                    self.hand, self.board)
                playedWords.append(word.GetString())

                self.board.PlaceWord(word, anchor, self.hand, anchorIndex,
                                     direction)
                self.concurrentExceptions = 0
                self.board.PrintBoard()

                print()
                print(playedWords)
                print()
                print("Hand Score: ", self.hand.GetScore())
                print("Bunch Score: ", self.bunch.ScoreBunch())
                print("Total Score: ",
                      self.hand.GetScore() + self.bunch.ScoreBunch())
                print("Items left in bunch: ", len(self.bunch.GetBunch()))

            except Exception as ex:
                #print("\t Error trying to get best move")
                print("\t", ex)
                self.concurrentExceptions += 1

            self.hand.AddTilesToHand(self.bunch.Peel())
            for t in self.hand.PeekHand():
                print(t.GetLetter(), end=" ")
            print()

            timeDiff = time.time() - timeStart
            print("Time:", timeDiff)
            self.timer = self.time - timeDiff

        if self.IsGoalState():
            print("Goal achieved! BRI is the winner!")

        if self.IsTimeOut():
            print("Game timed out! Sorry, try harder next time")

        handScore = self.hand.GetScore()
        bunchScore = self.bunch.ScoreBunch()
        print("Hand score: ", handScore)
        print("Bunch score: ", bunchScore)
        print("Total score: ", handScore + bunchScore)
        if self.concurrentExceptions > 5:
            print("Too many exceptions thrown")

        print("Words played were:", playedWords)

        results = []
        for t in self.hand.PeekHand():
            results.append(t.GetLetter())

        return [handScore + bunchScore, results]