Split Display (gui) and Maze (work), prepare for another model
- prepare for a shorter storage model without any resolution
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package org.artisanlogiciel.games.maze.model;
|
||||
|
||||
import org.artisanlogiciel.games.maze.MovesProvider;
|
||||
import org.artisanlogiciel.games.maze.WallsProvider;
|
||||
|
||||
public class HalfSquareModelCreator {
|
||||
|
||||
interface SetXY
|
||||
{
|
||||
void setXY(HalfSquareRasterModel model, int x, int y);
|
||||
}
|
||||
|
||||
HalfSquareRasterModel createFromFunc(int width, int height, SetXY func)
|
||||
{
|
||||
HalfSquareRasterModel model = new HalfSquareRasterModel(width,height);
|
||||
for (int y=0; y<model.getHeight();y++)
|
||||
{
|
||||
for (int x=0; x<model.getWidth();x++)
|
||||
{
|
||||
func.setXY(model,x,y);
|
||||
}
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
public HalfSquareRasterModel createFromWallsProvider(WallsProvider provider)
|
||||
{
|
||||
SetXY setWalls = ( model, x, y ) -> { model.setWalls(x,y, provider.getWalls(x,y)); };
|
||||
return createFromFunc( provider.getWidth(), provider.getHeight(), setWalls);
|
||||
}
|
||||
|
||||
public HalfSquareRasterModel createFromMovesProvider(MovesProvider provider)
|
||||
{
|
||||
SetXY setMoves = ( model, x, y ) -> { model.setMoves(x,y,provider.getMoves(x,y)); };
|
||||
return createFromFunc( provider.getWidth(), provider.getHeight(), setMoves);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package org.artisanlogiciel.games.maze.model;
|
||||
|
||||
import org.artisanlogiciel.games.maze.MovesProvider;
|
||||
import org.artisanlogiciel.games.maze.WallsProvider;
|
||||
|
||||
/**
|
||||
* minimal model without repetition
|
||||
* keep only left and down wall and rely on neightbor squeres to obtain right and up
|
||||
* left down compatible with Bricks
|
||||
*
|
||||
* LEFT 01
|
||||
* DOWN 10
|
||||
*
|
||||
* 2 bits for walls
|
||||
*
|
||||
* bit value :
|
||||
* - set : there is a wall , no move in that direction
|
||||
* - unset : move is possible , there is no wall
|
||||
*/
|
||||
public class HalfSquareRasterModel
|
||||
implements WallsProvider,
|
||||
MovesProvider,
|
||||
WidthHeightProvider
|
||||
{
|
||||
int LEFT = 0x01;
|
||||
int DOWN = 0x02;
|
||||
// RIGHT 4
|
||||
// UP 8
|
||||
|
||||
// 64 bits for long, 4 bits reserved
|
||||
int USED_BITS = 60;
|
||||
// 2 bits for LEFT UP, ( half square )
|
||||
int BITSX = 2;
|
||||
|
||||
int width;
|
||||
int height;
|
||||
|
||||
long lines[][];
|
||||
|
||||
public HalfSquareRasterModel(int width, int height)
|
||||
{
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
lines = new long[height][(BITSX * width)/USED_BITS];
|
||||
}
|
||||
|
||||
public void setLeftDown(int x, int y, short moves)
|
||||
{
|
||||
if (( x < width ) && ( y < height)) {
|
||||
int index = x % (USED_BITS / BITSX);
|
||||
long mask = 0xffffffff ^ ((moves & 0x3) << index);
|
||||
lines[y][(BITSX * x) / USED_BITS] &= mask;
|
||||
}
|
||||
}
|
||||
|
||||
public void setWalls(int x, int y, short moves)
|
||||
{
|
||||
short walls = (short) ~ moves;
|
||||
setMoves(x,y,moves);
|
||||
}
|
||||
|
||||
public void setMoves(int x, int y, short moves)
|
||||
{
|
||||
setLeftDown(x,y,moves); // left up
|
||||
setLeftDown(x+1,y,(short) (moves & (LEFT << BITSX))); // right
|
||||
setLeftDown(x,y-1,(short) (moves & (DOWN << BITSX))); // up
|
||||
}
|
||||
|
||||
public long getLeftDown(int x, int y)
|
||||
{
|
||||
if (( x < width ) && ( y < height)) {
|
||||
// left up
|
||||
long line = lines[y][(BITSX * x) / USED_BITS];
|
||||
int index = x % (USED_BITS / BITSX);
|
||||
return ((line >> index) & (LEFT | DOWN));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public short getWalls(int x, int y)
|
||||
{
|
||||
return (short) (
|
||||
getLeftDown(x,y) |
|
||||
( getLeftDown(x+1,y) & LEFT ) << USED_BITS | // right
|
||||
( getLeftDown( x, y-1) & DOWN) << USED_BITS // up
|
||||
);
|
||||
}
|
||||
|
||||
public short getMoves(int x, int y)
|
||||
{
|
||||
// moves are where there is no walls ...
|
||||
return (short) ((~ getWalls(x,y)) & 0x0f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.artisanlogiciel.games.maze.model;
|
||||
|
||||
import org.artisanlogiciel.games.maze.MovesProvider;
|
||||
import org.artisanlogiciel.games.maze.WallsProvider;
|
||||
|
||||
public interface LabyModelProvider
|
||||
extends WallsProvider,
|
||||
MovesProvider
|
||||
{
|
||||
/**
|
||||
* add a new direction(s) exiting ones are kept
|
||||
*/
|
||||
void addDirection(int x, int y, short path);
|
||||
|
||||
/* set direction(s) existing ones are lost */
|
||||
void setDirection(int x, int y, short path);
|
||||
|
||||
/**
|
||||
* is there no wall in that direction ?
|
||||
**/
|
||||
boolean canMoveInDirection(int x, int y, short direction);
|
||||
|
||||
/** like getMoves but include resolved information */
|
||||
short getPath(int x, int y);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.artisanlogiciel.games.maze.model;
|
||||
|
||||
public interface WidthHeightProvider {
|
||||
|
||||
int getWidth();
|
||||
|
||||
int getHeight();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user