From d4d30ae1fa3f7a9d29f7e3da14171e4706e4743c Mon Sep 17 00:00:00 2001 From: ParkSuMin Date: Wed, 9 Jul 2025 17:27:51 +0300 Subject: [PATCH] Player's turn complete --- include/Game.h | 3 + include/Player.h | 3 + include/stdafx.h | 8 +++ src/Game.cpp | 157 ++++++++++++++++++++++++++--------------------- 4 files changed, 100 insertions(+), 71 deletions(-) diff --git a/include/Game.h b/include/Game.h index 2ba1823..8a4f05b 100644 --- a/include/Game.h +++ b/include/Game.h @@ -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); diff --git a/include/Player.h b/include/Player.h index db2d9d6..3f72497 100644 --- a/include/Player.h +++ b/include/Player.h @@ -11,6 +11,9 @@ private: public: Player() : score(0.) {}; + void points(double _score){ + score += _score; + } std::vector>& get_army() { return army; } diff --git a/include/stdafx.h b/include/stdafx.h index cc08e87..c2488a8 100644 --- a/include/stdafx.h +++ b/include/stdafx.h @@ -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, diff --git a/src/Game.cpp b/src/Game.cpp index 432a168..88fa4b9 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -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; } - - if (is_active) { + while (true) { + 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 { + } } -} \ No newline at end of file +}