def game_tile_first(dir_player, tile_player, board): """Plays a game on board when tile_player is the first player""" board2 = [board[i].copy() for i in range(rules.SIZE)] if not dir_player(board): assert rules.game_over(board) is True i = 0 while tile_player(board2) is not None: rules.move_tile(tile_player(board2), board2) dir = dir_player(board2) i += 1 if dir is not None: board2 = rules.move_dir(dir, board2) else: assert rules.game_over(board2) is True return 2**rules.max_tile(board2), i
def move_tile(move): global player, num_tiles num_tiles += 1 rules.move_tile(move, board) tiles[move[0]][move[1]].update_from_board() if rules.game_over(board): player = GAME_OVER else: player = PLAY_DIR run_next_player()
def game_direction_first(dir_player, tile_player, board): i = 0 while not rules.game_over(board): direction = dir_player(board) assert (direction is not None) #On peut jouer assert (0 <= direction < 4) #On ne peut jouer que 4 directions assert (rules.move_dir_possible(direction, board)) board = rules.move_dir(direction, board) tuile = tile_player(board) assert (tuile is not None) rules.move_tile(tuile, board) i += 1 return (1 << rules.max_tile(board), i) #1 << a is 2 ** a
def test_move_dir_possible(direction, board, expected): if not STAGE_SLIDE_IS_OVER: assert_equal(rules.move_dir_possible(direction, board), expected) if expected: start_test("game not over", INDENT) assert not rules.game_over(board) return start_test("mimicking with rules.move_dir", INDENT) xboard = [board[i].copy() for i in range(rules.SIZE)] res = rules.move_dir(direction, xboard) if expected: if res == xboard: print("Error of rules.slide on input", xboard) print("Output is the same while it should be different") assert res != xboard elif res is not xboard: print("Error of rules.slide on input", xboard) print("Output is ", xboard) assert xboard is board start_test("purity", INDENT) assert_equal(board, xboard)
# for d in (rules.UP, rules.DOWN): vt.start_test('possible move dir on ' + rules.DIR_NAME[d] + ' (XFULLBOARD)') vt.test_move_dir_possible(d, myboard, True) # for d in (rules.LEFT, rules.RIGHT): vt.start_test('impossible move dir on ' + rules.DIR_NAME[d] + ' (XFULLBOARD)') vt.test_move_dir_possible(d, myboard, False) test_move_dir_possible() vt.start_test('game.over') assert rules.game_over(rules.FULLBOARD) vt.valid_current_stage("move_dir_possible + game_over") # ---- STAGE slide ----- vt.start_test('slide with zero tile') vt.test_slide([0, 0, 0, 0], [0, 0, 0, 0]) vt.start_test('slide with one tile (pos 0)') vt.test_slide([4, 0, 0, 0], [4, 0, 0, 0]) vt.start_test('slide with one tile (pos 1)') vt.test_slide([0, 1, 0, 0], [1, 0, 0, 0]) vt.start_test('slide with one tile (pos 2)')