Commands are:
F L R G X
1 2 3 4 5
1: 2: 3: 4: 5:
.
Here is the code for the simulator (written in Python):
level_number = 2
program = "XFF"
levels = [
[
"XXXXXXXXXX",
"XC CX",
"X S X",
"X X",
"X E X",
"X X",
"X X",
"X X",
"XC CX",
"XXXXXXXXXX"
],
[
"XXXXXXXXXX",
"XCX CX",
"X S XX",
"X XOX X",
"X CXEXC X",
"X XXX X",
"X C X",
"XX X",
"XC XCX",
"XXXXXXXXXX"
],
[
"XXXXXXXXXX",
"XS X",
"X EX",
"X X",
"X X",
"X CEX",
"X X",
"X X",
"X E E EX",
"XXXXXXXXXX"
],
[
"XXXXXXXXXX",
"X X",
"X S X X",
"X C O X",
"X X X X",
"X O C C X",
"X X XX",
"X C E X",
"X X X",
"XXXXXXXXXX"
],
[
"XXXXXXXXXX",
"XSOCO X",
"XOO O X X",
"XC CO X",
"XOOOO X X",
"X X",
"X X XX X X",
"X X",
"XC OEX",
"XXXXXXXXXX"
],
[
"XXXXXXXXXX",
"XC------CX",
"X - OE- X",
"X - X-X- X",
"X -C O - X",
"X -XX- - X",
"X - OC- X",
"X - X-X- X",
"XC- OS-CX",
"XXXXXXXXXX"
],
[
"XXXXXXXXXX",
"X O X",
"X X",
"X CE X",
"XSX ECOX",
"X CE X",
"XO X",
"X X",
"X X",
"XXXXXXXXXX"
],
[
"XXXXXXXXXX",
"XOEX XCX",
"X X X XOX",
"X OX X XCX",
"X XOXOXOX",
"XO X X XCX",
"X X X XOX",
"X OX X XCX",
"XS X OX",
"XXXXXXXXXX"
],
[
" X",
" ------- X",
" - - X",
" - --- - X",
" - -S- - X",
" - - - - X",
" - - - X",
" ----- -CX",
" -EX",
"XXXXXXXXXX"
],
[
"XXXXXXXXXX",
"X OOOOOX",
"X X X X",
"X C X X X",
"X X XE C X",
"X SX X X",
"X X X X",
"X CX x X",
"X X",
"XXXXXXXXXX"
],
[
"XXXXXXXXXE",
"XXCXSXCX ",
"XXOX XOX X",
"XX X ",
"XX.X X.XX ",
"XX X ",
"XXOX XOX X",
"XXCX XCX ",
"XXXX XXXX ",
"XC "
],
[
"XXXXXXXXXX",
"X - SX",
"XO-O - --X",
"X -O- O X",
"X-O- -- X",
"X- - O X",
"XO- - -OX",
"X---O- - X",
"XE C -C-CX",
"XXXXXXXXXX"
]
]
UP, RIGHT, DOWN, LEFT = [ 0, 1, 2, 3 ]
d = DOWN
level = 'X' * 11 + 'X'.join(levels[level_number - 1]) + 'X' * 11
p = level.find('S')
if p == -1:
raise "No start position"
level = level.replace('S', ' ')
pc = 0
bonus = 0
import sys
stack = []
commands_used = len(program) - program.count("'")
commands = 0
while True:
commands += 1
if commands > 10000:
raise "Too many commands - inifinite loop?"
if pc == len(program):
raise "End of program"
c = program[pc]
pc += 1
if c in "12345":
stack += [pc]
i = program.find("'" + c)
if i != -1:
pc = i + 2
elif c == "F":
next_p = p + [-11, 1, 11 ,-1][d]
next = level[next_p]
if next == "E":
score = 40 - commands_used + 10 * bonus
print "Exit reached: Score = %s, commands = %s" % (score, commands)
sys.exit(0)
if next in " C":
p = next_p
elif c == "L":
d = (d + 3) % 4
elif c == "R":
d = (d + 1) % 4
elif c == "G":
if level[p] == 'C':
bonus += 1
level = level[:p] + ' ' + level[p+1:]
elif c == ".":
if stack:
pc = stack.pop()
else:
pc = 0
elif c == "'":
pc += 1
elif c == "X":
next_p = p
while True:
next_p = next_p + [-11, 1, 11 ,-1][d]
next = level[next_p]
if next in "XOC":
break
if next in 'OC':
level = level[:next_p] + ' ' + level[next_p+1:]
else:
raise "Unknown command: " + c
No comments:
Post a Comment