add Save Text support and rework package

- move maze within package maze
This commit is contained in:
philippe lhardy
2020-10-17 21:08:16 +02:00
parent c3410838e1
commit c69d068caf
24 changed files with 590 additions and 385 deletions

View File

@@ -27,9 +27,12 @@ import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.artisanlogiciel.games.*;
import org.artisanlogiciel.games.maze.*;
import org.artisanlogiciel.games.maze.persist.MazePersistRaw;
import org.artisanlogiciel.games.maze.solve.DirectionPosition;
import org.artisanlogiciel.games.maze.solve.MazeResolutionListener;
import org.artisanlogiciel.games.maze.solve.SolvingModel;
import org.artisanlogiciel.games.stl.Maze3dParams;
import org.artisanlogiciel.games.stl.Wall3d;
import org.artisanlogiciel.games.stl.Wall3dStream;
import org.artisanlogiciel.util.UTF8Control;
@@ -143,8 +146,12 @@ public class Display extends JFrame {
}
void resolve() {
SolvingModel solving = new SolvingModel(model);
// should transform current model to be a SolvingModel
model = solving;
model.reset();
model.resolve(model.getWidth() - 1, model.getHeight() - 1, maze);
solving.resolve(solving.getWidth() - 1, solving.getHeight() - 1, maze);
}
void goNorth() {
@@ -311,6 +318,21 @@ public class Display extends JFrame {
}
}
void saveText() {
File outfile = getFileForExtension("txt");
writeSentence("Saving to " + outfile + " ...");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile));
out.write(model.toLabyMap().toString().getBytes());
out.flush();
out.close();
writeSentence("... Done.");
} catch (IOException io) {
io.printStackTrace(System.err);
}
}
void addStatus(String pStatus)
{
statusField.setText(pStatus);
@@ -414,6 +436,16 @@ public class Display extends JFrame {
}
};
saveStlButton.addActionListener(saveStlAction);
final JButton saveTextButton = new JButton(labels.getString("save") + " txt");
Action saveTextAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
//
addStatus("save txt");
setMazeName(saveName.getText());
saveText();
}
};
saveTextButton.addActionListener(saveTextAction);
stlsettings = new Maze3dSettings(new Maze3dParams());
@@ -425,6 +457,7 @@ public class Display extends JFrame {
saveMenu.add(stlsettings);
saveMenu.add(saveStlButton);
saveMenu.add(saveImcButton);
saveMenu.add(saveTextButton);
return saveMenu;
@@ -601,7 +634,7 @@ public class Display extends JFrame {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(infile);
model = new LabyModel("raw", inputStream);
model = new MazePersistRaw().parseInputStream("raw",inputStream);
} catch (IOException io) {
io.printStackTrace(System.err);
statusField.setText("[ERROR] Can't load " + infile.getAbsolutePath());
@@ -851,55 +884,43 @@ public class Display extends JFrame {
}
}
private void proceed()
{
// should redraw ...
invalidate();
repaint();
checkExit();
}
void goNorth() {
resetResolution();
if ((map.getWalls(sX, sY) & Brick.UP) == 0) {
if (map.canMoveInDirection(sX, sY,Brick.UP)) {
sY = sY - 1;
// should redraw ...
invalidate();
repaint();
checkExit();
proceed();
}
}
void goSouth() {
resetResolution();
if ((map.getWalls(sX, sY) & Brick.DOWN) == 0) {
if (map.canMoveInDirection(sX, sY,Brick.DOWN)) {
sY = sY + 1;
// should redraw ...
invalidate();
repaint();
checkExit();
proceed();
}
}
void goEast() {
resetResolution();
if ((map.getWalls(sX, sY) & Brick.RIGHT) == 0) {
if (map.canMoveInDirection(sX, sY, Brick.RIGHT)) {
sX = sX + 1;
// should redraw ...
invalidate();
repaint();
checkExit();
proceed();
}
}
void goWest() {
resetResolution();
if ((map.getWalls(sX, sY) & Brick.LEFT) == 0) {
if (map.canMoveInDirection(sX, sY, Brick.LEFT)) {
sX = sX - 1;
// should redraw ...
invalidate();
repaint();
checkExit();
proceed();
}
}
/**
@@ -1113,7 +1134,8 @@ public class Display extends JFrame {
addStatus("Saving to " + outfile + " ...");
try {
FileOutputStream out = new FileOutputStream(outfile);
model.streamOut("raw", out);
MazePersistRaw persist = new MazePersistRaw(model);
persist.streamOut("raw", out);
out.flush();
out.close();
addStatus("... Done.");
@@ -1135,7 +1157,7 @@ public class Display extends JFrame {
if ((pArgs.length > 0) && (pArgs[0].length() > 0)) {
try {
model = new LabyModel("raw", new FileInputStream(pArgs[0]));
model = new MazePersistRaw().parseInputStream("raw",new FileInputStream(pArgs[0]));
} catch (IOException io) {
io.printStackTrace(System.err);
System.exit(1);