Add reverse that allow to switch interior/exterior of a labyrinth
- invert create a new labyrinth walls are path of previous. - grow then new labrynth is bigger, else shrink.
This commit is contained in:
2
java/org/artisanlogiciel/games/maze/BrickCharRepr.java
Normal file
2
java/org/artisanlogiciel/games/maze/BrickCharRepr.java
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
package org.artisanlogiciel.games.maze;public class BrickCharRepr {
|
||||||
|
}
|
||||||
@@ -11,7 +11,8 @@ import java.util.Random;
|
|||||||
* openList if all moves from its position have not been resolved a node is
|
* openList if all moves from its position have not been resolved a node is
|
||||||
* tagged CLOSED when fully processed
|
* tagged CLOSED when fully processed
|
||||||
**/
|
**/
|
||||||
public class LabyModel implements WallsProvider {
|
public class LabyModel
|
||||||
|
implements WallsProvider, MovesProvider {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WARNING don't change those values, they are used as it is for
|
* WARNING don't change those values, they are used as it is for
|
||||||
@@ -704,6 +705,10 @@ public class LabyModel implements WallsProvider {
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public short getMoves(int x, int y)
|
||||||
|
{
|
||||||
|
return getPath(x,y);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* is there a wall in that direction ?
|
* is there a wall in that direction ?
|
||||||
**/
|
**/
|
||||||
@@ -764,4 +769,76 @@ public class LabyModel implements WallsProvider {
|
|||||||
return onewaywall ? ( ! hasWallInDirection(x,y, (short) (direction << FLAGLENGTH)) ) : ((getWalls(x, y) & direction ) == 0);
|
return onewaywall ? ( ! hasWallInDirection(x,y, (short) (direction << FLAGLENGTH)) ) : ((getWalls(x, y) & direction ) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean reverse(boolean grow)
|
||||||
|
{
|
||||||
|
// use current moves as walls ... and rebuild new direction from it.
|
||||||
|
|
||||||
|
int deltasize = grow ? 1 : -1;
|
||||||
|
int newwidth = getWidth() + deltasize;
|
||||||
|
int newheight = getHeight() + deltasize;
|
||||||
|
int delta = grow ? 1 : 0;
|
||||||
|
|
||||||
|
if ( ( newwidth < 1) || (newheight < 1 ) )
|
||||||
|
{
|
||||||
|
System.err.println("Can't reverse, too small");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
short newt[][] = new short[newwidth][newheight];
|
||||||
|
short[] rdmw = { Brick.RIGHT, Brick.DOWN };
|
||||||
|
short[] lumw = { Brick.UP, Brick.LEFT };
|
||||||
|
short[] ldmw = { Brick.LEFT, Brick.DOWN };
|
||||||
|
short[] urmw = { Brick.UP, Brick.RIGHT };
|
||||||
|
|
||||||
|
short[] allmoves = { LEFT,UP,RIGHT, DOWN };
|
||||||
|
for (int x = 0; x < getWidth() -1 ; x ++)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < getHeight() -1 ; y ++)
|
||||||
|
{
|
||||||
|
short walls = 0;
|
||||||
|
short ulwalls = (short) (t[x][y] >> FLAGLENGTH);
|
||||||
|
short rdwalls = (short) (t[x+1][y+1] >> FLAGLENGTH);
|
||||||
|
short ldwalls = (short) (t[x][y+1] >> FLAGLENGTH);
|
||||||
|
short ruwalls = (short) (t[x+1][y] >> FLAGLENGTH);
|
||||||
|
|
||||||
|
// invert moves to walls
|
||||||
|
for (int i = 0 ; i < 2; i ++)
|
||||||
|
{
|
||||||
|
if ((ulwalls & rdmw[i] ) != 0) {
|
||||||
|
walls |= lumw[i];
|
||||||
|
}
|
||||||
|
if ((rdwalls & lumw[i] ) != 0 ) {
|
||||||
|
walls |= rdmw[i];
|
||||||
|
}
|
||||||
|
if ((ldwalls & urmw[i] ) != 0) {
|
||||||
|
walls |= ldmw[i];
|
||||||
|
}
|
||||||
|
if ((ruwalls & ldmw[i] ) != 0 ) {
|
||||||
|
walls |= urmw[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// convert walls to moves, walls are actualy where there is no path : XOR to invert.
|
||||||
|
short moves = (short) ((walls ^ ( Brick.RIGHT | Brick.LEFT | Brick.UP | Brick.DOWN )) << FLAGLENGTH);
|
||||||
|
short finalmoves = 0;
|
||||||
|
if ( moves == 0 )
|
||||||
|
{
|
||||||
|
finalmoves = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (short m : allmoves) {
|
||||||
|
if ((moves & m) != 0) {
|
||||||
|
finalmoves |= m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newt[x+delta][y+delta] = finalmoves;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
width = newwidth;
|
||||||
|
height = newheight;
|
||||||
|
t = newt;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
11
java/org/artisanlogiciel/games/maze/MovesProvider.java
Normal file
11
java/org/artisanlogiciel/games/maze/MovesProvider.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package org.artisanlogiciel.games.maze;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get model represented by possible moves
|
||||||
|
*/
|
||||||
|
public interface MovesProvider {
|
||||||
|
|
||||||
|
/** return possible moves from this position */
|
||||||
|
short getMoves(int x, int y);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -45,6 +45,7 @@ implements StatusListener
|
|||||||
LabyModel model;
|
LabyModel model;
|
||||||
boolean autoSize;
|
boolean autoSize;
|
||||||
boolean statusEnable = true;
|
boolean statusEnable = true;
|
||||||
|
boolean mGrow = false;
|
||||||
|
|
||||||
MazeParams params = null;
|
MazeParams params = null;
|
||||||
JTextField statusField = null;
|
JTextField statusField = null;
|
||||||
@@ -150,6 +151,14 @@ implements StatusListener
|
|||||||
solving.resolve(solving.getWidth() - 1, solving.getHeight() - 1, maze);
|
solving.resolve(solving.getWidth() - 1, solving.getHeight() - 1, maze);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean reverse(boolean grow) {
|
||||||
|
boolean done = model.reverse(grow);
|
||||||
|
if ( done ) {
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
void goNorth() {
|
void goNorth() {
|
||||||
maze.goNorth();
|
maze.goNorth();
|
||||||
}
|
}
|
||||||
@@ -513,6 +522,18 @@ implements StatusListener
|
|||||||
|
|
||||||
resolveQuitBar.add(resolveButton);
|
resolveQuitBar.add(resolveButton);
|
||||||
|
|
||||||
|
JButton reverseButton = new JButton(labels.getString("reverse"));
|
||||||
|
reverseButton.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
//
|
||||||
|
addStatus("Reversing");
|
||||||
|
mGrow ^= reverse(mGrow);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
resolveQuitBar.add(reverseButton);
|
||||||
|
|
||||||
|
|
||||||
final JButton quitButton = new JButton(labels.getString("quit"));
|
final JButton quitButton = new JButton(labels.getString("quit"));
|
||||||
Action quitAction = new AbstractAction() {
|
Action quitAction = new AbstractAction() {
|
||||||
public void actionPerformed(ActionEvent evt) {
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
|||||||
@@ -11,4 +11,5 @@ save = Save
|
|||||||
quit = Quit
|
quit = Quit
|
||||||
title = A Maz ing
|
title = A Maz ing
|
||||||
seed = seed
|
seed = seed
|
||||||
load = Load
|
load = Load
|
||||||
|
reverse = Reverse
|
||||||
@@ -11,4 +11,5 @@ save = Sauver
|
|||||||
quit = Quitter
|
quit = Quitter
|
||||||
title = La Bireinte
|
title = La Bireinte
|
||||||
seed = graine
|
seed = graine
|
||||||
load = Charger
|
load = Charger
|
||||||
|
reverse = Inverse
|
||||||
Reference in New Issue
Block a user