-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
72 lines (59 loc) · 2.48 KB
/
Player.java
File metadata and controls
72 lines (59 loc) · 2.48 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
package TEST;
import java.util.Scanner;
public class Player {
private String name;
private Board board;
public Player(String name) {
this.name = name;
this.board = new Board();
}
public String getName() {
return name;
}
public void placeSingleShip() {
Scanner scanner = new Scanner(System.in);
Ship ship = new Ship(3); // Single ship of length 3
boolean placed = false;
while (!placed) {
System.out.println(name + ", enter coordinates for your ship :");
String start = scanner.next();
String end = scanner.next();
try {
// Parse starting and ending positions
int startX = Integer.parseInt(start.substring(1)) - 1; // Convert row to index
int startY = board.letterToIndex(start.charAt(0)); // Convert column letter to index
int endX = Integer.parseInt(end.substring(1)) - 1; // Convert row to index
int endY = board.letterToIndex(end.charAt(0)); // Convert column letter to index
// Validate input for horizontal or vertical placement
if (startX == endX || startY == endY) {
placed = board.placeShip(ship, startX, startY, endX, endY);
if (!placed) {
System.out.println("Invalid placement. Ensure the ship fits and doesn't overlap.");
}
} else {
System.out.println("Ships must be placed in a straight line. Try again.");
}
} catch (Exception e) {
System.out.println("Invalid input format. Use something like 'A1 to A3'.");
}
}
}
public void makeMove(Player opponent) {
Scanner scanner = new Scanner(System.in);
System.out.println(name + ", enter coordinates for your attack :");
String input = scanner.next();
try {
int x = Integer.parseInt(input.substring(1)) - 1; // Convert row to index
int y = board.letterToIndex(input.charAt(0)); // Convert column letter to index
System.out.println(opponent.board.receiveAttack(x, y));
} catch (Exception e) {
System.out.println("Invalid input. Format should be like 'A1'.");
}
}
public boolean hasLost() {
return board.allShipsSunk();
}
public void displayBoard(boolean hideShips) {
board.displayBoard(hideShips);
}
}