-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleShip.java
More file actions
124 lines (93 loc) · 3.26 KB
/
BattleShip.java
File metadata and controls
124 lines (93 loc) · 3.26 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package battleship;
import battleship.Board.Cell;
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class BattleShip extends Application {
private boolean running = false;
private Board enemyBoard, playerBoard;
private int shipsToPlace = 5;
private boolean enemyTurn = false;
private Random random = new Random();
private Parent createContent() {
BorderPane root = new BorderPane();
root.setPrefSize(600, 800);
root.setRight(new Text("CONTROLS:" + "\n" +
"Left click-Vertical Ships"+ "\n" + "Right click-Horizontal Ships"
+ "\n" + "Bottom Board = Player board" + "\n" + "Top Board = Enemy Board"
+ "\n" + "Red = kill" + "\n" + "Black = miss"
));
enemyBoard = new Board(true, event -> {
if (!running)
return;
Cell cell = (Cell) event.getSource();
if (cell.wasShot)
return;
enemyTurn = !cell.shoot();
if (enemyBoard.ships == 0) {
System.out.println("YOU WIN");
System.exit(0);
}
if (enemyTurn)
enemyMove();
});
playerBoard = new Board(false, event -> {
if (running)
return;
Cell cell = (Cell) event.getSource();
if (playerBoard.placeShip(new Ship(shipsToPlace, event.getButton() == MouseButton.PRIMARY), cell.x, cell.y)) {
if (--shipsToPlace == 0) {
startGame();
}
}
});
VBox vbox = new VBox(50, enemyBoard, playerBoard);
vbox.setAlignment(Pos.CENTER);
root.setCenter(vbox);
return root;
}
private void enemyMove() {
while (enemyTurn) {
int x = random.nextInt(10);
int y = random.nextInt(10);
Cell cell = playerBoard.getCell(x, y);
if (cell.wasShot)
continue;
enemyTurn = cell.shoot();
if (playerBoard.ships == 0) {
System.out.println("YOU LOSE");
System.exit(0);
}
}
}
private void startGame() {
// place enemy ships
int type = 5;
while (type > 0) {
int x = random.nextInt(10);
int y = random.nextInt(10);
if (enemyBoard.placeShip(new Ship(type, Math.random() < 0.5), x, y)) {
type--;
}
}
running = true;
}
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(createContent());
primaryStage.setTitle("Battleship");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}