-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKopfrechnen_gamemodes.py
171 lines (147 loc) · 5.28 KB
/
Kopfrechnen_gamemodes.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
import random
import math
# ==================== Gamemodes ====================
# How to add a new gamemode?
# 1. Create class, inherit from Gamemode or subclass e.g. Multiplication
# 2. Set title
# 3. Implement makeTask() method: return String: task, int: result
# 4. Add class to gamemodes list
# How to add a new category?
# 1. Create class, inherit from Gamemode
# 2. Set category-title
# 3. Add category as dictionary-item to gamemodes list just like "Multiplication"
class Gamemode:
category = "Gamemode"
title = "Gamemode"
def makeTask():
return "task", 0 # task, result
# ==================== Multiplication ====================
class Multiplication(Gamemode):
category = "Multiplication"
title = "Multiplication"
def makeTask():
return "calculation exercise", 0 # task, result
class TwoDigitSquares(Multiplication):
title = "2-digit squares"
def makeTask():
a = random.randint(10, 99)
return str(a) + "²", a**2 # task, result
class TwoDigitSquaresWith5(Multiplication):
title = "2-digit squares with 5"
def makeTask():
a = 10 * random.randint(1, 9) + 5
return str(a) + "²", a**2 # task, result
class TwoDigitMultiply11(Multiplication):
title = "2-digit multiply 11"
def makeTask():
a = random.randint(10, 99)
return str(a) + " * 11", a*11 # task, result
class TwoDigitMultiply(Multiplication):
title = "2-digit multiply"
def makeTask():
a = random.randint(1, 99)
b = random.randint(1, 99)
return str(a) + " * " + str(b), a*b # task, result
class TwoDigitMultiplySameTen(Multiplication):
title = "2-digit multiply same ten-digit"
def makeTask():
t = random.randint(1, 9)
a = 10*t + random.randint(1, 9)
b = 10*t + random.randint(1, 9)
return str(a) + " * " + str(b), a*b # task, result
class ThreeDigitSquaresWith5(Multiplication):
title = "3-digit squares with 5"
def makeTask():
a = 10 * random.randint(10, 99) + 5
return str(a) + "²", a**2 # task, result
# ==================== Addition ====================
class Addition(Gamemode):
category = "Addition"
title = "Addition"
def makeTask():
return "calculation exercise", 0 # task, result
class TwoDigitAddition(Addition):
title = "2-digit addition"
def makeTask():
a = random.randint(1, 99)
b = random.randint(1, 99)
return str(a) + " + " + str(b), a+b # task, result
class ThreeDigitAddition(Addition):
title = "3-digit addition"
def makeTask():
a = random.randint(1, 999)
b = random.randint(1, 999)
return str(a) + " + " + str(b), a+b # task, result
class FourDigitAddition(Addition):
title = "4-digit addition"
def makeTask():
a = random.randint(1, 9999)
b = random.randint(1, 9999)
return str(a) + " + " + str(b), a+b # task, result
class FiveDigitAddition(Addition):
title = "5-digit addition"
def makeTask():
a = random.randint(1, 99999)
b = random.randint(1, 99999)
return str(a) + " + " + str(b), a+b # task, result
class RandomizedAddition(Addition):
title = "Randomized addition"
def makeTask():
nNumbers = random.randint(2, 5)
sizes = [random.randint(1, 5) for i in range(nNumbers)]
numbers = [random.randint(10**(sizes[i]-1), 10**sizes[i]) for i in range(nNumbers)]
return " + ".join([str(n) for n in numbers]), sum(numbers) # task, result
# ==================== Approximation ====================
class Approximation(Gamemode):
category = "Approximation"
title = "Approximation"
def makeTask():
return "calculation exercise", 0 # task, result
class ApproxMult(Approximation):
title = "Approximate Multiplication"
def makeTask():
nNumbers = random.randint(2, 4)
sizes = [random.randint(1, 3) for i in range(nNumbers)]
numbers = [random.randint(10**(sizes[i]-1), 10**sizes[i]) for i in range(nNumbers)]
p = math.prod(numbers)
factor = random.random() + 0.5
comparison = int(p*factor)
if p <= comparison:
return (" * ".join([str(n) for n in numbers])) + ">" + str(comparison)+"?", 0 # task, result
else:
return (" * ".join([str(n) for n in numbers])) + ">" + str(comparison)+"?", 1 # task, result
# ==================== Mixed ===========================
class Mixed(Gamemode):
category = "Mixed"
title = "Mixed"
def randomCategory():
categories = [c for c in gamemodes.keys() if c != "Mixed"]
return random.choice(categories)
def makeTask():
category = Mixed.randomCategory()
gamemode = random.choice(gamemodes[category])
return gamemode.makeTask()
# ==================== Gamemodes list ====================
gamemodes = {
"Multiplication": [
TwoDigitSquares,
TwoDigitSquaresWith5,
TwoDigitMultiply11,
TwoDigitMultiplySameTen,
TwoDigitMultiply,
ThreeDigitSquaresWith5,
],
"Addition": [
TwoDigitAddition,
ThreeDigitAddition,
FourDigitAddition,
FiveDigitAddition,
RandomizedAddition,
],
"Mixed": [
Mixed,
],
"Approximation": [
ApproxMult,
],
}