-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
74 lines (64 loc) · 1.96 KB
/
Player.java
File metadata and controls
74 lines (64 loc) · 1.96 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
import java.util.ArrayList;
public class Player {
private ArrayList<Ship> ships;
private String[][] gameBoard;
private String[][] attackBoard;
private String name;
private Ship currentTargetShip;
public Player(String name, int playerNumber) {
this.ships = new ArrayList<>();
this.name = name;
this.gameBoard = createBoard(playerNumber);
this.attackBoard = createBoard(playerNumber);
}
public ArrayList<Ship> getShips() {
return ships;
}
public String[][] getGameBoard() {
return gameBoard;
}
public String[][] getAttackBoard() {
return attackBoard;
}
public String getName() {
return name;
}
private String[][] createBoard(int player) {
String[][] board = new String[11][11];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = "-";
}
}
String redBackground = "\u001B[41m";
String blueBackground = "\u001B[44m";
String reset = "\u001B[0m";
String character = " ";
if (player == 1) {
board[0][0] = redBackground + character + reset;
} else {
board[0][0] = blueBackground + character + reset;
}
for (int a = 1; a < board.length; a++) {
board[a][0] = String.valueOf((char) (a + 65 - 1));
}
for (int b = 1; b < board[0].length; b++) {
board[0][b] = String.valueOf(b);
}
return board;
}
public Ship getCurrentTargetShip() {
return currentTargetShip;
}
public void setCurrentTargetShip(Ship ship) {
currentTargetShip = ship;
}
public Ship getShipAt(char row, int col) {
for (Ship ship : ships) {
if (ship.containsCoordinate(row, col)) {
return ship;
}
}
return null; // No ship found at the specified coordinate
}
}