First attempts for moving player's unit
This commit is contained in:
44
src/Game.cpp
44
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,14 +160,21 @@ void Game::play() {
|
||||
cursorY = y;
|
||||
}
|
||||
|
||||
while (is_active) {
|
||||
if (is_active) {
|
||||
|
||||
if (check_lose_win(player_army)) {
|
||||
std::cout << "YOU LOSE";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
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_active = false;
|
||||
is_selected = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -189,10 +201,32 @@ void Game::play() {
|
||||
#ifdef DEBUG
|
||||
std::cout << "Unit selected at (" << cursorX << ", " << cursorY << ")!\n";
|
||||
#endif // DEBUG
|
||||
is_active = false;
|
||||
is_selected = true;
|
||||
}
|
||||
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