-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameBoard.java
78 lines (70 loc) · 1.95 KB
/
gameBoard.java
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
import java.util.Scanner;
public class gameBoard {
private ship[][] gameBoard;
public gameBoard() {
this.gameBoard = new ship[10][10];
}
public boolean placeShip(ship ship){
if(ship == null){
return false;
}if(ship.taille <= 0){
return false;
}
if(ship.x < 0 || ship.x >= 10 || ship.y < 0 || ship.y >= 10){
return false;
}
if(ship.x + ship.taille > 10 && ship.y + ship.taille > 10){
return false;
}
for(int i = 0; i < ship.taille; i++){
int x = ship.x;
int y = ship.y;
if (ship.taille > 1) {
if (i > 0) {
if (ship.x == x) {
y++;
} else {
x++;
}
}
}
if(this.gameBoard[x][y] != null){
return false;
}
}
for (int i = 0; i < ship.taille; i++) {
int x = ship.x;
int y = ship.y;
if (ship.taille > 1) {
if (i > 0) {
if (ship.x == x) {
y++;
} else {
x++;
}
}
}
this.gameBoard[x][y] = ship;
}
return true;
}
public boolean isShot(int x, int y) {
return this.gameBoard[x][y] != null && !this.gameBoard[x][y].isShot();
}
public void shoot(int x, int y){
if (this.gameBoard[x][y] != null) {
this.gameBoard[x][y].isShot();
this.gameBoard[x][y].sizeDecrease();
}
}
public boolean isEmpty(){
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (this.gameBoard[i][j] != null) {
return false;
}
}
}
return true;
}
}