コード例 #1
0
ファイル: TestGame.py プロジェクト: samitheberber/Tuqqna
class TestGameCreation(unittest.TestCase):

    def setUp(self):
        self.game = Game()

    def test_get_game_string(self):
        self.assertEquals(str(self.game), "")

    def test_create_the_game_with_board(self):
        self.game.setBoard(7,6)
        self.assertEquals(self.game.getWidth(), 7)
        self.assertEquals(self.game.getHeight(), 6)
        self.assertEquals(str(self.game), "")

    def test_add_new_player_in_game(self):
        player = "Test player"
        self.game.addPlayer(player)
        self.assertTrue(player in [p.getName() for p in self.game.getPlayers()])

    def test_add_few_more_players_in_game(self):
        try:
            for i in xrange(4):
                player = "Player no. %i" % i
                self.game.addPlayer(player)
                if player in [p.getName() for p in self.game.getPlayers()]:
                    continue
                else:
                    raise ValueError
        except ValueError:
            self.fail("Not every player added in game.")

    def test_set_player1(self):
        self.game.setBoard(7,6)
        testPlayer1 = "Test Player 1"
        self.game.addPlayer(testPlayer1)
        self.game.addPlayer("Test Player 2")
        self.game.addPlayer("Test Player 3")
        self.game.changePlayer(1, 0)
        self.assertEquals(self.game.getPlayer(1).getName(), testPlayer1)

    def test_set_player1_to_invalid_player(self):
        self.game.setBoard(7,6)
        self.game.addPlayer("Test Player 1")
        self.assertRaises(InvalidPlayerId, self.game.changePlayer, 1, 1)

    def test_set_player2(self):
        self.game.setBoard(7,6)
        testPlayer1 = "Test Player 1"
        self.game.addPlayer(testPlayer1)
        self.game.addPlayer("Test Player 2")
        self.game.addPlayer("Test Player 3")
        self.game.changePlayer(2, 0)
        self.assertEquals(self.game.getPlayer(2).getName(), testPlayer1)

    def test_set_player2_to_invalid_player(self):
        self.game.setBoard(7,6)
        self.game.addPlayer("Test Player 1")
        self.assertRaises(InvalidPlayerId, self.game.changePlayer, 2, 1)

    def test_set_invalid_player_number(self):
        self.game.setBoard(7,6)
        self.game.addPlayer("Test Player 1")
        self.assertRaises(InvalidPlayerNumber, self.game.changePlayer, 0, 0)

    def test_get_invalid_player_number(self):
        self.game.setBoard(7,6)
        self.assertRaises(InvalidPlayerNumber, self.game.getPlayer, 0)
コード例 #2
0
ファイル: TestGame.py プロジェクト: samitheberber/Tuqqna
class TestGamePlayerWinnings(unittest.TestCase):

    def setUp(self):
        self.game = Game()
        self.game.setBoard(7,6)
        self.game.addPlayer("Test Player1")
        self.game.addPlayer("Test Player2")
        self.game.addPlayer("Test Player3")

    def test_no_victories_at_start(self):
        for player in self.game.getPlayers():
            self.assertEquals(player.getVictories(), 0)
            self.assertEquals(player.getDefeats(), 0)

    def test_player1_has_one_victory_after_one_match(self):
        self.game.changePlayer(1, 0)
        self.game.changePlayer(2, 1)
        self.assertEquals(self.game.getPlayer(1).getVictories(), 0)
        try:
            for i in xrange(3):
                self.game.drop(0)
                self.game.drop(1)
            self.game.drop(0)
            self.fail("No winning :/")
        except Player1Wins:
            self.assertEquals(self.game.getPlayer(1).getVictories(), 1)
            self.assertEquals(self.game.getPlayer(2).getDefeats(), 1)

    def test_no_new_game_started(self):
        self.game.changePlayer(1, 0)
        self.game.changePlayer(2, 1)
        try:
            for i in xrange(3):
                self.game.drop(0)
                self.game.drop(1)
            self.game.drop(0)
            self.fail("No winning :/")
        except Player1Wins:
            self.assertRaises(AtFirstStartNewGame, self.game.drop, 0)

    def test_player1_has_two_victory_after_two_match(self):
        self.game.changePlayer(1, 0)
        self.game.changePlayer(2, 1)
        self.assertEquals(self.game.getPlayer(1).getVictories(), 0)
        self.assertEquals(self.game.getPlayer(2).getVictories(), 0)
        self.assertEquals(self.game.getPlayer(1).getDefeats(), 0)
        self.assertEquals(self.game.getPlayer(2).getDefeats(), 0)
        try:
            for i in xrange(3):
                self.game.drop(0)
                self.game.drop(1)
            self.game.drop(0)
            self.fail("No winning :/")
        except Player1Wins:
            self.assertEquals(self.game.getPlayer(1).getVictories(), 1)
            self.assertEquals(self.game.getPlayer(2).getVictories(), 0)
            self.assertEquals(self.game.getPlayer(1).getDefeats(), 0)
            self.assertEquals(self.game.getPlayer(2).getDefeats(), 1)
            self.game.newGame()
            try:
                for i in xrange(3):
                    self.game.drop(0)
                    self.game.drop(1)
                self.game.drop(0)
                self.fail("No winning :/")
            except Player1Wins:
                self.assertEquals(self.game.getPlayer(1).getVictories(), 2)
                self.assertEquals(self.game.getPlayer(2).getVictories(), 0)
                self.assertEquals(self.game.getPlayer(1).getDefeats(), 0)
                self.assertEquals(self.game.getPlayer(2).getDefeats(), 2)
            except Player2Wins:
                self.fail("Wrong Winner")
        except Player2Wins:
            self.fail("Wrong Winner")

    def test_player1_wins_once_and_player_two_wins_once(self):
        self.game.changePlayer(1, 0)
        self.game.changePlayer(2, 1)
        self.assertEquals(self.game.getPlayer(1).getVictories(), 0)
        self.assertEquals(self.game.getPlayer(2).getVictories(), 0)
        self.assertEquals(self.game.getPlayer(1).getDefeats(), 0)
        self.assertEquals(self.game.getPlayer(2).getDefeats(), 0)
        try:
            for i in xrange(3):
                self.game.drop(0)
                self.game.drop(1)
            self.game.drop(0)
            self.fail("No winning :/")
        except Player1Wins:
            self.assertEquals(self.game.getPlayer(1).getVictories(), 1)
            self.assertEquals(self.game.getPlayer(2).getVictories(), 0)
            self.assertEquals(self.game.getPlayer(1).getDefeats(), 0)
            self.assertEquals(self.game.getPlayer(2).getDefeats(), 1)
            self.game.newGame()
            try:
                for i in xrange(3):
                    self.game.drop(0)
                    self.game.drop(1)
                self.game.drop(2)
                self.game.drop(1)
                self.fail("No winning :/")
            except Player2Wins:
                self.assertEquals(self.game.getPlayer(1).getVictories(), 1)
                self.assertEquals(self.game.getPlayer(2).getVictories(), 1)
                self.assertEquals(self.game.getPlayer(1).getDefeats(), 1)
                self.assertEquals(self.game.getPlayer(2).getDefeats(), 1)
            except Player1Wins:
                self.fail("Wrong Winner")
        except Player2Wins:
            self.fail("Wrong Winner")
