-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleDotCom.java
More file actions
47 lines (39 loc) · 1.36 KB
/
SimpleDotCom.java
File metadata and controls
47 lines (39 loc) · 1.36 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
public class SimpleDotCom {
// locationCells - int array for cells storing
private int[] locationCells;
// numOfHints - int variable for storing number of hits
private int numOfHints = 0;
// checkYourself() - method which accepts user turn (String), checks it and returns "hit", "miss", "sink"
public String checkYourself(String stringGuess) {
// Get user turn as a string
// Convert user turn to int
int guess = Integer.parseInt(stringGuess);
// Default value is miss
String result = "Miss";
// Repeat with each cell
for (int cell : locationCells) {
// Compare user turn and cell placement
if (guess == cell) {
// If user correct
result = "Hit";
// +1 hit count
numOfHints += 1;
break;
}
}
// Check if this is a last cell
//If this is a third hit then return "sink"
if (numOfHints == locationCells.length) {
result = "Sink";
}
System.out.println(result);
return result;
}
// setLocationCells() - setter which accepts int array with three cells
public void setLocationCells(int[] locations) {
locationCells = locations;
}
public int getNumOfHints() {
return numOfHints;
}
}