Debug information about positions of units

Random position for flag and enemy (function Random)
This commit is contained in:
2025-07-07 17:42:31 +03:00
parent 253e1cf423
commit 7da1909c76
7 changed files with 110 additions and 52 deletions

View File

@@ -1,5 +1,41 @@
#include "Game.h"
void Game::Random(char symbol) {
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 || field[ny][nx] == ENEMY_CELL) {
return true;
}
}
}
return false;
};
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 &&
!isNearPlayer(x, y)) {
field[y][x] = symbol;
if (symbol == ENEMY_CELL)
gamers[1].set_army(x, y);
break;
}
attempts++;
}
}
void Game::clearScreen() {
#ifdef _WIN32
system("cls");
@@ -33,25 +69,7 @@ void Game::info() {
}
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()) {
for (int element = 0; element < ARMY; ++element) {
int x = 0, y = 0;
bool found = false;
@@ -89,7 +107,11 @@ void Game::init() {
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;
case ENTER:
found = false;
field[newY][newX] = PLAYER_CELL;
gamers[0].set_army(newX, newY);
break;
default: continue;
}
if (!found) break;
@@ -105,31 +127,40 @@ void Game::init() {
}
}
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++;
}
for (int element = 0; element < ARMY; ++element) {
Random(ENEMY_CELL);
}
for (int element = 0; element < 3; ++element) {
Random(FLAG_CELL);
}
#ifdef DEBUG
std::cout << "Please, look at debugger" << std::endl;
#endif // DEBUG
}
void Game::play() {
#ifndef DEBUG
#ifdef DEBUG
print_field();
auto& vec_1 = gamers[0].get_army();
auto& vec_2 = gamers[1].get_army();
std::cout << "Positions of player's units\n";
for (size_t i = 0; i < vec_1.size(); ++i) {
vec_1[i]->get_coordinates();
}
std::cout << std::endl;
std::cout << "Positions of enemy's units\n";
for (size_t i = 0; i < vec_2.size(); ++i) {
vec_2[i]->get_coordinates();
}
#else
return;
#endif // !DEBUG
#endif
}

View File

@@ -1,6 +1,10 @@
#include "Infantry.h"
#include "stdafx.h"
void Infantry::attack()
{
return;
}
void Infantry::get_coordinates() {
std::cout << x << " " << y << std::endl;
}