cutting-edge features: added buttons

This commit is contained in:
2025-10-16 14:43:32 +02:00
parent 00275cef8f
commit 90fc37b6f4

151
main.c
View File

@@ -1,13 +1,37 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <math.h>
#include <time.h>
struct crypto_coin {
struct chart {
size_t length;
double *prices;
};
struct coin {
const char *name;
const char *description;
double prices[30];
struct chart chart;
};
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
};
}
enum game_state {
GAME_BEGIN,
GAME_IDLE,
@@ -16,47 +40,106 @@ enum game_state {
GAME_END,
};
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;
}
struct game {
enum game_state state;
int bank;
int time;
struct crypto_coin coin;
struct coin coin;
struct button *button;
};
void game_draw(struct game *g)
void draw_box(size_t x, size_t y, size_t width, size_t height)
{
mvaddstr(0, 0, g->coin.name);
mvaddstr(1, 0, "-----------------");
mvaddstr(2, 0, g->coin.description);
mvaddch(y, x, '+');
mvaddch(y + height - 1, x, '+');
mvaddch(y + height - 1, x + width - 1, '+');
mvaddch(y, x + width - 1, '+');
char buf[50];
mvaddstr(3, 0, "You have: ");
sprintf(buf, "%i dollars.", g->bank);
mvaddstr(3, 15, buf);
for (int i = 0; i < 20; i++) {
mvaddch(i+5, 0, '|');
for (size_t i = 1; i < height - 1; i++) {
mvaddch(y + i, x, '|');
}
for (int i = 0; i < g->time; i++) {
color_set(1, NULL);
mvaddch(20 - g->coin.prices[i]+5, i+1, '-');
for (int j = 20 - g->coin.prices[i] + 1; j < 20; j++) {
color_set(2, NULL);
mvaddch(j+5, i+1, ' ');
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), '?');
}
}
g->time++;
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);
refresh();
}
int main(void)
{
srand(time(NULL));
initscr();
start_color();
init_pair(1, COLOR_BLACK, COLOR_RED);
init_pair(2, COLOR_BLACK, COLOR_GREEN);
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
cbreak();
noecho();
nonl();
@@ -68,25 +151,17 @@ int main(void)
struct game game = {
.state = GAME_BEGIN,
.bank = 1000,
.time = 0,
.coin = (struct crypto_coin) {
.name = "BitCoin",
.coin = (struct coin) {
.name = "Bitcoin",
.description = "A fucking scam.",
.prices = {
10, 11, 12, 12, 12, 12, 12,
13, 13, 13, 11, 11, 10, 10,
9, 9, 10, 10, 11, 11, 11, 12,
}
.chart = new_chart(30)
}
};
for (int i = 0; i < 20; i++) {
game_draw(&game);
sleep(1);
}
draw_game(&game);
refresh();
sleep(10);
sleep(300);
endwin();
}