diff --git a/.gitignore b/.gitignore index 9491a2f..4ad2bea 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ *.user *.userosscache *.sln.docstates +build/ # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs diff --git a/BattleCap.h b/BattleCap.h deleted file mode 100644 index c3d74ce..0000000 --- a/BattleCap.h +++ /dev/null @@ -1,8 +0,0 @@ -// BattleCap.h : включаемый файл для стандартных системных включаемых файлов -// или включаемые файлы для конкретного проекта. - -#pragma once - -#include - -// TODO: установите здесь ссылки на дополнительные заголовки, требующиеся для программы. diff --git a/CMakeLists.txt b/CMakeLists.txt index d9cd751..9f04b73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,15 @@ -# CMakeList.txt: проект CMake для BattleCap; включите исходный код и определения, +# CMakeList.txt: проект CMake для BattleCap; включите исходный код и определения, # укажите здесь логику для конкретного проекта. # cmake_minimum_required (VERSION 3.8) +if (WIN32) + add_compile_definitions(WINDOWS) +else() + add_compile_definitions(LINUX) +endif() +set(CMAKE_CXX_STANDARD 20) + # Включение горячей перезагрузки для компиляторов MSVC, если поддерживается. if (POLICY CMP0141) cmake_policy(SET CMP0141 NEW) @@ -11,8 +18,9 @@ endif() project ("BattleCap") +include_directories(include) # Добавьте источник в исполняемый файл этого проекта. -add_executable (BattleCap "BattleCap.cpp" "BattleCap.h") +add_executable (BattleCap "src/BattleCap.cpp" "include/BattleCap.h" "include/Soldier.h" "include/Game.h" "include/Infantry.h" "src/Game.cpp" "include/Player.h" "include/stdafx.h") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET BattleCap PROPERTY CXX_STANDARD 20) diff --git a/include/BattleCap.h b/include/BattleCap.h new file mode 100644 index 0000000..68a6e61 --- /dev/null +++ b/include/BattleCap.h @@ -0,0 +1,5 @@ +// или включаемые файлы для конкретного проекта. + +#pragma once + +// TODO: установите здесь ссылки на дополнительные заголовки, требующиеся для программы. diff --git a/include/Game.h b/include/Game.h new file mode 100644 index 0000000..c98d77b --- /dev/null +++ b/include/Game.h @@ -0,0 +1,29 @@ +#pragma once +#include "Player.h" +#include +#include + +constexpr auto SIZE = 10; +constexpr auto SIZE_FOR_ARRAY = SIZE - 1; + +class Game { +private: + char field[SIZE][SIZE]; + std::unique_ptr gamers; +public: + Game() : gamers(std::make_unique(2)) { + for (int i = 0; i < SIZE; ++i) { + for (int j = 0; j < SIZE; ++j) { + field[i][j] = '*'; + } + } + + }; + void info(); + void init(); + void play(); + +private: + void clearScreen(); + void print_field(); +}; \ No newline at end of file diff --git a/include/Infantry.h b/include/Infantry.h new file mode 100644 index 0000000..b903148 --- /dev/null +++ b/include/Infantry.h @@ -0,0 +1,14 @@ +#pragma once +#include "Soldier.h" +class Infantry : public Soldier { +public: + Infantry() : Soldier() {}; + int step() override + { + return 10; + } + void fire() override + { + return; + } +}; \ No newline at end of file diff --git a/include/Player.h b/include/Player.h new file mode 100644 index 0000000..bfc5539 --- /dev/null +++ b/include/Player.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include +#include "stdafx.h" + +class Player { +private: + char name; + std::vector> army; + double score; +public: + Player() { + score = 0.; + for (auto i = 0; i < 4; ++i) { + army.push_back(std::make_unique()); + } + } + std::vector>& get_army() { + return army; + } +}; \ No newline at end of file diff --git a/include/Soldier.h b/include/Soldier.h new file mode 100644 index 0000000..d216cf3 --- /dev/null +++ b/include/Soldier.h @@ -0,0 +1,14 @@ +#pragma once +class Soldier { +private: + double HP; + double attack; + double defence; + char type; +protected: + Soldier() : HP(100), attack(1), defence(1) {}; + Soldier(double _HP, double _attack, double _defence) : HP(_HP), attack(_attack), defence(_defence) {}; +public: + virtual int step() = 0; + virtual void fire() = 0; +}; \ No newline at end of file diff --git a/include/stdafx.h b/include/stdafx.h new file mode 100644 index 0000000..9748995 --- /dev/null +++ b/include/stdafx.h @@ -0,0 +1,31 @@ +#pragma once +#include +#include + +#ifdef WINDOWS +#include +#else +#include "curses.h" +#endif // WINDOWS + +#include "Soldier.h" +#include "Infantry.h" +#include "Game.h" +#include "Player.h" + +enum KEY_CODES +{ + ESC_KEY = 27, + UP_KEY = 119, + DOWN_KEY = 115, + LEFT_KEY = 97, + RIGHT_KEY = 100, + ENTER = 13 +}; + +enum CELL_CHARS { + EMPTY_CELL = '*', + CURSOR_CELL = '?', + ENEMY_CELL = 'E', + PLAYER_CELL = 'I' +}; \ No newline at end of file diff --git a/BattleCap.cpp b/src/BattleCap.cpp similarity index 61% rename from BattleCap.cpp rename to src/BattleCap.cpp index 04ec358..cb6b6e5 100644 --- a/BattleCap.cpp +++ b/src/BattleCap.cpp @@ -1,12 +1,13 @@ // BattleCap.cpp: определяет точку входа для приложения. // -#include "BattleCap.h" - -using namespace std; +#include "stdafx.h" int main() { - cout << "Hello CMake." << endl; + Game game; + game.init(); + game.play(); return 0; + } diff --git a/src/Game.cpp b/src/Game.cpp new file mode 100644 index 0000000..81af769 --- /dev/null +++ b/src/Game.cpp @@ -0,0 +1,135 @@ +#include "Game.h" + +void Game::clearScreen() { + #ifdef _WIN32 + system("cls"); + #else + system("clear"); + #endif +} + +void Game::print_field() { + clearScreen(); + for (const auto& row : field) { + for (char cell : row) { + std::cout << cell << ' '; + } + std::cout << '\n'; + } +} + +void Game::info() { + while (true) { + clearScreen(); + + std::cout << "Main author: ParkSuMin\n" + << "Email: tsettaro@paksurion.ru\n"; + int key = _getch(); + if (key == ESC_KEY) { // Escape + std::cout << "Exit" << std::endl; + break; + } + } +} + +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()) { + int x = 0, y = 0; + bool found = false; + + while (true) { + int start = 0; + int end = SIZE_FOR_ARRAY; + int i = rand() % (end - start + 1) + start; + int j = rand() % (end - start + 1) + start; + if (field[i][j] != PLAYER_CELL && field[i][j] == EMPTY_CELL) { + field[i][j] = CURSOR_CELL; + x = j; + y = i; + found = true; + break; + } + + } + + if (!found) return; + + 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) { + 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; + case RIGHT_KEY: newX = (x == SIZE_FOR_ARRAY) ? 0 : x + 1; break; + case ENTER: found = false; field[newY][newX] = PLAYER_CELL; break; + default: continue; + } + if (!found) break; + if (field[newY][newX] == PLAYER_CELL) { + newX = x; + newY = y; + continue; + } + field[y][x] = EMPTY_CELL; + field[newY][newX] = CURSOR_CELL; + x = newX; + y = newY; + } + } + + 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++; + } + + } +} + +void Game::play() { +#ifndef DEBUG + print_field(); +#else + return; +#endif // !DEBUG + +} \ No newline at end of file