-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
111 lines (95 loc) · 3.2 KB
/
State.java
File metadata and controls
111 lines (95 loc) · 3.2 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
package wordle_gui;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class State {
Statistics stats;
String correct_word;
boolean[] char_stack;
public State() {
stats = loadStats();
String[] words = {"SHAKE", "SHARE", "PANIC", "AMUSE", "SHADE"};
int wIndex = (int)(Math.random() * words.length);
correct_word = words[wIndex];
char_stack = new boolean[]{false,false,false,false,false};
}
public Statistics getStatsObject() {
return stats;
}
private Statistics loadStats() {
Statistics result;
ObjectInputStream statsLoader = null;
try {
// open reader for game save file
statsLoader = new ObjectInputStream(new FileInputStream("./stats.dat"));
// deserialize (decode) data from the save file as a stats object
result = (Statistics)statsLoader.readObject();
} catch (IOException ioEx) {
// on errors, make default empty stats!
result = new Statistics(0, 0);
System.err.println(ioEx);
} catch (ClassNotFoundException classEx) {
result = new Statistics(0, 0);
System.err.println(classEx);
} finally {
try {
if (statsLoader != null) {
statsLoader.close();
}
} catch (IOException ioEx) {}
}
// get the stats
return result;
}
public void saveStats() {
ObjectOutputStream statsSaver = null;
try {
statsSaver = new ObjectOutputStream(new FileOutputStream("./stats.dat"));
// save stats object to save file
statsSaver.writeObject(stats);
} catch (IOException ioEx) {
System.err.println(ioEx);
} finally {
try {
if (statsSaver != null) {
statsSaver.close();
}
} catch (IOException closeEx) {}
}
}
public int checkCharacter(String c, int index) {
char character = c.charAt(0);
if(correct_word.charAt(index) == character) {
char_stack[index] = true;
return 1;
}
else if (contains(character) == true) {
char_stack[index] = true;
return 2;
}
return 0;
}
private boolean contains(char c) {
for (int i = 0; i < correct_word.length(); i++) {
if(c == correct_word.charAt(i) && char_stack[i] == false )
return true;
}
return false;
}
public void resetStack() {
for(int i = 0; i < char_stack.length; i ++) {
char_stack[i] = false;
}
}
public void gameOver(boolean won) {
// register and save if win or loss, save stats
if(won == true) {
stats.setRoundsWon(stats.getRoundsWon() + 1);
} else {
stats.setRoundsLost(stats.getRoundsLost() + 1);
}
saveStats();
}
}