|
|
@@ -1,15 +1,32 @@ |
|
|
|
package SnakePkg;
|
|
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
import java.lang.Math;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The game's board representation
|
|
|
|
* @class Board
|
|
|
|
* @brief The game's board representation.
|
|
|
|
*
|
|
|
|
* The board in a square collection of tiles numbered in a
|
|
|
|
* boustrophedon (zig-zag) way. A number of snakes, ladders
|
|
|
|
* and apples which called elements are placed on the board
|
|
|
|
* for each game.
|
|
|
|
*
|
|
|
|
* @author Christos Choutouridis 8997
|
|
|
|
* @email cchoutou@ece.auth.gr
|
|
|
|
*/
|
|
|
|
public class Board {
|
|
|
|
/** Constants */
|
|
|
|
/**@{ */
|
|
|
|
static final int POINTS_MAX = 20; /**< The maximum absolute number of points for each apple */
|
|
|
|
static final int POINTS_STEP = 5; /**< The difference between different apple points */
|
|
|
|
/**@} */
|
|
|
|
|
|
|
|
/** @name Constructors */
|
|
|
|
/** @{ */
|
|
|
|
/** Default ctor */
|
|
|
|
/** A doing nothing default constructor
|
|
|
|
* @warining Avoid using this constructor as it requires all setters(or copy)
|
|
|
|
* and @ref createBoard() to be called after.
|
|
|
|
*/
|
|
|
|
Board () {
|
|
|
|
N = M =0;
|
|
|
|
tiles = null;
|
|
|
@@ -19,47 +36,56 @@ public class Board { |
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main constructor
|
|
|
|
* We make some simple input checking before memory allocation.
|
|
|
|
* @note
|
|
|
|
* May throw BadBoardInput
|
|
|
|
* @brief Creates a board for game
|
|
|
|
*
|
|
|
|
* This constructor allocates the memory for the board and elements and
|
|
|
|
* creates a board by placing all required elements on the board.
|
|
|
|
*
|
|
|
|
* @param N The row for the board
|
|
|
|
* @param M The columns of the board
|
|
|
|
* @param numOfSnakes Number of snakes to place
|
|
|
|
* @param numOfLadders Number of ladders to place
|
|
|
|
* @param numOfApples Number of Apples to place
|
|
|
|
*
|
|
|
|
* @warning
|
|
|
|
* We call @ref createBoard() inside this constructor in order for
|
|
|
|
* the board to be in "playable condition". This is preferable by the author.
|
|
|
|
* A constructor should(if possible) to leave the object in a usable condition.
|
|
|
|
* In order to follow the project requirements we create this functionality in a
|
|
|
|
* separate function @ref createBoard(). We believe that if a user can make a
|
|
|
|
* mistake he eventually will do it sometime. Here, if we leave the createBoard()
|
|
|
|
* call to user we are enabling him to make it.
|
|
|
|
*/
|
|
|
|
Board (int N, int M, int numOfSnakes, int numOfLadders, int numOfApples)
|
|
|
|
throws BadBoardInput {
|
|
|
|
// Input checking
|
|
|
|
if (numOfApples + numOfLadders*2 + numOfSnakes*2 >= N*M/2)
|
|
|
|
throw new BadBoardInput("Too many Apples, Snakes and ladders");
|
|
|
|
Board (int N, int M, int numOfSnakes, int numOfLadders, int numOfApples) {
|
|
|
|
// Init the board object
|
|
|
|
setN (N); // Input checked version (may throw)
|
|
|
|
setM (M); // Input checked version (may throw)
|
|
|
|
setN (N); // Input checked version (may throw)
|
|
|
|
setM (M); // Input checked version (may throw)
|
|
|
|
tiles = new int[N][M];
|
|
|
|
snakes = new Snake[numOfSnakes];
|
|
|
|
ladders = new Ladder[numOfLadders];
|
|
|
|
apples = new Apple[numOfApples];
|
|
|
|
createBoard (); // Complete board preparation and make all the element memory allocations
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor. We make a deep copy of B and we trust B's
|
|
|
|
* data to be valid
|
|
|
|
* @brief Copy constructor.
|
|
|
|
* We make a deep copy of B and we trust B's data to be valid.
|
|
|
|
* @param B The board we want to copy
|
|
|
|
* @note We don't use clone as long as we don't inherit Cloneable iface
|
|
|
|
* @note This requires Snake, Apple and Ladder copy constructors
|
|
|
|
*/
|
|
|
|
Board (Board B) {
|
|
|
|
this.N = B.getN();
|
|
|
|
this.M = B.getM();
|
|
|
|
N = B.getN();
|
|
|
|
M = B.getM();
|
|
|
|
tiles = new int[N][M];
|
|
|
|
snakes = new Snake[B.getSnakes().length];
|
|
|
|
ladders = new Ladder[B.getLadders().length];
|
|
|
|
apples = new Apple[B.getApples().length];
|
|
|
|
// Copy B's guts
|
|
|
|
for (int i =0 ; i<N ; ++i)
|
|
|
|
for (int j =0 ; j<M ; ++j)
|
|
|
|
tiles[i][j] = B.getTiles()[i][j];
|
|
|
|
for (int i =0 ; i<B.getSnakes().length ; ++i)
|
|
|
|
snakes[i] = B.getSnakes()[i];
|
|
|
|
for (int i =0 ; i<B.getLadders().length ; ++i)
|
|
|
|
ladders[i] = B.getLadders()[i];
|
|
|
|
for (int i =0 ; i<B.getApples().length ; ++i)
|
|
|
|
apples[i] = B.getApples()[i];
|
|
|
|
// Copy B's guts into new memory
|
|
|
|
setTiles(B.getTiles()); // primitive
|
|
|
|
copySnakes(B.getSnakes());
|
|
|
|
copyLadders(B.getLadders());
|
|
|
|
copyApples(B.getApples());
|
|
|
|
}
|
|
|
|
/** @} */
|
|
|
|
|
|
|
@@ -68,46 +94,27 @@ public class Board { |
|
|
|
|
|
|
|
/** Get value N */
|
|
|
|
int getN () { return N; }
|
|
|
|
/**
|
|
|
|
* Set value N
|
|
|
|
* @throws BadBoardInput
|
|
|
|
*/
|
|
|
|
void setN (int N) throws BadBoardInput {
|
|
|
|
// Input checking
|
|
|
|
if (N<=0)
|
|
|
|
throw new BadBoardInput("N is zero or Less");
|
|
|
|
this.N = N;
|
|
|
|
}
|
|
|
|
/** Set value N */
|
|
|
|
void setN (int N) { this.N = N; }
|
|
|
|
|
|
|
|
/** Get value M */
|
|
|
|
int getM () { return M; }
|
|
|
|
/**
|
|
|
|
* Set value M
|
|
|
|
* @throws BadBoardInput
|
|
|
|
*/
|
|
|
|
void setM (int M) throws BadBoardInput {
|
|
|
|
// Input checking
|
|
|
|
if (M<=0)
|
|
|
|
throw new BadBoardInput("M is zero or Less");
|
|
|
|
this.M = M;
|
|
|
|
}
|
|
|
|
/** Set value M */
|
|
|
|
void setM (int M) { this.M = M; }
|
|
|
|
|
|
|
|
/** Get reference to tiles */
|
|
|
|
int[][] getTiles () { return tiles; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set tiles (deep copy)
|
|
|
|
* Set tiles
|
|
|
|
* @param tiles Source of tiles to use
|
|
|
|
* @throws BadBoardInput
|
|
|
|
* @note This has to be called if the board is default constructed
|
|
|
|
*/
|
|
|
|
void setTiles (int[][] tiles) throws BadBoardInput {
|
|
|
|
// Input checking
|
|
|
|
if (tiles.length != N && tiles[0].length != M)
|
|
|
|
throw new BadBoardInput("tiles size missmatch");
|
|
|
|
void setTiles (int[][] tiles) {
|
|
|
|
// Check if we need allocation
|
|
|
|
if (this.tiles == null)
|
|
|
|
this.tiles = new int[N][M];
|
|
|
|
// Assign values (deep copy)
|
|
|
|
// Assign values
|
|
|
|
for (int i =0 ; i<N ; ++i)
|
|
|
|
for (int j =0 ; j<M ; ++j)
|
|
|
|
this.tiles[i][j] = tiles[i][j];
|
|
|
@@ -115,59 +122,69 @@ public class Board { |
|
|
|
|
|
|
|
/** Get reference to snakes */
|
|
|
|
Snake[] getSnakes() { return snakes; }
|
|
|
|
/**
|
|
|
|
* Set snakes
|
|
|
|
* @param snakes Reference to snakes to use
|
|
|
|
* @note This requires snakes must be allocated elsewhere.
|
|
|
|
*/
|
|
|
|
void setSnakes(Snake[] snakes) { this.snakes = snakes; }
|
|
|
|
|
|
|
|
/** Get reference to ladders */
|
|
|
|
Ladder[] getLadders() { return ladders; }
|
|
|
|
/**
|
|
|
|
* Set ladders
|
|
|
|
* @param ladders Reference to ladders to use
|
|
|
|
* @note This requires ladders must be allocated elsewhere.
|
|
|
|
*/
|
|
|
|
void setLadders(Ladder[] ladders) { this.ladders = ladders; }
|
|
|
|
|
|
|
|
/** Get reference to apples */
|
|
|
|
Apple[] getApples() { return apples; }
|
|
|
|
/**
|
|
|
|
* Set apples
|
|
|
|
* @param apples Reference to apples to use
|
|
|
|
* @note This requires apples must be allocated elsewhere.
|
|
|
|
*/
|
|
|
|
void setApples(Apple[] apples) { this.apples = apples; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set snakes (deep copy)
|
|
|
|
* Copy snakes (deep copy)
|
|
|
|
* @param snakes Source of snakes to use
|
|
|
|
* @throws BadBoardInput
|
|
|
|
* @note Requires Snake copy contructor
|
|
|
|
* @note Requires Snake copy constructor
|
|
|
|
* @note This has to be called if the board is default constructed
|
|
|
|
*/
|
|
|
|
void setSnakes(Snake[] snakes) throws BadBoardInput {
|
|
|
|
void copySnakes(Snake[] snakes) {
|
|
|
|
// Check if we need allocation
|
|
|
|
if (this.snakes == null)
|
|
|
|
this.snakes = new Snake[snakes.length];
|
|
|
|
// Input checking
|
|
|
|
if (this.snakes.length != snakes.length)
|
|
|
|
throw new BadBoardInput("snakes size missmatch");
|
|
|
|
// Assign values (deep copy)
|
|
|
|
for (int i =0 ; i<this.snakes.length ; ++i)
|
|
|
|
this.snakes[i] = new Snake(snakes[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get reference to ladders */
|
|
|
|
Ladder[] getLadders() { return ladders; }
|
|
|
|
/**
|
|
|
|
* Set ladders (deep copy)
|
|
|
|
* @param ladders Source of snakes to use
|
|
|
|
* @throws BadBoardInput
|
|
|
|
* @note Requires Ladder copy contructor
|
|
|
|
* Copy ladders (deep copy)
|
|
|
|
* @param ladders Source of ladders to use
|
|
|
|
* @note Requires Ladder copy constructor
|
|
|
|
* @note This has to be called if the board is default constructed
|
|
|
|
*/
|
|
|
|
void setLadders (Ladder[] ladders) throws BadBoardInput {
|
|
|
|
void copyLadders (Ladder[] ladders) {
|
|
|
|
// Check if we need allocation
|
|
|
|
if (this.ladders == null)
|
|
|
|
this.ladders = new Ladder[ladders.length];
|
|
|
|
// Input checking
|
|
|
|
if (this.ladders.length != ladders.length)
|
|
|
|
throw new BadBoardInput("ladders size missmatch");
|
|
|
|
// Assign values (deep copy)
|
|
|
|
for (int i =0 ; i<this.ladders.length ; ++i)
|
|
|
|
this.ladders[i] = new Ladder(ladders[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get reference to apples */
|
|
|
|
Apple[] getApples() { return apples; }
|
|
|
|
/**
|
|
|
|
* Set apples (deep copy)
|
|
|
|
* @param apples Source of snakes to use
|
|
|
|
* @throws BadBoardInput
|
|
|
|
* @note Requires Apple copy contructor
|
|
|
|
* Copy apples (deep copy)
|
|
|
|
* @param apples Source of apples to use
|
|
|
|
* @note Requires Apple copy constructor
|
|
|
|
* @note This has to be called if the board is default constructed
|
|
|
|
*/
|
|
|
|
void setApples (Apple[] apples) throws BadBoardInput {
|
|
|
|
void copyApples (Apple[] apples) {
|
|
|
|
// Check if we need allocation
|
|
|
|
if (this.apples == null)
|
|
|
|
this.apples = new Apple[apples.length];
|
|
|
|
// Input checking
|
|
|
|
if (this.apples.length != apples.length)
|
|
|
|
throw new BadBoardInput("ladders size missmatch");
|
|
|
|
// Assign values (deep copy)
|
|
|
|
for (int i =0 ; i<this.apples.length ; ++i)
|
|
|
|
this.apples[i] = new Apple(apples[i]);
|
|
|
@@ -176,20 +193,77 @@ public class Board { |
|
|
|
|
|
|
|
/** @name Exposed API members */
|
|
|
|
/** @{ */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the tile is a snake head. If so return the snake's
|
|
|
|
* tails tile. If not return the same tile
|
|
|
|
* @param tile The tile to check
|
|
|
|
* @return The result tile
|
|
|
|
*/
|
|
|
|
int checkSnake (int tile) {
|
|
|
|
for (int i =0 ; i<snakes.length ; ++i) {
|
|
|
|
if (snakes[i].getHeadId() == tile)
|
|
|
|
return snakes[i].getTailId();
|
|
|
|
}
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the tile is a ladder down step. If so return the ladder's
|
|
|
|
* up step tile. If not return the same tile.
|
|
|
|
* @note
|
|
|
|
* This also break the ladder if used
|
|
|
|
* @param tile The tile to check
|
|
|
|
* @return The result tile
|
|
|
|
*/
|
|
|
|
int checkLadder (int tile) {
|
|
|
|
for (int i =0 ; i<ladders.length ; ++i) {
|
|
|
|
if (ladders[i].getDownStepId() == tile &&
|
|
|
|
ladders[i].getBroken() == false) {
|
|
|
|
ladders[i].setBroken(true);
|
|
|
|
return ladders[i].getUpStepId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the tile is an apple tile. If so eat it and return the score difference
|
|
|
|
* @param tile The tile to check
|
|
|
|
* @return The score difference
|
|
|
|
*/
|
|
|
|
int checkApple (int tile) {
|
|
|
|
int ds =0; // delta-score
|
|
|
|
for (int i =0 ; i<apples.length ; ++i) {
|
|
|
|
if (apples[i].getAppleTileId() == tile) {
|
|
|
|
// eat it
|
|
|
|
ds = apples[i].getPoints();
|
|
|
|
apples[i].setPoints(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a playable board for the game
|
|
|
|
* Create a playable board for the game.
|
|
|
|
* @warning
|
|
|
|
* This is not required to be called after construction in order to ensure board's playable
|
|
|
|
* condition. In fact this function SHOULD NOT CALLED AT ALL.
|
|
|
|
* The project requirements expect this to be public. The preferable mode would be private.
|
|
|
|
* @see Board() constructor.
|
|
|
|
*/
|
|
|
|
void createBoard () {
|
|
|
|
_tile_numbering (); // Create tile numbering
|
|
|
|
_place_snakes (); // Place snakes
|
|
|
|
_place_apples (); // Place Apples
|
|
|
|
_place_ladders (); // place ladders
|
|
|
|
_tileNumbering (); // Create tile numbering
|
|
|
|
_placeSnakes (); // Place snakes
|
|
|
|
_placeApples (); // Place Apples
|
|
|
|
_placeLadders (); // place ladders
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* make and PRINT element boards
|
|
|
|
* @brief
|
|
|
|
* make and print element boards
|
|
|
|
* This is not required in order for the board to be playable
|
|
|
|
* It just produce a stdout output for convenience.
|
|
|
|
*/
|
|
|
|
void createElementBoard () {
|
|
|
|
String[][] elementBoardSnakes = new String[N][M];
|
|
|
@@ -212,11 +286,11 @@ public class Board { |
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
* Create the tile numbering
|
|
|
|
* Create the tile numbering in a boustrophedon (zig-zag) way.
|
|
|
|
* We use a starting point the tile[0][0] and as finish point
|
|
|
|
* the tile[N-1][M-1]
|
|
|
|
*/
|
|
|
|
private void _tile_numbering () {
|
|
|
|
private void _tileNumbering () {
|
|
|
|
for (int i=0, tile =1 ; i<N ; ++i) {
|
|
|
|
if (i%2 == 0) {
|
|
|
|
// Even row, go right
|
|
|
@@ -234,32 +308,33 @@ public class Board { |
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
* Place the snakes on the board
|
|
|
|
* The only constrain at this point is that snake tails must be
|
|
|
|
* below heads and in different tiles
|
|
|
|
* The only constrain at this point is that snake tails must be placed
|
|
|
|
* below heads and heads must be placed in separate tiles
|
|
|
|
*/
|
|
|
|
private void _place_snakes () {
|
|
|
|
private void _placeSnakes () {
|
|
|
|
int [] head = new int [snakes.length]; // temporary place holder for heads
|
|
|
|
int [] tail = new int [snakes.length]; // temporary place holder for tails
|
|
|
|
for (int i =0, tile =0 ; i<snakes.length ; ++i) {
|
|
|
|
// Keep getting heads until they are different from the previous
|
|
|
|
do
|
|
|
|
tile = (int)(M + Math.random() * (N-1) * M); // Don't use first row for heads
|
|
|
|
tile = _pickRandom (M+1, M*N); // Don't use first row for heads
|
|
|
|
while (_search (head, tile) >= 0);
|
|
|
|
head[i] = tile;
|
|
|
|
tail[i] = (int)(Math.random() * (head[i] - head[i]%M)); // Don't use heads row and up for tail
|
|
|
|
snakes[i] = new Snake(i, head[i], tail[i]); // Allocate snake
|
|
|
|
tail[i] = _pickRandom (1, head[i]-head[i]%M); // Don't use heads row and up for tail
|
|
|
|
snakes[i] = new Snake(i, head[i], tail[i]); // Allocate snake
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
* Place apples on the board
|
|
|
|
* At this point we have snakes. The constrains here are
|
|
|
|
* The constrains here are
|
|
|
|
* that apples have to lie on different tiles and not in some
|
|
|
|
* snake's head
|
|
|
|
* snake's head.
|
|
|
|
* @note We require we have snakes.
|
|
|
|
*/
|
|
|
|
private void _place_apples () {
|
|
|
|
int [] apple_tiles = new int [apples.length]; // temporary placeholder for heads
|
|
|
|
private void _placeApples () {
|
|
|
|
int [] apple_tiles = new int [apples.length]; // temporary placeholder for apples
|
|
|
|
int [] snake_tiles = new int [snakes.length]; // array with snake head tiles
|
|
|
|
for (int i =0 ; i<snakes.length ; ++i) // Load snake head tiles
|
|
|
|
snake_tiles[i] = snakes[i].getHeadId();
|
|
|
@@ -268,11 +343,12 @@ public class Board { |
|
|
|
// Keep getting tiles until they are different from the previous
|
|
|
|
// and not in some snake's head
|
|
|
|
do
|
|
|
|
tile = (int) (Math.random() * (N * M));
|
|
|
|
tile = _pickRandom (1, M*N);
|
|
|
|
while ((_search (apple_tiles, tile) >= 0) ||
|
|
|
|
(_search (snake_tiles, tile) >= 0));
|
|
|
|
apple_tiles[i] = tile;
|
|
|
|
int points = (int)(Math.random() *10) * 5; // get points
|
|
|
|
// get points
|
|
|
|
int points = _pickRandom (1, (POINTS_MAX/POINTS_STEP)) * POINTS_STEP;
|
|
|
|
boolean red = (boolean)(Math.random() >=0.5); // get color
|
|
|
|
// Allocate apple
|
|
|
|
if (red)
|
|
|
@@ -285,34 +361,49 @@ public class Board { |
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
* Place ladders on board
|
|
|
|
* At this point we have snakes and apples.
|
|
|
|
* @note
|
|
|
|
* We add a constrain for ladder so its up-setp has to be different from a
|
|
|
|
* snake's head tile. This ensures the each ladder and snake are independent
|
|
|
|
*
|
|
|
|
* We add constrains so each ladder's up-step tile has to be different from:
|
|
|
|
* * A snake's head tile. This ensures ladders and snakes are independent
|
|
|
|
* * A ladders's down-step. This ensure we eliminate ladder chains.
|
|
|
|
* * One other ladder's up-step. This is not critical but helps the printElement functionality
|
|
|
|
*
|
|
|
|
* We add constrains so each ladder's down-step tile has to be different from:
|
|
|
|
* * A snake's head tile. This ensures ladders and snakes are independent
|
|
|
|
* * A ladders's down-step. This is not critical but helps the printElement functionality
|
|
|
|
* * One other ladder's up-step. This ensure we eliminate ladder chains.
|
|
|
|
* @note We require we have snakes.
|
|
|
|
*/
|
|
|
|
private void _place_ladders () {
|
|
|
|
int [] upstep = new int [ladders.length]; // temporary place holder for up-steps
|
|
|
|
int [] downstep = new int [ladders.length]; // temporary place holder for down-step
|
|
|
|
int [] snake_tiles = new int [snakes.length]; // array with snake head tiles
|
|
|
|
private void _placeLadders () {
|
|
|
|
int [] up_step = new int [ladders.length]; // temporary place holder for up-steps
|
|
|
|
int [] down_step = new int [ladders.length]; // temporary place holder for down-step
|
|
|
|
int [] snake_tiles= new int [snakes.length]; // array with snake head tiles
|
|
|
|
for (int i =0 ; i<snakes.length ; ++i) // Load snake head tiles
|
|
|
|
snake_tiles[i] = snakes[i].getHeadId();
|
|
|
|
|
|
|
|
for (int i =0, tile =0 ; i<ladders.length ; ++i) {
|
|
|
|
// Keep getting up-steps until they are different from the previous
|
|
|
|
// Keep getting up-steps until they are different from the previous ladder tiles
|
|
|
|
// and not in some snake's head
|
|
|
|
do
|
|
|
|
tile = (int)(M + Math.random() * (N-1) * M); // Don't use first row for heads
|
|
|
|
while ((_search (upstep, tile) >= 0) ||
|
|
|
|
(_search (snake_tiles, tile) >= 0));
|
|
|
|
upstep[i] = tile;
|
|
|
|
downstep[i] = (int)(Math.random() * (upstep[i] - upstep[i]%M));
|
|
|
|
//^ Don't use up-step row and up for down-step
|
|
|
|
ladders[i] = new Ladder (i, upstep[i], downstep[i]); // Allocate ladder
|
|
|
|
tile = _pickRandom (M+1, M*N); // Don't use first row for up-steps
|
|
|
|
while ((_search (up_step, tile) >= 0)
|
|
|
|
|| (_search (down_step, tile) >= 0)
|
|
|
|
|| (_search (snake_tiles, tile) >= 0));
|
|
|
|
up_step[i] = tile;
|
|
|
|
// Keep getting down-steps until they are different from the previous ladder tiles
|
|
|
|
// and not in some snake's head
|
|
|
|
do
|
|
|
|
// Don't use up-step row and up for down-step
|
|
|
|
tile = _pickRandom (1, up_step[i]-up_step[i]%M);
|
|
|
|
while ((_search (up_step, tile) >= 0)
|
|
|
|
|| (_search (down_step, tile) >= 0)
|
|
|
|
|| (_search (snake_tiles, tile) >= 0));
|
|
|
|
down_step[i] = tile;
|
|
|
|
ladders[i] = new Ladder (i, up_step[i], down_step[i]); // Allocate ladder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make element array of snakes
|
|
|
|
* Make element array of snakes as required by the project
|
|
|
|
* @param elemSnakes
|
|
|
|
*/
|
|
|
|
private void _makeElementSnakes (String[][] elemSnakes) {
|
|
|
@@ -339,7 +430,7 @@ public class Board { |
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make element array of ladders
|
|
|
|
* Make element array of ladders as required by the project
|
|
|
|
* @param elemLadders
|
|
|
|
*/
|
|
|
|
private void _makeElementLadders (String[][] elemLadders) {
|
|
|
@@ -366,7 +457,7 @@ public class Board { |
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make element array of apples
|
|
|
|
* Make element array of apples as required by the project
|
|
|
|
* @param elemApples
|
|
|
|
*/
|
|
|
|
private void _makeElementApples (String[][] elemApples) {
|
|
|
@@ -401,10 +492,12 @@ public class Board { |
|
|
|
* @note
|
|
|
|
* As long as we use tiles[0][0] for first tile, this method
|
|
|
|
* has to print in reverse Y-axis order. For ex:
|
|
|
|
* <pre>
|
|
|
|
* 16 15 14 13
|
|
|
|
* 09 10 11 12
|
|
|
|
* 08 07 06 05
|
|
|
|
* 01 02 03 04
|
|
|
|
* </pre>
|
|
|
|
*/
|
|
|
|
private void _printElement (String[][] elem, String caption) {
|
|
|
|
System.out.print(caption);
|
|
|
@@ -418,6 +511,16 @@ public class Board { |
|
|
|
System.out.println("");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pick a random tile in range [from..to]
|
|
|
|
* @param from The first tile to consider
|
|
|
|
* @param to The last tile to consider
|
|
|
|
* @return The random pick
|
|
|
|
*/
|
|
|
|
private int _pickRandom (int from, int to) {
|
|
|
|
return from + (int)(Math.random() * (to - from));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Search algorithm
|
|
|
|
* @param array Array to search
|
|
|
|
* @param elem Element of type T to find inside of array
|
|
|
@@ -433,19 +536,14 @@ public class Board { |
|
|
|
}
|
|
|
|
/**@} */
|
|
|
|
|
|
|
|
/** @name Data members (private) */
|
|
|
|
/** @name Data members */
|
|
|
|
/** @{ */
|
|
|
|
private int N; //!< Board's row count
|
|
|
|
private int M; //!< Board's Column count
|
|
|
|
private int[][] tiles; //!< Board's tiles
|
|
|
|
private Snake[] snakes; //!< Board's snakes
|
|
|
|
private Ladder[] ladders; //!< Board's ladders
|
|
|
|
private Apple[] apples; //!< Board's apples
|
|
|
|
private int N; /**< Board's row count */
|
|
|
|
private int M; /**< Board's Column count */
|
|
|
|
private int[][] tiles; /**< Board's tiles */
|
|
|
|
private Snake[] snakes; /**< Board's snakes */
|
|
|
|
private Ladder[] ladders; /**< Board's ladders */
|
|
|
|
private Apple[] apples; /**< Board's apples */
|
|
|
|
/** @} */
|
|
|
|
}
|
|
|
|
|
|
|
|
class BadBoardInput extends Exception {
|
|
|
|
BadBoardInput() {}
|
|
|
|
BadBoardInput(String str) { super(str); }
|
|
|
|
//static final long SerialVersionUIDAdder = 0;
|
|
|
|
}
|