In this assignment, you will practice using 2-D arrays and applying loops and conditionals.
1. To start, familiarize yourself with how Nonograms work such as this one on Goobix. A video showing your sample Nonogram code running is also available.
2. A starter project is provided: Nonogram.zip, as well as this Nonogram API for the intended code. The starter code does not compile yet! You need to complete the steps below.
1. sample.fxml is the FXML file for the GUI. You do not need to edit this file.
2. Main.java is where the execution starts.
3. Controller.java connects the GUI to the functional parts of the code.
4. PuzzlePool.java maintains a collection of puzzles available from the Data subfolder.
5. Puzzle.java maintains a single Nonogram puzzle.
Main.java
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("MyVersion");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
PuzzlePool.java
public class PuzzlePool {
private File[] files; //Array of files available in the Data subfolder
private ArrayListresults; //List of puzzle names that correspond to the Puzzle objects
/**
* Loads all the text files from the Data subfolder,
* and stores each file into the files array.
* Also stores all filenames (before .txt) as the puzzle's title/name.
*/
public void loadFiles() {
results = new ArrayList<>();
String path = Paths.get(".\Data").toString();
files = new File(path).listFiles();
//System.out.println(path);
for (File file : files) {
if (file.isFile()) {
results.add(file.getName());
}
}
}
/**
* Selects a random puzzle from the collection of Puzzle objects available in array.
* @return the randomly selected puzzle
*/
public Puzzle getRandomPuzzle() {
int r = (new Random()).nextInt(results.size()); //new Random() here is an anonymous object
System.out.println("r=" + r); //used for debugging to know which is being chosen
return new Puzzle(files[r], 5); //game currently handles 5x5 puzzles only
}
}