-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
93 lines (85 loc) · 3.04 KB
/
Copy pathBoard.java
File metadata and controls
93 lines (85 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.Random;
/**
* This class generates the tank array
* and displays the board state.
*/
public final class Board {
public static Tank[][] tanksMatrix = new Tank[2][2];
public static int bulletsFiredCounter = 0;
public static final int MAX_AMOUNT_OF_TANKS = new Random().nextInt(4) + 1;
/**
* Returns a reference to a tank from its position.
*
* @param position A number from 1 to 4
* @return A reference to an element of the tanks array
*/
public static Tank getTankFromPosition(int position) {
if (position == 1)
return tanksMatrix[0][0];
else if (position == 2)
return tanksMatrix[0][1];
else if (position == 3)
return tanksMatrix[1][0];
else
return tanksMatrix[1][1];
}
/**
* Generates 1 to 4 tanks (randomly chosen number)
* and stores them in the tanks array.
*/
public static void generateTanks() {
int tanksCreatedCounter = 0;
// Traverse 2D array/matrix
for (int i = 0; i < tanksMatrix.length; i++) {
for (int j = 0; j < tanksMatrix[i].length; j++) {
// Initialize tank only if counter below limit
if (tanksCreatedCounter < MAX_AMOUNT_OF_TANKS) {
if (new Random().nextBoolean() == true) {
tanksMatrix[i][j] = new PanzerTank();
} else {
tanksMatrix[i][j] = new AlienTank();
}
tanksCreatedCounter++;
} else {
// Position left null
continue;
}
}
}
}
/**
* Prints the board state,
* displaying, for each tank,
* type and health
*/
public static void displayBoardState() {
System.out.println("-----------------");
System.out.println(("| " + tanksMatrix[0][0] + " | " + tanksMatrix[0][1] + " |")
.replace("null", " "));
System.out.println("-----------------");
System.out.println(("| " + tanksMatrix[1][0] + " | " + tanksMatrix[1][1] + " |")
.replace("null", " "));
System.out.println("-----------------");
System.out.println();
}
/**
* Prints the board state but
* numbered from 1 to 4 for
* nicer prompting on the user.
*
* @see displayBoardState()
*/
public static void displayNumberedBoardState() {
System.out.println();
System.out.println("-----------------");
System.out.println(("| " + tanksMatrix[0][0] + " | " + tanksMatrix[0][1] + " |")
.replace("null", " "));
System.out.println("| 1 | 2 |");
System.out.println("-----------------");
System.out.println(("| " + tanksMatrix[1][0] + " | " + tanksMatrix[1][1] + " |")
.replace("null", " "));
System.out.println("| 3 | 4 |");
System.out.println("-----------------");
System.out.println();
}
}