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 @Serializable
public class Board { public class Board {
/**
* The size of the board.
*/
public static final int BOARD_SIZE = 40;
/** /**
* Create Board attributes. * Create Board attributes.
*/ */
@@ -16,16 +21,19 @@ public class Board {
* This constructor is used to create a new board * This constructor is used to create a new board
*/ */
public Board() { public Board() {
infield = new Node[40]; infield = new Node[BOARD_SIZE];
for (int i = 0; i < 40; i++) { initializeBoard();
if (i % 10 == 0) { }
infield[i] = new StartNode(
i == 0 ? Color.AIRFORCE : /**
i == 10 ? Color.CYBER : * Initializes the board by setting up the nodes.
i == 20 ? Color.NAVY : * Start nodes, bonus nodes, and regular nodes are created based on their positions.
Color.ARMY */
); private void initializeBoard() {
} else if (i == 4 || i == 14 || i == 24 || i == 34) { for (int i = 0; i < BOARD_SIZE; i++) {
if (isStartNode(i)) {
infield[i] = createStartNode(i);
} else if (isBonusNode(i)) {
infield[i] = new BonusNode(); infield[i] = new BonusNode();
} else { } else {
infield[i] = new Node(null); 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 * This method returns the index of a specific piece on the board
* *