-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomMathProblemGenerator.cs
More file actions
125 lines (106 loc) · 4 KB
/
RandomMathProblemGenerator.cs
File metadata and controls
125 lines (106 loc) · 4 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
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RandomMathProblemGenerator : MonoBehaviour
{
private int operand1;
private int operand2;
private string operatorSymbol;
public int answer;
public List<int> randomizedAnswers; // Store the randomized order of answers
private bool hasGeneratedNewNumbers = false;
private void Start()
{
}
public void GenerateRandomMathProblem()
{
operand1 = UnityEngine.Random.Range(0, 10);
operand2 = UnityEngine.Random.Range(1, 10); // Ensure operand2 is not zero
int operatorIndex = UnityEngine.Random.Range(0, 4); // 0 for addition, 1 for subtraction, 2 for multiplication, 3 for division
switch (operatorIndex)
{
case 0: // Addition
operatorSymbol = "+";
answer = operand1 + operand2;
break;
case 1: // Subtraction
operatorSymbol = "-";
// Ensure operand2 is smaller or equal to operand1 to avoid negative answers.
if (operand2 > operand1)
{
int temp = operand1;
operand1 = operand2;
operand2 = temp;
}
answer = operand1 - operand2;
break;
case 2: // Multiplication
operatorSymbol = "x";
answer = operand1 * operand2;
break;
case 3: // Division
operatorSymbol = "/";
// Ensure an exact division with no remainder
int tempAnswer = operand1 * operand2;
operand1 = tempAnswer;
answer = operand1 / operand2;
break;
}
randomizedAnswers = GenerateRandomizedAnswers();
hasGeneratedNewNumbers = true;
}
private int GenerateIncorrectAnswer()
{
// Generate a random number between -5 to 5
int randomOffset = UnityEngine.Random.Range(-5, 6);
// Make sure the incorrect answer is different from the correct answer
while (randomOffset == 0 || randomOffset == answer)
{
randomOffset = UnityEngine.Random.Range(-5, 6);
}
return answer + randomOffset;
}
private List<int> GenerateRandomizedAnswers()
{
List<int> answers = new List<int> { answer };
for (int i = 0; i < 2; i++)
{
int incorrectAnswer = GenerateIncorrectAnswer();
while (answers.Contains(incorrectAnswer))
{
incorrectAnswer = GenerateIncorrectAnswer();
}
answers.Add(incorrectAnswer);
}
// Shuffle the answers list randomly
for (int i = 0; i < answers.Count; i++)
{
int randomIndex = UnityEngine.Random.Range(i, answers.Count);
int temp = answers[i];
answers[i] = answers[randomIndex];
answers[randomIndex] = temp;
}
return answers;
}
private string GetProblemText()
{
return $"{operand1} {operatorSymbol} {operand2} = ?";
}
private void OnGUI()
{
int labelWidth = 200;
int labelHeight = 50;
string problemText = GetProblemText();
// Draw the problem text in the middle of the screen
GUI.Label(new Rect(Screen.width / 2 - labelWidth / 2, Screen.height - labelHeight, labelWidth, labelHeight), problemText);
// Draw each answer label in the middle of the screen in one row
if (hasGeneratedNewNumbers)
{
for (int i = 0; i < randomizedAnswers.Count; i++)
{
GUI.Label(new Rect(Screen.width / 2 - labelWidth / 2 + (labelWidth + 10) * i / 2, Screen.height / 2 - labelHeight / 2 + 25, labelWidth, labelHeight), randomizedAnswers[i].ToString());
}
}
}
}