/* * Copyright (C) Artsiom D. * Copyright (C) shit-co.de */ #include #include #include #include #include #include "box.h" #include "ui.h" #include "util.h" 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, *bank, *title, *desc; title = new_button_box("Bitcoin"); desc = new_button_box("A crypto-scam :-)"); top = new_vertical_box(); append_box(top, title); append_box(top, desc); 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; 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, bank); bottom = new_horizontal_box(); bottom->fills = 3; append_box(bottom, left); append_box(bottom, right); ui = new_vertical_box(); append_box(ui, top); append_box(ui, bottom); return ui; } /* * 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(); }