-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattleshipPlayView.java
251 lines (240 loc) · 6.26 KB
/
BattleshipPlayView.java
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* The programatic interface for the Battleship Model class.
* This interface supports communication with both the view
* and controller classes in the Battleship application.
*
* @author Casey Riggin
* @author Christopher Mendoza
* @author Peter Kim
* @author Sai Chang
*/
import java.util.Scanner;
import java.util.regex.Pattern;
/**
* The Battleship's view and controller during its play phase
*
* @author Casey Riggin
* @author Christopher Mendoza
* @author Peter Kim
* @author Sai Chang
*/
class BattleshipPlayView {
private BattleshipModel model;
Scanner input;
/**
* Runs a game of Battleship and prompts user to play again
*
* @return returns true if Battleship will be played again or false
* otherwise
*/
public boolean runGame() {
model.moveToPlay(true);
boolean playAgain = true;
while (!model.isGameOver()) {
printBoard(model.isPlayerTurn());
askForFireShot();
}
printGameOver();
boolean askPrompt = true;
while (askPrompt) {
System.out.println("Play Again? y/n ");
String yes = input.nextLine();
if (yes.equalsIgnoreCase("n")) {
playAgain = false;
askPrompt = false;
}
if (yes.equalsIgnoreCase("y")) {
askPrompt = false;
}
if(askPrompt)
{
System.out.println("Sorry, you're mumbling. I'll ask again.");
}
}
return playAgain;
}
/**
* Constructor for the Battleship's play view
*
* @param model
* the model that the view and controller interact with
*/
public BattleshipPlayView(BattleshipModel model) {
this.model = model;
input = new Scanner(System.in);
}
/**
* Prints current player's attack and defense boards
*
* @param player
* true to set current player to 1 false to 2
*/
public void printBoard(boolean player) {
OffensiveTileStatus[][] offense = model.getOffensiveBoard(player);
DefenseTileStatus[][] defense = model.getDefenseBoard(player);
// Prints Offense board
System.out.println("\n\nPlayer " + (player ? "1" : "2")
+ "'s Offensive Grid\n");
for (int i = 0; i < model.getBoardSize(); i++) {
System.out.print(" ");
for (int j = 0; j < model.getBoardSize(); j++) {
System.out.print("+---");
}
System.out.println("+");
for (int j = 0; j < model.getBoardSize(); j++) {
String tile = " ";
switch (offense[i][j]) {
case UNKNOWN:
tile = "~";
break;
case MISS:
tile = "O";
break;
case HIT_SHIP:
tile = "X";
break;
case SUNK_SHIP:
tile = ""
+ model.getShipTypeAtCell(i, j, !player)
.getShipChar();
break;
}
if (j == 0) {
System.out.print((char) ('A' + i));
System.out.print(" ");
}
System.out.print("| " + tile + " ");
}
System.out.println("|");
}
System.out.print(" ");
for (int j = 0; j < model.getBoardSize(); j++) {
System.out.print("+---");
}
System.out.println("+");
System.out.print(" ");
for (int i = 1; i <= model.getBoardSize(); i++) {
if (i < 10) {
System.out.print(" " + i + " ");
} else {
System.out.print(" " + i + " ");
}
}
// Prints Defense board
System.out.println("\n\nPlayer " + (player ? "1" : "2")
+ "'s Defensive Grid\n");
for (int i = 0; i < model.getBoardSize(); i++) {
System.out.print(" ");
for (int j = 0; j < model.getBoardSize(); j++) {
System.out.print("+---");
}
System.out.println("+");
for (int j = 0; j < model.getBoardSize(); j++) {
String tile = "";
switch (defense[i][j]) {
case OCEAN:
tile = "~";
break;
case SHIP:
tile = ""
+ model.getShipTypeAtCell(i, j, player)
.getShipChar();
break;
}
if (j == 0) {
System.out.print((char) ('A' + i));
System.out.print(" ");
}
System.out.print("| " + tile + " ");
}
System.out.println("|");
}
System.out.print(" ");
for (int j = 0; j < model.getBoardSize(); j++) {
System.out.print("+---");
}
System.out.println("+");
System.out.print(" ");
for (int i = 1; i <= model.getBoardSize(); i++) {
if (i < 10) {
System.out.print(" " + i + " ");
} else {
System.out.print(" " + i + " ");
}
}
System.out.println("");
}
/**
* Asks for the shot from the current player
*/
public void askForFireShot() {
boolean attack = true;
char row;
int column;
int player;
if (model.isPlayerTurn()) {
player = 1;
} else {
player = 2;
}
while (attack) {
System.out.println("Admiral Player " + player
+ ", where shall we attack: ");
String place = input.nextLine();
place = place == null ? "" : place.toUpperCase().trim(); // make the
// response
// not
// case
// sensitive.
if (Pattern.matches(model.getRegexPattern(), place)) {
row = place.charAt(0);
column = Integer.parseInt(place.substring(1));
if (model.isValidAttackLocation(column, row)) {
System.out.println("Attacking row " + row + ", column "
+ column);
FireResult fireResult = model.attackLocation(column, row);
if (FireResult.MISS != fireResult) {
// check if ship sunk and print appropriate message
if (FireResult.HIT != fireResult) {
// exit the method now that the game is over.
// Figure out what kind of ship is at the given
// location and get its name
String shipName = model.getShipTypeAtCell(
("" + row).toUpperCase().charAt(0) - 'A',
column - 1, player != 1).getShipName();
System.out.println("Admiral "
+ (!model.isPlayerTurn() ? "1" : "2")
+ " says: Hit.. You sunk a my " + shipName
+ "!");
if (model.isGameOver()) {
return;
}
} else {
System.out.println("Hit!");
}
} else {
System.out.println("The attack was a miss...");
}
attack = false;
} else {
System.out.println("Invalid attack location, try again.");
}
} else {
System.out.println("Invalid attack location, try again.");
}
}
}
/**
* Prints a game over message given the end state
*/
public void printGameOver() {
if (model.isGameOver()) {
int player;
player = model.getWinner() ? 1 : 2;
System.out.println("Game over, Admiral Player " + player
+ " has achieved victory!");
} else {
throw new IllegalStateException();
}
}
}