-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_functions.py
More file actions
57 lines (49 loc) · 1.55 KB
/
game_functions.py
File metadata and controls
57 lines (49 loc) · 1.55 KB
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
import random
import socket
import pickle
import zlib
import time
def print_loading_dots(delay, amount=3, spacing=3):
for i in range(amount - 1):
print("."+" "*spacing,end="",flush=True)
time.sleep(delay)
print(".",end="",flush=True)
time.sleep(delay)
print()
def print_properties(obj):
print("\n".join(["{}: {}".format(v, w)
for v,w in filter(lambda x: type(x[1]).__name__ != "builtin_function_or_method",
[(y, getattr(obj, y)) for y in filter(lambda z: z[:2] != "__", dir(obj))])]))
def getRandomLineWeighted(filename):
with open(filename, "r") as f:
types = [x.split(",") for x in f.read().splitlines()]
for i in range(len(types)):
types[i][1] = int(types[i][1])
probability_sum = sum([x[1] for x in types])
roll = random.randint(1, probability_sum)
cumulative_sum = 0
for t in types:
cumulative_sum += t[1]
if cumulative_sum >= roll:
return t[0]
def restrictive_input(prompt, process, condition, default=None):
valid = False
while not valid:
if default:
x = input("{} (default: {}): ".format(prompt, default))
if len(x) == 0:
return default
else:
x = input(prompt)
try:
x = process(x)
valid = condition(x)
if not valid:
print("Invalid input!!")
except:
valid = False
return x
def encode_DP(DP):
return zlib.compress(pickle.dumps(DP),9)
def decode_DP(_DP):
return pickle.loads(zlib.decompress(_DP))