-
Notifications
You must be signed in to change notification settings - Fork 0
/
back.js
401 lines (398 loc) · 13.4 KB
/
back.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// 0 = tenger ; 1 = hajo ; 2 = rossz talalat ; 3 = jo talalat ; 4 = felesleges odamenni
/*
HIBAK
Kiszamolja mennyi hely van es az alapjan dont
*/
var JATEKVAN = false
var SHIP1 = false // ez azt nezi hogy az egyes hajot megtalalte e vagy sem
var DONESHIP = [false,false,false,false,false] // nezi melyik hajo van kesz
var STREAK = 0
//-------------Segédek----------------
function MasterReset(){
window.location.reload();
}
function PlayReset(){// uj jatek
document.getElementById("flotta").innerHTML =""
JATEKVAN = false
SHIP1 = false // ez azt nezi hogy az egyes hajot megtalalte e vagy sem
DONESHIP = [false,false,false,false,false] // nezi melyik hajo van kesz
STREAK = 0
}
//--------------Harc------------------
function StartFight(ownSea){
document.getElementById("btn").remove()
enemySea = []
var flottaDiv = document.getElementById("flotta")
flottaDiv.appendChild(MakeSea(false,enemySea,ownSea))
RandomPlace(enemySea,false)
MakeOnHover(false,enemySea)
document.getElementById("Ycurrent").style.backgroundColor="green"
document.getElementById("Ycurrent").style.borderRadius = "8px"
document.getElementById("Ecurrent").style.borderRadius = "8px"
}
function Wincheck(who,sea){ // true= own; false = enemy
cntr = 0
for (let y = 0; y < 10; y++) {
for (let x = 0; x < 10; x++) {
if (sea[y][x]==3){
cntr +=1
}
}
}
if (cntr == 15) {
if (who) {
alert("TE NYERTÉÉÉL :)))))))))")
}
else{
alert("ROBOT NYERT :(((((")
}
MasterReset()
}
}
function AiAllow(ownSea){
var aNB = [[0,-1],[0,+1],[-1,0],[+1,0]]//4 szomszed
var hNB = [[0,-1],[0,+1]]// horizontal szomszedok
var vNB = [[-1,0],[+1,0]]// vertical szomszedok
for (let y = 0; y < 10; y++) {
for (let x = 0; x < 10; x++) {//ez a ketto ciklus vegigmegy a matrix tengerunkon
if (ownSea[y][x]==3) {
//dontsuk el hogy horizontalis vagy verticalis! ezaltal felesleges helyek vizsgalatat skippelhetjuk
var hr = false // horizontal bool
try {
if(ownSea[y][x-1]==3 ) {
hr = true
}
} catch (error) {}
try {
if(ownSea[y][x+1]==3) {
hr = true
}
} catch (error) {}
var vr = false
try {
if(ownSea[y-1][x]==3) {
vr = true
}
} catch (error) {}
try {
if(ownSea[y+1][x]==3) {
vr = true
}
} catch (error) {}
if (hr) {//horizontal?
for (let i = 0; i < hNB.length; i++) {
try {
if (ownSea[y+hNB[i][0]][x+hNB[i][1]]==0 || ownSea[y+hNB[i][0]][x+hNB[i][1]]==1) {
return [y+hNB[i][0],x+hNB[i][1]]
}
} catch (error) {}
}
}
else if (vr) {//vertical?
for (let i = 0; i < vNB.length; i++) {
try {
if (ownSea[y+vNB[i][0]][x+vNB[i][1]]==0 || ownSea[y+vNB[i][0]][x+vNB[i][1]]==1) {
return [y+vNB[i][0],x+vNB[i][1]]
}
} catch (error) {}
}
}
else{//csak egy col!
for (let i = 0; i < aNB.length; i++) {
try {
if (ownSea[y+aNB[i][0]][x+aNB[i][1]]==0 || ownSea[y+aNB[i][0]][x+aNB[i][1]]==1) {
return [y+aNB[i][0],x+aNB[i][1]]
}
} catch (error) {}
}
}
}
}
}
return -1
}
function UselessStep(y,x,ownSea){
var nb = [[0,-1],[0,+1],[-1,0],[+1,0],[-1,-1],[+1,+1],[-1,+1],[+1,-1]]//osszes szomszed 8db
for (let i = 0; i < nb.length; i++) {
try {
if (ownSea[y+nb[i][0]][x+nb[i][1]]==3) {
ownSea[y][x] = 4
return true
}
} catch (error) {}
}
return false
}
function NewCordGen(ownSea) {
var sv = []
do {
var y = Math.floor(Math.random() * 10);
var x = Math.floor(Math.random() * 10);
} while (ownSea[y][x]==2 || ownSea[y][x]==3 || ownSea[y][x]==4 || UselessStep(y,x,ownSea));
sv.push(y)
sv.push(x)
return sv
}
function RobotStep(ownSea){
setTimeout(function() {
var y = -1
var x = -1
var sv= AiAllow(ownSea)
if (sv != -1) {
y = sv[0]
x = sv[1]
if (STREAK == 5) {
DONESHIP[4] = true
} else if (DONESHIP[4] && STREAK == 4) {
DONESHIP[3] = true
} else if (DONESHIP[3] && DONESHIP[4] && STREAK == 3) {
DONESHIP[2] = true
}
if (ownSea[y][x] == 0) {
if (STREAK == 5 || DONESHIP[4] && STREAK == 4 || DONESHIP[3] && DONESHIP[4] && STREAK == 3 || DONESHIP[2] && DONESHIP[3] && DONESHIP[4] && STREAK == 2) {
ownSea[y][x] = 4
STREAK = 0
sv = NewCordGen(ownSea)
y = sv[0]
x = sv[1]
}
}
else if (ownSea[y][x] == 4) {
STREAK = 0
sv = NewCordGen(ownSea)
y = sv[0]
x = sv[1]
}
}
else{
if (STREAK-1 >=0) {
DONESHIP[STREAK-1] = true
}
STREAK = 0
sv = NewCordGen(ownSea)
y = sv[0]
x = sv[1]
}
prev = [y,x]
document.getElementById(''+y+true+x).style.backgroundColor="red"
if (ownSea[y][x]==1) {
document.getElementById(''+y+true+x).style.backgroundColor="#8B0000"
document.getElementById(''+y+true+x).innerHTML="X"
document.getElementById(''+y+true+x).style.color="whitesmoke"
document.getElementById(''+y+true+x).style.fontSize="35px"
ownSea[y][x] = 3 // jo talalat
STREAK +=1
console.log(ownSea)
Wincheck(false,ownSea)
RobotStep(ownSea)
}
else{
ownSea[y][x] = 2 // rossz talalat
document.getElementById("Ycurrent").style.backgroundColor="green"
document.getElementById("Ecurrent").style.backgroundColor=""
}
JATEKVAN = false
}, 500);
}
function Durrr(y,x,enemySea,ownSea){
if (JATEKVAN) {
return
}
if (enemySea[y][x]==0) {
document.getElementById("Ecurrent").style.backgroundColor="green"
document.getElementById("Ycurrent").style.backgroundColor=""
document.getElementById(''+y+false+x).style.backgroundColor="red"
enemySea[y][x]=2// rossz talalat
JATEKVAN=true
RobotStep(ownSea)
}
else if(enemySea[y][x] != 2){
document.getElementById(''+y+false+x).style.backgroundColor="#8B0000"
document.getElementById(''+y+false+x).innerHTML="X"
document.getElementById(''+y+false+x).style.color="whitesmoke"
document.getElementById(''+y+false+x).style.fontSize="35px"
enemySea[y][x]=3 // jo talalat
Wincheck(true,enemySea)
}
}
//-------------Generalas---------------
function Neighbors8Check(check,sea){
var neighbors8 = [[0,-1],[0,+1],[-1,0],[+1,0],[-1,-1],[+1,+1],[-1,+1],[+1,-1]]//osszes szomszed
for (let i = 0; i < neighbors8.length; i++) {
if (check[0]-neighbors8[i][0]<0 || check[1]-neighbors8[i][1]< 0 || check[0]-neighbors8[i][0]>=10 || check[1]-neighbors8[i][1]>=10) {
continue
}
else if (sea[check[0]-neighbors8[i][0]][check[1]-neighbors8[i][1]] == 1) {
return false
}
}
return true
}
function Direction(y,x,i,sea){
var dir = Math.floor(Math.random() * 2) //0=ver 1=hor
var neighbors = [[0,-1],[0,+1],[-1,0],[+1,0]] // horizontal vizsgalattal kezd
if (dir==0) {
neighbors = [[-1,0],[+1,0],[0,-1],[0,+1]] // vertical vizsgalattal kezd
}
for (let k = 0; k < neighbors.length; k++) {
var ship = []
curr = [y,x]
if (!Neighbors8Check(curr,sea)) {
return []
}
var valid = true
ship.push(curr)
for (let j = 0; j < i-1; j++) {
var check = [curr[0]+neighbors[k][0],curr[1]+neighbors[k][1]] //vizsgalando cord
if (check[0] >= 0 && check[0] < 10 && check[1] >= 0 && check[1] < 10) {
if (sea[check[0]][check[1]] == 0) {
if (Neighbors8Check(check,sea)) {
curr = [check[0],check[1]]
ship.push(curr)
}
else{
return []
}
}
else{
return []
}
}
else{
return []
}
}
if (valid) {
return ship
}
}
return []
}
function RandomPlace(sea,who){
for (let i = 1; i <= 5; i++) {
while (true) {
var y = Math.floor(Math.random() * 10);// vertical
var x = Math.floor(Math.random() * 10);// horizontal
var ship = Direction(y,x,i,sea)
if (ship.length>0) {
for (let j = 0; j < ship.length; j++) {
if (who) {
document.getElementById(''+ship[j][0]+who+ship[j][1]).style.backgroundColor="black"
}
sea[ship[j][0]][ship[j][1]]=1
}
break
}
}
}
}
function MakeSea(who,sea,otherSea){// true = own ; false = enemy
var table = document.createElement("table")
var tblBody = document.createElement("tbody")
var div = document.createElement("div")
div.className="col"
var p = document.createElement("p")
if (who) {
p.innerHTML="Te"
p.id="Ycurrent"
}
else{
p.innerHTML="Ai"
p.id="Ecurrent"
}
p.style.fontSize="30px"
p.style.marginTop="40px"
div.appendChild(p)
div.style.textAlign="center"
for (let y = 0; y < 10; y++) {// rows
var tr = document.createElement("tr")
var sv = []
for (let x = 0; x < 10; x++) {// cols
var td = document.createElement("td")
td.style.width="50px"
td.style.height="50px"
td.style.backgroundColor="#006699"
td.style.border = '1px solid white'
td.id = ''+y+who+x
sv.push(0)
if (!who) {
td.onclick= function(){Durrr(y,x,sea,otherSea)}
}
tr.appendChild(td)
}
tblBody.appendChild(tr)
sea.push(sv)
}
table.appendChild(tblBody)
div.appendChild(table)
return div
}
function MakeOnHover(who,sea){
for (let y = 0; y < 10; y++) {
for (let x = 0; x < 10; x++) {
if (who) {
if (sea[y][x]==1) {
document.getElementById(''+y+true+x).style.cursor = "grab";
}
}
else{
document.getElementById(''+y+false+x).style.cursor = "crosshair";
}
}
}
}
function StartButton(ownSea){
var div = document.createElement("div")
div.className="col"
div.id="btn"
div.style.textAlign="center"
div.style.marginTop="100px"
var btn = document.createElement("button")
btn.style.width="500px"
btn.style.height="500px"
btn.style.backgroundColor="#006699"
btn.innerHTML="GO"
btn.onclick = function(){StartFight(ownSea)};
div.appendChild(btn)
return div
}
//---------------INFO--------------
function INFO(){
PlayReset()
var flottaDiv = document.getElementById("flotta")
var div = document.createElement("div")
div.id="aiLeiras"
var ul = document.createElement("ul")
var text = [
"A JATEK gombra kattintva megkapjuk a random legenerált hajóinkat <b>(fekete négyzetek)</b>.",
"Ha nem tetszik az állás akkor a gomb újboli legnyomása esetén új csatateret kapunk.",
"Ha megelégedett a lepakolással akkor a GO gombbal véglegesítve elkezdődik a játék.",
"Ezután az ellenfél táblájában belül lehet kattintani a tetszőleg (kilövendő) négyzetre.",
"Majd vársz a robot válaszára és vica-versa.",
"Győzze le az ai-t!, sok sikert"
]
for (let i = 0; i < text.length; i++) {
var p = document.createElement("p")
var li = document.createElement("li")
p.innerHTML=text[i]
p.style.fontSize="30px"
p.style.fontFamily="Roboto Mono"
p.style.color="whitesmoke"
p.style.marginTop="50px"
li.appendChild(p)
ul.appendChild(li)
}
div.appendChild(ul)
flottaDiv.appendChild(div)
}
//---------------MAIN--------------
function Play(){
PlayReset()
ownSea = []
svSea = [] // ennek nincs itt ertelme
var flottaDiv = document.getElementById("flotta")
flottaDiv.appendChild(MakeSea(true,ownSea,svSea))
RandomPlace(ownSea,true)
MakeOnHover(true,ownSea)
flottaDiv.appendChild(StartButton(ownSea))
}