示例#1
0
 def test_create_empty(self):
     bag = LetterBag()
     self.assertTrue(bag.contentCount() == 0,
                     "empty bag contentCount() is zero")
     try:
         l = bag.remove('c')
         self.assertTrue(False, "able to remove letter from empty bag")
     except Exception, e:
         self.assertTrue(True, "exception trying to remove from empty bag")
示例#2
0
 def test_create_empty(self):
     bag = LetterBag()
     self.assertTrue(
         bag.contentCount() == 0,
         "empty bag contentCount() is zero")
     try:
     	l = bag.remove('c')
     	self.assertTrue(False, "able to remove letter from empty bag")
     except Exception, e:
     	self.assertTrue(True, "exception trying to remove from empty bag")
示例#3
0
  def testShortGame(self):
    self.setUp()

    repository = GameStateRepository()

    joe = User.create('joe', '*****@*****.**')
    steve = User.create('steve', '*****@*****.**')
    v = PrintView()

    game = GameState.create()
    game.addPlayer(joe)
    game.addPlayer(steve)
    for p in game.players:
      p.bag = LetterBag.fromString('catgegae')
    game.start()
    self.assertTrue(game.leader().score == 0, "leader score==0")

    repository.register(game)
    id = repository.id(game)
    self.assertTrue(id != None, 'registering with repository set game id')

    game2 = repository.findById(id)

    self.assertTrue(game2.leader().score == 0, "leader score==0")

    game2.playWord(joe,0,0,True,'cat')
    repository.update(game2)

    game3 = repository.findById(id)     # same id retrieves game2 update
    self.assertTrue(game3.leader().score == 4, "same id retrieves update")

    self.tearDown()
示例#4
0
 def test_removeRandom(self):
     letters = 'thequickfoxtheolddog'
     total = len(letters)
     bag = LetterBag.fromString(letters)
     self.assertTrue(bag.contentCount() == total,
                     '{} letters in {}'.format(total, letters))
     bag2 = bag.removeRandom(7)
     expectedRemaining = total - 7
     self.assertTrue(bag.contentCount() == expectedRemaining,
                     '{} left after removing 7'.format(expectedRemaining))
     bag3 = bag.removeRandom(expectedRemaining)
     self.assertTrue(bag.contentCount() == 0,
                     'none left after removing all')
     bag2.addAll(bag3)
     for i in range(total):
         bag2.remove(letters[i])
     self.assertTrue(bag2.contentCount() == 0, 'found all letters in bag2')
示例#5
0
    def testScoreOnlyWhenAddingLetters(self):
        joe = self.joe
        steve = self.steve
        v = PrintView()
        game = self.game

        for p in game.players:
            p.bag = LetterBag.fromString('catgegae')
        self.assertTrue(game.leader().score == 0, "leader score==0")

        game.playWord(joe, 0, 0, True, 'cat')
        self.assertTrue(game.scoreForUser(joe) == 4, "score for cat is 4")
        try:
            game.playWord(steve, 0, 0, True, 'cat')
            self.assertTrue(
                False, 'playing word already played should raise exception')
        except ValueError, e:
            self.assertTrue(
                True, 'playing word already played raised error: {}'.format(e))
示例#6
0
    def testShortGame(self):
        joe = self.joe
        steve = self.steve
        v = PrintView()
        game = self.game

        for p in game.players:
            p.bag = LetterBag.fromString('catgegae')
        self.assertTrue(game.leader().score == 0, "leader score==0")

        game.playWord(joe, 0, 0, True, 'cat')
        game.playWord(steve, 0, 0, False, 'cage')
        game.playWord(joe, 0, 2, True, 'gag')
        game.playWord(steve, 2, 2, False, 'gate')
        game.skipTurn(joe)
        game.skipTurn(steve)
        v.showGameState(game)
        self.assertTrue(game.gameOver(), "game is over")
        self.assertTrue(game.leader().player.name == 'steve',
                        "steve is the winner")
        self.assertTrue(game.leader().score == 13, "winner score is 13")
示例#7
0
  def testScoreOnlyWhenAddingLetters(self):
    joe = self.joe
    steve = self.steve
    v = PrintView()
    game = self.game

    for p in game.players:
      p.bag = LetterBag.fromString('catgegae')
    self.assertTrue(game.leader().score == 0, "leader score==0")

    game.playWord(joe,0,0,True,'cat')
    self.assertTrue(game.scoreForUser(joe) == 4, "score for cat is 4")
    try:
        game.playWord(steve,0,0,True,'cat')
        self.assertTrue(
            False,
            'playing word already played should raise exception')
    except ValueError, e:
        self.assertTrue(
            True,
            'playing word already played raised error: {}'.format(e))
