First attempts for moving player's unit
This commit is contained in:
104
src/Game.cpp
104
src/Game.cpp
@@ -47,8 +47,8 @@ void Game::print_field(int cursorX = -1, int cursorY = -1) {
|
|||||||
clearScreen();
|
clearScreen();
|
||||||
for (int i = 0; i <= SIZE_FOR_ARRAY; ++i) {
|
for (int i = 0; i <= SIZE_FOR_ARRAY; ++i) {
|
||||||
for (int j = 0; j <= SIZE_FOR_ARRAY; ++j) {
|
for (int j = 0; j <= SIZE_FOR_ARRAY; ++j) {
|
||||||
if (i == cursorY && j == cursorX && field[i][j] == PLAYER_CELL) {
|
// FOR FURTHER DEVELOPMENT: field[i][j] == PLAYER_CELL
|
||||||
// Highlight player's unit in green when selected
|
if (i == cursorY && j == cursorX) {
|
||||||
std::cout << "\033[32m" << field[i][j] << "\033[0m ";
|
std::cout << "\033[32m" << field[i][j] << "\033[0m ";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -141,7 +141,12 @@ void Game::init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool check_lose_win(auto& player) {
|
||||||
|
return player.empty();
|
||||||
|
}
|
||||||
|
|
||||||
void Game::play() {
|
void Game::play() {
|
||||||
|
bool is_selected = false;
|
||||||
bool is_active = true;
|
bool is_active = true;
|
||||||
bool unit_selected = false;
|
bool unit_selected = false;
|
||||||
|
|
||||||
@@ -154,45 +159,74 @@ void Game::play() {
|
|||||||
cursorX = x;
|
cursorX = x;
|
||||||
cursorY = y;
|
cursorY = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_active) {
|
||||||
|
|
||||||
while (is_active) {
|
if (check_lose_win(player_army)) {
|
||||||
print_field(cursorX, cursorY);
|
std::cout << "YOU LOSE";
|
||||||
std::cout << "Your turn! Use 'a' or 's' to select a unit, ENTER to confirm, ESC to exit.\n";
|
exit(0);
|
||||||
|
|
||||||
int key = _getch();
|
|
||||||
if (key == ESC_KEY) {
|
|
||||||
std::cout << "Exit\n";
|
|
||||||
is_active = false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (key) {
|
while (!is_selected) {
|
||||||
case LEFT_KEY:
|
print_field(cursorX, cursorY);
|
||||||
if (!player_army.empty()) {
|
std::cout << "Your turn! Use 'a' or 's' to select a unit, ENTER to confirm, ESC to exit.\n";
|
||||||
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();
|
int key = _getch();
|
||||||
cursorX = x;
|
if (key == ESC_KEY) {
|
||||||
cursorY = y;
|
std::cout << "Exit\n";
|
||||||
|
is_selected = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case RIGHT_KEY:
|
switch (key) {
|
||||||
if (!player_army.empty()) {
|
case LEFT_KEY:
|
||||||
current_unit_index = (current_unit_index == player_army.size() - 1) ? 0 : current_unit_index + 1;
|
if (!player_army.empty()) {
|
||||||
auto [x, y] = player_army[current_unit_index]->get_coordinates();
|
current_unit_index = (current_unit_index == 0) ? player_army.size() - 1 : current_unit_index - 1;
|
||||||
cursorX = x;
|
auto [x, y] = player_army[current_unit_index]->get_coordinates();
|
||||||
cursorY = y;
|
cursorX = x;
|
||||||
}
|
cursorY = y;
|
||||||
break;
|
}
|
||||||
case ENTER:
|
break;
|
||||||
if (!player_army.empty() && field[cursorY][cursorX] == PLAYER_CELL) {
|
case RIGHT_KEY:
|
||||||
unit_selected = true;
|
if (!player_army.empty()) {
|
||||||
#ifdef DEBUG
|
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";
|
std::cout << "Unit selected at (" << cursorX << ", " << cursorY << ")!\n";
|
||||||
#endif // DEBUG
|
#endif // DEBUG
|
||||||
is_active = false;
|
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