-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakthrough.py
128 lines (87 loc) · 2.82 KB
/
breakthrough.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from random import randint, choice
bfVocabulary = "+-><{}!?"
mapSize = 8
distance = 5 # less than map size
actorMap = {}
def generate_script_slots(dist):
ss = {}
length = (0 + dist) * dist // 2
for script in range(0, length): # get each individual length key to distance
ss[script] = ""
for command in range(script):
ss[command] += choice(bfVocabulary)
print(F"Generated {dist} scripts with a total of {length} commands")
return ss
# populate slots with brainFuck script
pass
script_slots = generate_script_slots(dist=distance)
# generate map slots
for i in range(mapSize):
actorMap[i] = randint(0, len(script_slots) - 1) # length of script to load
# write a bf compiler in python
def bf_compile(plain_text):
bfCodeToReturn = ""
for p in plain_text:
if p not in bfVocabulary:
continue
bfCodeToReturn += p
return bfCodeToReturn
# a text to code function
def bf_interperator(bf_script_with_comments, stepCap=50):
commandsForever = 0
ACTOR_VARIABLE = "Holy grail"
scopeStack = []
script_pointer = 0 # where in the bf code the main-thread is executing (code line:char)
actor_pointer = 0 # where on the actorMap the script is being executed on (buffer caret)
while script_pointer < len(bf_script_with_comments):
commandsForever += 1
char = bf_script_with_comments[actor_pointer]
if char == "+":
actorMap[actor_pointer] += 1
if char == "-":
actorMap[actor_pointer] -= 1
if char == ">":
actor_pointer += 1
if char == "<":
actor_pointer -= 1
if char == "!":
ACTOR_VARIABLE = actorMap[actor_pointer]
if char == "?":
actorMap[actor_pointer] = ACTOR_VARIABLE
if char == "{":
scopeStack.append(script_pointer)
if char == "}" and bf_script_with_comments[script_pointer]:
actor_pointer = scopeStack.pop()
script_pointer += 1
if commandsForever >= stepCap:
raise F"Runtime Error: number of consecutive commands of {commandsForever}, scope-depth: {len(scopeStack)}"
# check for Error
if scopeStack: raise F"Syntax Error: expected ']', got {actor_pointer}"
generation = 0
# run game
def step_simulation():
global generation
generation += 1
print("0-11: ", end="")
# prepare to transition to the next generation of the ActorMap
previous_actorMap = actorMap
bf_interperator(bf_compile(script_slots[previous_actorMap[int(input())]]))
# play game
def run_simulation():
step_simulation()
def display_board(num_of_col):
for i in actorMap:
# formatting
if i != 0:
print(end='\t')
if i % num_of_col == 0:
print()
elif i == 0:
continue
# print script holding of actor
print(actorMap[i], end="", sep="\t")
print()
# test run
while True:
display_board(num_of_col=8)
step_simulation()