def test_wheel_timer(): task_data = Path(utils.project_root() / 'tasks' / 'part-0-mock' / 'wheels.desc').read_text() task = GridTask(Task.parse(task_data)) actionlist = ('QQDD' + 'F' + 'ZZZZZZZZZZZZZZZZZZZZ' + 'F' + 'ZZZZZZZZZZZZZZZZZZZZ' + 'ZZZZZZZZZ' + 'ZZZZZZZZZZZZZZZZZZZZ' + 'ZZZZZZZZZZZZZZZZZZZZ' + 'ZZZZZZZZZZ' + 'D') game = Game(task) for a in actionlist: game.apply_action(Action.simple(a)) solution = compose_actions(game.get_actions()) expected_score = game.finished() assert expected_score is None game = Game(task) for a in actionlist: game.apply_action(Action.simple(a)) game.apply_action(Action.WSAD('D')) solution = compose_actions(game.get_actions()) expected_score = game.finished() er = validate.run(task_data, solution) assert er.time is not None assert er.time == expected_score
def test_clone_errors(): actionlist = [Action.parse('DDDDDDDDAAAAAAWWWWWWWWWAACSSSSSSSS'), Action.parse('DDDDDLDZ')] try: run_cloned_game(actionlist) except InvalidActionException: return else: assert False
def test_clones_game(): act_str = 'DWADDDDDDDDWAASAAAAEB(1,0)AWWWWAWWWWCDDDDDDDDSSSAZZSFSDWA#CSSSEDDWAASSSSSDDWWW#SDDDDSDDSSSZZSSSAA'.split('#') actionlist = [Action.parse(s) for s in act_str] run_cloned_game(actionlist)
def test_error(actions): actions = Action.parse(actions) run_for_errors(actions)
def test_teleport(): actions = Action.parse('DWDDDDDDDWWAAWWDDWWAAAARASSSSAAASSWWWWWWWWDDSSSST(4,7)WWDDDD') run_one_bot_game(actions)
def test_drill_and_wheels(): actions = Action.parse('WWWFDDLWAQQWWDDSSAWDDQWQQSSSSQAAEEDDDQWWWW') run_one_bot_game(actions)
def test_building_manips(): actions = Action.parse('WB(0,1)WWWWWWWDDSSSSSSSADDDDDWWWWWWAAWWDDDDSSSSSSSSA') run_one_bot_game(actions)
def test_turns(): actions = Action.parse('DQWWWWWWWWEDDEWDDDDDADQSSSSSSSSEAAAAAEEWWWWWWEDDDESSSS') run_one_bot_game(actions)
def test_simple_moves(): actions = Action.parse('WWWWWWWWWDDSSSSSSSSSDDDDWWWWWWWWWAASSDDDDDWWAZZSSSSSSSSS') run_one_bot_game(actions)
def test_compose(): actions = [[Action.WSAD(c) for c in 'WSAD'] + [ Action.wait(), Action.turnCW(), Action.turnCCW(), Action.attach(1, -2), Action.wheels(), Action.drill(), ]] assert compose_actions(actions) == 'WSADZEQB(1,-2)FL' actions = [ [Action.WSAD(c) for c in 'WSAD'] + [Action.wait(), Action.turnCW(), Action.turnCCW()], [ Action.attach(1, -2), Action.wheels(), Action.drill(), ] ] assert compose_actions(actions) == 'WSADZEQ#B(1,-2)FL'
def interactive(task_number): task_data = utils.get_problem_raw(task_number) use_db = task_number <= 999 if use_db: conn = db.get_conn() cur = conn.cursor() cur.execute( ''' SELECT id, data FROM tasks WHERE name = %s ''', [f'prob-{task_number:03d}']) [task_id, task_data_db] = cur.fetchone() task_data_db = zlib.decompress(task_data_db).decode() assert task_data_db == task_data task = GridTask(Task.parse(task_data)) game = Game(task) score = None with contextlib.closing(Display(game)) as display: code, c = '', '' display.draw_initial(game) while not score: display.draw(game, f'lastchar = {code} {c!r}') code = display.stdscr.getch() action = None c = chr(code).upper() if c in '\x1B': break if c in Action.SIMPLE: action = Action.simple(c) # to perform complex action type it without spaces: B(1,-1) elif c in Action.PARAM: while c[-1] != ')': code = display.stdscr.getch() c = c + chr(code).upper() action = Action.parameterized(c) if display.current == 0: botcount = len(game.bots) if action: try: game.apply_action(action, display.current) except InvalidActionException as exc: display.draw_error(str(exc)) else: display.current = (display.current + 1) % botcount score = game.finished() if score is not None: print(f'Score: {score}') result = validate_replay(task_data, score, game.get_actions()) print(result) if use_db: submit_replay(conn, task_id, result) else: mock_solutions = utils.project_root( ) / 'outputs' / 'mock_solutions' mock_solutions.mkdir(parents=True, exist_ok=True) with mock_solutions / f'prob-{task_number}.sol' as fin: fin.write_text(result.solution)