-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
145 lines (125 loc) · 4.76 KB
/
game.py
File metadata and controls
145 lines (125 loc) · 4.76 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
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
# Environment variables
from dotenv import load_dotenv
import os
from os.path import join, dirname
import random
import threading
import ctypes
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
addition = int(os.environ.get("addition"))
multiplication = int(os.environ.get("multiplication"))
subtraction = int(os.environ.get("subtraction"))
division = int(os.environ.get("division"))
operations = {
"add": addition,
"sub": subtraction,
"mul": multiplication,
"div": division
}
used_operations = [i for i in operations.keys() if operations[i] > 0]
add_x1 = int(os.environ.get("add_x1"))
add_x2 = int(os.environ.get("add_x2"))
add_x3 = int(os.environ.get("add_x3"))
add_x4 = int(os.environ.get("add_x4"))
mul_x1 = int(os.environ.get("mul_x1"))
mul_x2 = int(os.environ.get("mul_x2"))
mul_x3 = int(os.environ.get("mul_x3"))
mul_x4 = int(os.environ.get("mul_x4"))
decimals = int(os.environ.get("decimals"))
decimal_places = os.environ.get("decimal_places")
penalize_errors = os.environ.get("penalize_errors")
secs = int(os.environ.get("time"))
class Game(threading.Thread):
def __init__(self):
# Represents players current score
threading.Thread.__init__(self)
self._score = 0
self._secs = secs
self._run = True
def prompt(self, x, y, op):
try:
print(str(x) + op + str(y) + ": ")
user_val = int(input())
return user_val
except:
pass
def run(self):
while self._run:
# First choose between add, sub, mul, div
rand_index = random.randrange(len(used_operations))
op = used_operations[rand_index]
if decimals == 0:
if op == "add": # Addition
x = random.randrange(add_x1, add_x2)
y = random.randrange(add_x1, add_x2)
response = self.prompt(x, y, " + ")
if response != x + y:
if penalize_errors == 1:
self._score -= 1
print("Wrong Answer!")
else:
print("Correct Answer!")
self._score += 1
elif op == "sub": # Subtraction
x = random.randrange(add_x1, add_x2)
y = random.randrange(add_x1, add_x2)
response = self.prompt(x, y, " - ")
if response != x - y:
if penalize_errors == 1:
self._score -= 1
print("Wrong Answer!")
else:
print("Correct Answer!")
self._score += 1
elif op == "mul": # Multiplication
x = random.randrange(mul_x1, mul_x2)
y = random.randrange(mul_x3, mul_x4)
response = self.prompt(x, y, " * ")
if response != x * y:
if penalize_errors == 1:
self._score -= 1
print("Wrong Answer!")
else:
print("Correct Answer")
self._score += 1
else: # Division
x = random.randrange(mul_x1, mul_x2)
y = random.randrange(mul_x3, mul_x4)
while y % x != 0:
x = random.randrange(mul_x1, mul_x2)
y = random.randrange(mul_x3, mul_x4)
response = self.prompt(y, x, "/")
if response != y / x:
if penalize_errors == 1:
self._score -= 1
print("Wrong Answer!")
else:
print("Correct Answer!")
self._score += 1
else:
if op == "add": # Addition
pass
elif op == "sub": # Subtraction
pass
elif op == "mul": # Multiplication
pass
else: # Division
pass
def get_score(self):
return self._score
def raise_exception(self):
thread_id = self.get_id()
self._run = False
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id,
ctypes.py_object(SystemExit))
if res > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
print('Exception raise failure')
def get_id(self):
# returns id of the respective thread
if hasattr(self, '_thread_id'):
return self._thread_id
for id, thread in threading._active.items():
if thread is self:
return id