Skip to content
Browse files

Slight Game object refactor:

- Add "in_progress" flag attr
- Change api to add players after init
  • Loading branch information...
1 parent 5ec1e0c commit 70d4695f507e181f87d164aa59a092a29e8a1308 @caseman committed Oct 3, 2011
Showing with 50 additions and 7 deletions.
  1. +13 −3 gravita/game.py
  2. +37 −4 gravita/test/test_game.py
View
16 gravita/game.py
@@ -13,14 +13,24 @@
class Game(object):
- def __init__(self, players, map):
- assert len(players) == 2, 'Only two players are currently supported'
- self.players = players
+ def __init__(self, map):
+ self.players = []
self.map = map
self.turn = 0
+ @property
+ def in_progress(self):
+ return self.turn > 0
+
+ def add_player(self, player):
+ assert len(self.players) < 2, 'Only two players are currently supported'
+ assert not self.in_progress, 'Cannot add player, game already started'
+ assert player not in self.players, 'Player already added to this game'
+ self.players.append(player)
+
def begin_turn(self):
"""Begin a new turn in the game"""
+ assert len(self.players) == 2, 'Too few players to start game'
self.turn += 1
for player in self.players:
player.begin_turn()
View
41 gravita/test/test_game.py
@@ -4,25 +4,58 @@ class GameTestCase(unittest.TestCase):
def test_init_game(self):
from gravita.game import Game
- players = ('foo', 'bar')
map = object()
- game = Game(players, map)
- self.assertEqual(game.players, players)
+ game = Game(map)
+ self.assertEqual(game.players, [])
self.assert_(game.map is map)
self.assertEqual(game.turn, 0)
+ self.assertFalse(game.in_progress)
+
+ def test_add_player(self):
+ from gravita.game import Game
+ map = object()
+ game = Game(map)
+ self.assertEqual(game.players, [])
+ p1 = MockPlayer()
+ p2 = MockPlayer()
+ game.add_player(p1)
+ self.assertEqual(game.players, [p1])
+ self.assertRaises(Exception, game.add_player, p1)
+ self.assertEqual(game.players, [p1])
+ game.add_player(p2)
+ self.assertEqual(game.players, [p1, p2])
+ self.assertRaises(Exception, game.add_player, MockPlayer())
+
+ def test_begin_turn_before_ready(self):
+ from gravita.game import Game
+ map = object()
+ game = Game(map)
+ self.assertRaises(Exception, game.begin_turn)
+ self.assertEqual(game.turn, 0)
+ game.add_player(MockPlayer())
+ self.assertRaises(Exception, game.begin_turn)
+ self.assertEqual(game.turn, 0)
def test_begin_turn(self):
from gravita.game import Game
p1, p2 = players = (MockPlayer(), MockPlayer())
- game = Game(players, None)
+ game = Game(None)
+ game.add_player(p1)
+ game.add_player(p2)
self.assertEqual(p1.turn, 0)
self.assertEqual(p2.turn, 0)
+ self.assertEqual(game.turn, 0)
+ self.assertFalse(game.in_progress)
game.begin_turn()
self.assertEqual(p1.turn, 1)
self.assertEqual(p2.turn, 1)
+ self.assertEqual(game.turn, 1)
+ self.assertTrue(game.in_progress)
game.begin_turn()
self.assertEqual(p1.turn, 2)
self.assertEqual(p2.turn, 2)
+ self.assertEqual(game.turn, 2)
+ self.assertTrue(game.in_progress)
class MockPlayer(object):

0 comments on commit 70d4695

Please sign in to comment.
Something went wrong with that request. Please try again.