Radically restructure game mechanics
John Smith

John Smith commited on 2022-341 23:25:47
Showing 1 changed files, with 15 additions and 31 deletions.


Now, instead of having a fixed number of trials per game,
the game has a fixed total time, and the player must answer
as many questions as they can within that time;
the game provides a total score based on quantity and quality
of those responses.
... ...
@@ -14,53 +14,37 @@ local TYPES = {
14 14
     ln = {
15 15
         -- how to generate the problem (returns problem-object)
16 16
         gen = function()
17
-            return math.exp(6.9 * (2 * math.random() - 1))
17
+            return math.exp(6.9 * math.random())
18 18
         end,
19 19
         -- how to present the problem (for string.format)
20 20
         disp = "ln(%.2f)",
21 21
         -- produce correct answer-object from problem-object
22 22
         func = math.log,
23
-        -- check if guess g for problem p is correct
24
-        good = function(p, g) return math.abs(math.log(p) - g) < 0.05 end,
23
+        -- check quality of guess g for problem p
24
+        score = function(p, g) return -(math.log(math.abs(math.log(p) - g), 10) + 1.5)^3 end,
25 25
     },
26 26
 }
27 27
 
28 28
 -- ideally, configure these parameters with options/inputs
29
-local COUNT = 10
30 29
 local TYPE = TYPES.ln
31
-local TRIES = 1
32
-local TIME = 4
30
+local TIME = 30
33 31
 
34
-local sc = 0
35
-local oc = 0
36
-for i = 1,COUNT do
32
+local ts = 0
33
+local st = os.time()
34
+local pc = 0
35
+while os.time() - st < TIME do
37 36
     local rp = TYPE.gen()
38 37
     local ans = TYPE.func(rp)
39 38
     io.write(string.format(TYPE.disp .. " ? ", cup(rp)))
40 39
     io.flush()
41
-    local st = os.time()
42
-    local inp
43
-    local fails = 0
44
-    while true do
45 40
     inp = tonumber(io.read("l")) or 0
46
-        if os.time() - st > TIME then
47
-            if TYPE.good(rp, inp) then
48
-                oc = oc + 1
49
-            end
50
-            io.write(string.format("too slow! (%.2f)\n", ans))
51
-            break
52
-        elseif TYPE.good(rp, inp) then
53
-            io.write("✓ correct\n")
54
-            sc = sc + 1
55
-            break
56
-        elseif fails < TRIES then
57
-            io.write("wrong, try again ")
58
-            io.flush()
59
-            fails = fails + 1
41
+    local sc = TYPE.score(rp, inp)
42
+    if sc > 0 then
43
+        io.write(string.format("✓ correct (%.3f)\n", ans))
60 44
     else
61
-            io.write(string.format("wrong (%.2f)\n", ans))
62
-            break
63
-        end
45
+        io.write(string.format("wrong (%.3f)\n", ans))
64 46
     end
47
+    ts = ts + sc
48
+    pc = pc + 1
65 49
 end
66
-io.write(string.format("%d/%d correct, of which %d were too slow\n", (sc + oc), COUNT, oc))
50
+io.write(string.format("score %.1f from %d problems in %.1f seconds\n", ts, pc, TIME))
67 51