Highlight selected unit

Debug information about selected unit
This commit is contained in:
2025-07-07 23:34:34 +03:00
parent 7da1909c76
commit ee925cebc9
4 changed files with 64 additions and 28 deletions

View File

@@ -29,5 +29,5 @@ public:
private: private:
void Random(char); void Random(char);
void clearScreen(); void clearScreen();
void print_field(); void print_field(int, int);
}; };

View File

@@ -1,11 +1,10 @@
#pragma once #pragma once
class Infantry { class Infantry {
private: private:
int x; int x;
int y; int y;
public: public:
Infantry(int _x, int _y) : x(_x), y(_y) {}; Infantry(int _x, int _y) : x(_x), y(_y) {};
void get_coordinates(); std::pair<int,int> get_coordinates();
void attack(); void attack();
}; };

View File

@@ -2,10 +2,9 @@
void Game::Random(char symbol) { void Game::Random(char symbol) {
auto isNearPlayer = [this](int x, int y) { 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 dy = -1; dy <= 1; ++dy) {
for (int dx = -1; dx <= 1; ++dx) { 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> if (dx == 0 && dy == 0) continue;
int nx = (x + dx + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1); int nx = (x + dx + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1);
int ny = (y + dy + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1); int ny = (y + dy + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1);
@@ -44,11 +43,17 @@ void Game::clearScreen() {
#endif #endif
} }
void Game::print_field() { void Game::print_field(int cursorX = -1, int cursorY = -1) {
clearScreen(); clearScreen();
for (const auto& row : field) { for (int i = 0; i <= SIZE_FOR_ARRAY; ++i) {
for (char cell : row) { for (int j = 0; j <= SIZE_FOR_ARRAY; ++j) {
std::cout << cell << ' '; if (i == cursorY && j == cursorX && field[i][j] == PLAYER_CELL) {
// Highlight player's unit in green when selected
std::cout << "\033[32m" << field[i][j] << "\033[0m ";
}
else {
std::cout << field[i][j] << ' ';
}
} }
std::cout << '\n'; std::cout << '\n';
} }
@@ -142,25 +147,57 @@ void Game::init() {
} }
void Game::play() { void Game::play() {
#ifdef DEBUG bool is_active = true;
print_field(); bool unit_selected = false;
auto& vec_1 = gamers[0].get_army(); int current_unit_index = 0;
auto& vec_2 = gamers[1].get_army(); auto& player_army = gamers[0].get_army();
std::cout << "Positions of player's units\n"; int cursorX = -1, cursorY = -1;
for (size_t i = 0; i < vec_1.size(); ++i) { if (!player_army.empty()) {
vec_1[i]->get_coordinates(); auto [x, y] = player_army[0]->get_coordinates();
cursorX = x;
cursorY = y;
} }
std::cout << std::endl;
std::cout << "Positions of enemy's units\n"; while (is_active) {
for (size_t i = 0; i < vec_2.size(); ++i) { print_field(cursorX, cursorY);
vec_2[i]->get_coordinates(); std::cout << "Your turn! Use 'a' or 's' to select a unit, ENTER to confirm, ESC to exit.\n";
int key = _getch();
if (key == ESC_KEY) {
std::cout << "Exit\n";
is_active = false;
break;
}
switch (key) {
case LEFT_KEY:
if (!player_army.empty()) {
current_unit_index = (current_unit_index == 0) ? player_army.size() - 1 : current_unit_index - 1;
auto [x, y] = player_army[current_unit_index]->get_coordinates();
cursorX = x;
cursorY = y;
}
break;
case RIGHT_KEY:
if (!player_army.empty()) {
current_unit_index = (current_unit_index == player_army.size() - 1) ? 0 : current_unit_index + 1;
auto [x, y] = player_army[current_unit_index]->get_coordinates();
cursorX = x;
cursorY = y;
}
break;
case ENTER:
if (!player_army.empty() && field[cursorY][cursorX] == PLAYER_CELL) {
unit_selected = true;
#ifdef DEBUG
std::cout << "Unit selected at (" << cursorX << ", " << cursorY << ")!\n";
#endif // DEBUG
is_active = false;
}
break;
default: continue;
}
} }
#else
return;
#endif
} }

View File

@@ -5,6 +5,6 @@ void Infantry::attack()
return; return;
} }
void Infantry::get_coordinates() { std::pair<int, int> Infantry::get_coordinates() {
std::cout << x << " " << y << std::endl; return { x, y };
} }