Working product with sustainable legacies
This commit is contained in:
21
README.md
Normal file
21
README.md
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
Geachte speler,
|
||||||
|
|
||||||
|
Heb je wel eens rijk willen zijn?
|
||||||
|
|
||||||
|
Vast wel. Mensen met goede bedoelingen (zoals ik) hebben jouw daarom
|
||||||
|
meermaals geappt op Telegram om je te vragen of je bij een bedrijf
|
||||||
|
XYZ wilt werken. De werktijden zijn slechts 30 min per week, en je
|
||||||
|
verdient 500 euro per dag!
|
||||||
|
|
||||||
|
Misschien twijfel je nog. Geen probleem! Voor kleine spelers zoals
|
||||||
|
jij hebben we een spel genaamd 'gambling-curses'. In dit spel zie je
|
||||||
|
een live grafiek van de laatste Bitcoin-prijs, en heb je 10.000
|
||||||
|
euro op je bankaccount. Je kunt gemakkelijk traden door op 's' en
|
||||||
|
'b' te klikken. ('s' betekent SELL, 'b' betekent BUY).
|
||||||
|
|
||||||
|
Aangezien de cryptomarkt zeker NIET gamblen is, en er wel degelijk
|
||||||
|
een strategie achter zit, kun je zo gaan oefenen.
|
||||||
|
|
||||||
|
Met vriendelijke groet,
|
||||||
|
|
||||||
|
Crypto/Dropshipping Guru
|
||||||
61
box.c
61
box.c
@@ -17,13 +17,14 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
|
||||||
static void draw_rectangle(double x, double y, double width, double height);
|
static void draw_rectangle(unsigned int x, unsigned int y, unsigned int width,
|
||||||
static void draw_chart(struct box *, double x, double y, double width,
|
unsigned int height);
|
||||||
double height);
|
static void draw_chart(struct box *, unsigned int x, unsigned int y,
|
||||||
static void draw_container(struct box *, double x, double y, double width,
|
unsigned int width, unsigned int height);
|
||||||
double height);
|
static void draw_container(struct box *, unsigned int x, unsigned int y,
|
||||||
static void draw_button(struct box *, double x, double y, double width,
|
unsigned int width, unsigned int height);
|
||||||
double height);
|
static void draw_button(struct box *, unsigned int x, unsigned int y,
|
||||||
|
unsigned int width, unsigned int height);
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -34,44 +35,41 @@ append_box(struct box *parent, struct box *child)
|
|||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
draw_rectangle(double fx, double fy, double fwidth, double fheight)
|
draw_rectangle(unsigned int x, unsigned int y, unsigned int width,
|
||||||
|
unsigned int height)
|
||||||
{
|
{
|
||||||
size_t x = llround(fx);
|
|
||||||
size_t y = llround(fy);
|
|
||||||
size_t w = llround(fwidth) - 1;
|
|
||||||
size_t h = llround(fheight) - 1;
|
|
||||||
|
|
||||||
/* corners */
|
/* corners */
|
||||||
|
|
||||||
ui_char(x, y, '+');
|
ui_char(x, y, '+');
|
||||||
ui_char(x, y + h, '+');
|
ui_char(x, y + height, '+');
|
||||||
ui_char(x + w, y + h, '+');
|
ui_char(x + width, y + height, '+');
|
||||||
ui_char(x + w, y, '+');
|
ui_char(x + width, y, '+');
|
||||||
|
|
||||||
/* vertical borders */
|
/* vertical borders */
|
||||||
|
|
||||||
for (size_t i = 1; i < h; i++) {
|
for (size_t i = 1; i < height; i++) {
|
||||||
ui_char(x, y + i, '|');
|
ui_char(x, y + i, '|');
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 1; i < h; i++) {
|
for (size_t i = 1; i < height; i++) {
|
||||||
ui_char(x + w, y + i, '|');
|
ui_char(x + width, y + i, '|');
|
||||||
}
|
}
|
||||||
|
|
||||||
/* horizontal borders */
|
/* horizontal borders */
|
||||||
|
|
||||||
for (size_t i = 1; i < w; i++) {
|
for (size_t i = 1; i < width; i++) {
|
||||||
ui_char(x + i, y, '-');
|
ui_char(x + i, y, '-');
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 1; i < w; i++) {
|
for (size_t i = 1; i < width; i++) {
|
||||||
ui_char(x + i, y + h, '-');
|
ui_char(x + i, y + height, '-');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
draw_chart(struct box *chart, double x, double y, double width, double height)
|
draw_chart(struct box *chart, unsigned int x, unsigned int y,
|
||||||
|
unsigned int width, unsigned int height)
|
||||||
{
|
{
|
||||||
long int actual_diff;
|
long int actual_diff;
|
||||||
double diff, price;
|
double diff, price;
|
||||||
@@ -98,7 +96,8 @@ draw_chart(struct box *chart, double x, double y, double width, double height)
|
|||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
draw_button(struct box *b, double x, double y, double width, double height)
|
draw_button(struct box *b, unsigned int x, unsigned int y, unsigned int width,
|
||||||
|
unsigned int height)
|
||||||
{
|
{
|
||||||
ui_color_on(b->color);
|
ui_color_on(b->color);
|
||||||
draw_rectangle(x, y, width, height);
|
draw_rectangle(x, y, width, height);
|
||||||
@@ -108,10 +107,11 @@ draw_button(struct box *b, double x, double y, double width, double height)
|
|||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
draw_container(struct box *b, double x, double y, double width, double height)
|
draw_container(struct box *b, unsigned int x, unsigned int y,
|
||||||
|
unsigned int width, unsigned int height)
|
||||||
{
|
{
|
||||||
double current, length, total_fills, pixels_per_fill;
|
unsigned int current, i, length, total_fills, pixels_per_fill;
|
||||||
struct box *child;
|
struct box *child;
|
||||||
|
|
||||||
switch (b->type) {
|
switch (b->type) {
|
||||||
case BOX_VERTICAL:
|
case BOX_VERTICAL:
|
||||||
@@ -125,13 +125,13 @@ draw_container(struct box *b, double x, double y, double width, double height)
|
|||||||
}
|
}
|
||||||
|
|
||||||
total_fills = 0;
|
total_fills = 0;
|
||||||
for (size_t i = 0; i < b->length; i++) {
|
for (i = 0; i < b->length; i++) {
|
||||||
total_fills += b->children[i]->fills;
|
total_fills += b->children[i]->fills;
|
||||||
}
|
}
|
||||||
|
|
||||||
pixels_per_fill = length / total_fills;
|
pixels_per_fill = length / total_fills;
|
||||||
|
|
||||||
for (size_t i = 0; i < b->length; i++) {
|
for (i = 0; i < b->length; i++) {
|
||||||
child = b->children[i];
|
child = b->children[i];
|
||||||
length = child->fills * pixels_per_fill;
|
length = child->fills * pixels_per_fill;
|
||||||
|
|
||||||
@@ -206,7 +206,8 @@ new_vertical_box(void)
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
draw_box(struct box *b, double x, double y, double width, double height)
|
draw_box(struct box *b, unsigned int x, unsigned int y, unsigned int width,
|
||||||
|
unsigned int height)
|
||||||
{
|
{
|
||||||
switch (b->type) {
|
switch (b->type) {
|
||||||
case BOX_BUTTON:
|
case BOX_BUTTON:
|
||||||
|
|||||||
5
box.h
5
box.h
@@ -22,7 +22,7 @@ enum box_type {
|
|||||||
struct box {
|
struct box {
|
||||||
enum box_type type;
|
enum box_type type;
|
||||||
enum ui_color color;
|
enum ui_color color;
|
||||||
double fills;
|
unsigned int fills;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
/* button */
|
/* button */
|
||||||
@@ -47,4 +47,5 @@ struct box *new_chart_box(struct chart *);
|
|||||||
|
|
||||||
void append_box(struct box *parent, struct box *child);
|
void append_box(struct box *parent, struct box *child);
|
||||||
|
|
||||||
void draw_box(struct box *, double x, double y, double width, double height);
|
void draw_box(struct box *, unsigned int x, unsigned int y, unsigned int width,
|
||||||
|
unsigned int height);
|
||||||
|
|||||||
15
chart.c
15
chart.c
@@ -6,11 +6,14 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "chart.h"
|
#include "chart.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
|
||||||
struct chart new_chart(unsigned int length)
|
struct chart
|
||||||
|
new_chart(unsigned int length)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
double price;
|
double price;
|
||||||
@@ -25,8 +28,16 @@ struct chart new_chart(unsigned int length)
|
|||||||
|
|
||||||
for (i = 0; i < length; i++) {
|
for (i = 0; i < length; i++) {
|
||||||
chart.prices[i] = price;
|
chart.prices[i] = price;
|
||||||
price += (double)rand() / RAND_MAX - 0.4;
|
price += (double)rand() / RAND_MAX * 4 - 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return chart;
|
return chart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
update_chart(struct chart *chart)
|
||||||
|
{
|
||||||
|
memmove(chart->prices, chart->prices + 1, (chart->length - 1) * sizeof(double));
|
||||||
|
chart->prices[chart->length - 1] = chart->prices[chart->length - 2] + (double)rand() / RAND_MAX * 4 - 2;
|
||||||
|
}
|
||||||
|
|||||||
1
chart.h
1
chart.h
@@ -14,4 +14,5 @@ struct chart {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void update_chart(struct chart *);
|
||||||
struct chart new_chart(unsigned int length);
|
struct chart new_chart(unsigned int length);
|
||||||
|
|||||||
120
main.c
120
main.c
@@ -5,23 +5,36 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <curses.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <curses.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "box.h"
|
#include "box.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
|
||||||
int
|
enum game_state {
|
||||||
main(void)
|
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;
|
*title, *desc;
|
||||||
struct chart chart;
|
|
||||||
|
|
||||||
ui_init();
|
|
||||||
|
|
||||||
title = new_button_box("Bitcoin");
|
title = new_button_box("Bitcoin");
|
||||||
desc = new_button_box("A crypto-scam :-)");
|
desc = new_button_box("A crypto-scam :-)");
|
||||||
|
|
||||||
@@ -29,21 +42,25 @@ main(void)
|
|||||||
append_box(top, title);
|
append_box(top, title);
|
||||||
append_box(top, desc);
|
append_box(top, desc);
|
||||||
|
|
||||||
chart = new_chart(400);
|
left = new_chart_box(&game->chart);
|
||||||
left = new_chart_box(&chart);
|
|
||||||
left->fills = 5;
|
left->fills = 5;
|
||||||
|
|
||||||
buy = new_button_box("buy");
|
buy = new_button_box("buy");
|
||||||
buy->color = UI_GREEN;
|
buy->color = UI_GREEN;
|
||||||
sell = new_button_box("sell");
|
sell = new_button_box("sell");
|
||||||
sell->color = UI_RED;
|
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;
|
sell->color = UI_BLUE;
|
||||||
|
|
||||||
right = new_vertical_box();
|
right = new_vertical_box();
|
||||||
append_box(right, buy);
|
append_box(right, buy);
|
||||||
append_box(right, sell);
|
append_box(right, sell);
|
||||||
append_box(right, scam);
|
append_box(right, bank);
|
||||||
|
|
||||||
bottom = new_horizontal_box();
|
bottom = new_horizontal_box();
|
||||||
bottom->fills = 3;
|
bottom->fills = 3;
|
||||||
@@ -54,11 +71,82 @@ main(void)
|
|||||||
append_box(ui, top);
|
append_box(ui, top);
|
||||||
append_box(ui, bottom);
|
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();
|
ui_end();
|
||||||
}
|
}
|
||||||
|
|||||||
5
ui.c
5
ui.c
@@ -22,6 +22,7 @@ ui_init(void)
|
|||||||
init_pair(UI_BLUE, COLOR_BLUE, COLOR_BLACK);
|
init_pair(UI_BLUE, COLOR_BLUE, COLOR_BLACK);
|
||||||
cbreak();
|
cbreak();
|
||||||
noecho();
|
noecho();
|
||||||
|
nodelay(stdscr, TRUE);
|
||||||
nonl();
|
nonl();
|
||||||
intrflush(stdscr, FALSE);
|
intrflush(stdscr, FALSE);
|
||||||
keypad(stdscr, TRUE);
|
keypad(stdscr, TRUE);
|
||||||
@@ -34,12 +35,12 @@ void ui_end()
|
|||||||
endwin();
|
endwin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_string(size_t x, size_t y, char *s)
|
void ui_string(unsigned int x, unsigned int y, char *s)
|
||||||
{
|
{
|
||||||
mvaddstr(y, x, s);
|
mvaddstr(y, x, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_char(size_t x, size_t y, char c)
|
void ui_char(unsigned int x, unsigned int y, char c)
|
||||||
{
|
{
|
||||||
mvaddch(y, x, c);
|
mvaddch(y, x, c);
|
||||||
}
|
}
|
||||||
|
|||||||
4
ui.h
4
ui.h
@@ -22,8 +22,8 @@ void ui_end(void);
|
|||||||
void ui_color_on(enum ui_color);
|
void ui_color_on(enum ui_color);
|
||||||
void ui_color_off(enum ui_color);
|
void ui_color_off(enum ui_color);
|
||||||
|
|
||||||
void ui_char(size_t x, size_t y, char c);
|
void ui_char(unsigned int x, unsigned int y, char c);
|
||||||
void ui_string(size_t x, size_t y, char *s);
|
void ui_string(unsigned int x, unsigned int y, char *s);
|
||||||
|
|
||||||
size_t ui_width(void);
|
size_t ui_width(void);
|
||||||
size_t ui_height(void);
|
size_t ui_height(void);
|
||||||
|
|||||||
Reference in New Issue
Block a user