Skip to content

Commit

Permalink
[THUD] Fixed loading files from inside the jar, and 1.1 Release.
Browse files Browse the repository at this point in the history
  • Loading branch information
Saucistophe committed Dec 17, 2015
1 parent 1652cc1 commit 3436cf8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Thud/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = '1.0'
version = '1.1'

apply plugin:'application'
ext.mainClass = mainClassName = "org.saucistophe.thud.display.Display"
Expand Down
6 changes: 4 additions & 2 deletions Thud/src/main/java/org/saucistophe/thud/display/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,6 +40,8 @@ public class Display
*/
private static final JFrame MAIN_FRAME = new JFrame();



/**
The main display panel.
*/
Expand Down Expand Up @@ -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)
{
Expand Down
5 changes: 3 additions & 2 deletions Thud/src/main/java/org/saucistophe/thud/model/Piece.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
58 changes: 38 additions & 20 deletions Thud/src/main/java/org/saucistophe/thud/model/boards/Board.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -328,42 +332,43 @@ 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<String> 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();

// Create the relevant square board.
board.squares = new Piece[longestLine][numberOfLines];

try (Stream<String> lines = Files.lines(inputFile.toPath()))
int lineNumber = 0;
for (String line : lines)
{
int lineNumber = 0;
// For each line:
for (String line : (Iterable<String>) 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.
Expand All @@ -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.
Expand Down

0 comments on commit 3436cf8

Please sign in to comment.