Brainfuck in Python
Jump to navigation
Jump to search
It is possible to emulate Brainfuck in Python.
Code:
##### Initialization. ##### array = [0 for i in range(32767)] # 32k RAM. current_value = 0 # Pointer for array. ##### Actual code. ##### # Your commands here... #
To make the > command, do the following:
if current_value == 32767: # Wrap current_value = 0 else: current_value += 1 # Move right
To make the < command, do the following:
if current_value == 0: # Wrap current_value = 32767 else: current_value -= 1 # Move left
To make the + command, do the following:
if array[current_value] == 255: # Wrap array[current_value] = 0 else: array[current_value] += 1 # Increment
To make the - command, do the following:
if array[current_value] == 0: # Wrap array[current_value] = 255 else: array[current_value] -= 1 # Decrement
To make the , command, do the following:
array[current_value] = ord(input(' Enter exactly ONE character... >')) # Ask for input
To make the . command, do the following:
print(end=chr(array[current_value])) # Prints it out
To make the [ command, do the following:
while array[current_value] != 0: # Opens a loop
Also indent the items below.
To make the ] command, remove one layer of indentation.