- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDriver.java
More file actions
Latest commit
97 lines (85 loc) · 3.92 KB
/
Copy pathFileDriver.java
File metadata and controls
97 lines (85 loc) · 3.92 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
importjavax.swing.*;
importjavax.swing.filechooser.FileNameExtensionFilter;
importjava.io.*;
importjava.util.Scanner;
/**
* This class is used to save and load games.
* It is a subclass of the SudokuGUI class.
* It inherits all the methods and variables of the SudokuGUI class.
*
* JFILECHOOSER has been refernced from Oracle.com(2024).
* Credited in the References in Report
*
* @author Arun Kumar Sekar
* @version January 2024
*
*/
publicclassFileDriver {
/**
* This method loads the previously saved game from the file (sudukoGUI.txt) in the same state it was saved in.
* Constructor for objects of class FileDriver
* Calls the constructor of the SudokuGUI class
* Uses the file chooser from the swing package to enable the external file browser
* Displays the file chooser to the user
*
* @throws FileNotFoundException if the file is not found
*/
publicstaticvoidsaveGame(SudokutheGame, intgridSize, JTextAreastatusArea) {
JFileChooserfileChooser = newJFileChooser();
fileChooser.setFileFilter(newFileNameExtensionFilter("Sudoku Files", "txt"));
intoption = fileChooser.showSaveDialog(null);
if (option == JFileChooser.APPROVE_OPTION) {
FilesaveFile = fileChooser.getSelectedFile();
if (!saveFile.getName().endsWith(".txt")) {
saveFile = newFile(saveFile.getParentFile(), saveFile.getName() + ".txt");
}
try (PrintWriterwriter = newPrintWriter(saveFile)) {
writer.println(gridSize);
for (inti = 0; i < gridSize; i++) {
for (intj = 0; j < gridSize; j++) {
StringcellState = theGame.getIndividualMove(i, j);
writer.println(i + " " + j + " " + cellState);
}
}
statusArea.setText("Game saved to " + saveFile.getName());
} catch (FileNotFoundExceptione) {
statusArea.setText("Error saving game: " + e.getMessage());
}
}
}
/**
* This method loads the previously saved game from the file (sudukoGUI.txt) in the same state it was saved in.
* Constructor for objects of class FileDriver
* Calls the constructor of the SudokuGUI class
* Uses the file chooser from the swing package to enable the external file browser
* Displays the file chooser to the user
*
* @throws FileNotFoundException if the file is not found
*/
publicstaticvoidloadGame(SudokutheGame, intgridSize, JTextAreastatusArea, SudokuGUIsudokuGUI) {
JFileChooserfileChooser = newJFileChooser();
fileChooser.setFileFilter(newFileNameExtensionFilter("Sudoku Files", "txt"));
intoption = fileChooser.showOpenDialog(null); // parent component to JFileChooser is null
if (option == JFileChooser.APPROVE_OPTION) {
FileloadFile = fileChooser.getSelectedFile();
try (Scannerscanner = newScanner(loadFile)) {
intsavedGridSize = scanner.nextInt();
if (savedGridSize != gridSize) {
thrownewIllegalStateException("Saved game size does not match current game size.");
}
while (scanner.hasNextInt()) {
introw = scanner.nextInt();
intcol = scanner.nextInt();
Stringvalue = scanner.next();
theGame.makeMove(Integer.toString(row), Integer.toString(col), value);
}
sudokuGUI.displayGame();
statusArea.setText("Game loaded from " + loadFile.getName());
} catch (FileNotFoundExceptione) {
statusArea.setText("Error loading game: " + e.getMessage());
} catch (IllegalStateExceptione) {
statusArea.setText(e.getMessage());
}
}
}
}// End of FileDriver