-
Notifications
You must be signed in to change notification settings - Fork 0
/
beginners_edition.py
352 lines (257 loc) · 7.72 KB
/
beginners_edition.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from random import randint
# global restrictions
range_step = 32
bf_vocabulary = "+-><{}!?"
def bf_compile(rule):
bf_code_to_return = ""
for sym in rule:
if sym not in bf_vocabulary:
continue
bf_code_to_return += sym
return bf_code_to_return
# assign 50 steps to your buffer
STARTING_SUM = 250
pl_buf = []
op_buf = []
pl_rules = []
op_rules = []
pl_pointer = 0
op_pointer = 0
pl_low_range = 0
pl_high_range = 31
op_low_range = 0
op_high_range = 31
# load content from file
def load_from_file(filename, arr, is_integer_array=False):
arr.clear()
f = open(filename, 'r')
lines = f.readlines()
for line in range(len(lines)):
if line < len(lines) - 1:
if is_integer_array:
arr.append(int(lines[line][:-1]))
else:
arr.append(lines[line][:-1])
else:
if is_integer_array:
arr.append(int(lines[line]))
else:
arr.append(lines[line])
def get_rule_range(the_range_of_the_byte_value):
return the_range_of_the_byte_value >> 5
# a text to code function
def bf_interperator(picked_index, pl_buf, op_buf, pl_pointer, op_pointer, pl_low_range, pl_high_range, step_cap):
steps_taken = 0
scope_stack = []
# where in the bf code the main-thread is executing (code line:char)
script_pointer = 0
if 0 <= get_rule_range(pl_buf[picked_index]) - 1 <= 5:
rule = bf_compile(pl_rules[get_rule_range(pl_buf[picked_index]) - 1])
else:
print("invalid rule range of", pl_buf[picked_index])
return 0
while (script_pointer < len(rule)):
steps_taken += 1
if rule[script_pointer] == "+":
if pl_buf[pl_pointer] < 255:
pl_buf[pl_pointer] += 1
if rule[script_pointer] == "-":
if pl_buf[pl_pointer] > 0:
pl_buf[pl_pointer] -= 1
if rule[script_pointer] == ">":
if pl_pointer < 7:
pl_pointer += 1
if rule[script_pointer] == "<":
if pl_pointer > 0:
pl_pointer -= 1
if rule[script_pointer] == "!":
op_buf[op_pointer] = pl_buf[pl_pointer]
if rule[script_pointer] == "?":
pl_buf[pl_pointer] = op_buf[op_pointer]
if rule[script_pointer] == "{":
scope_stack.append(script_pointer)
if rule[script_pointer] == "}" and pl_low_range <= pl_buf[pl_pointer] < pl_high_range:
script_pointer = scope_stack.pop()
elif pl_low_range > pl_buf[pl_pointer]:
pl_low_range -= 32
pl_high_range -= 32
elif pl_high_range < pl_buf[pl_pointer]:
pl_low_range += 32
pl_high_range += 32
script_pointer += 1
# check for Error
if scope_stack:
raise "Syntax Error: expected '}', got " + rule[script_pointer]
if steps_taken >= step_cap:
return step_cap
return steps_taken
# start a game
# both player setup initial arrangment
# shuffle half of the rules at random
# flip a coin of who starts
def set_player_buffer(buf):
buf.clear()
remaning = STARTING_SUM
for b in range(8):
print("byte "+str(b+1)+"/8")
while True:
if remaning <= 0:
buf.append(0)
break
else:
stream = input(
"assign rule key value (" + str(remaning) + " value(s) remaning): ")
if stream == "":
buf.append(0)
break
attempt = int(stream)
if attempt <= remaning:
buf.append(attempt)
remaning -= attempt
break
else:
print("Error: use a integer less than " +
str(remaning + 1) + ".")
def setup_rules_for_ranges(rules):
symbols_used = 0
print("Vocabulary:", bf_vocabulary)
for rule in range(6):
stream = input("Write rule for keys in range " + str((rule + 1)
* range_step) + "-" + str((rule + 2) * range_step - 1) + ": ")
symbols_used += len(stream)
rules.append(stream)
#print("symbols_used", symbols_used)
# mix 3 random rules between players
def mix_rules(pl_rules, op_rules):
rule_pick = [0, 1, 2, 3, 4, 5]
for _ in range(3):
pick = randint(0, len(rule_pick) - 1)
temp = pl_rules[rule_pick[pick]]
pl_rules[rule_pick[pick]] = op_rules[rule_pick[pick]]
op_rules[rule_pick[pick]] = temp
rule_pick.pop(pick)
def ready(pl_buf, op_buf, pl_rules, op_rules):
pl_buf_sum = 0
op_buf_sum = 0
for p in pl_buf:
pl_buf_sum += int(p)
for o in op_buf:
op_buf_sum += int(o)
return\
pl_buf_sum == STARTING_SUM and\
op_buf_sum == STARTING_SUM and\
len(pl_rules) == 6 and\
len(op_rules) == 6
def print_player_buffer(buf):
for s in buf:
print(s, end="\t")
print()
def turn(pl_buf, op_buf, pl_pointer, op_pointer, pl_low_range, pl_high_range):
pl_steps_remaining = sum(pl_buf)
while pl_steps_remaining > 0:
print("\t" * op_pointer + "|")
print_player_buffer(op_buf)
print()
print("\t" * pl_pointer + "|")
print_player_buffer(pl_buf)
print()
stream = input("pick a buffer index that matches the rule you want to execute (" + str(pl_steps_remaining) + " steps remaning): ")
if stream == '':
print("'Enter' entered, ending turn...")
break
pl_steps_remaining -= bf_interperator(int(stream), pl_buf, op_buf, pl_pointer, op_pointer, pl_low_range, pl_high_range, pl_steps_remaining)
def game(pl_buf, op_buf, pl_pointer, op_pointer):
if not ready(pl_buf, op_buf, pl_rules, op_rules):
print("--set_rules and --set_buffer values before playing")
return
turns = 0
pl_win = max(op_buf) < 32 and min(pl_buf) > 223
op_win = max(pl_buf) < 32 and min(op_buf) > 223
while (True):
if turns % 2 == 0:
# prints available rules
for rule in range(6):
print(str((rule + 1) * range_step) + "-" + str((rule + 2) * range_step - 1) + ": " + str(pl_rules[rule]))
turn(pl_buf, op_buf, pl_pointer, op_pointer, pl_low_range, pl_high_range)
if (pl_win):
print("player win!")
break
else:
# prints available rules
for rule in range(6):
print(str((rule + 1) * range_step) + "-" + str((rule + 2) * range_step - 1) + ": " + str(op_rules[rule]))
turn(op_buf, pl_buf, op_pointer, pl_pointer, op_low_range, op_high_range)
if (op_win):
print("opponent win!")
break
turns += 1
def show_help():
print(
"""
-h, --help display this text
--set_rules each player sets 6
rules between key
values of 32-223
--set_buffer each player
distributes 50
key values among
8 bytes within
their buffer.
-s, --start start the game."""
)
if __name__ == "__main__":
while True:
# main prompt
stream = input('> ')
# help prompt
if stream in ("-h", "--help"):
show_help()
# set rules prompt
elif stream == "--set_rules":
print("0: pl_rules:", "set" if len(pl_rules) else "not set")
print("1: op_rules:", "set" if len(op_rules) else "not set")
print("2: back")
stream = input("which player (0-2): ")
if stream == "0":
setup_rules_for_ranges(pl_rules)
elif stream == "1":
setup_rules_for_ranges(op_rules)
else:
continue
# set buffer values prompt
elif stream == "--set_buffer":
print("0: pl_buffer:", "set" if sum(pl_buf) == STARTING_SUM else "not set")
print("1: op_buffer:", "set" if sum(op_buf) == STARTING_SUM else "not set")
print("2: back")
stream = input("which player(0-2): ")
if stream == "0":
set_player_buffer(pl_buf)
if stream == "1":
set_player_buffer(op_buf)
else:
continue
# start game prompt
elif stream in ("-s", "--start"):
print("0: pl")
print("1: op")
print("2: back")
stream = input("which player goes first: (0-2): ")
if stream == "0":
load_from_file("pl_buf.txt", pl_buf, True)
load_from_file("op_buf.txt", op_buf, True)
load_from_file("pl_rules.bf", pl_rules, False)
load_from_file("op_rules.bf", op_rules, False)
game(pl_buf, op_buf, pl_pointer, op_pointer)
elif stream == "1":
load_from_file("pl_buf.txt", pl_buf, True)
load_from_file("op_buf.txt", op_buf, True)
load_from_file("pl_rules.bf", pl_rules, False)
load_from_file("op_rules.bf", op_rules, False)
game(op_buf, pl_buf, op_pointer, pl_pointer)
else:
continue
elif stream in ("-q", "--quit"):
break
# exception prompt
else:
print("-h or --help for help")