예제 #1
0
    def test_AddTilesToHand_ValidTiles_TilesInHand(self):
        # Arrange
        playerName = "Player One"
        tiles = []
        hand = Hand(playerName, tiles)

        letterA = 'A'
        valueA = 1
        frequencyA = 1
        primeNumberA = 1
        tileA = Tile(letterA, valueA, frequencyA, primeNumberA)

        letterB = 'B'
        valueB = 2
        frequencyB = 1
        primeNumberB = 1
        tileB = Tile(letterB, valueB, frequencyB, primeNumberB)

        expected = [tileA, tileB]

        # Act
        hand.AddTilesToHand(expected)
        result = hand.PeekHand()

        # Assert
        self.assertEqual(result, expected)
예제 #2
0
    def test_PeekHand_EmptyHand_ReturnsFalse(self):
        # Arrange
        playerName = "Player One"
        tiles = []
        hand = Hand(playerName, tiles)

        # Act
        result = hand.PeekHand()

        # Assert
        self.assertFalse(result)
예제 #3
0
    def test_RemoveTileFromHand_TileInHand_TileRemoved(self):
        # Arrange
        playerName = "Player One"
        tiles = []
        letter = 'A'
        value = 1
        frequency = 1
        primeNumber = 1
        tile = Tile(letter, value, frequency, primeNumber)
        tiles.append(tile)
        hand = Hand(playerName, tiles)

        beforeRemove = hand.PeekHand()
        self.assertIn(tile, beforeRemove)

        # Act
        hand.RemoveTileFromHand(tile)
        result = hand.PeekHand()

        # Assert
        self.assertNotIn(tile, result)
예제 #4
0
    def ScoreWord(self, wordToPlay, hand):
        handTiles = hand.PeekHand()
        newHand = Hand("Test", handTiles)
        for tile in wordToPlay.GetTiles():
            newHand.RemoveTileFromHand(tile)

        handTiles = newHand.PeekHand()

        word = Word(handTiles)
        words = Words()
        result = words.WordSearch(word).GetDict()

        count = 0
        for value in result.values():
            count += len(value)
        return math.sqrt(count) / 10
예제 #5
0
    def test_AddTileToHand_ValidTile_TileInHand(self):
        # Arrange
        playerName = "Player One"
        tiles = []
        hand = Hand(playerName, tiles)

        letter = 'A'
        value = 1
        frequency = 1
        primeNumber = 1
        tile = Tile(letter, value, frequency, primeNumber)

        # Act
        hand.AddTileToHand(tile)
        result = hand.PeekHand()

        # Assert
        self.assertIn(tile, result)
예제 #6
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]