-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileStorer.java
More file actions
266 lines (230 loc) · 7.8 KB
/
Copy pathFileStorer.java
File metadata and controls
266 lines (230 loc) · 7.8 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
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
262
263
264
265
266
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class FileStorer {
//boardsizes: 4,5,6 time: unlimited,1,3,5
private String[][][] HighScoreNames = new String[7][][];
private int[][][] HighScorePoints = new int[7][][];
private Scanner currentReader1;
private Scanner currentReader2;
private PrintWriter currentWriter1;
private PrintWriter currentWriter2;
private int boardSize;
private int timeLimit;
private ArrayList<Integer> settingsArray = new ArrayList<Integer>();
private String settings = "settings.txt";
private String highscore = "highscore.txt";
private String dictionary = "WordList.txt";
private String line;
File settingsFile = new File(settings);
File dictionaryFile = new File(dictionary);
File highscoreFile = new File(highscore);
public FileStorer(){
for(int k = 4; k <= 6; k++)
{
HighScoreNames[k] = new String[6][];
HighScorePoints[k] = new int[6][];
HighScoreNames[k][0] = new String[10];
HighScoreNames[k][1] = new String[10];
HighScoreNames[k][3] = new String[10];
HighScoreNames[k][5] = new String[10];
HighScorePoints[k][0] = new int[10];
HighScorePoints[k][1] = new int[10];
HighScorePoints[k][3] = new int[10];
HighScorePoints[k][5] = new int[10];
try {
currentReader1 = new Scanner(new File("highscores/" + k + "0_points.txt"));
currentReader2 = new Scanner(new File("highscores/" + k + "0_names.txt"));
}
catch(Exception e)
{
System.out.println("You done fucked up");
}
for(int n = 0; n < 10; n++)
{
HighScorePoints[k][0][n] = currentReader1.nextInt();
HighScoreNames[k][0][n] = currentReader2.nextLine();
}
try {
currentReader1 = new Scanner(new File("highscores/" + k + "1_points.txt"));
currentReader2 = new Scanner(new File("highscores/" + k + "1_names.txt"));
}
catch(Exception e)
{
System.out.println("You done fucked up");
}
for(int n = 0; n < 10; n++)
{
HighScorePoints[k][1][n] = currentReader1.nextInt();
HighScoreNames[k][1][n] = currentReader2.nextLine();
}
try {
currentReader1 = new Scanner(new File("highscores/" + k + "3_points.txt"));
currentReader2 = new Scanner(new File("highscores/" + k + "3_names.txt"));
}
catch(Exception e)
{
System.out.println("You done fucked up");
}
for(int n = 0; n < 10; n++)
{
HighScorePoints[k][3][n] = currentReader1.nextInt();
HighScoreNames[k][3][n] = currentReader2.nextLine();
}
try {
currentReader1 = new Scanner(new File("highscores/" + k + "5_points.txt"));
currentReader2 = new Scanner(new File("highscores/" + k + "5_names.txt"));
}
catch(Exception e)
{
System.out.println("You done fucked up");
}
for(int n = 0; n < 10; n++)
{
HighScorePoints[k][5][n] = currentReader1.nextInt();
HighScoreNames[k][5][n] = currentReader2.nextLine();
}
}
try{
currentReader1 = new Scanner(new File ("settings.txt"));
}
catch(Exception e)
{
System.out.println("You done fucked up");
}
boardSize = currentReader1.nextInt();
timeLimit = currentReader1.nextInt();
settingsArray.add(5);
settingsArray.add(5);
}
public void mySort(String filename, ArrayList<String> arrayList)
{
Scanner inFile = new Scanner(filename);
StringBuilder highscoreInt = new StringBuilder();
StringBuilder newHighScore = new StringBuilder();
line = (arrayList.get(arrayList.size()-1));
for(int i=0;i<line.length();i++)
{
if(Character.isDigit(line.charAt(i)))
newHighScore.append(line.charAt(i));
}
int counter = 0;
Boolean flag = true;
while (((line = inFile.nextLine()) != null) && flag)
{
for(int i=0;i<line.length();i++)
{
if(Character.isDigit(line.charAt(i)))
highscoreInt.append(line.charAt(i));
}
if (Integer.parseInt(highscoreInt.toString()) < Integer.parseInt(newHighScore.toString()))
{
flag = false;
String temp ="";
temp = arrayList.get(counter);
arrayList.set(counter, arrayList.get(arrayList.size()-1));
for (int j = arrayList.size()-2; j >= counter; j++)
arrayList.set(j+1, arrayList.get(j));
arrayList.set(counter+1,temp);
}
counter ++;
}
}
public Boolean checkIfNewHighScore(int testValue)
{
return (HighScorePoints[boardSize][timeLimit][9] < testValue);
}
public int[] getAllHighScorePoints(int xbyx, int countdown)
{
return HighScorePoints[xbyx][countdown];
}
public String[] getAllHighScoreNames(int xbyx, int countdown)
{
return HighScoreNames[xbyx][countdown];
}
//assumes that the score is a high score
public void newHighScore(String name, int score)
{
int scorePlacement = 9;
for(int k = 9; k >= 0; k--)
{
if(score > HighScorePoints[boardSize][timeLimit][k])
{
scorePlacement = k;
}
}
for(int k = 8; k >= scorePlacement; k--)
{
HighScorePoints[boardSize][timeLimit][k + 1] = HighScorePoints[boardSize][timeLimit][k];
HighScoreNames[boardSize][timeLimit][k + 1] = HighScoreNames[boardSize][timeLimit][k];
}
HighScorePoints[boardSize][timeLimit][scorePlacement] = score;
HighScoreNames[boardSize][timeLimit][scorePlacement] = name;
try {
currentWriter1 = new PrintWriter("highscores/" + boardSize + timeLimit + "_points.txt");
currentWriter2 = new PrintWriter("highscores/" + boardSize + timeLimit + "_names.txt");
for(int k = 0; k < 10; k++)
{
currentWriter1.println(HighScorePoints[boardSize][timeLimit][k] + "");
currentWriter2.println(HighScoreNames[boardSize][timeLimit][k]);
}
currentWriter1.close();
currentWriter2.close();
} catch(Exception e)
{
System.out.println("You done fucked up");
}
}
public void setSettings(int newTimeLimit, int newBoardSize) throws IOException{
boardSize = newBoardSize;
timeLimit = newTimeLimit;
settingsArray.set(0, timeLimit);
settingsArray.set(1, boardSize);
FileWriter outFile = new FileWriter("settings.txt");
outFile.append(Integer.toString(newBoardSize));
outFile.append(System.lineSeparator());
outFile.append(Integer.toString(newTimeLimit));
outFile.close();
}
public int getTimeLimit()
{
return timeLimit;
}
public int getBoardSize()
{
return boardSize;
}
public ArrayList<Integer> getSettings()
{
return settingsArray;
}
public void setTimeLimit(int newLimit)
{
timeLimit = newLimit;
settingsArray.set(1, newLimit);
}
public void setBoardSize(int newBoardSize)
{
boardSize = newBoardSize;
settingsArray.set(0, newBoardSize);
}
public Boolean checkForWord(String word) throws FileNotFoundException
{
Scanner inFile = new Scanner(dictionaryFile);
while (inFile.hasNextLine()) {
line = inFile.nextLine();
if (line.equalsIgnoreCase(word))
{
return true;
}
}
return false;
}
public static void main(String[] args) {
FileStorer test = new FileStorer();
test.newHighScore("Billy", 33);
test.newHighScore("Dominic", 17);
test.newHighScore("Jimbo", 66);
}
}