prepare for hexagon display & other models
- display cell as hexagon is Display.mHexagon if set ( not default ) will need a checkbox to set this - various new models preparation...
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package org.artisanlogiciel.games.maze.model;
|
||||
|
||||
/**
|
||||
* store an array booleans in a long
|
||||
*/
|
||||
|
||||
public class BooleanLongSet {
|
||||
|
||||
long[] internal;
|
||||
|
||||
// 62 bits for long, 2 bits reserved
|
||||
int USED_BITS = 62;
|
||||
int width;
|
||||
|
||||
public BooleanLongSet(int width, int USED_BITS)
|
||||
{
|
||||
internal = new long[width];
|
||||
this.width = width;
|
||||
this.USED_BITS = USED_BITS;
|
||||
}
|
||||
|
||||
boolean isSet(int x) {
|
||||
int group = x / USED_BITS;
|
||||
if (group < width) {
|
||||
int pos = x % USED_BITS;
|
||||
long mask = 1 << pos;
|
||||
return (internal[group] & mask) != 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void set(int x, boolean value)
|
||||
{
|
||||
int group = x / USED_BITS;
|
||||
if (group < width) {
|
||||
int pos = x % USED_BITS;
|
||||
long mask = 1 << pos;
|
||||
internal[group] &= mask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.artisanlogiciel.games.maze.model;
|
||||
|
||||
public interface HalfSquareProvider {
|
||||
|
||||
long getLeftDown(int x, int y);
|
||||
}
|
||||
@@ -16,11 +16,13 @@ import org.artisanlogiciel.games.maze.WallsProvider;
|
||||
* 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
|
||||
WidthHeightProvider,
|
||||
HalfSquareProvider
|
||||
{
|
||||
int LEFT = 0x01;
|
||||
int DOWN = 0x02;
|
||||
@@ -42,7 +44,16 @@ public class HalfSquareRasterModel
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
lines = new long[height][(BITSX * width)/USED_BITS];
|
||||
lines = new long[height][getLongs()];
|
||||
}
|
||||
|
||||
public int getLongs()
|
||||
{
|
||||
return (BITSX * width)/USED_BITS;
|
||||
}
|
||||
|
||||
public long[][] getLines() {
|
||||
return lines;
|
||||
}
|
||||
|
||||
public void setLeftDown(int x, int y, short moves)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package org.artisanlogiciel.games.maze.model;
|
||||
|
||||
public class HexagonModelProvider {
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package org.artisanlogiciel.games.maze.model;
|
||||
|
||||
import org.artisanlogiciel.games.maze.Brick;
|
||||
import org.artisanlogiciel.games.maze.MovesProvider;
|
||||
import org.artisanlogiciel.games.maze.WallsProvider;
|
||||
|
||||
public class LineColumnModel
|
||||
implements WallsProvider,
|
||||
MovesProvider
|
||||
{
|
||||
|
||||
final BooleanLongSet lines[];
|
||||
final BooleanLongSet columns[];
|
||||
|
||||
public LineColumnModel(int width, int height)
|
||||
{
|
||||
lines = new BooleanLongSet[height];
|
||||
columns = new BooleanLongSet[width];
|
||||
}
|
||||
|
||||
BooleanLongSet getLine(int column)
|
||||
{
|
||||
BooleanLongSet l = lines[column];
|
||||
if ( l == null )
|
||||
{
|
||||
l= new BooleanLongSet(getWidth(),63);
|
||||
lines[column] = l;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
BooleanLongSet getColumn(int line)
|
||||
{
|
||||
BooleanLongSet c = columns[line];
|
||||
if ( c == null )
|
||||
{
|
||||
c = new BooleanLongSet(getHeight(),63);
|
||||
columns[line] = c;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getWalls(int x, int y) {
|
||||
return (short) (
|
||||
(getLine(y).isSet(x) ? Brick.UP : 0)
|
||||
+ (getColumn(x).isSet(y) ? Brick.LEFT : 0)
|
||||
+ (getLine(y+1).isSet(x) ? Brick.DOWN : 0)
|
||||
+ (getColumn(x+1).isSet(y) ? Brick.RIGHT : 0)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
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 columns.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return lines.length;
|
||||
}
|
||||
|
||||
public void setWalls(int x, int y, short walls)
|
||||
{
|
||||
getLine(y).set(x, (walls & Brick.UP) != 0);
|
||||
getColumn(x).set(y, (walls & Brick.LEFT) != 0);
|
||||
getLine(y+1).set(x, (walls & Brick.DOWN) != 0);
|
||||
getColumn(x+1).set(y, (walls & Brick.RIGHT) != 0);
|
||||
}
|
||||
|
||||
public void setMoves(int x, int y, short moves)
|
||||
{
|
||||
short walls = (short) ~ moves;
|
||||
setWalls(x,y,walls);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user