-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ships.java
133 lines (108 loc) · 3.22 KB
/
Ships.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
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
125
126
127
128
129
130
131
132
133
import java.util.Random;
public class Ships extends Game {
private int battleship;
private int destroyer;
private int submarine;
private int boat;
public int BoatDefine = 2;
public int SubmarineDefine = 3;
public int DestroyerDefine = 4;
public int BattleshipDefine = 5;
public int getBattleshipSize() {
return 4;
}
public int getBattleshipCount() {
return battleship;
}
public void setBattleship(int battleship) {
this.battleship = battleship;
}
public int getDestroyerSize() {
return 3;
}
public int getDestroyerCount() {
return destroyer;
}
public void setDestroyer(int destroyer) {
this.destroyer = destroyer;
}
public int getSubmarineSize() {
return 2;
}
public int getSubmarineCount() {
return submarine;
}
public void setSubmarine(int submarine) {
this.submarine = submarine;
}
public int getBoatSize() {
return 1;
}
public int getBoatCount() {
return boat;
}
public void setBoat(int boat) {
this.boat = boat;
}
public void setEasy() {
this.setBoat(1);
this.setSubmarine(1);
this.setDestroyer(1);
this.setBattleship(0);
}
public void setNormal() {
this.setBoat(2);
this.setSubmarine(1);
this.setDestroyer(1);
this.setBattleship(1);
}
public void setHard() {
this.setBoat(2);
this.setSubmarine(1);
this.setDestroyer(1);
this.setBattleship(1);
}
public void setRandom() {
for (int i = 0; i < this.getBoatCount(); i++) {
setShip(this.getBoatSize(), this.BoatDefine);
}
for (int i = 0; i < this.getSubmarineCount(); i++) {
setShip(this.getSubmarineSize(), this.SubmarineDefine);
}
for (int i = 0; i < this.getDestroyerCount(); i++) {
setShip(this.getDestroyerSize(), this.DestroyerDefine);
}
for (int i = 0; i < this.getBattleshipCount(); i++) {
setShip(this.getBattleshipSize(), this.BattleshipDefine);
}
}
public String getType(int type) {
if (type == this.BoatDefine) {
return "Boat";
} else if (type == this.SubmarineDefine) {
return "Submarine";
} else if (type == this.DestroyerDefine) {
return "Destroyer";
} else if (type == this.BattleshipDefine) {
return "Battleship";
}
return "";
}
public void setShip(int size, int type) {
if (size <= 0 || size >= 5) {
return;
}
Random r = new Random();
int x = r.nextInt(worldgrid.getWorld().length - size);
int y = r.nextInt(worldgrid.getWorld()[0].length - size);
for (int i = 0; i < size; i++) {
if (worldgrid.getPointWorld(x, y + i) >= 2) {
setShip(size, type);
return;
}
}
for (int i = 0; i < size; i++) {
worldgrid.setPointWorld(x, y + i, type);
}
}
}