First draft of 2nd version
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
package host.labyrinth;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
/**
|
||||
@@ -36,6 +37,7 @@ class Board {
|
||||
tiles = null;
|
||||
supplies =null;
|
||||
walls = new ArrayList<Edge>();
|
||||
moves = new ArrayList<Integer[]>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,6 +54,7 @@ class Board {
|
||||
tiles = new Tile[N*N];
|
||||
supplies = new Supply[S];
|
||||
walls = new ArrayList<Edge>();
|
||||
moves = new ArrayList<Integer[]>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,6 +80,9 @@ class Board {
|
||||
this.supplies = b.supplies.clone();
|
||||
for (Edge it: b.walls)
|
||||
this.walls.add(new Edge(it));
|
||||
for (Integer[] m : b.moves) {
|
||||
this.moves.add(m);
|
||||
}
|
||||
}
|
||||
/** @} */
|
||||
|
||||
@@ -174,6 +180,15 @@ class Board {
|
||||
&& !(Position.toID(row, col) == 0 && direction == Direction.DOWN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function to check if there is a supply on the tile or not
|
||||
* @param tileId The tile to check
|
||||
* @return Yes/no
|
||||
*/
|
||||
boolean hasSupply (int tileId) {
|
||||
return (Const.noSupply != tiles[tileId].hasSupply(supplies)) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to pick supply from a tile. If succeed it also erases the
|
||||
* supply from the board.
|
||||
@@ -191,6 +206,7 @@ class Board {
|
||||
return supplyId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A plain fair dice functionality provided by the board.
|
||||
* @return A random direction;
|
||||
@@ -202,6 +218,27 @@ class Board {
|
||||
|
||||
/** @return the size of each site of the board. */
|
||||
int size () { return N; }
|
||||
|
||||
int[][] getOpponentMoves (int playerId) {
|
||||
int[][] ret = new int[moves.size()-1][Const.moveItems];
|
||||
int ii= 0, ri =0;
|
||||
for (Integer[] m : moves) {
|
||||
if (ii != playerId)
|
||||
ret[ri++] = Arrays.stream(m).mapToInt(i->i).toArray();
|
||||
++ii;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int generatePlayerId () {
|
||||
moves.add(null);
|
||||
return moves.size() -1;
|
||||
}
|
||||
|
||||
void updateMove(int[] m, int playerId) {
|
||||
moves.set(playerId, Arrays.stream(m).boxed().toArray(Integer[]::new));
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
@@ -233,6 +270,13 @@ class Board {
|
||||
*/
|
||||
ArrayList<Edge> getWalls() { return walls; }
|
||||
|
||||
/**
|
||||
* @note Use it with care. Any use of this function results to what Sean Parent calls "incidental data-structure".
|
||||
* <a href="https://github.com/sean-parent/sean-parent.github.io/blob/master/better-code/03-data-structures.md"> see also here</a>
|
||||
* @return Reference to inner walls array.
|
||||
*/
|
||||
ArrayList<Integer[]> getMoves() { return moves; }
|
||||
|
||||
void setN(int N) { this.N = N; }
|
||||
void setS(int S) { this.S = S; }
|
||||
void setW(int W) { this.W = W; }
|
||||
@@ -257,6 +301,13 @@ class Board {
|
||||
*/
|
||||
void setWalls (ArrayList<Edge> walls) { this.walls= walls; }
|
||||
|
||||
/**
|
||||
* @param moves Reference to moves that we want to act as replacement for the inner moves vector.
|
||||
* @note Use with care.
|
||||
* Any call to this function will probably add memory for the garbage collector.
|
||||
*/
|
||||
void setMoves(ArrayList<Integer[]> moves) { this.moves =moves; }
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
@@ -562,5 +613,6 @@ class Board {
|
||||
* Array to hold all the walls using the edge representation
|
||||
* required by the closed room preventing algorithm.
|
||||
*/
|
||||
private ArrayList<Integer[]> moves;
|
||||
/** @} */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user