-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
139 lines (122 loc) · 5.16 KB
/
game.cpp
File metadata and controls
139 lines (122 loc) · 5.16 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
130
131
132
133
134
135
136
137
138
139
#include "game.h"
#include <array>
#include <iomanip>
#include "windows.h"
void Game::Loop()
{
std::map<std::string, Vector> directions = {
{"left", {-1, 0}},
{"up", {0, -1}},
{"right", {1, 0}},
{"down", {0, 1}},
};
bool continue_step = false;
bool running = true;
while (running) {
continue_step = false;
if (gamePhase == GamePhase::gpPlacing) {
Render();
std::string coord_inp, direction_inp, type_inp;
int ship_type;
std::cout << playerStep << "> ";
std::cin >> coord_inp >> direction_inp >> ship_type;
Vector coord = {coord_inp[0] - 'a', coord_inp[1] - '0' - 1};
//std::cout << coord.x << " " << coord.y << std::endl;
GameArea* gameArea = playerAreas[playerStep];
PlaceResult res = gameArea->PlaceShip(coord, directions[direction_inp], (ShipType)(ship_type-1));
if (res == PlaceResult::prForbidden || res == PlaceResult::prAlreadyPlaced) {
std::cout << "нельзя так размещать" << std::endl;
Sleep(messageShowDelay);
continue_step = true;
} else if (res == PlaceResult::prPlaced) {
std::cout << "размещено" << std::endl;
Sleep(messageShowDelay);
}
int allPlacedPlayerCount = 0;
for (GameArea* gameArea : playerAreas) {
if (gameArea->GetDockedShipsCount() == 0)
allPlacedPlayerCount++;
}
if (allPlacedPlayerCount == 2) {
gamePhase = GamePhase::gpHitting;
}
}
if (gamePhase == GamePhase::gpHitting) {
Render();
std::string coord_inp;
std::cout << playerStep << "> ";
std::cin >> coord_inp;
Vector coord = {coord_inp[0] - 'a', coord_inp[1] - '0' - 1};
int oppositePlayer = playerStep == 0 ? 1 : 0;
GameArea* gameArea = playerAreas[oppositePlayer];
HitResult res = gameArea->HitShip(coord);
if (res == HitResult::hrMissed) {
std::cout << "промах" << std::endl;
Sleep(messageShowDelay);
} else if (res == HitResult::hrForbidden) {
std::cout << "уже стрелял сюда попробуй другое место" << std::endl;
Sleep(messageShowDelay);
continue_step = true;
} else if (res == HitResult::hrStruck) {
std::cout << "попал" << std::endl;
Sleep(messageShowDelay);
continue_step = true;
} else if (res == HitResult::hrSinked) {
std::cout << "затопил" << std::endl;
Sleep(messageShowDelay);
continue_step = true;
}
if (gameArea->GetDockedShipsCount() >= gameArea->GetTotalShipCount()) {
std::cout << "==== ИГРОК " << playerStep << " ВЫИГРАЛ ====" << std::endl;
running = false;
}
}
if (!continue_step)
if (playerStep == 0)
playerStep = 1;
else
playerStep = 0;
}
}
void Game::Render()
{
system("cls");
if (gamePhase == gpPlacing) {
std::cout << "==== РЕЖИМ РАЗМЕЩЕНИЯ ====" << std::endl;
std::cout << "разместите корабль (<координата> <поворот> <кол-во палуб>) (a4 right 2)" << std::endl;
RenderGameArea(playerStep, playerAreas[playerStep], false);
}
else if (gamePhase == gpHitting) {
std::cout << "==== РЕЖИМ ПОДБИТИЯ ====" << std::endl;
std::cout << "подбейте корабль (координата) (a4)" << std::endl;
int oppositePlayer = playerStep == 0 ? 1 : 0;
RenderGameArea(playerStep, playerAreas[playerStep], false);
std::cout << std::endl;
RenderGameArea(oppositePlayer, playerAreas[oppositePlayer], true);
}
}
void Game::RenderGameArea(int playerId, GameArea* gameArea, bool hide)
{
std::array<std::string, 4> shipTypesNames{ "один", "два", "три", "четыре" };
std::cout << playerId << " a b c d e f g h i j" << std::endl;
for (int y = 0; y < 10; y++) {
std::cout << std::setw(2) << y + 1 << " ";
for (int x = 0; x < 10; x++) {
CellType cell = gameArea->GetCell({ x, y });
//std::cout << (int)cell;
if (cell == CellType::ctNone || cell == CellType::ctShipArea || (cell == CellType::ctShip && hide))
std::cout << ". ";
if (cell == CellType::ctMiss)
std::cout << "M ";
if (cell == CellType::ctHit)
std::cout << "X ";
if (cell == CellType::ctShip && !hide)
std::cout << "S ";
if (cell == CellType::ctSinked)
std::cout << "# ";
}
if (y >= 0 && y < 4)
std::cout << shipTypesNames[y] << ": " << gameArea->GetDockedShipsCount((ShipType)y);
std::cout << std::endl;
}
}