|
/**
* Board.java
*
* @author Martin Klöckner, Daniel Höpfl
*/
package SpringerPack;
/**
* class which saves the data while Backtracking
* it also implements a locking-mechanism, because of
* the thread of the calculation the output thread
* could access the fields at the same time which cause
* a wrong output on the board on the screen.
*/
public class Board {
private int mSize;
private int mFeld[][] = null;
public Board(int inSize)
{
mSize = inSize;
if(mSize < 5)
mSize = 5;
mFeld = new int[mSize+4][mSize+4];
for(int x=0;x < mSize; x++)
for(int y=0; y < mSize; y++)
setXY(x, y, 0);
for(int i=-2;i < mSize+2;i++)
{
// left
setXY(-1, i, -1);
setXY(-2, i, -1);
// up
setXY(i, -1, -1);
setXY(i, -2, -1);
// right
setXY(mSize+0, i, -1);
setXY(mSize+1, i, -1);
// down
setXY(i, mSize+0, -1);
setXY(i, mSize+1, -1);
}
}
/**
* InnerClass which solves the locking-problem, described above
*/
class mInnerLockClass {
int mLocked = 0;
public synchronized boolean setLock()
{
if(mLocked == 0)
{
mLocked++;
return true;
}
return false;
}
public synchronized void unsetLock()
{
mLocked--;
}
};
private mInnerLockClass mBoardLock = new mInnerLockClass();
/**
* locks the board. so nobody can access data, after this call
*/
public void lockBoard()
{
while(!mBoardLock.setLock())
;
}
/**
* unlocks the board. everybody is able again to access the data of the Board-class.
*/
public void unlockBoard()
{
mBoardLock.unsetLock();
}
/**
* sets the value on a postion.
*
* @param inX X-coordinate of the data
*
* @param inY Y-coordinate of the data
*
* @param inValue value which is set
*/
public void setXY(int inX, int inY, int inValue)
{
if(inX < -2 ||
inY < -2 ||
inX > mSize+1 ||
inY > mSize+1)
{
return;
}
mFeld[inX+2][inY+2] = inValue;
}
/**
* returns the value of a postion
*
* @param inX X-coordinate of the data
*
* @param inY Y-coordinate of the data
*/
public int getXY(int inX, int inY)
{
if(inX < -2 ||
inY < -2 ||
inX > mSize+1 ||
inY > mSize+1)
{
return -1;
}
return mFeld[inX+2][inY+2];
}
}
|