diff --git a/Thud/build.gradle b/Thud/build.gradle index b5aa8f4..cfb3af8 100644 --- a/Thud/build.gradle +++ b/Thud/build.gradle @@ -1,4 +1,4 @@ -version = '1.0' +version = '1.1' apply plugin:'application' ext.mainClass = mainClassName = "org.saucistophe.thud.display.Display" diff --git a/Thud/src/main/java/org/saucistophe/thud/display/Display.java b/Thud/src/main/java/org/saucistophe/thud/display/Display.java index c9bba88..5f6d960 100644 --- a/Thud/src/main/java/org/saucistophe/thud/display/Display.java +++ b/Thud/src/main/java/org/saucistophe/thud/display/Display.java @@ -26,6 +26,7 @@ import static org.saucistophe.thud.model.Piece.DWARF; import static org.saucistophe.thud.model.Piece.EMPTY; import org.saucistophe.thud.model.boards.Board; +import static org.saucistophe.thud.model.boards.Board.readFromStream; import org.saucistophe.thud.model.players.NegamaxPlayer; import org.saucistophe.thud.model.players.Player; @@ -39,6 +40,8 @@ public class Display */ private static final JFrame MAIN_FRAME = new JFrame(); + + /** The main display panel. */ @@ -84,8 +87,7 @@ public static void main(String[] args) { // Load the initial board. ClassLoader classLoader = Display.class.getClassLoader(); - File file = new File(classLoader.getResource("initialBoard.thud").getFile()); - initialBoard = readFromFile(file); + initialBoard = readFromStream(classLoader.getResourceAsStream("initialBoard.thud")); } catch (Exception ex) { diff --git a/Thud/src/main/java/org/saucistophe/thud/model/Piece.java b/Thud/src/main/java/org/saucistophe/thud/model/Piece.java index 8e3b4f4..e170aaf 100644 --- a/Thud/src/main/java/org/saucistophe/thud/model/Piece.java +++ b/Thud/src/main/java/org/saucistophe/thud/model/Piece.java @@ -13,13 +13,14 @@ private Piece(String text) public static Piece fromText(String text) { - for (Piece candidateValue : values()) + for (Piece candidateValue : values()) { - if(candidateValue.text.equals(text)) + if (candidateValue.text.equals(text)) { return candidateValue; } } + System.err.println("Unknown piece type " + text); return null; } } diff --git a/Thud/src/main/java/org/saucistophe/thud/model/boards/Board.java b/Thud/src/main/java/org/saucistophe/thud/model/boards/Board.java index 5446119..1ded305 100644 --- a/Thud/src/main/java/org/saucistophe/thud/model/boards/Board.java +++ b/Thud/src/main/java/org/saucistophe/thud/model/boards/Board.java @@ -1,9 +1,12 @@ package org.saucistophe.thud.model.boards; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.io.PrintWriter; -import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -17,6 +20,7 @@ import static org.saucistophe.thud.model.Piece.EMPTY; import static org.saucistophe.thud.model.Piece.OUT; import static org.saucistophe.thud.model.Piece.TROLL; +import org.saucistophe.utils.Constants; /** The board corresponds to a state of the game, and contains an 2D array of @@ -328,19 +332,24 @@ public void writeToFile(File outputFile) } /** - Reads a board from a thud! file. + Reads a board from a stream to a thud! file. - @param inputFile The file to read. + @param inputStream The stream of the file to read. @return The stored board. - @throws IOException In case of problems when acessing or reading the file. + @throws java.io.IOException When there's a problem reading the file. */ - public static Board readFromFile(File inputFile) throws IOException + public static Board readFromStream(InputStream inputStream) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Constants.ENCODING)); + + // Turn the file to an array of strings. + List lines = reader.lines().collect(Collectors.toList()); + // First check the longest line in the file. - int longestLine = Files.lines(inputFile.toPath()).mapToInt(String::length).max().getAsInt(); + int longestLine = lines.stream().mapToInt(String::length).max().getAsInt(); // Also get the number of lines. - int numberOfLines = (int) Files.lines(inputFile.toPath()).count(); + int numberOfLines = lines.size(); // TODO add something to decide which class. Board board = new RegularBoard(); @@ -348,22 +357,18 @@ public static Board readFromFile(File inputFile) throws IOException // Create the relevant square board. board.squares = new Piece[longestLine][numberOfLines]; - try (Stream lines = Files.lines(inputFile.toPath())) + int lineNumber = 0; + for (String line : lines) { - int lineNumber = 0; - // For each line: - for (String line : (Iterable) lines::iterator) + // For each character: + int charNumber = 0; + for (char c : line.toCharArray()) { - // For each character: - int charNumber = 0; - for (char c : line.toCharArray()) - { - // Turn the character to a piece. - board.squares[charNumber][lineNumber] = Piece.fromText("" + c); - charNumber++; - } - lineNumber++; + // Turn the character to a piece. + board.squares[charNumber][lineNumber] = Piece.fromText("" + c); + charNumber++; } + lineNumber++; } // Change the top-left corner to the playing side. @@ -389,6 +394,19 @@ public static Board readFromFile(File inputFile) throws IOException return board; } + /** + Reads a board from a thud! file. + + @param inputFile The file to read. + @return The stored board. + + @throws IOException In case of problems when acessing or reading the file. + */ + public static Board readFromFile(File inputFile) throws IOException + { + return readFromStream(new FileInputStream(inputFile)); + } + /** Conveniency method for getting a square's piece from its coordinate.