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)
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)
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")
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)