Working product with sustainable legacies
This commit is contained in:
120
main.c
120
main.c
@@ -5,23 +5,36 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <curses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <curses.h>
|
||||
#include <time.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user