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

@@ -8,8 +8,6 @@ if (WIN32)
else()
add_compile_definitions(LINUX)
endif()
set(CMAKE_CXX_STANDARD 20)
# Включение горячей перезагрузки для компиляторов MSVC, если поддерживается.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
@@ -19,7 +17,6 @@ endif()
include_directories(include)
project ("BattleCap")
include_directories(include)
# Добавьте источник в исполняемый файл этого проекта.
add_executable (BattleCap
"src/BattleCap.cpp"
@@ -30,4 +27,15 @@ if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET BattleCap PROPERTY CXX_STANDARD 20)
endif()
target_compile_definitions(BattleCap
PRIVATE
_CRT_SECURE_NO_WARNINGS
GLFW_INCLUDE_NONE
PUBLIC
$<$<CONFIG:Debug>:DEBUG>
$<$<CONFIG:RelWithDebInfo>:OE_DEBUG>
$<$<CONFIG:Release>:RELEASE>
$<$<CONFIG:MinSizeRel>:OE_RELEASE>
)
# TODO: Добавьте тесты и целевые объекты, если это необходимо.

View File

@@ -5,6 +5,7 @@
constexpr auto SIZE = 10;
constexpr auto SIZE_FOR_ARRAY = SIZE - 1;
constexpr auto ARMY = 5;
class Game {
private:
@@ -17,13 +18,16 @@ public:
field[i][j] = '*';
}
}
gamers[0].set_name('P');
gamers[1].set_name('E');
};
void info();
void init();
void play();
private:
void Random(char);
void clearScreen();
void print_field();
};

View File

@@ -1,5 +1,11 @@
#pragma once
struct Infantry {
class Infantry {
private:
int x;
int y;
public:
Infantry(int _x, int _y) : x(_x), y(_y) {};
void get_coordinates();
void attack();
};

View File

@@ -9,13 +9,17 @@ private:
std::vector<std::unique_ptr<Infantry>> army;
double score;
public:
Player() {
score = 0.;
for (auto i = 0; i < 4; ++i) {
army.push_back(std::make_unique<Infantry>());
}
}
Player() : score(0.) {};
std::vector<std::unique_ptr<Infantry>>& get_army() {
return army;
}
void set_army(int x, int y) {
army.push_back(std::make_unique<Infantry>(x, y));
}
void set_name(char _name) {
name = _name;
}
};

View File

@@ -26,5 +26,6 @@ enum CELL_CHARS {
EMPTY_CELL = '*',
CURSOR_CELL = '?',
ENEMY_CELL = 'E',
PLAYER_CELL = 'I'
PLAYER_CELL = 'I',
FLAG_CELL = 'F'
};

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;
}