コード例 #3
0
ファイル: game.py プロジェクト: samitheberber/Tuqqna
class CliUIGame(object):
    """\
Game interface for cli ui.

Method names are enough describing.
"""

    def __init__(self):
        self._game = Game()
        self._coinPosition = None
        self._message = "Game started."

    def addPlayer(self, name):
        self._game.addPlayer(name)
        self._message = "Added %s." % name

    def getPlayers(self):
        return [p.getName() for p in self._game.getPlayers()]

    def getPlayer1(self):
        player = self._game.getPlayer(1)
        if player:
            return player.getName()
        else:
            return "(none)"

    def getPlayer2(self):
        player = self._game.getPlayer(2)
        if player:
            return player.getName()
        else:
            return "(none)"

    def hasPlayers(self):
        return self._game.getPlayer(1) and self._game.getPlayer(2)

    def setBoard(self, width, height):
        self._game.setBoard(width, height)
        self._message = "Set board to %ix%i." % (width, height)
        self._game.newGame()
        self._coinPosition = None

    def boardWidth(self):
        return self._game.getWidth()

    def boardHeight(self):
        return self._game.getHeight()

    def player1(self, name):
        if not name in self.getPlayers():
            self._message = "Not found %s." % name
        else:
            self._game.changePlayer(1, self.getPlayers().index(name))
            self._message = "Selected %s to player 1." % name

    def player2(self, name):
        if not name in self.getPlayers():
            self._message = "Not found %s." % name
        else:
            self._game.changePlayer(2, self.getPlayers().index(name))
            self._message = "Selected %s to player 2." % name

    def isStarted(self):
        if self._game.isStarted():
            self._message = "Game is started."
            return True
        else:
            return False

    def coinPosition(self):
        msg = self._message
        if self.isStarted() and self._coinPosition == None:
            self._coinPosition = int(self.boardWidth() / 2)
        self._message = msg
        return self._coinPosition

    def moveCoinLeft(self):
        if not self.isStarted():
            return
        position = self.coinPosition()-1
        if position >= 0:
            self._coinPosition = position
            self._message = ""
        else:
            self._message = "Can't move more to left."

    def moveCoinRight(self):
        if not self.isStarted():
            return
        position = self.coinPosition()+1
        if position < self.boardWidth():
            self._coinPosition = position
            self._message = ""
        else:
            self._message = "Can't move more to right."

    def dropCoin(self):
        msg = self._message
        if not self.isStarted():
            return
        self._message = msg
        try:
            self._game.drop(self.coinPosition())
            self._message = ""
        except Player1Wins:
            self._message = "Winner is %s." % self._game.getPlayer(1)
            raise Player1Wins
        except Player2Wins:
            self._message = "Winner is %s." % self._game.getPlayer(2)
            raise Player2Wins
        except GameHasBeenEnded:
            self._message = "Game ends draw."
            raise GameHasBeenEnded

    def coinLanded(self):
        return self._game.getLast()

    def latestMessage(self):
        return str(self._message)