Player's turn complete

This commit is contained in:
2025-07-09 17:27:51 +03:00
parent 5577f14f5b
commit d4d30ae1fa
4 changed files with 100 additions and 71 deletions

View File

@@ -6,6 +6,7 @@
constexpr auto SIZE = 10;
constexpr auto SIZE_FOR_ARRAY = SIZE - 1;
constexpr auto ARMY = 5;
constexpr auto FLAGS = 3;
class Game {
private:
@@ -27,6 +28,8 @@ public:
void play();
private:
bool handleUnitSelection(const auto&, size_t&, int&, int&);
bool handleUnitMovement(auto, int&, int&);
void Random(char);
void clearScreen();
void print_field(int, int);

View File

@@ -11,6 +11,9 @@ private:
public:
Player() : score(0.) {};
void points(double _score){
score += _score;
}
std::vector<std::unique_ptr<Infantry>>& get_army() {
return army;
}

View File

@@ -12,6 +12,14 @@
#include "Game.h"
#include "Player.h"
// TODO
/*enum class State {
SELECT_UNIT,
MOVE_UNIT,
ENEMY_TURN
};
*/
enum KEY_CODES
{
ESC_KEY = 27,

View File

@@ -1,5 +1,9 @@
#include "Game.h"
static bool check_lose_win(auto& player) {
return player.empty();
}
void Game::Random(char symbol) {
auto isNearPlayer = [this](int x, int y) {
for (int dy = -1; dy <= 1; ++dy) {
@@ -44,7 +48,6 @@ void Game::clearScreen() {
}
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) {
// FOR FURTHER DEVELOPMENT: field[i][j] == PLAYER_CELL
@@ -98,16 +101,10 @@ void Game::init() {
while (true) {
clearScreen();
print_field();
int key = _getch();
if (key == ESC_KEY) {
std::cout << "Exit\n";
exit(0);
}
int newX = x, newY = y;
switch (key) {
switch (_getch()) {
case ESC_KEY: std::cout << "Exit\n"; exit(0);
case UP_KEY: newY = (y == 0) ? SIZE_FOR_ARRAY : y - 1; break;
case DOWN_KEY: newY = (y == SIZE_FOR_ARRAY) ? 0 : y + 1; break;
case LEFT_KEY: newX = (x == 0) ? SIZE_FOR_ARRAY : x - 1; break;
@@ -136,16 +133,13 @@ void Game::init() {
Random(ENEMY_CELL);
}
for (int element = 0; element < 3; ++element) {
for (int element = 0; element < FLAGS; ++element) {
Random(FLAG_CELL);
}
}
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;
@@ -159,74 +153,95 @@ void Game::play() {
cursorX = x;
cursorY = y;
}
while (true) {
if (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_selected = true;
break;
if (check_lose_win(player_army)) {
std::cout << "YOU LOSE\n";
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;
}
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_selected = true;
}
break;
default: continue;
}
}
while (!is_selected) {
clearScreen();
print_field(cursorX, cursorY);
std::cout << "Your turn! Use 'a' or 's' to select a unit, ENTER to confirm, ESC to exit.\n";
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 (_getch()) {
case ESC_KEY: std::cout << "Exit\n"; exit(0);
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_selected = true;
}
break;
default: continue;
}
}
switch (key) {
bool is_moved = false;
while (!is_moved) {
clearScreen();
print_field(cursorX, cursorY);
int cp_x = cursorX;
int cp_y = cursorY;
std::cout << "Let's move!" << std::endl;
switch (_getch()) {
case ESC_KEY: std::cout << "Exit\n"; exit(0);
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;
case RIGHT_KEY: cursorX = (cursorX == SIZE_FOR_ARRAY) ? 0 : cursorX + 1; break;
}
is_moved = true;
switch (field[cursorY][cursorX])
{
case PLAYER_CELL:
is_moved = false;
cursorX = cp_x;
cursorY = cp_y;
break;
case ENEMY_CELL:
std::cout << "BANG! GOT IT! GOT YOUR POINTS\n";
gamers[0].points(50.);
std::swap(field[cursorY][cursorX], field[cp_y][cp_x]);
break;
case FLAG_CELL:
std::cout << "YUMMY!\n";
gamers[0].points(10.);
std::swap(field[cursorY][cursorX], field[cp_y][cp_x]);
break;
default:
std::swap(field[cursorY][cursorX], field[cp_y][cp_x]);
break;
}
}
is_moved = true;
}
else {
}
}
}