def test_invalid_move_touching_side(self): self.game = bg.Game(bg.Player("A", 1), bg.Player("B", 2)) self.game.move(self.game.playerA, { "id": 3, "x": 3, "y": 4, "rotates": 1, "flip": False }) self.game.move(self.game.playerB, { "id": 1, "x": 9, "y": 9, "rotates": 0, "flip": False }) self.assertRaises( RuntimeError, lambda: self.game.move( self.game.playerA, { "id": 4, "x": 9, "y": 7, "rotates": 0, "flip": False }, ), )
def test_wrong_turn_order(self): self.game = bg.Game(bg.Player("A", 1), bg.Player("B", 2)) self.game.move(self.game.playerA, { "id": 3, "x": 3, "y": 4, "rotates": 11, "flip": False }) self.game.move(self.game.playerB, { "id": 1, "x": 9, "y": 9, "rotates": 0, "flip": False }) self.assertRaises( PermissionError, lambda: self.game.move( self.game.playerB, { "id": 4, "x": 9, "y": 7, "rotates": 0, "flip": False }, ), )
def test_skip_move_beginning_game(self): self.game = bg.Game(bg.Player("A", 1), bg.Player("B", 2, strat.random_bvalue_strategy)) self.game.move(self.game.playerA, None) self.game.move(self.game.playerB) self.game.move(self.game.playerA, None) self.game.move(self.game.playerB) print(self.game.board.show())
def _get_game(name, autogame=False): if autogame: player = bg.Player(name, 1) robot_player = bg.Player("Robot", 2, strat.random_bvalue_strategy_with_rotates) game = bg.Game(player, robot_player) ser_game = Game() ser_game.id = game.id ser_game.robot_game = True gid = game.id ser_game.persisted_game = dill.dumps(game).hex() ser_game.save() return game.id, game.state, player.input_for_JSON() else: with transaction.atomic(): if WaitingGame.objects.count() > 0: wg = WaitingGame.objects.earliest("created") ser_game = get_object_or_404(Game, pk=wg.gid) now = timezone.now() if (now - ser_game.last_active).total_seconds() < 10: game = dill.loads(bytes.fromhex(ser_game.persisted_game)) player = bg.Player(name, 2) game.add_player(player) ser_game.persisted_game = dill.dumps(game).hex() ser_game.save() wg.delete() return game.id, game.state, player.input_for_JSON() else: wg.delete() return _get_game(name) else: player = bg.Player(name, 1) game = bg.Game(player) wg = WaitingGame() wg.gid = game.id wg.save() ser_game = Game() ser_game.id = game.id ser_game.persisted_game = dill.dumps(game).hex() ser_game.save() return game.id, game.state, player.input_for_JSON()
def run(): game = bg.Game( bg.Player("A", 1, strat.random_bvalue_strategy_with_rotates), bg.Player("B", 2, strat.random_bvalue_strategy), ) while not game.finished: if game.next_A: game.move(game.playerA) else: game.move(game.playerB) print(game.show())
def test_get_game_result(self): self.game = bg.Game(bg.Player("A", 1), bg.Player("B", 2, strat.random_bvalue_strategy)) self.game.playerA.score = 5 self.game.playerB.score = 7 self.game.state = bg.GameState.FINISHED self.assertEqual( self.game.get_game_result_for_JSON(), { "winner": "2", "score": { "1": "5", "2": "7" } }, )
def test_succesful_moves_sequence_and_game_show(self): self.game = bg.Game(bg.Player("A", 1), bg.Player("B", 2)) self.game.move(self.game.playerA, { "id": 0, "x": 4, "y": 4, "rotates": 0, "flip": False }) self.game.move(self.game.playerB, { "id": 7, "x": 8, "y": 8, "rotates": 0, "flip": False }) self.game.move(self.game.playerA, { "id": 10, "x": 5, "y": 5, "rotates": 3, "flip": True }) self.game.move(self.game.playerB, { "id": 10, "x": 10, "y": 4, "rotates": 0, "flip": True }) self.game.move(self.game.playerA, { "id": 7, "x": 9, "y": 4, "rotates": 0, "flip": False }) self.game.move(self.game.playerB, { "id": 14, "x": 4, "y": 7, "rotates": 1, "flip": False }) self.assertMultiLineEqual( self.game.board.show(), """00000000000000 00000000000000 00000000000000 00000000000000 00001002000000 00000112200000 00000012000000 00000012000000 00000010220000 00001100220000 00001102000000 00002222000000 00000000000000 00000000000000 """, )