From ee925cebc904a5747520e9a75c2664a1dc7fe9ff Mon Sep 17 00:00:00 2001 From: ParkSuMin Date: Mon, 7 Jul 2025 23:34:34 +0300 Subject: [PATCH] Highlight selected unit Debug information about selected unit --- include/Game.h | 2 +- include/Infantry.h | 3 +- src/Game.cpp | 81 +++++++++++++++++++++++++++++++++------------- src/Infantry.cpp | 6 ++-- 4 files changed, 64 insertions(+), 28 deletions(-) diff --git a/include/Game.h b/include/Game.h index 47a4504..2ba1823 100644 --- a/include/Game.h +++ b/include/Game.h @@ -29,5 +29,5 @@ public: private: void Random(char); void clearScreen(); - void print_field(); + void print_field(int, int); }; \ No newline at end of file diff --git a/include/Infantry.h b/include/Infantry.h index f48601f..4e782c0 100644 --- a/include/Infantry.h +++ b/include/Infantry.h @@ -1,11 +1,10 @@ #pragma once - class Infantry { private: int x; int y; public: Infantry(int _x, int _y) : x(_x), y(_y) {}; - void get_coordinates(); + std::pair get_coordinates(); void attack(); }; \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index aeb041e..52df6e5 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -2,10 +2,9 @@ void Game::Random(char symbol) { auto isNearPlayer = [this](int x, int y) { - // Проверяем все 8 соседних клеток for (int dy = -1; dy <= 1; ++dy) { for (int dx = -1; dx <= 1; ++dx) { - if (dx == 0 && dy == 0) continue; // Пропускаем текущую клетку + if (dx == 0 && dy == 0) continue; int nx = (x + dx + 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 } -void Game::print_field() { +void Game::print_field(int cursorX = -1, int cursorY = -1) { clearScreen(); - for (const auto& row : field) { - for (char cell : row) { - std::cout << cell << ' '; + for (int i = 0; i <= SIZE_FOR_ARRAY; ++i) { + for (int j = 0; j <= SIZE_FOR_ARRAY; ++j) { + 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'; } @@ -142,25 +147,57 @@ void Game::init() { } void Game::play() { -#ifdef DEBUG - print_field(); + bool is_active = true; + bool unit_selected = false; - auto& vec_1 = gamers[0].get_army(); - auto& vec_2 = gamers[1].get_army(); + int current_unit_index = 0; + auto& player_army = gamers[0].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(); + int cursorX = -1, cursorY = -1; + if (!player_army.empty()) { + auto [x, y] = player_army[0]->get_coordinates(); + cursorX = x; + cursorY = y; } - 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(); + while (is_active) { + print_field(cursorX, cursorY); + 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 - - } \ No newline at end of file diff --git a/src/Infantry.cpp b/src/Infantry.cpp index fc62ab4..23b61a6 100644 --- a/src/Infantry.cpp +++ b/src/Infantry.cpp @@ -5,6 +5,6 @@ void Infantry::attack() return; } -void Infantry::get_coordinates() { - std::cout << x << " " << y << std::endl; -} +std::pair Infantry::get_coordinates() { + return { x, y }; +} \ No newline at end of file