Skip to content
Snippets Groups Projects
Commit 5f6d9206 authored by steyer's avatar steyer
Browse files

GameBoardJson kész (nincs tesztelve)

parent 1dd3dbbb
No related branches found
No related tags found
No related merge requests found
package game;
import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
......
package game;
import java.io.File;
import java.io.*;
import javax.json.*;
public class GameBoardJson {
public static GameBoard loadFromFile(File f) {
return new GameBoard(10, 10); //TODO load
public static GameBoard loadFromFile(File f) throws FileNotFoundException {
FileInputStream fis = new FileInputStream(f);
JsonReader jr = Json.createReader(fis);
JsonObject boardObject = jr.readObject();
jr.close();
JsonObject sizeObject = boardObject.getJsonObject("size");
int height = sizeObject.getInt("height");
int width = sizeObject.getInt("width");
GameBoard board = new GameBoard(height, width);
JsonArray rows= boardObject.getJsonArray("cells");
for (int i = 0; i < height; i++) {
JsonArray currentRow = rows.getJsonArray(i);
for (int j = 0; j < width; j++) {
boolean currentCellAlive = currentRow.getBoolean(j);
if (currentCellAlive)
board.get(i,j).setAlive(true);
}
}
public static void saveToFile(GameBoard board, File destination) {
return board;
}
public static void saveToFile(GameBoard board, File destination) throws IOException {
int height = board.getHeight();
int width = board.getWidth();
......@@ -22,11 +43,18 @@ public class GameBoardJson {
}
JsonObject boardObject = Json.createObjectBuilder()
.add("File", destination.getName())
.add("Size",
.add("file", destination.getName())
.add("size",
Json.createObjectBuilder()
.add("Height", height)
.add("height", height)
.add("width", width))
.add("Cells", rowsBuilder.build()).build();
.add("cells", rowsBuilder.build()).build();
FileOutputStream fout = new FileOutputStream(destination);
JsonWriter jw = Json.createWriter(fout);
jw.write(boardObject);
jw.close();
}
}
package game;
public class GameBoardSaver {
//TODO JFileChooser
}
package game;
import javax.json.Json;
import javax.json.JsonWriter;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment