Correct field's initialization

This commit is contained in:
2025-07-07 02:33:19 +03:00
parent d4d3ade78b
commit f13bf95ba2
11 changed files with 265 additions and 14 deletions

13
src/BattleCap.cpp Normal file
View File

@@ -0,0 +1,13 @@
// BattleCap.cpp: определяет точку входа для приложения.
//
#include "stdafx.h"
int main()
{
Game game;
game.init();
game.play();
return 0;
}

135
src/Game.cpp Normal file
View File

@@ -0,0 +1,135 @@
#include "Game.h"
void Game::clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void Game::print_field() {
clearScreen();
for (const auto& row : field) {
for (char cell : row) {
std::cout << cell << ' ';
}
std::cout << '\n';
}
}
void Game::info() {
while (true) {
clearScreen();
std::cout << "Main author: ParkSuMin\n"
<< "Email: tsettaro@paksurion.ru\n";
int key = _getch();
if (key == ESC_KEY) { // Escape
std::cout << "Exit" << std::endl;
break;
}
}
}
void Game::init() {
auto isNearPlayer = [this](int x, int y) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> 8 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int dy = -1; dy <= 1; ++dy) {
for (int dx = -1; dx <= 1; ++dx) {
if (dx == 0 && dy == 0) continue; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int nx = (x + dx + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1);
int ny = (y + dy + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1);
if (field[ny][nx] == PLAYER_CELL) {
return true;
}
}
}
return false;
};
for (const auto& element : gamers[0].get_army()) {
int x = 0, y = 0;
bool found = false;
while (true) {
int start = 0;
int end = SIZE_FOR_ARRAY;
int i = rand() % (end - start + 1) + start;
int j = rand() % (end - start + 1) + start;
if (field[i][j] != PLAYER_CELL && field[i][j] == EMPTY_CELL) {
field[i][j] = CURSOR_CELL;
x = j;
y = i;
found = true;
break;
}
}
if (!found) return;
while (true) {
clearScreen();
print_field();
int key = _getch();
if (key == ESC_KEY) {
std::cout << "Exit\n";
exit(0);
}
int newX = x, newY = y;
switch (key) {
case UP_KEY: newY = (y == 0) ? SIZE_FOR_ARRAY : y - 1; break;
case DOWN_KEY: newY = (y == SIZE_FOR_ARRAY) ? 0 : y + 1; break;
case LEFT_KEY: newX = (x == 0) ? SIZE_FOR_ARRAY : x - 1; break;
case RIGHT_KEY: newX = (x == SIZE_FOR_ARRAY) ? 0 : x + 1; break;
case ENTER: found = false; field[newY][newX] = PLAYER_CELL; break;
default: continue;
}
if (!found) break;
if (field[newY][newX] == PLAYER_CELL) {
newX = x;
newY = y;
continue;
}
field[y][x] = EMPTY_CELL;
field[newY][newX] = CURSOR_CELL;
x = newX;
y = newY;
}
}
for (const auto& element : gamers[1].get_army()) {
int attempts = 0;
const int MAX_ATTEMPTS = 100;
while (attempts < MAX_ATTEMPTS) {
int x = rand() % (SIZE_FOR_ARRAY + 1);
int y = rand() % (SIZE_FOR_ARRAY + 1);
if (field[y][x] == EMPTY_CELL && // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>
field[y][x] != PLAYER_CELL && // <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
!isNearPlayer(x, y)) { // <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
field[y][x] = ENEMY_CELL;
break;
}
attempts++;
}
}
}
void Game::play() {
#ifndef DEBUG
print_field();
#else
return;
#endif // !DEBUG
}