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 test_board_move_with_rotations(self): b = bg.Board() b.moves_count = 3 player = bg.Player("Name", 1) b.board[9][7] = 1 b.board[9][8] = 1 b.board[9][9] = 2 b.board[9][10] = 2 b.board[9][11] = 2 b.board[9][12] = 2 b.board[10][7] = 2 b.board[10][8] = 2 b.board[10][11] = 2 b.board[11][8] = 2 b.board[11][10] = 2 b.board[12][7] = 2 b.board[12][8] = 2 b.board[12][10] = 2 b.board[12][11] = 2 b.board[12][12] = 2 b.board[13][12] = 2 print(b.show()) blox = player.get_blox(10) blox.rotate(3) print(blox.show()) self.assertTrue(b._is_allowed(blox, 10, 9))
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 _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 test_board_move_block_on_border(self): b = bg.Board() b.moves_count = 3 player = bg.Player("Name", 1) b.board[10][8] = 1 b.board[10][9] = 1 b.board[10][10] = 1 print(b.show()) blox = player.get_blox(16) print(blox.show()) self.assertTrue(b._is_allowed(blox, 11, 7))
def test_board_move_not_allowed_touching_side(self): b = bg.Board() b.moves_count = 3 player = bg.Player("Name", 1) b.board[0][2] = 1 b.board[1][0] = 1 b.board[1][1] = 1 b.board[1][2] = 1 b.board[2][2] = 1 b.board[3][3] = 1 b.board[4][2] = 1 b.board[4][3] = 1 b.board[4][4] = 1 b.board[3][5] = 1 b.board[3][6] = 1 b.board[3][7] = 1 print(b.show()) blox = player.get_blox(13) print(blox.show()) self.assertFalse(b._is_allowed(blox, 1, 4))
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 """, )