-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
90 lines (85 loc) · 2.06 KB
/
game.cpp
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
#include "game.h"
#include "stdio.h"
int Game::checkAbout(int Map[10][10], int i, int j, bool horizontal, int sizeShip)
{
i--;
j--;
sizeShip+=2;
if (horizontal){
for (int ic=i; ic<(i+3); ic++){
if (ic<0||ic>9) continue;
for (int jc=j; jc<(j+sizeShip); jc++){
if (jc<0||jc>9) continue;
if (Map[ic][jc]>1){return 1;};
}
}
}
else {
for (int ic=i; ic<(i+sizeShip); ic++){
if (ic<0||ic>9) continue;
for (int jc=j; jc<(j+3); jc++){
if (jc<0||jc>9) continue;
if (Map[ic][jc]>1){return 1;};
}
}
}
return 0;
}
void Game::genShips(int Map[10][10])
{
int i=0;
int j=0;
int size[10]={4, 3, 3, 2, 2, 2, 1, 1, 1, 1};
int id=3;
for (int s=0; s<10;s++){
bool horizontal=rand()%2;
if (horizontal){
i=rand()%10;
j=rand()%(11-size[s]);
if (checkAbout(Map, i, j, horizontal, size[s])){s--; continue;};
for (int ss=0; ss<size[s]; ss++){
Map[i][j+ss]=id;
}
id++;
}
else {
j=rand()%10;
i=rand()%(11-size[s]);
if (checkAbout(Map, i, j, horizontal, size[s])){s--; continue;};
for (int ss=0; ss<size[s]; ss++){
Map[i+ss][j]=id;
}
id++;
}
}
}
int Game::checkAbout(int Map[10][10], int i, int j)
{
i--;
j--;
int sizeShip=1;
sizeShip+=2;
for (int ic=i; ic<(i+3); ic++){
if (ic<0||ic>9) continue;
for (int jc=j; jc<(j+sizeShip); jc++){
if (jc<0||jc>9) continue;
if (Map[ic][jc]==2){return 1;};
}
}
return 0;
}
Game::Game(){}
void Game::newGame(){
Win=10;
WinF=10;
BotDmg=0;
for (int i=0; i<10; i++){
for (int j=0; j<10; j++){
masMap[i][j]=0;
masMapF[i][j]=0;
}
}
srand(time(NULL));
genShips(masMap);
genShips(masMapF);
}