Updated 'Board' class.

Updated the 'Board' class by rewriting the constructor, to make it maintainable and scalable.
This commit is contained in:
Daniel Grigencha
2024-12-05 04:46:01 +01:00
parent 354cdc0a9c
commit bfe8a20f92

View File

@@ -7,6 +7,11 @@
*/
@Serializable
public class Board {
/**
* The size of the board.
*/
public static final int BOARD_SIZE = 40;
/**
* Create Board attributes.
*/
@@ -16,16 +21,19 @@ public class Board {
* This constructor is used to create a new board
*/
public Board() {
infield = new Node[40];
for (int i = 0; i < 40; i++) {
if (i % 10 == 0) {
infield[i] = new StartNode(
i == 0 ? Color.AIRFORCE :
i == 10 ? Color.CYBER :
i == 20 ? Color.NAVY :
Color.ARMY
);
} else if (i == 4 || i == 14 || i == 24 || i == 34) {
infield = new Node[BOARD_SIZE];
initializeBoard();
}
/**
* Initializes the board by setting up the nodes.
* Start nodes, bonus nodes, and regular nodes are created based on their positions.
*/
private void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
if (isStartNode(i)) {
infield[i] = createStartNode(i);
} else if (isBonusNode(i)) {
infield[i] = new BonusNode();
} else {
infield[i] = new Node(null);
@@ -33,6 +41,36 @@ public Board() {
}
}
/**
* Checks if the given index is a start node.
*
* @param i the index to check
* @return true if the index is a start node, false otherwise
*/
private boolean isStartNode(int i) {
return i % 10 == 0;
}
/**
* Checks if the given index is a bonus node.
*
* @param i the index to check
* @return true if the index is a bonus node, false otherwise
*/
private boolean isBonusNode(int i) {
return i % 10 == 4;
}
/**
* Creates a start node with the appropriate color based on the index.
*
* @param i the index of the start node
* @return a new StartNode with the corresponding color
*/
private StartNode createStartNode(int i) {
return new StartNode(Color.getColor(i));
}
/**
* This method returns the index of a specific piece on the board
*
@@ -68,7 +106,7 @@ public void setPieceOnBoard(int index, Piece piece) {
}
@Override
public String toString(){
public String toString() {
return "Default Board";
}
}