class TestModel_EmptyModel(unittest.TestCase): def setUp(self): self.model = ConnectFourModel(PubSub()) def test_get_num_rows_when_no_board(self): self.assertIsNone(self.model.get_num_rows()) def test_get_num_columns_when_no_board(self): self.assertIsNone(self.model.get_num_columns()) def test_get_num_to_win_when_no_board(self): self.assertIsNone(self.model.get_num_to_win()) def test_get_num_players_when_none(self): self.assertEqual(self.model.get_num_players(), 0) def test_get_current_player_when_none(self): self.assertIsNone(self.model.get_current_player()) def test_used_colors_when_none(self): self.assertEqual(len(self.model.used_colors), 0) def test_add_max_players(self): num = len(Color) for index, name in enumerate(string.ascii_lowercase[:num]): self.model._add_player(name, Color(index)) self.assertEqual(len(self.model.used_colors), num) self.assertEqual(self.model.get_num_players(), num)
def __init__(self, room): self.room = room self.pubsub = PubSub() self.model = ConnectFourModel(self.pubsub) self._create_subscriptions()
class RoomState(): """Room-specific state, including model and model event handlers.""" def __init__(self, room): self.room = room self.pubsub = PubSub() self.model = ConnectFourModel(self.pubsub) self._create_subscriptions() def _create_subscriptions(self): responses = { ModelAction.board_created: self.on_board_created, ModelAction.player_added: self.on_player_added, ModelAction.player_removed: self.on_player_removed, ModelAction.game_started: self.on_game_started, ModelAction.next_player: self.on_next_player, ModelAction.try_again: self.on_try_again, ModelAction.color_played: self.on_color_played, ModelAction.game_won: self.on_game_won, ModelAction.game_draw: self.on_game_draw, } for action, response in responses.iteritems(): self.pubsub.subscribe(action, response) def on_player_added(self, player): socketio.emit('playerAdded', { 'player': player.get_json(), }, room=self.room) def on_player_removed(self, player): socketio.emit('playerRemoved', { 'player': player.get_json(), }, room=self.room) def on_board_created(self, board): socketio.emit('boardCreated', { 'board': board.get_json(), }, room=self.room) def on_game_started(self): socketio.emit('gameStarted', {}, room=self.room) def on_next_player(self, player): socketio.emit('nextPlayer', { 'player': player.get_json(), }, room=self.room) def on_try_again(self, player, reason): socketio.emit('tryAgain', { 'player': player.get_json(), 'reason': reason.name, }, room=self.room) def on_color_played(self, color, position): socketio.emit('colorPlayed', { 'color': color.name, 'position': position, }, room=self.room) def on_game_won(self, winner, winning_positions): socketio.emit('gameWon', { 'winner': winner.get_json(), 'players': self.model.get_json_players(), 'winningPositions': list(sorted(winning_positions)), }, room=self.room) def on_game_draw(self): socketio.emit('gameDraw', { 'players': self.model.get_json_players(), }, room=self.room)
#!/usr/bin/env python import argparse from connectfour.model import ConnectFourModel from connectfour.pubsub import PubSub from connectfour.gui.view import GUIView from connectfour.logger.view import LogView parser = argparse.ArgumentParser( description='Play Connect Four with a GUI.') parser.add_argument('--log', dest='log', action='store_true', help='Log actions to the console') args = parser.parse_args() pubsub = PubSub() model = ConnectFourModel(pubsub) if args.log: log_view = LogView(pubsub) gui_view = GUIView(pubsub, model)
def setUp(self): self.model = ConnectFourModel(PubSub())
def create_two_player_model(): model = ConnectFourModel(PubSub()) model._add_player(P0_NAME, P0_COLOR) model._add_player(P1_NAME, P1_COLOR) model._create_board(TEST_ROWS, TEST_COLUMNS, TEST_TO_WIN) return model