From 87ff65d0b43245fa2420f1ca056263b5e706a42f Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Thu, 2 Oct 2025 23:18:43 -0400 Subject: [PATCH] readd: win/lose conditions --- include/elements.hpp | 2 ++ src/elements.cpp | 20 ++++++++++++++++++++ src/main.cpp | 16 +++++++++------- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/elements.hpp b/include/elements.hpp index de3f2a2..ae33a12 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -13,6 +13,8 @@ Block** create_block_buffer(int starting_array_index, int starting_symbol, int r void rasterize_block(Point** buffer, Block* block, int starting_index); void rasterize_buffer(Point** buffer, Block** block_buffer, int offset); void trim_block_buffer_on_collision(Point** buffer, int buffer_size, Block** block_buffer, int &block_buffer_size, Ball &ball); +void cleanup(Block** block_buffer, int block_buffer_size); +bool is_block_buffer_empty(Block** block_buffer, int block_buffer_size); enum Direction { LEFT = -1, diff --git a/src/elements.cpp b/src/elements.cpp index 7ea151b..d7c1abe 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -34,6 +34,26 @@ Block** create_block_buffer(int starting_array_index, int starting_symbol, int r return block_buffer; } +bool is_block_buffer_empty(Block** block_buffer, int block_buffer_size) { + int total_clear_blocks = 0; + for(int i = 0; i < block_buffer_size; i++) { + if(block_buffer[i] == nullptr) { + total_clear_blocks++; + } + } + if(total_clear_blocks == block_buffer_size) { + return true; + } + return false; +} + +void cleanup(Block** block_buffer, int block_buffer_size) { + for(int i = 0; i < block_buffer_size; i++) { + delete block_buffer[i]; + } + delete[] block_buffer; +} + void rasterize_block(Point** buffer, Block* block, int starting_index) { for(int i = 0; i < block->_size; i++) { buffer[starting_index + i] = block->m_nodes[i]; diff --git a/src/main.cpp b/src/main.cpp index 49c5845..06e5c51 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,15 +27,17 @@ int main() { window.clear_display(); window.update_board(buffer, buffer_size); window.draw_display(FPMS); - // Create a clause that closes game on lives = 0 + if(paddle.m_hearts <= 0) { + running = false; + std::cout << "No Hearts left! You lose!\n"; + } + if(is_block_buffer_empty(block_buffer, block_buffer_size)) { + running = false; + std::cout << "No Blocks left! You win!\n"; + } std::cout << "Hearts left: " << paddle.m_hearts << "\n"; } input_thread.join(); - // TODO - // move below to a cleanup function instead - for(int i = 0; i < block_buffer_size; i++) { - delete block_buffer[i]; - } - delete[] block_buffer; + cleanup(block_buffer, block_buffer_size); return 0; }