-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI.java
More file actions
150 lines (139 loc) · 4.31 KB
/
Copy pathCLI.java
File metadata and controls
150 lines (139 loc) · 4.31 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.util.Scanner;
import java.util.InputMismatchException;
/**
* The Command Line Interface for the program aka Main.
*/
public final class CLI {
/**
* Calls the tank generation function and then real main.
*/
public static void main(String[] args) {
Board.generateTanks();
CLI.runEndlessProgram();
}
/**
* The real main flow of the program.
*/
private static void runEndlessProgram() {
/*
* option 0 exits the program
* game ends when all tanks are at health 0
*/
while (CLI.isGameOver() == false) {
CLI.displayProgram();
CLI.askForOption();
try {
Thread.sleep(3000); // 3 secs
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
CLI.printEmptyLines();
}
}
/**
* Determine if all tanks have a health level of 0.
*
* @return true if all tanks are dead
*/
private static boolean isGameOver() {
for (int position = 1; position <= Board.MAX_AMOUNT_OF_TANKS; position++) {
if (Board.getTankFromPosition(position).health != 0) {
return false;
} else {
continue;
}
}
// If this runs, game is over
CLI.sayBye();
return true;
}
/**
* Shamelessly plug the program (xd).
*/
private static void greetUser() {
System.out.println();
System.out.println(" panzer minigame");
System.out.println(" by luis and wendy");
System.out.println();
System.out.println(" practice 2/2");
System.out.println(" for st0242");
System.out.println();
}
/**
* Print the ending message (spoiler alert).
*/
private static void sayBye() {
System.out.println("game is over");
System.out.println("i am not sad. are you sad?");
System.out.println();
System.out.println("this program does not make sense. it's just a project for uni.");
System.out.println();
}
/**
* A flow for printing the greeting/title, features, and board state.
*/
private static void displayProgram() {
CLI.greetUser();
CLI.showFeatures();
Board.displayBoardState();
}
/**
* Prints the list of features.
*/
private static void showFeatures() {
System.out.println(" features");
System.out.println();
System.out.println("0 quit");
System.out.println("1 shoot tank");
System.out.println("2 atomic bomb");
System.out.println("3 mutant tank");
System.out.println("4 granny quote");
System.out.println("5 bullets fired");
System.out.println();
}
/**
* Prompt the user for a number and then call the corresponding function.
* It also prevents people from typing non integers.
*/
private static void askForOption() {
System.out.print("i'd like to do # ");
try {
int featureChosen = new Scanner(System.in).nextInt();
callAction(featureChosen);
if (featureChosen <= 3) {
Board.displayBoardState();
}
} catch (InputMismatchException e) {
System.out.println("General, that was not a number...");
}
}
/**
* Calls the corresponding function according to the feature.
*
* @param option The number of the feature to call.
*/
private static void callAction(int option) {
System.out.println();
if (option == 0)
System.exit(2022);
else if (option == 1)
PlayerActions.shootBullet();
else if (option == 2)
PlayerActions.activateAtomicBomb();
else if (option == 3)
PlayerActions.activateMutantTank();
else if (option == 4)
PlayerActions.rememberGrannyWithAQuote();
else if (option == 5)
PlayerActions.displayFormattedBulletsFired();
else
System.out.println("General, you must choose a valid option number.");
System.out.println();
}
/**
* Add 4 new lines to separate, uhm, sessions?
*/
private static void printEmptyLines() {
System.out.println("\n\n\n\n");
}
}