First attempts for moving player's unit
This commit is contained in:
102
src/Game.cpp
102
src/Game.cpp
@@ -47,8 +47,8 @@ void Game::print_field(int cursorX = -1, int cursorY = -1) {
|
||||
clearScreen();
|
||||
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
|
||||
// FOR FURTHER DEVELOPMENT: field[i][j] == PLAYER_CELL
|
||||
if (i == cursorY && j == cursorX) {
|
||||
std::cout << "\033[32m" << field[i][j] << "\033[0m ";
|
||||
}
|
||||
else {
|
||||
@@ -141,7 +141,12 @@ void Game::init() {
|
||||
}
|
||||
}
|
||||
|
||||
static bool check_lose_win(auto& player) {
|
||||
return player.empty();
|
||||
}
|
||||
|
||||
void Game::play() {
|
||||
bool is_selected = false;
|
||||
bool is_active = true;
|
||||
bool unit_selected = false;
|
||||
|
||||
@@ -155,44 +160,73 @@ void Game::play() {
|
||||
cursorY = y;
|
||||
}
|
||||
|
||||
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";
|
||||
if (is_active) {
|
||||
|
||||
int key = _getch();
|
||||
if (key == ESC_KEY) {
|
||||
std::cout << "Exit\n";
|
||||
is_active = false;
|
||||
break;
|
||||
if (check_lose_win(player_army)) {
|
||||
std::cout << "YOU LOSE";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
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;
|
||||
while (!is_selected) {
|
||||
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_selected = true;
|
||||
break;
|
||||
}
|
||||
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
|
||||
|
||||
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;
|
||||
#endif // DEBUG
|
||||
is_selected = true;
|
||||
}
|
||||
break;
|
||||
default: continue;
|
||||
}
|
||||
break;
|
||||
default: continue;
|
||||
}
|
||||
|
||||
bool is_moved = false;
|
||||
while (!is_moved) {
|
||||
print_field(cursorX, cursorY);
|
||||
std::cout << "Let's move!" << std::endl;
|
||||
int key = _getch();
|
||||
if (key == ESC_KEY) {
|
||||
std::cout << "Exit\n";
|
||||
break;
|
||||
}
|
||||
|
||||
switch (key) {
|
||||
case UP_KEY: cursorY = (cursorY == 0) ? SIZE_FOR_ARRAY : cursorY - 1; break;
|
||||
case DOWN_KEY: cursorY = (cursorY == SIZE_FOR_ARRAY) ? 0 : cursorY + 1; break;
|
||||
case LEFT_KEY: cursorX = (cursorX == 0) ? SIZE_FOR_ARRAY : cursorX - 1; break;
|
||||
}
|
||||
is_moved = true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user