Files
gambling-curses/main.c

215 lines
3.7 KiB
C
Raw Normal View History

2025-10-15 14:15:01 +02:00
#include <stdio.h>
#include <unistd.h>
2025-10-16 14:43:32 +02:00
#include <stdlib.h>
#include <string.h>
2025-10-15 14:15:01 +02:00
#include <curses.h>
2025-10-16 14:43:32 +02:00
#include <math.h>
#include <time.h>
2025-10-15 14:15:01 +02:00
2025-10-16 14:43:32 +02:00
struct coin {
2025-10-15 14:15:01 +02:00
const char *name;
const char *description;
2025-10-16 14:43:32 +02:00
struct chart chart;
2025-10-15 14:15:01 +02:00
};
struct chart {
double *prices;
size_t length;
};
2025-10-16 14:43:32 +02:00
struct chart new_chart(size_t length)
{
double *prices = malloc(length * sizeof(double));
prices[0] = 20;
for (size_t i = 1; i < length; i++) {
prices[i] = prices[i - 1] + (rand() % 3 - 1.5);
}
return (struct chart) {
.length = length,
.prices = prices
};
}
2025-10-15 14:15:01 +02:00
enum game_state {
GAME_BEGIN,
GAME_IDLE,
GAME_BUY,
GAME_SELL,
GAME_END,
};
enum color {
color_red,
color_green,
color_blue,
2025-10-16 14:43:32 +02:00
};
struct button *new_button(char *name, enum color color, size_t width, size_t height)
2025-10-16 14:43:32 +02:00
{
struct button *button = malloc(sizeof(struct button));
button->name = name;
button->width = width;
button->height = height;
button->next = NULL;
button->color = color;
button->highlighted = false;
2025-10-16 14:43:32 +02:00
return button;
}
2025-10-15 14:15:01 +02:00
struct game {
enum game_state state;
int bank;
2025-10-16 14:43:32 +02:00
struct coin coin;
struct button *button;
2025-10-15 14:15:01 +02:00
};
void draw_button(struct button *b, size_t x, size_t y)
2025-10-15 14:15:01 +02:00
{
draw_box(x, y, b->width, b->height);
if (b->highlighted) {
attron(A_UNDERLINE);
2025-10-16 14:43:32 +02:00
}
attron(COLOR_PAIR(b->color));
mvaddstr(y + b->height / 2, x + b->width / 2 - strlen(b->name) / 2, b->name);
2025-10-16 14:43:32 +02:00
if (b->highlighted) {
attroff(A_UNDERLINE);
2025-10-16 14:43:32 +02:00
}
attroff(COLOR_PAIR(b->color));
2025-10-16 14:43:32 +02:00
}
void draw_chart(struct chart *chart, size_t x, size_t y)
{
const size_t height = 30;
const size_t width = chart->length * 2 + 1;
draw_box(x, y, width, height + 1);
2025-10-16 14:43:32 +02:00
for (size_t i = 0; i < chart->length; i++) {
for (size_t j = 0; j < chart->prices[i]; j++) {
mvaddch(30 - (y + j), x + (i * 2) + 1, '?');
2025-10-15 14:15:01 +02:00
}
}
const size_t button_width = (double)(width) / 3;
2025-10-16 14:43:32 +02:00
struct button *buy = new_button("buy", color_red, button_width, 5);
struct button *sell = new_button("sell", color_blue, button_width, 5);
struct button *scam = new_button("scam", color_green, button_width, 5);
2025-10-16 14:43:32 +02:00
buy->next = sell;
sell->next = scam;
scam->next = buy;
2025-10-16 14:43:32 +02:00
buy->highlighted = true;
draw_button(buy, x, y + height + 1);
draw_button(sell, x + button_width, y + height + 1);
draw_button(scam, x + button_width * 2, y + height + 1);
2025-10-16 14:43:32 +02:00
}
enum box_type {
BOX_TYPE_BUTTON,
BOX_TYPE_CHART,
BOX_TYPE_HORIZONTAL,
BOX_TYPE_VERTICAL,
};
struct box {
enum box_type type;
size_t width;
size_t height;
enum color color;
union {
/* chart */
struct {
double *prices;
size_t length;
};
/* button */
char *name;
/* horizontal/vertical box */
struct {
struct box *children;
size_t length;
};
}
};
2025-10-16 14:43:32 +02:00
void draw_game(struct game *game)
{
clear();
mvaddstr(0, 0, game->coin.name);
mvaddstr(1, 0, game->coin.description);
draw_chart(&game->coin.chart, 5, 5);
2025-10-15 14:15:01 +02:00
refresh();
}
struct box *new_chart_box()
struct box *new_container_box(enum box_type type)
{
struct box *box = malloc(sizeof(box));
box->width = 0;
box->height = 0;
box->type = type;
box->children = malloc(sizeof(box) * 100);
box->length = 0;
return box;
}
struct box *new_vertical_box(void)
{
return new_container_box(BOX_TYPE_VERTICAL);
}
struct box *new_horizontal_box(void)
{
return new_container_box(BOX_TYPE_HORIZONTAL);
}
struct box *new
2025-10-15 14:15:01 +02:00
int main(void)
{
2025-10-16 14:43:32 +02:00
srand(time(NULL));
2025-10-15 14:15:01 +02:00
initscr();
start_color();
init_pair(color_green, COLOR_GREEN, COLOR_BLACK);
init_pair(color_red, COLOR_RED, COLOR_BLACK);
init_pair(color_blue, COLOR_BLUE, COLOR_BLACK);
2025-10-15 14:15:01 +02:00
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
clear();
refresh();
struct game game = {
.state = GAME_BEGIN,
.bank = 1000,
2025-10-16 14:43:32 +02:00
.coin = (struct coin) {
.name = "Bitcoin",
2025-10-15 14:15:01 +02:00
.description = "A fucking scam.",
2025-10-16 14:43:32 +02:00
.chart = new_chart(30)
2025-10-15 14:15:01 +02:00
}
};
2025-10-16 14:43:32 +02:00
draw_game(&game);
2025-10-15 14:15:01 +02:00
refresh();
2025-10-16 14:43:32 +02:00
sleep(300);
2025-10-15 14:15:01 +02:00
endwin();
}