DEV: 3rd assignment draft

This commit is contained in:
2020-12-18 16:34:11 +02:00
parent 2230cac96e
commit c2c5d5e46f
9 changed files with 651 additions and 99 deletions
+57 -38
View File
@@ -13,7 +13,6 @@
package host.labyrinth;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.IntFunction;
/**
@@ -35,9 +34,10 @@ class Board {
this.S = 0;
this.W = 0;
tiles = null;
supplies =null;
supplies = null;
walls = new ArrayList<Edge>();
moves = new ArrayList<Integer[]>();
moves = new int[Const.numOfPlayers][Player.MOVE_DATA_SIZE];
playerCount =0;
}
/**
@@ -54,7 +54,8 @@ class Board {
tiles = new Tile[N*N];
supplies = new Supply[S];
walls = new ArrayList<Edge>();
moves = new ArrayList<Integer[]>();
moves = new int[Const.numOfPlayers][Player.MOVE_DATA_SIZE];
playerCount =0;
}
/**
@@ -72,17 +73,28 @@ class Board {
*/
Board(Board b) {
// Copy primitives
this.N = b.N;
this.S = b.S;
this.W = b.W;
// Clone arrays
this.tiles = b.tiles.clone();
this.supplies = b.supplies.clone();
this.N = b.N;
this.S = b.S;
this.W = b.W;
tiles = new Tile[b.tiles.length];
supplies = new Supply[b.supplies.length];
walls = new ArrayList<Edge>();
moves = new int[Const.numOfPlayers][Player.MOVE_DATA_SIZE];
playerCount =b.playerCount;
// clone moves array of array of primitives
for (int i=0 ; i<b.moves.length ; ++i)
this.moves[i] = b.moves[i].clone();
// Clone arrays of objects
for (int i=0 ; i<b.tiles.length ; ++i)
this.tiles[i] = new Tile(b.tiles[i]);
for (int i=0 ; i<b.supplies.length ; ++i)
this.supplies[i] = new Supply(b.supplies[i]);
// clone vectors
for (Edge it: b.walls)
this.walls.add(new Edge(it));
for (Integer[] m : b.moves) {
this.moves.add(m);
}
this.walls.add(new Edge(it));
}
/** @} */
@@ -219,30 +231,35 @@ class Board {
/** @return the size of each site of the board. */
int size () { return N; }
/**
* Boards utility to give access to other player moves.
*
* @param playerId The id of player who asks
* @return The moves data of all other players
*/
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;
}
/**
* Utility function to create player IDs
* @return The generated player id.
*/
int generatePlayerId () {
moves.add(null);
return moves.size() -1;
int generatePlayerId () throws Exception {
if (playerCount < Const.numOfPlayers)
return playerCount++;
else
throw new Exception("Maximum number of players exceeded");
}
/**
* Boards utility to give access to other player Id.
*
* @param playerId The id of player who asks
* @return The other player's Id.
*/
int getOpponentId(int playerId) {
return Const.numOfPlayers - (playerId +1);
}
/**
* Boards utility to give access to other player moves.
*
* @param playerId The id of player who asks
* @return The moves data of other player
*/
int[] getOpponentMove (int playerId) {
return moves[getOpponentId(playerId)];
}
/**
@@ -256,7 +273,8 @@ class Board {
* @param playerId The id of the player who update his/her data.
*/
void updateMove(int[] m, int playerId) {
moves.set(playerId, Arrays.stream(m).boxed().toArray(Integer[]::new));
//moves.set(playerId, Arrays.stream(m).boxed().toArray(Integer[]::new));
moves[playerId] = m;
}
/** @} */
@@ -295,7 +313,7 @@ class Board {
* <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; }
int[][] getMoves() { return moves; }
void setN(int N) { this.N = N; }
void setS(int S) { this.S = S; }
@@ -326,7 +344,7 @@ class Board {
* @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; }
void setMoves(int[][] moves) { this.moves =moves; }
/** @} */
@@ -633,6 +651,7 @@ class Board {
* Array to hold all the walls using the edge representation
* required by the closed room preventing algorithm.
*/
private ArrayList<Integer[]> moves;
private int[][] moves;
private int playerCount;
/** @} */
}