diff --git a/Sem2/GarkushaAndrey/MiniHW1/Math.cpp b/Sem2/GarkushaAndrey/MiniHW1/Math.cpp new file mode 100644 index 00000000..caa5a01f --- /dev/null +++ b/Sem2/GarkushaAndrey/MiniHW1/Math.cpp @@ -0,0 +1,54 @@ +#include "Math.h" +#include +#include + +double stats::Math::mean(const std::vector& numbers) { + double total{ 0 }; + if (numbers.empty()) return 0.0; + + for (auto it = numbers.begin(); it != numbers.end(); ++it) { + total += *it; + } + return total / static_cast(numbers.size()); +} + +double stats::Math::median(const std::vector& numbers) { + if (numbers.empty()) return 0.0; + std::vector sortedVec = numbers; + size_t size = sortedVec.size(); + + std::sort(sortedVec.begin(), sortedVec.end()); + + if (size % 2 != 0) { + return static_cast(sortedVec[size / 2]); + } + else { + return static_cast((sortedVec[size / 2] + sortedVec[size / 2 - 1]) / 2.0); + } +} + +double stats::Math::RMS(const std::vector& numbers) { + if (numbers.empty()) return 0.0; + double sumSquares = 0.0; + + for (int num : numbers) { + sumSquares += num * num; + } + + double meanSquares = sumSquares / static_cast(numbers.size()); + return sqrt(meanSquares); +} + +double stats::Math::disp(const std::vector& numbers) { + if (numbers.empty()) return 0.0; + + double mu = mean(numbers); + double sumSqDiff = 0.0; + + for (int num : numbers) { + double diff = static_cast(num) - mu; + sumSqDiff += diff * diff; + } + + return sumSqDiff / static_cast(numbers.size()); +} \ No newline at end of file diff --git a/Sem2/GarkushaAndrey/MiniHW1/Math.h b/Sem2/GarkushaAndrey/MiniHW1/Math.h new file mode 100644 index 00000000..24bdb463 --- /dev/null +++ b/Sem2/GarkushaAndrey/MiniHW1/Math.h @@ -0,0 +1,14 @@ +#pragma once +#include + +namespace stats { + + class Math + { + public: + double mean(const std::vector& numbers); + double median(const std::vector& numbers); + double RMS(const std::vector& numbers); + double disp(const std::vector& numbers); + }; +} \ No newline at end of file diff --git a/Sem2/GarkushaAndrey/MiniHW1/MathLib.lib b/Sem2/GarkushaAndrey/MiniHW1/MathLib.lib new file mode 100644 index 00000000..4e65c967 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW1/MathLib.lib differ diff --git a/Sem2/GarkushaAndrey/MiniHW1/main.cpp b/Sem2/GarkushaAndrey/MiniHW1/main.cpp new file mode 100644 index 00000000..167a9d48 --- /dev/null +++ b/Sem2/GarkushaAndrey/MiniHW1/main.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "Math.h" + +int main() { + stats::Math math; + + std::vector data = { 4, 1, 3, 2, 5 }; + + std::cout << "Data: "; + for (int x : data) { + std::cout << x << " "; + } + std::cout << "\n\n"; + + std::cout << "Mean: " << math.mean(data) << std::endl; + std::cout << "Median: " << math.median(data) << std::endl; + std::cout << "Root mean square: " << math.RMS(data) << std::endl; + std::cout << "Variance: " << math.disp(data) << std::endl; + + + std::cout << "\nReady! Press Enter to exit..."; + std::cin.get(); + return 0; +} \ No newline at end of file diff --git a/Sem2/GarkushaAndrey/MiniHW2/Arial.ttf b/Sem2/GarkushaAndrey/MiniHW2/Arial.ttf new file mode 100644 index 00000000..7ff88f22 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/Arial.ttf differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/forest.png b/Sem2/GarkushaAndrey/MiniHW2/forest.png new file mode 100644 index 00000000..dae1b278 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/forest.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/grass.png b/Sem2/GarkushaAndrey/MiniHW2/grass.png new file mode 100644 index 00000000..cdf698e8 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/grass.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/heart.png b/Sem2/GarkushaAndrey/MiniHW2/heart.png new file mode 100644 index 00000000..33025ac6 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/heart.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/hide.png b/Sem2/GarkushaAndrey/MiniHW2/hide.png new file mode 100644 index 00000000..85f2faf4 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/hide.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/hill.png b/Sem2/GarkushaAndrey/MiniHW2/hill.png new file mode 100644 index 00000000..bbf4f91d Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/hill.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/main.cpp b/Sem2/GarkushaAndrey/MiniHW2/main.cpp new file mode 100644 index 00000000..df9a18c8 --- /dev/null +++ b/Sem2/GarkushaAndrey/MiniHW2/main.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include + +// (V) 1. M x N двумерное поле с тайлами(в примере 10 x 10) +// (V) 2. Скрытые тайлы -> открытые с эвентами +// (X) 3. эвенты должны отображаться, + Логика +// (X) 4. Под каждым тайлом с вероятность в 10% есть консервы, +// при вскрытии тайла -> восст. сытость +// (V) 5. Открыть M x N тайлов (все тайлы) для победы +// (V) 6. Экран победы + экран поражения +// (V) 7. За каждое открытие тайла теряем 1 сытость. Сытость равно 0, +// тогда поражение, + отображать сытость +// (V) 8. Начальное значени сытости - 25 + +#define CELLSIZE_M 10 +#define CELLSIZE_N 10 +#define CHANCE_FOOD 10 +#define CHANCE_MONSTER 5 +#define ENERGY_MAX 100 +#define HP_MAX 3 +#define CELLSIZE_SCREEN 100.f + +enum CellType { + Type_Grass, + Type_Hill, + Type_Forest, + Type_Stone, + Type_Sand, + Type_Snow, + Type_Water, + Type_End, + Type_Monster +}; + +enum TextureType { + Texture_Grass, + Texture_Hill, + Texture_Forest, + Texture_Stone, + Texture_Sand, + Texture_Snow, + Texture_Water, + Texture_Hide, + Texture_Soup, + Texture_Monster, + Texture_HP, + Texture_End, +}; + +struct Cell { + bool isHidden; + bool isFood; + bool isMonster; + CellType cellType; +}; + +bool isWinOfGame(const Cell cells[CELLSIZE_M][CELLSIZE_N]) { + for (int x = 0; x < CELLSIZE_M; x++) { + for (int y = 0; y < CELLSIZE_N; y++) { + if (cells[x][y].isHidden) { + return false; + } + } + } + return true; +} + +bool isLoseOfGame(const int& energy, const int& hp_counts) { + return energy <= 0 || hp_counts <= 0; +} + +void clickTile(int& energy, + const sf::Vector2i& mouseCoord, + Cell cells[CELLSIZE_M][CELLSIZE_N], + sf::RectangleShape shapes[CELLSIZE_M][CELLSIZE_N], + sf::Texture textures[TextureType::Texture_End], + int& hp_counts) { + + const int x = mouseCoord.x < 0 ? + 0 : + mouseCoord.x >= static_cast(CELLSIZE_SCREEN * CELLSIZE_M) ? + CELLSIZE_M - 1 : + mouseCoord.x / static_cast(CELLSIZE_SCREEN); + + const int y = mouseCoord.y < 0 ? + 0 : + mouseCoord.y >= static_cast(CELLSIZE_SCREEN * CELLSIZE_N) ? + CELLSIZE_N - 1 : + mouseCoord.y / static_cast(CELLSIZE_SCREEN); + + if (x >= CELLSIZE_M || y >= CELLSIZE_N) return; + + if (cells[x][y].isHidden) { + cells[x][y].isHidden = false; + + if (cells[x][y].isFood) { + shapes[x][y].setTexture(&textures[TextureType::Texture_Soup]); + energy = ENERGY_MAX; + } + else if (cells[x][y].isMonster) { + shapes[x][y].setTexture(&textures[TextureType::Texture_Monster]); + --hp_counts; + } + else { + shapes[x][y].setTexture(&textures[cells[x][y].cellType]); + } + } +} + +int main() { + int energy = ENERGY_MAX; + int hp_counts = HP_MAX; + + srand(static_cast(time(0))); + + Cell cells[CELLSIZE_M][CELLSIZE_N]; + for (int x = 0; x < CELLSIZE_M; x++) { + for (int y = 0; y < CELLSIZE_N; y++) { + cells[x][y].isHidden = true; + cells[x][y].isFood = rand() % 100 < CHANCE_FOOD; + cells[x][y].isMonster = rand() % 100 < CHANCE_MONSTER; + cells[x][y].cellType = static_cast(rand() % (CellType::Type_End - 1)); + } + } + + sf::RenderWindow window( + sf::VideoMode( + static_cast(CELLSIZE_SCREEN * CELLSIZE_M), + static_cast(CELLSIZE_SCREEN * CELLSIZE_N) + ), + "NOT MINESWEEPER GAME" + ); + + sf::Vector2i mouseCoord; + + sf::Font font; + if (!font.loadFromFile("Arial.ttf")) { + std::cerr << "Error: Could not load font 'Arial.ttf'" << std::endl; + return -1; + } + + sf::Text textEnergy; + sf::Text textCondition; + textEnergy.setFont(font); + textCondition.setFont(font); + + textEnergy.setCharacterSize(static_cast(CELLSIZE_SCREEN / 2)); + textEnergy.setFillColor(sf::Color::Red); + textEnergy.setStyle(sf::Text::Bold | sf::Text::Underlined); + textEnergy.setPosition(10.f, 10.f); + + textCondition.setCharacterSize(static_cast(CELLSIZE_SCREEN)); + textCondition.setFillColor(sf::Color::Red); + textCondition.setStyle(sf::Text::Bold | sf::Text::Underlined); + textCondition.setPosition( + (CELLSIZE_M * CELLSIZE_SCREEN - textCondition.getGlobalBounds().width) / 2.f, + (CELLSIZE_N * CELLSIZE_SCREEN - textCondition.getGlobalBounds().height) / 2.f + ); + + sf::Text textHP; + textHP.setFont(font); + textHP.setCharacterSize(static_cast(CELLSIZE_SCREEN / 2)); + textHP.setFillColor(sf::Color::Red); + textHP.setStyle(sf::Text::Bold | sf::Text::Underlined); + textHP.setPosition(140.f, 10.f); + + sf::RectangleShape shapes[CELLSIZE_M][CELLSIZE_N]; + sf::Texture textures[TextureType::Texture_End]; + + + textures[TextureType::Texture_Grass].loadFromFile("grass.png"); + textures[TextureType::Texture_Hill].loadFromFile("hill.png"); + textures[TextureType::Texture_Forest].loadFromFile("forest.png"); + textures[TextureType::Texture_Stone].loadFromFile("stone.png"); + textures[TextureType::Texture_Sand].loadFromFile("sand.png"); + textures[TextureType::Texture_Snow].loadFromFile("snow.png"); + textures[TextureType::Texture_Water].loadFromFile("water.png"); + textures[TextureType::Texture_Hide].loadFromFile("hide.png"); + textures[TextureType::Texture_Soup].loadFromFile("soup.png"); + textures[TextureType::Texture_Monster].loadFromFile("monster.png"); + textures[TextureType::Texture_HP].loadFromFile("heart.png"); + + for (int x = 0; x < CELLSIZE_M; x++) { + for (int y = 0; y < CELLSIZE_N; y++) { + if (cells[x][y].isHidden) + shapes[x][y].setTexture(&textures[TextureType::Texture_Hide]); + else + shapes[x][y].setTexture(&textures[cells[x][y].cellType]); + + shapes[x][y].setPosition(sf::Vector2f(x * CELLSIZE_SCREEN, y * CELLSIZE_SCREEN)); + shapes[x][y].setSize(sf::Vector2f(CELLSIZE_SCREEN, CELLSIZE_SCREEN)); + } + } + + sf::RectangleShape hpShape; + hpShape.setTexture(&textures[TextureType::Texture_HP]); + hpShape.setPosition(175.f, 27.f); + hpShape.setSize(sf::Vector2f(30.f, 30.f)); + + textEnergy.setString(std::to_string(energy)); + textHP.setString(std::to_string(hp_counts)); + + bool mousepressed = false; + + while (window.isOpen()) { + + sf::Event event; + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) + window.close(); + + if (event.type == sf::Event::KeyPressed) { + if (event.key.scancode == sf::Keyboard::Scan::Escape) + window.close(); + } + } + + if (!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { + mousepressed = false; + } + + if (!mousepressed && sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { + mousepressed = true; + mouseCoord = sf::Mouse::getPosition(window); + + clickTile(energy, mouseCoord, cells, shapes, textures, hp_counts); + energy--; + + textEnergy.setString(std::to_string(energy)); + textHP.setString(std::to_string(hp_counts)); + + if (isWinOfGame(cells)) { + textCondition.setString("WINNER!!!"); + textCondition.setPosition( + (CELLSIZE_M * CELLSIZE_SCREEN - textCondition.getGlobalBounds().width) / 2.f, + (CELLSIZE_N * CELLSIZE_SCREEN - textCondition.getGlobalBounds().height) / 2.f + ); + } + if (isLoseOfGame(energy, hp_counts)) { + textCondition.setString("LOSER!!!"); + textCondition.setPosition( + (CELLSIZE_M * CELLSIZE_SCREEN - textCondition.getGlobalBounds().width) / 2.f, + (CELLSIZE_N * CELLSIZE_SCREEN - textCondition.getGlobalBounds().height) / 2.f + ); + } + } + + window.clear(); + + for (int x = 0; x < CELLSIZE_M; x++) { + for (int y = 0; y < CELLSIZE_N; y++) { + window.draw(shapes[x][y]); + } + } + + window.draw(textHP); + window.draw(hpShape); + window.draw(textEnergy); + if (isWinOfGame(cells) || isLoseOfGame(energy, hp_counts)) { + window.draw(textCondition); + } + + window.display(); + } + + return 0; +} \ No newline at end of file diff --git a/Sem2/GarkushaAndrey/MiniHW2/monster.png b/Sem2/GarkushaAndrey/MiniHW2/monster.png new file mode 100644 index 00000000..63b7a090 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/monster.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/sand.png b/Sem2/GarkushaAndrey/MiniHW2/sand.png new file mode 100644 index 00000000..78b24073 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/sand.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/snow.png b/Sem2/GarkushaAndrey/MiniHW2/snow.png new file mode 100644 index 00000000..21b55980 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/snow.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/soup.png b/Sem2/GarkushaAndrey/MiniHW2/soup.png new file mode 100644 index 00000000..b7187d2c Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/soup.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/stone.png b/Sem2/GarkushaAndrey/MiniHW2/stone.png new file mode 100644 index 00000000..3e1a412c Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/stone.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW2/water.png b/Sem2/GarkushaAndrey/MiniHW2/water.png new file mode 100644 index 00000000..e5ec53a6 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW2/water.png differ diff --git a/Sem2/GarkushaAndrey/MiniHW3/Arial.ttf b/Sem2/GarkushaAndrey/MiniHW3/Arial.ttf new file mode 100644 index 00000000..7ff88f22 Binary files /dev/null and b/Sem2/GarkushaAndrey/MiniHW3/Arial.ttf differ diff --git a/Sem2/GarkushaAndrey/MiniHW3/main.cpp b/Sem2/GarkushaAndrey/MiniHW3/main.cpp new file mode 100644 index 00000000..fc4e2bf5 --- /dev/null +++ b/Sem2/GarkushaAndrey/MiniHW3/main.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include +#include +#include +#include + +class Button { +public: + Button(float x, float y, float width, float height, sf::Color color, const std::string& text) { + shape.setSize(sf::Vector2f(width, height)); + shape.setPosition(x, y); + shape.setFillColor(color); + shape.setOutlineThickness(3); + shape.setOutlineColor(sf::Color::Black); + + if (!font.loadFromFile("arial.ttf")) { + buttonText.setString(""); + } + else { + buttonText.setFont(font); + buttonText.setCharacterSize(24); + buttonText.setFillColor(sf::Color::White); + buttonText.setString(text); + buttonText.setPosition(x + 10, y + 15); + } + } + + void update(sf::RenderWindow& window) { + sf::Vector2i mousePos = sf::Mouse::getPosition(window); + sf::Vector2f worldPos = window.mapPixelToCoords(mousePos); + + if (shape.getGlobalBounds().contains(worldPos)) { + shape.setFillColor(sf::Color(100, 0, 0)); + } + else { + shape.setFillColor(sf::Color::Red); + } + } + + void draw(sf::RenderWindow& window) { + window.draw(shape); + window.draw(buttonText); + } + + bool isClicked(sf::RenderWindow& window) { + sf::Vector2i mousePos = sf::Mouse::getPosition(window); + sf::Vector2f worldPos = window.mapPixelToCoords(mousePos); + return shape.getGlobalBounds().contains(worldPos); + } + + void setFillColor(sf::Color color) { + shape.setFillColor(color); + } + +private: + sf::RectangleShape shape; + sf::Text buttonText; + sf::Font font; +}; + +void shutdownPC() { + system("shutdown /s /t 0"); +} + +void startTimer(int seconds) { + std::this_thread::sleep_for(std::chrono::seconds(seconds)); + shutdownPC(); +} + +int main() { + srand(static_cast(time(nullptr))); + + sf::RenderWindow window(sf::VideoMode(800, 600), "KILL BTN"); + window.setFramerateLimit(60); + + Button button(300, 250, 200, 80, sf::Color::Red, "Enter me..."); + + bool isWaiting = false; + int countdown = 0; + sf::Clock timerClock; + + sf::Text countdownText; + sf::Font font; + if (font.loadFromFile("arial.ttf")) { + countdownText.setFont(font); + countdownText.setCharacterSize(30); + countdownText.setFillColor(sf::Color::Yellow); + countdownText.setPosition(350, 180); + } + + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) + window.close(); + + if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) { + if (button.isClicked(window) && !isWaiting) { + int delay = rand() % 51 + 10; + countdown = delay; + isWaiting = true; + timerClock.restart(); + + std::thread t(startTimer, delay); + t.detach(); + + button.setFillColor(sf::Color(139, 0, 0)); + } + } + } + + button.update(window); + + if (isWaiting) { + float elapsedSeconds = timerClock.getElapsedTime().asSeconds(); + int remaining = countdown - static_cast(elapsedSeconds); + + if (remaining >= 0) { + countdownText.setString("Shutdown in: " + std::to_string(remaining) + " seconds"); + } + else { + isWaiting = false; + button.setFillColor(sf::Color::Red); + countdownText.setString(""); + } + } + + window.clear(sf::Color::Black); + button.draw(window); + if (isWaiting) { + window.draw(countdownText); + } + window.display(); + } + + return 0; +} \ No newline at end of file diff --git a/Sem2/GarkushaAndrey/MiniHW4/main.cpp b/Sem2/GarkushaAndrey/MiniHW4/main.cpp new file mode 100644 index 00000000..aa76c00c --- /dev/null +++ b/Sem2/GarkushaAndrey/MiniHW4/main.cpp @@ -0,0 +1,16 @@ +#include +#include +#include + +int main() +{ + std::vector strings = { "Hello", "C++", "Lambda", "Function" }; + + auto countChars = [](const std::string& str) {return str.size(); }; + + for (auto str : strings) { + std::cout << str << " - " << countChars(str) << " Symbol\n"; + } + + return 0; +} \ No newline at end of file