jadex.web.examples.puzzle.Board Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-web Show documentation
Show all versions of jadex-applications-web Show documentation
Jadex examples that can be deployed as web application.
The newest version!
package jadex.web.examples.puzzle;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jadex.commons.beans.PropertyChangeListener;
import jadex.commons.beans.PropertyChangeSupport;
/**
* The board containing places, pieces and played moves.
*/
public class Board implements Serializable, Cloneable
{
//-------- attributes --------
/** The moves. */
protected List moves;
/** The pieces. */
protected Map pieces;
/** The size. */
protected int size;
/** The hole position. */
protected Position hole_pos;
/** The helper object for bean events. */
public PropertyChangeSupport pcs;
//-------- constructors --------
/**
* Create a new board.
*/
public Board()
{
this(5);
}
/**
* Create a new board.
*/
public Board(int size)
{
this.moves = new ArrayList();
this.pieces = new HashMap();
this.size = size;
//this.pcs = new ConcurrentPropertyChangeSupport(this);
this.pcs = new PropertyChangeSupport(this);
// Initialize the board with pieces.
int middle = size/2;
this.hole_pos = new Position(middle, middle);
for(int x=0; x=middle && y>=middle)
{
//System.out.println("b: "+x+" "+y+" ");
pieces.put(new Position(x, y), new Piece(false));
}
}
}
}
}
/**
* Get a piece for a location.
*/
public Piece getPiece(Position pos)
{
return pieces.get(pos);
}
/**
* Get possible moves.
* @return Get all possible move.
*/
public List getPossibleMoves()
{
List ret = new ArrayList();
int hx = hole_pos.getX();
int hy = hole_pos.getY();
int[] cols = new int[]{-1,-1,1,1,-1,-1,1,1};
Position[] fig_pos = new Position[]{
new Position(hx+1, hy), // b
new Position(hx, hy+1), // b
new Position(hx-1, hy), // w
new Position(hx, hy-1), // w
new Position(hx+2, hy), // b
new Position(hx, hy+2), // b
new Position(hx-2, hy), // w
new Position(hx, hy-2) // w
};
for(int i=0; i=fig_pos.length/2;
Piece piece = getPiece(fig_pos[i]);
if(piece!=null)
{
int white_piece = piece.isWhite()?1 : -1;
if(white_piece==white)
{
if(!jump)
{
assert isPossibleMove(new Move(fig_pos[i], hole_pos));
ret.add(new Move(fig_pos[i], hole_pos));
}
else
{
Piece jp = getPiece(fig_pos[i-fig_pos.length/2]);
if(jp!=null && jp.isWhite()!=piece.isWhite())
{
assert isPossibleMove(new Move(fig_pos[i], hole_pos));
ret.add(new Move(fig_pos[i], hole_pos));
}
}
}
}
}
//assert ret.equals(getOldPossibleMoves()):ret+" "+getOldPossibleMoves();
//System.out.println("PosMovs: "+ret);
return ret;
}
/**
* Do a move.
* @param move The move.
*/
public boolean move(Move move)
{
if (!isPossibleMove(move))
{
return false;
}
Piece piece = getPiece(move.getStart());
pieces.remove(move.getStart());
pieces.put(move.getEnd(), piece);
moves.add(move);
hole_pos = move.getStart();
pcs.firePropertyChange("solution", null, move);
return true;
}
/**
* Takeback a move.
*/
public boolean takeback()
{
if (moves.size() == 0)
{
return false;
}
Move move = moves.get(moves.size()-1);
Piece piece = getPiece(move.getEnd());
pieces.remove(move.getEnd());
pieces.put(move.getStart(), piece);
moves.remove(moves.size()-1);
hole_pos = move.getEnd();
pcs.firePropertyChange("solution", null, move);
return true;
}
/**
* Test if it is a solution.
* @return True, if solution.
*/
public boolean isSolution()
{
int middle = size/2;
if (!isFreePosition(new Position(middle, middle)))
{
return false;
}
boolean ret = true;
for(int y=0; y= middle && y >= middle && !getPiece(new Position(x, y)).isWhite())
{
ret = false;
}
}
}
}
return ret;
}
/**
* Get all moves made so far.
*/
public synchronized List getMoves()
{
return Collections.unmodifiableList(moves);
}
/**
* Get all moves made so far.
*/
public synchronized Move getLastMove()
{
return moves.size()>0? moves.get(moves.size()-1): null;
}
/**
* Test if the last move was with a white piece.
* When no move was made, it return true.
* @return True, is last move was with white piece.
*/
public synchronized boolean wasLastMoveWhite()
{
boolean ret = true;
if(moves.size()>0)
{
Move last = moves.get(moves.size()-1);
ret = getPiece(last.getEnd()).isWhite();
}
return ret;
}
/**
* Test if aposition is free.
*/
public synchronized boolean isFreePosition(Position pos)
{
int middle = size/2;
int x = pos.getX();
int y = pos.getY();
// There is no piece on the square and the square is
// on the board and it is no forbidden position.
return pieces.get(pos)==null
&& (x>=0 && y>=0 && x=middle && y>=middle);
}
/**
* Get a piece for a location.
*/
protected boolean isPossibleMove(Move move)
{
// Check if on start position is a piece.
// Check if the end position is free.
Piece piece = getPiece(move.getStart());
if (piece == null || !isFreePosition(move.getEnd()))
{
return false;
}
int turn = piece.isWhite()? 1: -1;
int xs = move.getStart().getX();
int ys = move.getStart().getY();
int xe = move.getEnd().getX();
int ye = move.getEnd().getY();
// Check if normal move and check if jump move.
boolean normalmove = ((xe-xs)*turn==1 && ye-ys==0) || ((ye-ys)*turn==1 && xe-xs==0);
Position xin = new Position(xs+turn, ys);
Position yin = new Position(xs, ys+turn);
boolean jumpmove = ((xe-xs)*turn==2 && ye-ys==0 && !isFreePosition(xin) && getPiece(xin).isWhite()!=piece.isWhite())
|| ((ye-ys)*turn==2 && xe-xs==0 && !isFreePosition(yin) && getPiece(yin).isWhite()!=piece.isWhite());
if (!normalmove && !jumpmove)
{
return false;
}
/*if(!(((xe-xs)*turn==1 && ye-ys==0) || ((ye-ys)*turn==1 && xe-xs==0))
&& !(((xe-xs)*turn==2 && ye-ys==0 && isFreePosition(new Position(xs+turn, ys)))
|| ((ye-ys)*turn==2 && xe-xs==0 && isFreePosition(new Position(xs, ys+turn)))))
return false;*/
return true;
}
/**
* Get the board size.
*/
public int getSize()
{
return size;
}
/**
* Get the current board position.
*/
public List getCurrentPosition()
{
List ret = new ArrayList();
for(int y=0; y();
clone.pieces = new HashMap();
clone.pcs = new PropertyChangeSupport(this);
for(int i=0; i moves = board.getPossibleMoves();
System.out.println(moves);
board.move((Move)moves.get(0));
moves = board.getPossibleMoves();
System.out.println(moves);
board.move((Move)moves.get(0));
System.out.println(board.isPossibleMove(new Move(new Position(2, 0), new Position(2, 2))));
System.out.println(board.isPossibleMove(new Move(new Position(4, 2), new Position(5,2))));
board.move((Move)moves.get(0));
System.out.println(board.getPossibleMoves());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy