-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHitOrMiss.java
More file actions
85 lines (79 loc) · 2.58 KB
/
HitOrMiss.java
File metadata and controls
85 lines (79 loc) · 2.58 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
package cs3500.pa03.Model;
import java.util.ArrayList;
import java.util.List;
/**
* Represents whether a hit or miss occurred.
*/
public class HitOrMiss {
/**
* @param points the points to be checked
* @param AiBoard the board to be checked
* @param trackerBoard the tracker board to be checked
* @return the list of successful hits
*/
public List<Coord> humanHit(List<Coord> points, GameBoard AiBoard, char[][] trackerBoard) {
ArrayList<Coord> humanSuccessfulHits = new ArrayList<>();
for (Coord c : points) {
int x = c.getX();
int y = c.getY();
if (AiBoard.getBoard(x, y) != '0') {
AiBoard.setBoard(x, y, 'H');
trackerBoard[x][y] = 'H';
humanSuccessfulHits.add(c);
AiBoard.sinkShip();
} else {
AiBoard.setBoard(x, y, 'M');
trackerBoard[x][y] = 'M';
}
}
return humanSuccessfulHits;
}
/**
* @param points the points to be checked
* @param HumanBoard the board to be checked
* @return the list of successful hits
*/
public List<Coord> aiHit(List<Coord> points, GameBoard HumanBoard) {
ArrayList<Coord> aiSuccessfulHits = new ArrayList<>();
for (Coord c : points) {
int x = c.getX();
int y = c.getY();
if (HumanBoard.getBoard(x, y) != '0') {
HumanBoard.setBoard(x, y, 'H');
aiSuccessfulHits.add(c);
HumanBoard.sinkShip();
} else {
HumanBoard.setBoard(x, y, 'M');
}
}
return aiSuccessfulHits;
}
/**
* @param shots
* @param ships
* @return a list of coordinates representing where the ai plyaer hit the opponent
*/
public List<Coord> AiServerBoardHits(List<Coord> shots, List<Ship> ships) {
ArrayList<Coord> oppenentSuccessfulHits = new ArrayList<>();
for (Ship s : ships) {
for (Coord c : shots) {
Coord firstCoord = s.getFirstCoord();
if (s.getDirection() == DirectionType.HORIZONTAL) {
for (int i = 0; i < s.getShipLength(); i++) {
if (c.getX() == firstCoord.getX() + i && c.getY() == firstCoord.getY()) {
oppenentSuccessfulHits.add(c);
}
}
} else {
for (int i = 0; i < s.getShipLength(); i++) {
if (c.getX() == firstCoord.getX() && c.getY() == firstCoord.getY() + i) {
oppenentSuccessfulHits.add(c);
}
}
}
}
}
System.out.println("Opponent successful hits: " + oppenentSuccessfulHits);
return oppenentSuccessfulHits;
}
}