Логическая схема игры "Разрушитель блоков"

Added on: Mar 18, 2025
User Prompt

сделать логическую схему #include
#include
#include
#include
#include
#include
#include
#include

const int WIDTH = 20;
const int HEIGHT = 15;
const int MAX_BLOCKS = 50;
const std::string TOP_FILE = "top10.txt";

struct Block {
int x, y;
bool destroyed;
};

struct Player {
std::string name;
int score;
};

class Game {
private:
std::vector blocks;
int score;
bool gameOver;
int ballX, ballY;
int ballDirX, ballDirY;
int paddleX;

void initializeBlocks() {
blocks.clear();
for (int i = 0; i < MAX_BLOCKS; ++i) {
Block block;
block.x = rand() % (WIDTH - 2) + 1;
block.y = rand() % (HEIGHT / 2);
block.destroyed = false;
blocks.push_back(block);
}
}

void draw() {
system("clear");
for (int i = 0; i < WIDTH + 2; ++i) std::cout << "#";
std::cout << std::endl;

for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if (x == 0) std::cout << "#";
if (y == ballY && x == ballX) std::cout << "O";
else if (y == HEIGHT - 1 && x >= paddleX && x < paddleX + 3) std::cout << "=";
else {
bool blockDrawn = false;
for (const auto& block : blocks) {
if (block.x == x && block.y == y && !block.destroyed) {
std::cout << "X";
blockDrawn = true;
break;
}
}
if (!blockDrawn) {
std::cout << " ";
}
}
if (x == WIDTH - 1) std::cout << "#";
}
std::cout << std::endl;
}

for (int i = 0; i < WIDTH + 2; ++i) std::cout << "#";
std::cout << "\nScore: " << score << std::endl;
}

void update() {
ballX += ballDirX;
ballY += ballDirY;

if (ballX <= 0 || ballX >= WIDTH - 1) ballDirX *= -1;
if (ballY <= 0) ballDirY *= -1;

if (ballY == HEIGHT - 2 && ballX >= paddleX && ballX < paddleX + 3) {
ballDirY *= -1;
}

for (auto& block : blocks) {
if (!block.destroyed && ballX == block.x && ballY == block.y) {
block.destroyed = true;
score += 10;
ballDirY *= -1;
}
}

if (ballY >= HEIGHT) {
gameOver = true;
}

if (std::all_of(blocks.begin(), blocks.end(), [](const Block& block) { return block.destroyed; })) {
gameOver = true;
}
}

void saveTopScores(const std::string& playerName) {
std::vector players;

try {
std::ifstream fileIn(TOP_FILE);
if (fileIn.is_open()) {
Player player;
while (fileIn >> player.name >> player.score) {
players.push_back(player);
}
fileIn.close();
}

bool found = false;
for (auto& player : players) {
if (player.name == playerName) {
if (player.score < score) {
player.score = score;
}
found = true;
break;
}
}

if (!found && players.size() < 10) {
players.push_back({ playerName, score });
}

std::sort(players.begin(), players.end(), [](const Player& a, const Player& b) {
return a.score < b.score;
});
players.resize(std::min(players.size(), size_t(10)));
}
catch (const std::exception& e) {
std::cerr << "Ошибка при работе с файлом: "
<< e.what() << std::endl;
}

try {
std::ofstream fileOut(TOP_FILE);
for (const auto& player : players) {
fileOut << player.name << " " << player.score << std::endl;
}
}
catch (const std::exception& e) {
std::cerr << "Ошибка при записи в файл: " << e.what() << std::endl;
}
}

public:
Game() : score(0), gameOver(false), ballX(WIDTH / 2), ballY(HEIGHT / 2), ballDirX(1), ballDirY(-1), paddleX(WIDTH / 2 - 1) {
initializeBlocks();
}

void run() {
std::string playerName;

std::cout << "Введите ваше имя: ";
std::cin >> playerName;

while (!gameOver) {
draw();
if (std::cin.peek() != '\n') {
char ch = std::cin.get();
if (ch == 'a' && paddleX > 0) {
paddleX--;
}
if (ch == 'd' && paddleX < WIDTH - 3) {
paddleX++;
}
}
update();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

draw();
std::cout << "Игра окончена! Ваш счет: " << score << std::endl;
saveTopScores(playerName);
}
};

int main() {
srand(static_cast(time(nullptr)));
Game game;
game.run();
return 0;
}