diff --git a/Makefile b/Makefile index 60c81e6..780202d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC = g++ CC_FLAGS = -s -O3 `pkg-config --cflags --libs gtkmm-4.0` -Wall -Wextra #debug flags: -#CC_FLAGS = -ggdb -O3 `pkg-config --cflags --libs gtkmm-4.0` -Wall -Wextra +#CC_FLAGS = -ggdb `pkg-config --cflags --libs gtkmm-4.0` -Wall -Wextra all: build/bin/pupes-slots diff --git a/src/color.h b/src/color.h new file mode 100644 index 0000000..c5816d0 --- /dev/null +++ b/src/color.h @@ -0,0 +1,17 @@ +#define RESET "\033[0m" +#define BLACK "\033[30m" /* Black */ +#define RED "\033[31m" /* Red */ +#define GREEN "\033[32m" /* Green */ +#define YELLOW "\033[33m" /* Yellow */ +#define BLUE "\033[34m" /* Blue */ +#define MAGENTA "\033[35m" /* Magenta */ +#define CYAN "\033[36m" /* Cyan */ +#define WHITE "\033[37m" /* White */ +#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */ +#define BOLDRED "\033[1m\033[31m" /* Bold Red */ +#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ +#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ +#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ +#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ +#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ +#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ \ No newline at end of file diff --git a/src/main-window.cpp b/src/main-window.cpp index 1bbbcc3..c789b74 100644 --- a/src/main-window.cpp +++ b/src/main-window.cpp @@ -1,16 +1,17 @@ #include "main-window.h" -#include "glib.h" +#include "color.h" #include "glibmm/ustring.h" -#include "gtk/gtk.h" #include "gtkmm/image.h" #include "ultra-mega-functions.h" #include #include +#include #include #include #include #include #include +#include MainWindow::MainWindow() : m_button_spin("spin"), m_button_reset("reset money"), @@ -75,7 +76,7 @@ MainWindow::~MainWindow() {} void MainWindow::on_button_quit() { set_visible(false); } // define symbols for slots -Glib::ustring symbol[8]{ +const Glib::ustring symbol[8]{ IMG_FOLDER "/Arch.svg", IMG_FOLDER "/Debian.svg", IMG_FOLDER "/Slackware.svg", @@ -101,7 +102,7 @@ void MainWindow::randomise_slots(bool save, Glib::ustring *slot_status) { } } -bool MainWindow::refresh_slots(int timer_number, unsigned long long bet) { +bool MainWindow::slots_spining(int timer_number, unsigned long long bet) { std::cout << "This is timer " << timer_number; @@ -127,8 +128,80 @@ bool MainWindow::refresh_slots(int timer_number, unsigned long long bet) { { if ((slot_status[0].compare(slot_status[1])) == 0 && slot_status[0].compare(slot_status[2]) == 0) { + money += (bet * 2); + m_label_money.set_label("money: " + std::to_string(money)); + spawn_alert("Jackpot!", + "All symbols are the same your bet doubled.\nDon't stop!"); + goto end_win_check; + } else { + { + // vector goes brr + std::vector combo; + std::vector win_main( + (sizeof(slot_status) / sizeof(*slot_status)), false); + + for (uint8_t i = 0; i < 2; i++) { + if (i == 0) { + combo = {0, 1, 2}; + } else if (i == 1) { + combo = {3, 4, 5}; + } else { + std::cerr << RED + "Critical ERROR:\nindex out of bounds detected\n app " + "will terminate in few seconds\n" RESET; + spawn_alert("Critical ERROR", + "index out of bounds detected\n app " + "will terminate in few seconds"); + sleep(5); + exit(15); + } + + // WARNING you are about to enter "for loop hell" + // proceed with caution + + for (int j = 0; j < (sizeof(slot_status) / sizeof(*slot_status)); + j++) { + bool wining_ok = false; + + for (int k = 0; k < (combo.size()); k++) { + if ((slot_status[j].compare(symbol[combo[k]])) == 0) { + wining_ok = true; + } + } + if (wining_ok) { + win_main[j] = true; + } + } + for (int j = 0; j < (win_main.size()); j++) { + if (!win_main[j]) { + win_main.assign(win_main.size(), false); + break; + } + if (j == ((win_main.size()) - 1)) { + money += (bet * 1.5); + m_label_money.set_label("money: " + std::to_string(money)); + if (i == 0) { + spawn_alert( + "Distro combo!", + "your bet multiplied by factor of 1.5.\n Don't stop!"); + } else if (i == 1) { + spawn_alert( + "BSD combo!", + "your bet multiplied by factor of 1.5.\n Don't stop!"); + } + // "goto" mentioned + goto end_win_check; + } + } + } + } + // this code gets executed only if you lose, + // otherwise it jumps to goto label + spawn_alert("You Lose", "Your bet was lost. :(\nbut don't stop"); } } + // goto label!!! + end_win_check: can_spin = true; // return false, to stop timer @@ -161,19 +234,28 @@ void MainWindow::on_button_spin() { try { bet = std::stoull(m_entry_bet.get_text()); } catch (...) { - std::cerr << "input a number\n"; + spawn_alert("invalid bet", "your bet is either:\n● not a number\n● too " + "high for program to handle"); + return; + } + if (bet > money) { + spawn_alert("Not enough money!", + "You placed bet higher than you curent balance"); return; } can_spin = false; spins++; m_label_spins.set_label("spins: " + std::to_string(spins)); - count_value = get_random_num(30, 50); + money -= bet; + m_label_money.set_label("money: " + std::to_string(money)); + + count_value = get_random_num(10, 25); // Creation of a new object prevents long lines and shows us a little // how slots work. We have 0 parameters and bool as a return value // after calling sigc::bind. sigc::slot my_slot = sigc::bind( - sigc::mem_fun(*this, &MainWindow::refresh_slots), m_timer_number, bet); + sigc::mem_fun(*this, &MainWindow::slots_spining), m_timer_number, bet); // This is where we connect the slot to the Glib::signal_timeout() auto conn = Glib::signal_timeout().connect(my_slot, 300); @@ -187,6 +269,22 @@ void MainWindow::on_button_spin() { // Print some info to the console for the user: std::cout << "added timeout " << m_timer_number++ << std::endl; } else { - std::cerr << "you are already spining\n"; + spawn_alert("you are already spining!", ""); } } + +void MainWindow::spawn_alert(Glib::ustring message, Glib::ustring submessage) { + m_info_dialog = Gtk::AlertDialog::create(); + if (!m_info_dialog) + m_info_dialog = Gtk::AlertDialog::create(); + else { + // Reset values that may have been set by on_button_question_clicked(). + m_info_dialog->set_buttons({}); + m_info_dialog->set_default_button(-1); + m_info_dialog->set_cancel_button(-1); + } + + m_info_dialog->set_message(message); + m_info_dialog->set_detail(submessage); + m_info_dialog->show(*this); +} diff --git a/src/main-window.h b/src/main-window.h index 70b9cd2..4e5aea7 100644 --- a/src/main-window.h +++ b/src/main-window.h @@ -15,11 +15,12 @@ public: virtual ~MainWindow(); private: - // Signal handlers: + // Signal handlers and functions: void on_button_quit(); void on_button_spin(); - bool refresh_slots(int timer_number, unsigned long long bet); + bool slots_spining(int timer_number, unsigned long long bet); void randomise_slots(bool save, Glib::ustring *slot_status); + void spawn_alert(Glib::ustring message, Glib::ustring submessage); // Child widgets: Gtk::Grid m_grid_quit, m_grid_main; @@ -47,6 +48,9 @@ private: // Each timer counts back from COUNT_VALUE to 0 and is removed when it reaches // 0 std::map m_counters; + + // add alert dialog + Glib::RefPtr m_info_dialog; }; #endif /* GTKMM_EXAMPLEWINDOW_H */ \ No newline at end of file diff --git a/src/ultra-mega-functions.cpp b/src/ultra-mega-functions.cpp index 6e7cf64..0e27a79 100644 --- a/src/ultra-mega-functions.cpp +++ b/src/ultra-mega-functions.cpp @@ -1,4 +1,6 @@ #include "ultra-mega-functions.h" +#include "glibmm/ustring.h" +#include "main-window.h" #include int get_random_num(int min, int max) {