-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlop.java
204 lines (173 loc) · 7.83 KB
/
Plop.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
Composants:
BoardModel *3 playboard, compboard, visuboard
bool[5] *2 PlayShipSet, CompShipSet
switch (ship type)
4 boutons pour les 4 directions
turn
skillLevel compLVL=BEGINNER;
bool[10][10] compshots=false;
coor2D lastCompHit;
Direction dir=NORTH;
initialisation:
new BoardModel(Available) *2
ships placement:
select ship type (switch)
select start case ->cellModel startcell
si ship/= submarine
show available directions
testDirection(cellModel startcell, direction dir, ShipType bateau) {
bool ok=true;
cellModel test=startcell;
int i=0;
while(i<bateau.size-1 && ok) {
if (adjacentCell(test, dir).CellType==Available) {
i++
test=(adjacentCell(test, dir)
}
else ok=false;
}
return ok;
}
select direction
placeShip(cellModel startcell, direction dir, ShipType bateau) {
cellModel cell=startcell;
for i=0 i<bateau.getsize {
cell.setCellType(shipTypeToCellType(bateau));
cell=(adjacentCell(cell, dir);
}
}
replaceAll(available,ocean)
pour Ordi: randomCell(available, true) pour startcell
pour visuboard :replaceALL(available, unknown)
Jeu:
coord2D coord;
lastCompHit=coord2D(playboard.BOARD_SIZE+1,playboard.BOARD_SIZE+1);
if turn==players {
get cell clicked->cellModel cellclicked
coord=visuboard.cellCoord(cellclicked);
if compboard.getcellmodel(coord.row,coord.column).getCellType().isAShip() {
compfleet.updateHits(cellTypeToShipType(compboard.getcellmodel(coord.row,coord.column).getCellType()))
compboard.getcellmodel(coord.row,coord.column).setCellType(HIT);
visuboard.getcellmodel(coord.row,coord.column).setCellType(HIT);
}
else {
visuboard.getcellmodel(coord.row,coord.column).setCellType(OCEAN);
}
turn=COMPUTER;
}
//bool[10][10] compshots
//coor2D lastCompHit
else {// turn==computer
if compLVL==BEGINER {
bool test=false;
while !test {
coord=playboard.cellCoords(playboard.randomCell(AVAILABLE, false));
if !compshots[coord.row][coord.column] {
compshots[coord.row][coord.column]=true;
test=true;
if playboard.getcellmodel(coord.row,coord.column).getCellType().isAShip() {
playfleet.updateHits(cellTypeToShipType(playboard.getcellmodel(coord.row,coord.column).getCellType()))
playboard.getcellmodel(coord.row,coord.column).setCellType(HIT);
lastCompHit=coord;
}
}
}
} else if compLVL==MEDIUM {
//Direction dir
bool test=false;
if lastCompHit.getRow>playboard.BOARD_SIZE {
while !test {
coord=playboard.cellCoords(playboard.randomCell(AVAILABLE, false));
if !compshots[coord.row][coord.column] {
compshots[coord.row][coord.column]=true;
test=true;
if playboard.getcellmodel(coord.row,coord.column).getCellType().isAShip() {
playfleet.updateHits(cellTypeToShipType(playboard.getcellmodel(coord.row,coord.column).getCellType()))
playboard.getcellmodel(coord.row,coord.column).setCellType(HIT);
lastCompHit=coord;
}
}
}
} else {
coord=lastCompHit;
bool north,est,south,west=false;
while !test {
if dir==NORTH {
if coord.getColumn()>0 {
coord.setColumn(coord.getColumn()-1)
} else {
north=true;
if !est {dir=EST} else if !south {dir=SOUTH} else if !west {dir=WEST}
}
if !compshots[coord.row][coord.column] {
test=true
} else if playboard.getcellmodel(coord.row,coord.column).getCellType()==OCEAN {
coord=lastCompHit;
north=true;
if !est {dir=EST} else if !south {dir=SOUTH} else if !west {dir=WEST}
}
} else if dir==EST {
if coord.getRow()<playbord.BOARD_SIZE {
coord.setRow(coord.getRow()+1)
} else {
est=true;
if !north {dir=NORTH} else if !south {dir=SOUTH} else if !west {dir=WEST}
}
if !compshots[coord.row][coord.column] {
test=true
} else if playboard.getcellmodel(coord.row,coord.column).getCellType()==OCEAN {
coord=lastCompHit;
est=true;
if !north {dir=NORTH} else if !south {dir=SOUTH} else if !west {dir=WEST}
}
} else if dir==SOUTH {
if coord.getColumn()<playbord.BOARD_SIZE {
coord.setColumn(coord.getColumn()+1)
} else {
south=true;
if !north {dir=NORTH} else if !est {dir=EST} else if !west {dir=WEST}
}
if !compshots[coord.row][coord.column] {
test=true
} else if playboard.getcellmodel(coord.row,coord.column).getCellType()==OCEAN {
coord=lastCompHit;
south=true;
if !north {dir=NORTH} else if !est {dir=EST} else if !west {dir=WEST}
}
} else if dir==WEST {
if coord.getRow()>0 {
coord.setRow(coord.getRow()-1)
} else {
west=true;
if !north {dir=NORTH} else if !south {dir=SOUTH} else if !est {dir=EST}
}
if !compshots[coord.row][coord.column] {
test=true
} else if playboard.getcellmodel(coord.row,coord.column).getCellType()==OCEAN {
coord=lastCompHit;
west=true;
if !north {dir=NORTH} else if !south {dir=SOUTH} else if !est {dir=EST}
}
}
if north && est && south && west {
lastCompHit.setRow(playboard.BOARD_SIZE+1);
LastCompHit.setColumn(playboard.BOARD_SIZE+1);
while !test {
coord=playboard.cellCoords(playboard.randomCell(AVAILABLE, false));
if !compshots[coord.row][coord.column] {
test=true;
}
}
}
}
compshots[coord.row][coord.column]=true;
test=true;
if playboard.getcellmodel(coord.row,coord.column).getCellType().isAShip() {
playfleet.updateHits(cellTypeToShipType(playboard.getcellmodel(coord.row,coord.column).getCellType()))
playboard.getcellmodel(coord.row,coord.column).setCellType(HIT);
lastCompHit=coord;
}
}
}
turn=PLAYER;
}