-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
71 lines (64 loc) · 2.04 KB
/
Player.java
File metadata and controls
71 lines (64 loc) · 2.04 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
import java.util.*;
public class Player{
public Player (Board opponent) {
opponentBoard = opponent;
}
public static Board opponentBoard;
public void makeGuess(){
opponentBoard.printBoard();
System.out.println("Please enter the X coordinate of your attack.");
Scanner console = new Scanner(System.in);
String starting = console.next();
while (cont(starting)==false){
System.out.println("Input invalid. Please try again.");
starting = console.next();
}
int attackcoordinateX = Integer.parseInt(starting);
System.out.println("Please enter the Y coordinate of your attack.");
starting = console.next();
while (cont(starting)==false){
System.out.println("Input invalid. Please try again.");
starting = console.next();
}
int attackcoordinateY = Integer.parseInt(starting);
int result = opponentBoard.checkHit(attackcoordinateY, attackcoordinateX);
while (result == 2){
System.out.println("Input already guessed. Please try again.");
System.out.println("Please enter the X coordinate of your attack.");
attackcoordinateX = console.nextInt();
while (attackcoordinateX < 0 || attackcoordinateX > 9 ){
System.out.println("Input invalid. Please try again.");
attackcoordinateX = console.nextInt();
}
System.out.println("Please enter the Y coordinate of your attack.");
attackcoordinateY = console.nextInt();
while (attackcoordinateY < 0 || attackcoordinateY > 9 ){
System.out.println("Input invalid. Please try again.");
attackcoordinateY = console.nextInt();
}
result = opponentBoard.checkHit(attackcoordinateX, attackcoordinateY);
}
if (result == 1){
opponentBoard.printBoard();
System.out.println("Shot missed.");
}
if (result == 0){
opponentBoard.printBoard();
System.out.println("Boat hit.");
}
}
public boolean cont(String x){
try {
int n = Integer.parseInt(x);
if(n < 0 || n > 9) {
return false;
}
return true;
} catch(NumberFormatException e) {
return false;
}
}
public boolean isWinner() {
return opponentBoard.allShipsSunk();
}
}