示例#8
0
 def test_removeRandom(self):
     letters = 'thequickfoxtheolddog'
     total = len(letters)
     bag = LetterBag.fromString(letters)
     self.assertTrue(
         bag.contentCount() == total,
         '{} letters in {}'.format(total,letters))
     bag2 = bag.removeRandom(7)
     expectedRemaining = total - 7
     self.assertTrue(
         bag.contentCount() == expectedRemaining,
         '{} left after removing 7'.format(expectedRemaining))
     bag3 = bag.removeRandom(expectedRemaining)
     self.assertTrue(
         bag.contentCount() == 0,
         'none left after removing all')
     bag2.addAll(bag3)
     for i in range(total):
         bag2.remove(letters[i])
     self.assertTrue(
         bag2.contentCount() == 0,
         'found all letters in bag2')
示例#9
0
  def testShortGame(self):
    joe = self.joe
    steve = self.steve
    v = PrintView()
    game = self.game

    for p in game.players:
      p.bag = LetterBag.fromString('catgegae')
    self.assertTrue(game.leader().score == 0, "leader score==0")

    game.playWord(joe,0,0,True,'cat')
    game.playWord(steve,0,0,False,'cage')
    game.playWord(joe,0,2,True,'gag')
    game.playWord(steve,2,2,False,'gate')
    game.skipTurn(joe)
    game.skipTurn(steve)
    v.showGameState(game)
    self.assertTrue(game.gameOver(), "game is over")
    self.assertTrue(
        game.leader().player.name == 'steve',
        "steve is the winner")
    self.assertTrue(game.leader().score == 13, "winner score is 13")
示例#10
0
 def test_letterAtIndex(self):
     bag = LetterBag.fromString('aaaccez')
     self.assertTrue(bag.letterAtIndex(0) == 'a', "bag has an 'a' at 0")
     self.assertTrue(bag.letterAtIndex(2) == 'a', "bag has an 'a' at 2")
     self.assertTrue(bag.letterAtIndex(6) == 'z', "bag has an 'z' at 6")
     self.assertTrue(bag.letterAtIndex(5) == 'e', "bag has an 'e' at 5")
示例#11
0
 def test_simple_example(self):
     bag = LetterBag()
     bag.add('a')
     self.assertTrue(bag.contentCount() == 1,
                     "size is one after adding one")
     bag.add('z')
     self.assertTrue(bag.contentCount() == 2, "2 after adding second")
     bag.add('z')
     self.assertTrue(bag.contentCount() == 3, "3 after adding third")
     try:
         l = bag.remove('c')
         self.assertTrue(False, "able to remove letter not in bag")
     except Exception, e:
         self.assertTrue(True,
                         "exception trying to remove letter not in bag")
示例#12
0
 def restoreTransients(self, state):
     """set transient values from persistent fields"""
     state.bagOfLetters = LetterBag.fromString(state.letters)
     state.boardContent = list(state.board)  # list support random access
     return state
示例#13
0
 def test_simple_example(self):
     bag = LetterBag()
     bag.add('a')
     self.assertTrue(
         bag.contentCount() == 1,
         "size is one after adding one")
     bag.add('z')
     self.assertTrue(bag.contentCount() == 2, "2 after adding second")
     bag.add('z')
     self.assertTrue(bag.contentCount() == 3, "3 after adding third")
     try:
         l = bag.remove('c')
         self.assertTrue(False, "able to remove letter not in bag")
     except Exception, e:
         self.assertTrue(
             True,
             "exception trying to remove letter not in bag")
示例#14
0
 def restoreTransients(self, state):
     """set (derive) transient values from persistent fields"""
     state.game = state.gameKey.get()
     state.player = state.userKey.get()
     state.bag = LetterBag.fromString(state.letters)
     return state
示例#15
0
 def test_letterAtIndex(self):
     bag = LetterBag.fromString('aaaccez')
     self.assertTrue( bag.letterAtIndex(0) == 'a', "bag has an 'a' at 0")
     self.assertTrue( bag.letterAtIndex(2) == 'a', "bag has an 'a' at 2")
     self.assertTrue( bag.letterAtIndex(6) == 'z', "bag has an 'z' at 6")
     self.assertTrue( bag.letterAtIndex(5) == 'e', "bag has an 'e' at 5")
示例#16
0
 def restoreTransients(self, state):
     """set transient values from persistent fields"""
     state.bagOfLetters = LetterBag.fromString(state.letters)
     state.boardContent = list(state.board)  # list support random access
     return state
示例#17
0
 def restoreTransients(self, state):
     """set (derive) transient values from persistent fields"""
     state.game = state.gameKey.get()
     state.player = state.userKey.get()
     state.bag = LetterBag.fromString(state.letters)
     return state