-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
129 lines (122 loc) · 3.07 KB
/
Copy pathPlayer.cpp
File metadata and controls
129 lines (122 loc) · 3.07 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
125
126
127
128
129
#include "Player.hpp"
Player::Player() :
frame(13, 14,
" 1 "
" 1234567890 "
" ---------- "
"A| |"
"B| |"
"C| |"
"D| |"
"E| |"
"F| |"
"G| |"
"H| |"
"I| |"
"J| |"
" ---------- "
)
{
}
void Player::setmappos(int x, int y)
{
int offsetx = x - mapposx, offsety = y - mapposy;
for (int i = 0; i < 10; i++)
{
ships[i].posx += offsetx;
ships[i].posy += offsety;
}
mapposx = x;
mapposy = y;
}
void Player::control(char gch)
{
if (gch == '\t')
{
ships[controllingshipind].iscontrolling = 0;
ships[controllingshipind].isfalsepos = 0;
controllingshipind++;
controllingshipind %= 10;
ships[controllingshipind].iscontrolling = 1;
}
else if (gch == 'q' || gch == 'Q')
{
ships[controllingshipind].iscontrolling = 0;
ships[controllingshipind].isfalsepos = 0;
controllingshipind--;
controllingshipind += 10 * (controllingshipind < 0);
ships[controllingshipind].iscontrolling = 1;
}
else if (gch == 'n' || gch == 'N')
{
system("cls");
cci.bVisible = 1;
SetConsoleCursorInfo(conshand, &cci);
cout << "Enter your name: ";
cin >> name;
if (name.size() > 23)name.erase(23, name.size() - 23);
cci.bVisible = 0;
SetConsoleCursorInfo(conshand, &cci);
}
else
{
setcontrol(&(ships[controllingshipind]));
::control(gch);
}
Ship &csh = ships[controllingshipind];
csh.posx += (csh.posx < mapposx + 2) * (mapposx + 2 - csh.posx);
csh.posy += (csh.posy < mapposy + 3) * (mapposy + 3 - csh.posy);
csh.posx += (csh.posx > mapposx + 11 - (!csh.dir) * (csh.size - 1)) * (mapposx + 11 - (!csh.dir) * (csh.size - 1) - csh.posx);
csh.posy += (csh.posy > mapposy + 12 - (csh.dir) * (csh.size - 1)) * (mapposy + 12 - (csh.dir) * (csh.size - 1) - csh.posy);
ships[controllingshipind].isfalsepos = 0;
for (int i = 0; i < 10; i++)
{
if (i == controllingshipind) continue;
if (iscrossing(ships[i], ships[controllingshipind])) ships[controllingshipind].isfalsepos = 1;
}
}
void Player::draw()
{
::draw(frame, mapposx, mapposy);
for (int i = 0; i < 10; i++) if (i != controllingshipind) ships[i].draw();
ships[controllingshipind].draw();
SetConsoleCursorPosition(conshand, COORD{ short(nameposx - name.size() / 2), short(nameposy) });
cout << name;
}
bool isplayerlosed(Player &pl)
{
bool losed = 1;
for (int i = 0; i < 10; i++) for (int j = 0; j < pl.ships[i].size; j++) if (pl.ships[i].isattacked.get()[j] == 0)losed = 0;
return losed;
}
bool hittopl(Player &pl, int x, int y, int& hittedshind)
{
for (int i = 0; i < 10; i++)
{
bool hit = 0;
int index;
if (pl.ships[i].dir == Horizontal)
{
if (y == pl.ships[i].posy) if (x >= pl.ships[i].posx && x < pl.ships[i].posx + pl.ships[i].size)
{
hit = 1;
index = x - pl.ships[i].posx;
}
}
else if (pl.ships[i].dir == Vertical)
{
if (x == pl.ships[i].posx) if (y >= pl.ships[i].posy && y < pl.ships[i].posy + pl.ships[i].size)
{
hit = 1;
index = y - pl.ships[i].posy;
}
}
if (hit)
{
pl.ships[i].isattacked.get()[index] = 1;
hittedshind = i;
return 1;
}
}
return 0;
}