Highlight selected unit
Debug information about selected unit
This commit is contained in:
@@ -29,5 +29,5 @@ public:
|
||||
private:
|
||||
void Random(char);
|
||||
void clearScreen();
|
||||
void print_field();
|
||||
void print_field(int, int);
|
||||
};
|
||||
@@ -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<int,int> get_coordinates();
|
||||
void attack();
|
||||
};
|
||||
81
src/Game.cpp
81
src/Game.cpp
@@ -2,10 +2,9 @@
|
||||
|
||||
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>
|
||||
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
|
||||
|
||||
|
||||
}
|
||||
@@ -5,6 +5,6 @@ void Infantry::attack()
|
||||
return;
|
||||
}
|
||||
|
||||
void Infantry::get_coordinates() {
|
||||
std::cout << x << " " << y << std::endl;
|
||||
std::pair<int, int> Infantry::get_coordinates() {
|
||||
return { x, y };
|
||||
}
|
||||
Reference in New Issue
Block a user