diff --git a/CMakeLists.txt b/CMakeLists.txt index b313a08..f8c1a5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,8 +8,6 @@ if (WIN32) else() add_compile_definitions(LINUX) endif() -set(CMAKE_CXX_STANDARD 20) - # Включение горячей перезагрузки для компиляторов MSVC, если поддерживается. if (POLICY CMP0141) cmake_policy(SET CMP0141 NEW) @@ -19,7 +17,6 @@ endif() include_directories(include) project ("BattleCap") -include_directories(include) # Добавьте источник в исполняемый файл этого проекта. add_executable (BattleCap "src/BattleCap.cpp" @@ -30,4 +27,15 @@ if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET BattleCap PROPERTY CXX_STANDARD 20) endif() +target_compile_definitions(BattleCap + PRIVATE + _CRT_SECURE_NO_WARNINGS + GLFW_INCLUDE_NONE + PUBLIC + $<$:DEBUG> + $<$:OE_DEBUG> + $<$:RELEASE> + $<$:OE_RELEASE> +) + # TODO: Добавьте тесты и целевые объекты, если это необходимо. diff --git a/include/Game.h b/include/Game.h index c98d77b..47a4504 100644 --- a/include/Game.h +++ b/include/Game.h @@ -5,6 +5,7 @@ constexpr auto SIZE = 10; constexpr auto SIZE_FOR_ARRAY = SIZE - 1; +constexpr auto ARMY = 5; class Game { private: @@ -17,13 +18,16 @@ public: field[i][j] = '*'; } } - + gamers[0].set_name('P'); + gamers[1].set_name('E'); }; + void info(); void init(); void play(); private: + void Random(char); void clearScreen(); void print_field(); }; \ No newline at end of file diff --git a/include/Infantry.h b/include/Infantry.h index c5da856..f48601f 100644 --- a/include/Infantry.h +++ b/include/Infantry.h @@ -1,5 +1,11 @@ #pragma once -struct Infantry { +class Infantry { +private: + int x; + int y; +public: + Infantry(int _x, int _y) : x(_x), y(_y) {}; + void get_coordinates(); void attack(); }; \ No newline at end of file diff --git a/include/Player.h b/include/Player.h index 81dbc80..db2d9d6 100644 --- a/include/Player.h +++ b/include/Player.h @@ -9,13 +9,17 @@ private: std::vector> army; double score; public: - Player() { - score = 0.; - for (auto i = 0; i < 4; ++i) { - army.push_back(std::make_unique()); - } - } + Player() : score(0.) {}; + std::vector>& get_army() { return army; } + + void set_army(int x, int y) { + army.push_back(std::make_unique(x, y)); + } + + void set_name(char _name) { + name = _name; + } }; \ No newline at end of file diff --git a/include/stdafx.h b/include/stdafx.h index 598255c..cc08e87 100644 --- a/include/stdafx.h +++ b/include/stdafx.h @@ -26,5 +26,6 @@ enum CELL_CHARS { EMPTY_CELL = '*', CURSOR_CELL = '?', ENEMY_CELL = 'E', - PLAYER_CELL = 'I' + PLAYER_CELL = 'I', + FLAG_CELL = 'F' }; \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 81af769..aeb041e 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,5 +1,41 @@ #include "Game.h" +void Game::Random(char symbol) { + auto isNearPlayer = [this](int x, int y) { + // 8 + for (int dy = -1; dy <= 1; ++dy) { + for (int dx = -1; dx <= 1; ++dx) { + if (dx == 0 && dy == 0) continue; // + + int nx = (x + dx + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1); + int ny = (y + dy + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1); + + if (field[ny][nx] == PLAYER_CELL || field[ny][nx] == ENEMY_CELL) { + return true; + } + } + } + return false; + }; + + int attempts = 0; + const int MAX_ATTEMPTS = 100; + + while (attempts < MAX_ATTEMPTS) { + int x = rand() % (SIZE_FOR_ARRAY + 1); + int y = rand() % (SIZE_FOR_ARRAY + 1); + + if (field[y][x] == EMPTY_CELL && + !isNearPlayer(x, y)) { + field[y][x] = symbol; + if (symbol == ENEMY_CELL) + gamers[1].set_army(x, y); + break; + } + attempts++; + } +} + void Game::clearScreen() { #ifdef _WIN32 system("cls"); @@ -33,25 +69,7 @@ void Game::info() { } void Game::init() { - - auto isNearPlayer = [this](int x, int y) { - // 8 - for (int dy = -1; dy <= 1; ++dy) { - for (int dx = -1; dx <= 1; ++dx) { - if (dx == 0 && dy == 0) continue; // - - int nx = (x + dx + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1); - int ny = (y + dy + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1); - - if (field[ny][nx] == PLAYER_CELL) { - return true; - } - } - } - return false; - }; - - for (const auto& element : gamers[0].get_army()) { + for (int element = 0; element < ARMY; ++element) { int x = 0, y = 0; bool found = false; @@ -89,7 +107,11 @@ void Game::init() { case DOWN_KEY: newY = (y == SIZE_FOR_ARRAY) ? 0 : y + 1; break; case LEFT_KEY: newX = (x == 0) ? SIZE_FOR_ARRAY : x - 1; break; case RIGHT_KEY: newX = (x == SIZE_FOR_ARRAY) ? 0 : x + 1; break; - case ENTER: found = false; field[newY][newX] = PLAYER_CELL; break; + case ENTER: + found = false; + field[newY][newX] = PLAYER_CELL; + gamers[0].set_army(newX, newY); + break; default: continue; } if (!found) break; @@ -105,31 +127,40 @@ void Game::init() { } } - for (const auto& element : gamers[1].get_army()) { - int attempts = 0; - const int MAX_ATTEMPTS = 100; - - while (attempts < MAX_ATTEMPTS) { - int x = rand() % (SIZE_FOR_ARRAY + 1); - int y = rand() % (SIZE_FOR_ARRAY + 1); - - if (field[y][x] == EMPTY_CELL && // - field[y][x] != PLAYER_CELL && // (. ) - !isNearPlayer(x, y)) { // - field[y][x] = ENEMY_CELL; - break; - } - attempts++; - } - + for (int element = 0; element < ARMY; ++element) { + Random(ENEMY_CELL); } + + for (int element = 0; element < 3; ++element) { + Random(FLAG_CELL); + } + +#ifdef DEBUG + std::cout << "Please, look at debugger" << std::endl; +#endif // DEBUG + } void Game::play() { -#ifndef DEBUG +#ifdef DEBUG print_field(); + + auto& vec_1 = gamers[0].get_army(); + auto& vec_2 = gamers[1].get_army(); + + std::cout << "Positions of player's units\n"; + for (size_t i = 0; i < vec_1.size(); ++i) { + vec_1[i]->get_coordinates(); + } + std::cout << std::endl; + + std::cout << "Positions of enemy's units\n"; + for (size_t i = 0; i < vec_2.size(); ++i) { + vec_2[i]->get_coordinates(); + } #else return; -#endif // !DEBUG +#endif + } \ No newline at end of file diff --git a/src/Infantry.cpp b/src/Infantry.cpp index 9ccf98b..fc62ab4 100644 --- a/src/Infantry.cpp +++ b/src/Infantry.cpp @@ -1,6 +1,10 @@ -#include "Infantry.h" +#include "stdafx.h" void Infantry::attack() { return; } + +void Infantry::get_coordinates() { + std::cout << x << " " << y << std::endl; +}