Created
June 21, 2016 15:41
-
-
Save nazywam/d9b36e8bbd39c97225a38e064e6205b0 to your computer and use it in GitHub Desktop.
b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# > < v ^ - zmiana ruchu | |
moves = {'<':[-1, 0], '>':[1, 0], '^':[0, -1], 'V':[0, 1]} | |
# 1234567890 - push na stos | |
numbers = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9} | |
# +-*/% - operacje na 2 wartościach na górze stosu | |
operations = ['+', '-', '*', '/'] | |
# = - jeśli 2 górne wartości są równe to pomiń następną instrukcje | |
equals = '=' | |
# | - zamień 2 wartości na stosie | |
swap = '|' | |
# & - sklonuj wartość na górze stosu | |
clone = '#' | |
# . - wypisz pierwszą wartość ze stosu | |
write = '.' | |
# @ - zakończ program | |
finish = '@' | |
# hold values | |
stack = [] | |
board = """ | |
>9>#.1|-#0=V@ | |
__^________<_""" | |
board = board.strip().split('\n') | |
eFlag = False | |
crsX = 0 | |
crsY = 0 | |
spdX = 0 | |
spdY = 0 | |
def handleMove(move): | |
global spdX | |
global spdY | |
change = moves.get(move) | |
spdX = change[0] | |
spdY = change[1] | |
def handleNumber(number): | |
global stack | |
toPush = numbers.get(number) | |
stack.append(toPush) | |
def handleOperation(operation): | |
global stack | |
a = stack.pop() | |
b = stack.pop() | |
out = eval(str(a) + operation + str(b)) | |
stack.append(out) | |
def handleEquals(): | |
global eFlag | |
global stack | |
a = stack.pop() | |
b = stack.pop() | |
if(a == b): | |
eFlag = True | |
def handleSwap(): | |
global stack | |
a = stack.pop() | |
b = stack.pop() | |
stack.append(a) | |
stack.append(b) | |
def handleClone(): | |
global stack | |
a = stack.pop() | |
stack.append(a) | |
stack.append(a) | |
def handleWrite(): | |
global stack | |
a = stack.pop() | |
print(a) | |
def handleFinish(): | |
exit() | |
def handleTile(tile): | |
if(tile in moves): | |
handleMove(tile) | |
if(tile in numbers): | |
handleNumber(tile) | |
if(tile in operations): | |
handleOperation(tile) | |
if(tile == equals): | |
handleEquals() | |
if(tile == swap): | |
handleSwap() | |
if(tile == clone): | |
handleClone() | |
if(tile == write): | |
handleWrite() | |
if(tile == finish): | |
handleFinish() | |
# main loop | |
while True: | |
if(not eFlag): | |
handleTile(board[crsY][crsX]) | |
else: | |
eFlag = False | |
crsX += spdX | |
crsY += spdY | |
if(crsX >= len(board[0]) or crsX < 0 or crsY >= len(board) or crsY < 0): | |
print("Cursor out of board, exiting....") | |
exit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment