-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomQuestions.java
261 lines (211 loc) · 5.74 KB
/
RandomQuestions.java
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;
import java.util.Iterator;
public class RandomQuestions extends PlayGame
{
public String questionsFile, answersFile;
public static ArrayList<String> questions;
public static ArrayList<String[]> answers;
private static int questionsCounter = 0;
private static int rightAnswer;
private static int[] correctAnswers;
public RandomQuestions()
{
questions = new ArrayList<String>();
answers = new ArrayList<String[]>();
questionsFile = "D:\\Users\\Liebm\\IntroJava\\SpongebobTrivia\\src\\Questions";
answersFile ="D:\\Users\\Liebm\\IntroJava\\SpongebobTrivia\\src\\Answers";
String questionContents = accessFolder(questionsFile);
String answerContents = accessFolder(answersFile);
questions = questionsToArrayList(questionContents);
answers = answersToArrayList(answerContents);
correctAnswers = new int[questions.size()];
java.util.Arrays.fill(correctAnswers, 0);
shuffle(questions, answers);
shuffleAnswers();
}
private String accessFolder(String selectedFile)
{
String fileContents = "";
try{
Scanner files = new Scanner(new File(selectedFile));
while(files.hasNextLine())
{
fileContents = fileContents.concat(files.nextLine() + "\n");
}} catch (IOException exc){
exc.printStackTrace();}
return fileContents;
}
private ArrayList<String> questionsToArrayList(String fileContents)
{
for(int i = 0; i < fileContents.length(); i++)
{
int questionMark = fileContents.indexOf('?');
if(fileContents.charAt(questionMark) == '?')
{
String savedQuestion = fileContents.substring(0, questionMark + 1);
questions.add(savedQuestion);
fileContents = fileContents.substring(questionMark + 2, fileContents.length());
questionsCounter++;
questionsToArrayList(fileContents);
String noNumberQ = questions.get(--questionsCounter).substring(3, questionMark + 1);
questions.set(questionsCounter, noNumberQ);
return questions;
}
}
return questions;
}
private ArrayList<String[]> answersToArrayList(String fileContents)
{
String[][] answersIn2D = answersTo2DArray(fileContents);
for(int i = 0; i < questions.size(); i++)
{
answers.add(answersIn2D[i]);
}
return answers;
}
private static String[][] answersTo2DArray(String fileContents)
{
int numOfQs = questions.size();
int abcd = 4;
String[][] answersIn2D = new String[numOfQs][abcd];
Scanner answerLine = new Scanner(fileContents);
for(int i = 0; i < numOfQs; i++)
{
for(int j = 0; j < abcd; j++)
{
while(answerLine.hasNextLine())
{
if(j == 0)
{
String answer = answerLine.nextLine();
answer = answer.substring(3, answer.length());
answersIn2D[i][j] = answer;
break;
}
else {
String answer = answerLine.nextLine();
answer = answer.substring(1, answer.length());
answersIn2D[i][j] = answer;
break; }
}
}
}
return answersIn2D;
}
private static void shuffle(ArrayList<String> shuffledQ,
ArrayList<String[]> shuffledA)
{
int chosenIndex;
int questionsSize = questions.size();
ArrayList<String[]> copy = new ArrayList<>();
for(String[] copy2 : answers)
copy.add(copy2);
int[] emptyAnswers = new int[questions.size()];
ArrayList<Integer> ans = new ArrayList<>();
for(int i = 0; i < questionsSize; i++)
{
System.out.println(correctAnswers[i]);
rightAnswer = correctAnswers[i];
for(int j = 0; j < answers.get(i).length; j++)
{
int answerIndex = (int)(Math.random() * answers.get(i).length);
if(j == rightAnswer || answerIndex == rightAnswer)
{
if (answerIndex == rightAnswer)
{
rightAnswer = j;
}
else if(j == rightAnswer)
rightAnswer = answerIndex;
}
System.out.println(" - " + rightAnswer);
swapAnswers(i, j, answerIndex, copy);
}
emptyAnswers[i] = rightAnswer;
if((questionsSize-1) == i) {
correctAnswers = emptyAnswers;
for(int m = 0; m < questionsSize; m++)
{
chosenIndex = (int)(Math.random() * questionsSize);
swap(m, chosenIndex, emptyAnswers);
}
}
}
for(int h : correctAnswers)
System.out.print(h + ", ");
System.out.println("\n");
}
private static void swap(int i, int j, int[] arr)
String temp = questions.get(i);
String[] temp2 = answers.get(i);
questions.set(i, questions.get(j));
answers.set(i, answers.get(j));
questions.set(j, temp);
answers.set(j, temp2);
arr[i] = (arr[i] + arr[j]) - (arr[j] = arr[i]);
}
private static void swapAnswers(int i, int j, int a, ArrayList<String[]> copy)
{
String[] row = copy.get(i);
String currentColIndexCopy = row[j];
String randomIndexToSwitch = row[a];
row[j] = randomIndexToSwitch;
row[a] = currentColIndexCopy;
}
public static int getSize()
{
return questions.size();
}
public static void getQuestion()
{
System.out.println(questions.get(0));
}
public static void getAnswers()
{
for(String abcd : answers.get(0))
System.out.println(abcd);
}
//"default" modifier
boolean correctAnswer(int usersChoice, int num)
{
int x = correctAnswers[num];
if(usersChoice == x)
{
restarting();
return true;
}
return false;
}
private static void restarting()
{
questions.add(questions.get(0));
questions.remove(0);
answers.add(answers.get(0));
answers.remove(0);
}
public boolean allQuestionsAsked(int num)
{
if(num == questions.size())
{
newShuffle();
return true;
}
return false;
}
public void newShuffle()
{
shuffle(questions, answers);
}
public void print()
{
for(int i = 0; i < questions.size(); i++)
{
System.out.println(questions.get(i));
for(String abcd : answers.get(i))
System.out.println(abcd);
System.out.println(correctAnswers[i]);
}
}
}