From a392132fa98865288735c1d93292d1a5b2c8f123 Mon Sep 17 00:00:00 2001 From: Artsiom Dzenisiuk Date: Sat, 18 Oct 2025 20:44:30 +0200 Subject: [PATCH] Working product with sustainable legacies --- README.md | 21 ++++++++++ box.c | 61 +++++++++++++-------------- box.h | 5 ++- chart.c | 15 ++++++- chart.h | 1 + main.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++-------- ui.c | 5 ++- ui.h | 4 +- 8 files changed, 178 insertions(+), 54 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6256095 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +Geachte speler, + +Heb je wel eens rijk willen zijn? + +Vast wel. Mensen met goede bedoelingen (zoals ik) hebben jouw daarom +meermaals geappt op Telegram om je te vragen of je bij een bedrijf +XYZ wilt werken. De werktijden zijn slechts 30 min per week, en je +verdient 500 euro per dag! + +Misschien twijfel je nog. Geen probleem! Voor kleine spelers zoals +jij hebben we een spel genaamd 'gambling-curses'. In dit spel zie je +een live grafiek van de laatste Bitcoin-prijs, en heb je 10.000 +euro op je bankaccount. Je kunt gemakkelijk traden door op 's' en +'b' te klikken. ('s' betekent SELL, 'b' betekent BUY). + +Aangezien de cryptomarkt zeker NIET gamblen is, en er wel degelijk +een strategie achter zit, kun je zo gaan oefenen. + +Met vriendelijke groet, + +Crypto/Dropshipping Guru diff --git a/box.c b/box.c index e96d8ec..741a5b2 100644 --- a/box.c +++ b/box.c @@ -17,13 +17,14 @@ #include "util.h" -static void draw_rectangle(double x, double y, double width, double height); -static void draw_chart(struct box *, double x, double y, double width, - double height); -static void draw_container(struct box *, double x, double y, double width, - double height); -static void draw_button(struct box *, double x, double y, double width, - double height); +static void draw_rectangle(unsigned int x, unsigned int y, unsigned int width, + unsigned int height); +static void draw_chart(struct box *, unsigned int x, unsigned int y, + unsigned int width, unsigned int height); +static void draw_container(struct box *, unsigned int x, unsigned int y, + unsigned int width, unsigned int height); +static void draw_button(struct box *, unsigned int x, unsigned int y, + unsigned int width, unsigned int height); void @@ -34,44 +35,41 @@ append_box(struct box *parent, struct box *child) static void -draw_rectangle(double fx, double fy, double fwidth, double fheight) +draw_rectangle(unsigned int x, unsigned int y, unsigned int width, + unsigned int height) { - size_t x = llround(fx); - size_t y = llround(fy); - size_t w = llround(fwidth) - 1; - size_t h = llround(fheight) - 1; - /* corners */ ui_char(x, y, '+'); - ui_char(x, y + h, '+'); - ui_char(x + w, y + h, '+'); - ui_char(x + w, y, '+'); + ui_char(x, y + height, '+'); + ui_char(x + width, y + height, '+'); + ui_char(x + width, y, '+'); /* vertical borders */ - for (size_t i = 1; i < h; i++) { + for (size_t i = 1; i < height; i++) { ui_char(x, y + i, '|'); } - for (size_t i = 1; i < h; i++) { - ui_char(x + w, y + i, '|'); + for (size_t i = 1; i < height; i++) { + ui_char(x + width, y + i, '|'); } /* horizontal borders */ - for (size_t i = 1; i < w; i++) { + for (size_t i = 1; i < width; i++) { ui_char(x + i, y, '-'); } - for (size_t i = 1; i < w; i++) { - ui_char(x + i, y + h, '-'); + for (size_t i = 1; i < width; i++) { + ui_char(x + i, y + height, '-'); } } static void -draw_chart(struct box *chart, double x, double y, double width, double height) +draw_chart(struct box *chart, unsigned int x, unsigned int y, + unsigned int width, unsigned int height) { long int actual_diff; double diff, price; @@ -98,7 +96,8 @@ draw_chart(struct box *chart, double x, double y, double width, double height) static void -draw_button(struct box *b, double x, double y, double width, double height) +draw_button(struct box *b, unsigned int x, unsigned int y, unsigned int width, + unsigned int height) { ui_color_on(b->color); draw_rectangle(x, y, width, height); @@ -108,10 +107,11 @@ draw_button(struct box *b, double x, double y, double width, double height) static void -draw_container(struct box *b, double x, double y, double width, double height) +draw_container(struct box *b, unsigned int x, unsigned int y, + unsigned int width, unsigned int height) { - double current, length, total_fills, pixels_per_fill; - struct box *child; + unsigned int current, i, length, total_fills, pixels_per_fill; + struct box *child; switch (b->type) { case BOX_VERTICAL: @@ -125,13 +125,13 @@ draw_container(struct box *b, double x, double y, double width, double height) } total_fills = 0; - for (size_t i = 0; i < b->length; i++) { + for (i = 0; i < b->length; i++) { total_fills += b->children[i]->fills; } pixels_per_fill = length / total_fills; - for (size_t i = 0; i < b->length; i++) { + for (i = 0; i < b->length; i++) { child = b->children[i]; length = child->fills * pixels_per_fill; @@ -206,7 +206,8 @@ new_vertical_box(void) void -draw_box(struct box *b, double x, double y, double width, double height) +draw_box(struct box *b, unsigned int x, unsigned int y, unsigned int width, + unsigned int height) { switch (b->type) { case BOX_BUTTON: diff --git a/box.h b/box.h index ca49bee..5c29477 100644 --- a/box.h +++ b/box.h @@ -22,7 +22,7 @@ enum box_type { struct box { enum box_type type; enum ui_color color; - double fills; + unsigned int fills; union { /* button */ @@ -47,4 +47,5 @@ struct box *new_chart_box(struct chart *); void append_box(struct box *parent, struct box *child); -void draw_box(struct box *, double x, double y, double width, double height); +void draw_box(struct box *, unsigned int x, unsigned int y, unsigned int width, + unsigned int height); diff --git a/chart.c b/chart.c index e1c6da0..782639f 100644 --- a/chart.c +++ b/chart.c @@ -6,11 +6,14 @@ #include +#include + #include "chart.h" #include "util.h" -struct chart new_chart(unsigned int length) +struct chart +new_chart(unsigned int length) { unsigned int i; double price; @@ -25,8 +28,16 @@ struct chart new_chart(unsigned int length) for (i = 0; i < length; i++) { chart.prices[i] = price; - price += (double)rand() / RAND_MAX - 0.4; + price += (double)rand() / RAND_MAX * 4 - 2; } return chart; } + + +void +update_chart(struct chart *chart) +{ + memmove(chart->prices, chart->prices + 1, (chart->length - 1) * sizeof(double)); + chart->prices[chart->length - 1] = chart->prices[chart->length - 2] + (double)rand() / RAND_MAX * 4 - 2; +} diff --git a/chart.h b/chart.h index edc402c..057f139 100644 --- a/chart.h +++ b/chart.h @@ -14,4 +14,5 @@ struct chart { }; +void update_chart(struct chart *); struct chart new_chart(unsigned int length); diff --git a/main.c b/main.c index b1394c2..a953c92 100644 --- a/main.c +++ b/main.c @@ -5,23 +5,36 @@ */ +#include +#include #include #include -#include +#include #include "box.h" #include "ui.h" +#include "util.h" -int -main(void) +enum game_state { + GAME_IDLE, /* can buy AND sell */ + GAME_BOUGHT, /* cannot buy again */ + GAME_SOLD, /* cannot sell again */ +}; + +struct game { + unsigned int dollars; + struct chart chart; + enum game_state state; +}; + + +struct box * +initialize_ui_from_game(struct game *game) { - struct box *ui, *top, *bottom, *left, *right, *buy, *sell, *scam, + struct box *ui, *top, *bottom, *left, *right, *buy, *sell, *bank, *title, *desc; - struct chart chart; - - ui_init(); - + title = new_button_box("Bitcoin"); desc = new_button_box("A crypto-scam :-)"); @@ -29,21 +42,25 @@ main(void) append_box(top, title); append_box(top, desc); - chart = new_chart(400); - left = new_chart_box(&chart); + left = new_chart_box(&game->chart); left->fills = 5; buy = new_button_box("buy"); buy->color = UI_GREEN; sell = new_button_box("sell"); sell->color = UI_RED; - scam = new_button_box("scam"); + + char *bank_text = emalloc(500); + + sprintf(bank_text, "YOU HAVE $%d.", game->dollars); + + bank = new_button_box(bank_text); sell->color = UI_BLUE; right = new_vertical_box(); append_box(right, buy); append_box(right, sell); - append_box(right, scam); + append_box(right, bank); bottom = new_horizontal_box(); bottom->fills = 3; @@ -54,11 +71,82 @@ main(void) append_box(ui, top); append_box(ui, bottom); - draw_box(ui, 0, 0, ui_width(), ui_height()); + return ui; +} - ui_refresh(); - - sleep(300); + +/* + * Buy 50 coins, price = last price in chart. + */ +void game_buy(struct game *game) +{ + if (game->state != GAME_IDLE) { + return; + } + + game->dollars -= game->chart.prices[game->chart.length - 1] * 50; + game->state = GAME_BOUGHT; +} + + +/* + * Sell 50 coins, price = last price in chart. + */ +void game_sell(struct game *game) +{ + if (game->state != GAME_BOUGHT) { + return; + } + + game->dollars += game->chart.prices[game->chart.length - 1] * 50; + game->state = GAME_IDLE; +} + + +int +main(void) +{ + int framerate; + struct box *ui; + struct game game; + + framerate = 10; + + game.state = GAME_IDLE; + game.dollars = 10000; + game.chart = new_chart(200); + + ui_init(); + + ui = initialize_ui_from_game(&game); + + for (;;) { + draw_box(ui, 0, 0, ui_width(), ui_height()); + + ui_refresh(); + + clock_t current = clock(); + clock_t max = current + CLOCKS_PER_SEC * (1.0/framerate); + + for (;;) { + switch (getch()) { + case 'b': + game_buy(&game); + break; + case 's': + game_sell(&game); + break; + } + + if (clock() >= max) { + break; + } + } + + update_chart(&game.chart); + ui_clear(); + ui = initialize_ui_from_game(&game); + } ui_end(); } diff --git a/ui.c b/ui.c index bd556b2..a099639 100644 --- a/ui.c +++ b/ui.c @@ -22,6 +22,7 @@ ui_init(void) init_pair(UI_BLUE, COLOR_BLUE, COLOR_BLACK); cbreak(); noecho(); + nodelay(stdscr, TRUE); nonl(); intrflush(stdscr, FALSE); keypad(stdscr, TRUE); @@ -34,12 +35,12 @@ void ui_end() endwin(); } -void ui_string(size_t x, size_t y, char *s) +void ui_string(unsigned int x, unsigned int y, char *s) { mvaddstr(y, x, s); } -void ui_char(size_t x, size_t y, char c) +void ui_char(unsigned int x, unsigned int y, char c) { mvaddch(y, x, c); } diff --git a/ui.h b/ui.h index ee6ad87..c304fb8 100644 --- a/ui.h +++ b/ui.h @@ -22,8 +22,8 @@ void ui_end(void); void ui_color_on(enum ui_color); void ui_color_off(enum ui_color); -void ui_char(size_t x, size_t y, char c); -void ui_string(size_t x, size_t y, char *s); +void ui_char(unsigned int x, unsigned int y, char c); +void ui_string(unsigned int x, unsigned int y, char *s); size_t ui_width(void); size_t ui_height(void);