-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrong_result_generator.gd
More file actions
47 lines (34 loc) · 1.47 KB
/
Copy pathwrong_result_generator.gd
File metadata and controls
47 lines (34 loc) · 1.47 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
class_name WrongResultGenerator
extends RefCounted
## Generates wrong answers based on the correct result.
## Similar to a Java utility class with a static-like method.
## Generates an array of wrong results that are close to the correct answer
## but never equal to it.
##
## @param proper_result: The correct answer to avoid
## @param count: How many wrong answers to generate (default 3)
## @return: Array of wrong answer integers
func generate(proper_result: int, count: int = 3) -> Array[int]:
var wrong_results: Array[int] = []
var attempts = 0
var max_attempts = 100 # Safety limit to avoid infinite loop
while wrong_results.size() < count and attempts < max_attempts:
var wrong = _generate_single_wrong(proper_result)
# Ensure no duplicates and not equal to correct answer
if wrong != proper_result and wrong not in wrong_results:
wrong_results.append(wrong)
attempts += 1
return wrong_results
## Generates a single wrong answer close to the correct one
func _generate_single_wrong(proper_result: int) -> int:
# Strategy: generate numbers within a range around the correct answer
# This makes the game challenging but fair
var offset = randi_range(-5, 5)
# Make sure offset is not 0 (which would give the correct answer)
if offset == 0:
offset = 1 if randi() % 2 == 0 else -1
var result = proper_result + offset
# Ensure result is positive (no negative answers for multiplication)
if result < 1:
result = proper_result + abs(offset)
return result