-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.java
262 lines (246 loc) · 6.03 KB
/
Player.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
252
253
254
255
256
257
258
259
260
261
262
/**
* Player class for the game of battleship; holds information for one player.
* Holds the player's board, fleet, and record of attacks.
*/
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class Player {
private String playerName;
private Board board;
public boolean loss;
private ShipList fleet;
private AttackRecord record;
private boolean human;
public Player(String aname, boolean h)
/**
* Instantiates a new player.
*/
{
playerName = aname;
board = new Board();
fleet = new ShipList();
record = new AttackRecord();
human = h;
}
public void placeShip(int m, int n)
/**
* Places a new ship on the board.
* If the player is human, placement input will be checked against
* the current board to see whether the ship can be placed without
* running into other ships or off the board.
*
* If the player is not human, input will be generated randomly
* and checked in the same way.
*
* Once proper placement for a ship has been obtained, the ship
* will be added to the board and to the player's ShipList.
*
* @param m ship length
* @param n ship type: 1- aircraft carrier; 2- battleship;
* 3- destroyer; 4- submarine; 5- patrol boat
*/
{
int x = 0;
int y = 0;
boolean valid = false;
boolean alignment = false;
while(valid == false)
{
if(human)
{
String ship = "";
if(n==1)
{
ship = "aircraft carrier";
}
if(n==2)
{
ship = "battleship";
}
if(n==3)
{
ship = "destroyer";
}
if(n==4)
{
ship = "submarine";
}
if(n==5)
{
ship = "patrolboat";
}
try
{
System.out.println("Please place your " + ship + ". This gamepiece is " + m + " squares long.");
System.out.println("Should the orientation be: 1 - horizontal, or 2 - vertical?");
Scanner orientation = new Scanner(System.in);
int a = orientation.nextInt();
if(a != 1 && a != 2)
{
InputMismatchException e = new InputMismatchException();
throw e;
}
else if(a == 1)
{
alignment = true;
}
else if(a == 2)
{
alignment = false;
}
System.out.println("Please name a starting point on the vertical axis.");
Scanner vertical = new Scanner(System.in);
x = vertical.nextInt();
System.out.println("Please name a starting point on the horizontal axis.");
Scanner horizontal = new Scanner(System.in);
y = horizontal.nextInt();
valid = board.checkAvailibility(alignment, m, x, y);
if (valid == false)
{
System.out.println("There's no place on the board for this ship. Please put it somewhere else.");
}
}
catch (InputMismatchException e)
{
System.out.println("The information you have input is not valid. Please try again.");
valid = false;
}
}
else
{
Random random = new Random();
alignment = random.nextBoolean();
x = (int) (Math.random()*10);
y = (int) (Math.random()*10);
valid = board.checkAvailibility(alignment, m, x, y);
}
}
board.addShip(alignment, m, x, y, n);
}
public void displaytoMe()
/**
* Prints the board to the player.
*/
{
board.displaytoMe();
}
public void displaytoEnemy()
/**
* Prints the board to the player's enemy.
*/
{
board.displaytoEnemy();
}
public Coordinates attack()
/**
* Generates coordinates for an attack.
*
* If the player is human, input will be checked to make sure those same
* coordinates have not been attacked already, and if those coordinates are
* in range of the board.
*
* If the player is not human, coordinates will be generated randomly
* and those will also be checked.
*
* Once appropriate coordinates have been found, the player will attack,
* and record this attack in the AttackRecord.
*
* @return attackCoordinates An object of the Coordinates class
* that contains coordinates for an attack.
*/
{
System.out.println("It is " + playerName + "'s turn to attack.");
int x = 0;
int y = 0;
boolean redundant = true;
if(human)
{
while(redundant)
{
try
{
System.out.println("Please select a coordinate on the vertical axis to target.");
Scanner targetx = new Scanner(System.in);
x = targetx.nextInt();
if (x < 0 || x > 9)
{
InputMismatchException f = new InputMismatchException();
throw f;
}
System.out.println("Please select a coordinate on the horizontal axis to target.");
Scanner targety = new Scanner(System.in);
y = targety.nextInt();
if (y < 0 || y > 9)
{
InputMismatchException f = new InputMismatchException();
throw f;
}
if(record.isRedundant(x, y))
{
System.out.println("You've already hit there. Please select different coordinates.");
}
else
{
redundant = false;
}
}
catch (InputMismatchException f)
{
System.out.println("This is not a valid coordinate. Coordinates must be integers between 0 and 9.");
redundant = true;
}
}
}
else
{
while (redundant)
{
x = (int) (Math.random()*10);
y = (int) (Math.random()*10);
if(record.isRedundant(x, y) == false)
{
redundant = false;
}
}
}
Coordinates attackCoordinates = new Coordinates(x, y);
record.addAttack(attackCoordinates);
return attackCoordinates;
}
public void receiveAttack(Coordinates c)
/**
* Deals with an attack from the enemy player.
* Checks coordinates to see whether there was
* a ship at those coordinates.
*
* @param c coordinates for an attack initiated
* by the enemy.
*/
{
int status = board.checkHit(c);
if (status == 0)
{
System.out.println("The attack has missed.");
board.receiveMiss(c);
}
else
{
System.out.println(playerName + "'s ship has been hit.");
board.receiveHit(c);
fleet.shipHit(status);
}
}
public boolean hasLost()
/**
* Checks whether the player has lost.
* @return loss Returns true when this player has lost.
*/
{
if (fleet.isLost())
{
loss = true;
}
return loss;
}
}