Example #1
0
class TestGameOnPlay(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.changePlayer(1, 0)
        self.game.changePlayer(2, 1)

    def test_game_is_started(self):
        self.assertTrue(self.game.isStarted())

    def test_game_is_started_string(self):
        gameString = """\
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
"""
        self.assertEquals(str(self.game), gameString)

    def test_player1_drops_on_left(self):
        try:
            self.game.drop(0)
            gameString = """\
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
|O| | | | | | |
---------------
"""
            self.assertEquals(str(self.game), gameString)
        except:
            self.fail("Error on first drop.")

    def test_player1_drops_on_left_coordinates(self):
        try:
            self.game.drop(0)
            self.assertEquals(self.game.getLast(), (0, 5))
        except:
            self.fail("Error on first drop.")

    def test_players_drop_on_left_to_full(self):
        try:
            for i in xrange(6):
                self.game.drop(0)
            gameString = """\
---------------
|X| | | | | | |
---------------
|O| | | | | | |
---------------
|X| | | | | | |
---------------
|O| | | | | | |
---------------
|X| | | | | | |
---------------
|O| | | | | | |
---------------
"""
            self.assertEquals(str(self.game), gameString)
        except:
            self.fail("Error on dropping.")

    def test_players_drop_on_left_to_full_coordinates(self):
        try:
            for i in xrange(6):
                self.game.drop(0)
            self.assertEquals(self.game.getLast(), (0, 0))
        except:
            self.fail("Error on dropping.")

    def test_players_drop_on_left_to_over_full(self):
        try:
            for i in xrange(7):
                self.game.drop(0)
            self.fail("Error on dropping.")
        except NoMoreSlotsInColumn:
            gameString = """\
---------------
|X| | | | | | |
---------------
|O| | | | | | |
---------------
|X| | | | | | |
---------------
|O| | | | | | |
---------------
|X| | | | | | |
---------------
|O| | | | | | |
---------------
"""
            self.assertEquals(str(self.game), gameString)

    def test_player1_wins(self):
        try:
            for i in xrange(3):
                self.game.drop(0)
                self.game.drop(1)
            self.game.drop(0)
            self.fail("Error on player 1 winning.")
        except Player1Wins:
            gameString = """\
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
|O| | | | | | |
---------------
|O|X| | | | | |
---------------
|O|X| | | | | |
---------------
|O|X| | | | | |
---------------
"""
            self.assertEquals(str(self.game), gameString)

    def test_player2_wins(self):
        try:
            for i in xrange(3):
                self.game.drop(0)
                self.game.drop(1)
            self.game.drop(2)
            self.game.drop(1)
            self.fail("Error on player 2 winning.")
        except Player2Wins:
            gameString = """\
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| |X| | | | | |
---------------
|O|X| | | | | |
---------------
|O|X| | | | | |
---------------
|O|X|O| | | | |
---------------
"""
            self.assertEquals(str(self.game), gameString)

    def test_game_end_draw(self):
        try:
            x = 0
            y = 1
            for i in xrange(3):
                for j in xrange(3):
                    self.game.drop(x)
                    self.game.drop(y)
                x += 2
                y += 2
            for i in xrange(3):
                self.game.drop(6)
                self.game.drop(0)
            x = 1
            y = 2
            for i in xrange(3):
                for j in xrange(3):
                    self.game.drop(x)
                    self.game.drop(y)
                x += 2
                y += 2
            self.fail("Failed to get correct error.")
        except GameHasBeenEnded:
            pass

    def test_one_end_case(self):
        try:
            self.game.drop(0)
            self.game.drop(1)
            self.game.drop(2)
            self.game.drop(3)
            self.game.drop(1)
            self.game.drop(2)
            self.game.drop(3)
            self.game.drop(3)
            self.game.drop(2)
            self.game.drop(1)
            self.game.drop(3)
            self.fail("Error on player 2 winning.\n%s" % self.game)
        except Player1Wins:
            gameString = """\
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | |O| | | |
---------------
| |X|O|X| | | |
---------------
| |O|X|O| | | |
---------------
|O|X|O|X| | | |
---------------
"""
            self.assertEquals(str(self.game), gameString)
Example #2
0
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)