-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinesweeperStore.java
More file actions
executable file
·161 lines (129 loc) · 5.46 KB
/
MinesweeperStore.java
File metadata and controls
executable file
·161 lines (129 loc) · 5.46 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
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.io.FileWriter;
import java.io.IOException;
import cell.*;
import java.util.ArrayList;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileReader;
import java.io.Reader;
import grid.*;
public class MinesweeperStore {
/**
* Passata la lista di game, la salva in un file data.json
*
* @param games
*/
public static void save(MinesweeperGames games){
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
for(MinesweeperGame game : games.getListGame()){
JSONObject gameObj = new JSONObject();
gameObj.put("id", game.getId());
gameObj.put("size", game.getSize());
gameObj.put("numBomb", game.getNumBomb());
gameObj.put("numBombRemain", game.getNumBombRemain());
gameObj.put("isDone", game.isDone());
gameObj.put("isWin", game.isWin());
if(game.getGame() != null)
gameObj.put("game", getJsonCell(game.getGame().getGrid()));
gameObj.put("display", getJsonCell(game.getDisplay().getGrid()));
array.add(gameObj);
}
obj.put("games", array);
try (FileWriter file = new FileWriter("data.json")) {
file.write(array.toJSONString());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Serve a trasformare una Grid in Array json, viene utilizzato in MinesweeperStore.save
*
* @param grid
* @return
*/
private static JSONArray getJsonCell(Cell[][] grid){
JSONArray rowJson = new JSONArray();
for(int i = 0; i < grid.length; i++){
JSONArray colJson = new JSONArray();
for(int j = 0; j < grid[i].length; j++){
JSONObject cellJson = new JSONObject();
cellJson.put("type", grid[i][j].getType());
if(grid[i][j].getType().equals("Hidden")){
cellJson.put("marked", ((CellHidden) grid[i][j]).isMarked());
} else if (grid[i][j].getType().equals("CloseBomb")){
cellJson.put("numBombClose", ((CellCloseBomb) grid[i][j]).getNumBombClose());
}
colJson.add(cellJson);
}
rowJson.add(colJson);
}
return rowJson;
}
/**
* Legge dal file data.json e costruisce un MinesweeperGames
*
* @return
*/
public static MinesweeperGames load(){
ArrayList<MinesweeperGame> games = new ArrayList<>();
JSONParser jsonParser = new JSONParser();
try (Reader reader = new FileReader("data.json")){
Object obj = jsonParser.parse(reader);
JSONArray gamesJson = (JSONArray) obj;
//System.out.println(gamesJson);
gamesJson.forEach(game -> parseMinesweeperGame((JSONObject) game, games));
}catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return new MinesweeperGames(games);
}
/**
* Ricompone un game da un JSONObject, viene utilizzato in MinesweeperStore.load
*/
private static ArrayList<MinesweeperGame> parseMinesweeperGame(JSONObject gameJson, ArrayList<MinesweeperGame> games){
int id = (int) ((long) gameJson.get("id"));
int size = (int) ((long) gameJson.get("size"));
int numBomb = (int) ((long) gameJson.get("numBomb"));
int numBombRemain = (int) ((long) gameJson.get("numBombRemain"));
Boolean isDone = (Boolean) gameJson.get("isDone");
Boolean isWin = (Boolean) gameJson.get("isWin");
GridGame game = null;
if((JSONArray) gameJson.get("game") != null){
game = new GridGame(parseCell((JSONArray) gameJson.get("game"), size));
}
GridDisplay display = new GridDisplay(parseCell((JSONArray) gameJson.get("display"), size));
MinesweeperGame tmp = new MinesweeperGame(id, size, numBomb, numBombRemain, isDone, isWin, game, display);
games.add(tmp);
return games;
}
/**
* Ricompone una cella da un JSONArray, viene utilizzato in MinesweeperStore.parseMinesweeperGame
*/
private static Cell[][] parseCell(JSONArray gridJson, int size){
Cell grid[][] = new Cell[size][size];
for(int i = 0; i < size; i++){
JSONArray colJson = (JSONArray) gridJson.get(i);
for(int j = 0; j < size; j++){
JSONObject cellJson = (JSONObject) colJson.get(j);
String type = (String) cellJson.get("type");
if(type.equals("Hidden")){
Boolean marked = (Boolean) cellJson.get("marked");
grid[i][j] = new CellHidden(marked);
} else if (type.equals("CloseBomb")){
int numBombClose = (int) ((long) cellJson.get("numBombClose"));
grid[i][j] = new CellCloseBomb(numBombClose);
} else if (type.equals("Bomb")){
grid[i][j] = new CellBomb();
} else if (type.equals("Clear")){
grid[i][j] = new CellClear();
}
}
}
return grid;
}
}