-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaverAndLoader.java
More file actions
199 lines (172 loc) · 7.16 KB
/
Copy pathSaverAndLoader.java
File metadata and controls
199 lines (172 loc) · 7.16 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
package Modular;
import Data.Data;
import Objects.Chess;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class SaverAndLoader {
public SaverAndLoader() {
}
public void loadConfig() {
try {
File config = new File("config.ini");
if (!config.exists()) {
Data.ifFirstGame = 1;
saveConfig();
return;
}
FileInputStream fileInputStream = new FileInputStream(config);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
HashMap<Object, Object> file = (HashMap<Object, Object>) objectInputStream.readObject();
Data.pattern = (int) file.get("pattern");
if (Data.pattern == 0) {
Data.menu.button5.setSelected(true);
} else if (Data.pattern == 1) {
Data.menu.button6.setSelected(true);
}
} catch (IOException | ClassNotFoundException e) {
System.out.println("文件加载错误");
} catch (NullPointerException nullPointerException) {
System.out.println("文件加载错误");
}
}
public void saveConfig() {
try {
File config = new File("config.ini");
HashMap<Object, Object> file = new HashMap<>();
file.put("pattern", Data.pattern);
if (!config.exists()) {
if (config.createNewFile()) {
System.out.println("第一次游戏,成功创建配置文件");
} else {
System.out.println("创建配置文件失败,请检查运行目录下是否有与 \"config.gobang\" 重名的文件");
}
}
FileOutputStream fileOutputStream = new FileOutputStream(config);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(file);
} catch (IOException e) {
System.out.println("储存配置错误");
}
}
public void saveFileDefaultPath() {
File file = new File("file.gobang");
try {
HashMap<Object, Object> data = new HashMap<>();
data.put("pattern", Data.pattern);
data.put("gameState", Data.gameState);
data.put("lastChessPositionAIX", Data.lastChessPositionAIX);
data.put("lastChessPositionAIY", Data.lastChessPositionAIY);
if (!file.exists()) {
file.createNewFile();
}
data.put("chessArray", Data.chessArray);
FileOutputStream fileOutputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(data);
} catch (IOException e) {
System.out.println("存档错误");
}
}
public void saveFileOtherPath() {
String fileName = "file.gobang";
File file = new File(fileName);
JFileChooser jFileChooser = new JFileChooser("./");
int i = 1;
while (file.exists()) {
fileName = "file" + "(" + i + ")" + ".gobang";
file = new File(fileName);
i++;
}
jFileChooser.setSelectedFile(file);
jFileChooser.showSaveDialog(Data.frame);
file = jFileChooser.getSelectedFile();
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("创建文件失败");
}
}
try {
HashMap<Object, Object> data = new HashMap<>();
data.put("pattern", Data.pattern);
data.put("gameState", Data.gameState);
data.put("lastChessPositionAIX", Data.lastChessPositionAIX);
data.put("lastChessPositionAIY", Data.lastChessPositionAIY);
data.put("data", data);
data.put("chessArray", Data.chessArray);
FileOutputStream fileOutputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(data);
} catch (IOException e) {
System.out.println("存档错误");
}
}
public void loadFileOtherPath() {
JFileChooser jFileChooser = new JFileChooser("./");
jFileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
} else {
String name = f.getName();
if (name.endsWith("gobang")) {
return true;
}
}
return false;
}
@Override
public String getDescription() {
return "需要\".gobang\" 文件";
}
});
jFileChooser.showOpenDialog(Data.frame);
File file = jFileChooser.getSelectedFile();
try {
FileInputStream fileInputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
HashMap<Object, Object> data = (HashMap<Object, Object>) objectInputStream.readObject();
Data.chessArray = (ArrayList<Chess>) data.get("chessArray");
Data.pattern = (int) data.get("pattern");
Data.gameState = (int) data.get("gameState");
Data.lastChessPositionAIX = (int) data.get("lastChessPositionAIX");
Data.lastChessPositionAIY = (int) data.get("lastChessPositionAIY");
} catch (IOException ioException) {
System.out.println("文件读取错误");
} catch (ClassNotFoundException e) {
System.out.println("文件加载错误");
} catch (NullPointerException nullPointerException) {
System.out.println("未选择文件");
}
}
public void loadDefaultFile() {
try {
File config = new File("file.gobang");
FileInputStream fileInputStream = new FileInputStream(config);
if (!config.exists()) {
saveConfig();
}
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
HashMap<Object, Object> file = (HashMap<Object, Object>) objectInputStream.readObject();
Data.pattern = (int) file.get("pattern");
Data.gameState = (int) file.get("gameState");
Data.lastChessPositionAIX = (int) file.get("lastChessPositionAIX");
Data.lastChessPositionAIY = (int) file.get("lastChessPositionAIY");
Data.chessArray = (ArrayList<Chess>) file.get("chessArray");
if (Data.pattern == 0) {
Data.menu.button5.setSelected(true);
} else if (Data.pattern == 1) {
Data.menu.button6.setSelected(true);
}
} catch (IOException | ClassNotFoundException e) {
System.out.println("配置文件未发现");
} catch (NullPointerException nullPointerException) {
System.out.println("文件加载错误");
}
}
}