Files
gambling-curses/main.c

168 lines
3.0 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 chart {
size_t length;
double *prices;
};
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
};
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,
};
2025-10-16 14:43:32 +02:00
struct button {
char *name;
size_t width;
size_t height;
struct button *next;
};
struct button *new_button(char *name, size_t width, size_t height)
{
struct button *button = malloc(sizeof(struct button));
button->name = name;
button->width = width;
button->height = height;
button->next = NULL;
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
};
2025-10-16 14:43:32 +02:00
void draw_box(size_t x, size_t y, size_t width, size_t height)
2025-10-15 14:15:01 +02:00
{
2025-10-16 14:43:32 +02:00
mvaddch(y, x, '+');
mvaddch(y + height - 1, x, '+');
mvaddch(y + height - 1, x + width - 1, '+');
mvaddch(y, x + width - 1, '+');
for (size_t i = 1; i < height - 1; i++) {
mvaddch(y + i, x, '|');
2025-10-15 14:15:01 +02:00
}
2025-10-16 14:43:32 +02:00
for (size_t i = 1; i < height - 1; i++) {
mvaddch(y + i, x + width - 1, '|');
}
for (size_t i = 1; i < width - 1; i++) {
mvaddch(y, x + i, '-');
}
for (size_t i = 1; i < width - 1; i++) {
mvaddch(y + height - 1, x + i, '-');
}
}
void draw_button(struct button *b, size_t x, size_t y)
{
draw_box(x, y, b->width, b->height);
mvaddstr(y + b->height / 2, x + b->width / 2 - strlen(b->name) / 2, b->name);
}
void draw_chart(struct chart *chart, size_t x, size_t y)
{
draw_box(x - 1, y - 1, (chart->length * 2) + 8, 23);
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), '?');
2025-10-15 14:15:01 +02:00
}
}
2025-10-16 14:43:32 +02:00
struct button *buy = new_button("buy", 20, 5);
struct button *sell = new_button("sell", 20, 5);
struct button *scam = new_button("scam", 20, 5);
attron(COLOR_PAIR(1));
draw_button(buy, 4, 28);
attroff(COLOR_PAIR(1));
attron(COLOR_PAIR(2));
draw_button(sell, 28, 28);
attroff(COLOR_PAIR(2));
attron(COLOR_PAIR(3));
draw_button(scam, 52, 28);
attroff(COLOR_PAIR(3));
}
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();
}
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();
2025-10-16 14:43:32 +02:00
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK);
init_pair(3, 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();
}