-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
208 lines (185 loc) · 5.52 KB
/
Player.java
File metadata and controls
208 lines (185 loc) · 5.52 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
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
//Player class
import java.util.*;
public class Player
{
private String [][] myBoard, opponentBoard;
private ArrayList<Ship> myShips;
private int playerNumber;
private String message = "";
public Player(int playerNum_in)
{
myBoard = new String[11][11];
opponentBoard = new String[11][11];
myShips = new ArrayList<Ship>();
playerNumber = playerNum_in;
}
public int getNum()
{
return playerNumber;
}
public String name()
{
return "Player "+playerNumber;
}
public String[][] getMyBoard()
{
return myBoard;
}
public String[][] getOppBoard()
{
return opponentBoard;
}
public ArrayList<Ship> getShips()
{
return myShips;
}
public String getMessage()
{
return message;
}
public void setMessage(String message_in)
{
message = message_in;
}
public boolean isValidFire(int col, char row_in)
{
int row = row_in - 64;
if(col < 1 || col > 10)
return false;
else if(row < 1 || row > 10)
return false;
else if(opponentBoard[row][col].equals("-"))
return true;
else
return false;
}
public void opponentBoardFiredAt(int col, char row_in, Player opponent)
{
int row = (row_in - 'A') + 1;
char fireResult = '*';
if(opponent.shipHit(col, row))
{
fireResult = 'H';
}
opponentBoard[row][col] = Character.toString(fireResult);
}
public void playerBoardFiredAt(int col, char row_in)
{
int row = (row_in - 'A') + 1;
char fireResult = 'X';
myBoard[row][col] = Character.toString(fireResult);
}
public void opponentBoardUpdateSunk(Player opponent)
{
for(int i = 0; i < opponent.getShips().size(); i++)
{
if(opponent.getShips().get(i).isSunk())
{
//check if first location of ship has an 'S' on the board
if(opponentBoard[opponent.getShips().get(i).getRow()][opponent.getShips().get(i).getCol()] != Character.toString('S'))
{
//loop through all the ship's locations and set them on the board to 'S'
for(int j = 0; j < opponent.getShips().get(i).getLocations().length; j++)
{
opponentBoard[(opponent.getShips().get(i).getLocations())[j][0]][(opponent.getShips().get(i).getLocations())[j][1]] = Character.toString('S');
}
}
}
}
}
public void playerBoardUpdateSunk()
{
for(int i = 0; i < myShips.size(); i++)
{
if(myShips.get(i).isSunk())
{
//check if first location of ship has an 'S' on the board
if(myBoard[myShips.get(i).getRow()][myShips.get(i).getCol()] != Character.toString('S'))
{
//loop through all the ship's locations and set them on the board to 'S'
for(int j = 0; j < myShips.get(i).getLocations().length; j++)
{
myBoard[(myShips.get(i).getLocations())[j][0]][(myShips.get(i).getLocations())[j][1]] = Character.toString('S');
}
}
}
}
}
public boolean shipHit(int col, int row)
{
boolean success = false;
char rowChar = (char)(row+64);
for(int i = 0; i < myShips.size(); i++)
{
success = myShips.get(i).isHit(col, row);
if(success)
{
if(myShips.get(i).isSunk())
{
message = "Your opponent shot at "+rowChar+" "+col+" and sunk your ship!";
}
else
{
message = "Your opponent shot at "+rowChar+" "+col+" and hit your ship!";
}
return success;
}
}
message = "Your opponent shot at "+rowChar+" "+col+" and missed.";
System.out.println("Miss!");
return success;
}
public boolean placeShip(int length, char direction, int row, int column)
{
//Check validity of placement
for(int i = 0; i < length; i++)
{
if(direction == 'H')
{
//Checks if ship runs off board
if( (column+i) > 10)
{
System.out.println("Invalid placement, try again.");
return false;
}
//Checks if ship collides with another ship
if(!myBoard[row][column+i].equals("-"))
{
System.out.println("Invalid placement, try again.");
return false;
}
}
else if(direction == 'V')
{
//Checks if ship runs off board
if( (row+i) > 10)
{
System.out.println("Invalid placement, try again.");
return false;
}
//Checks if ship collides with another ship
if(!myBoard[row+i][column].equals("-"))
{
System.out.println("Invalid placement, try again.");
return false;
}
}
}
//Create and place the ship if it has been found to be valid
String shipID = Integer.toString(length);
Ship theShip = new Ship(length,direction,row,column);
myShips.add(theShip);
for(int i = 0; i < length; i++)
{
if(direction == 'H')
{
myBoard[row][column+i] = shipID;
}
else if(direction == 'V')
{
myBoard[row+i][column] = shipID;
}
}
return true;
}
}