aboutsummaryrefslogtreecommitdiff
path: root/source/app.d
blob: 4006188034690ebb497f5156e234e06b10e1144c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import deimos.ncurses;
import std.string : toStringz;
import core.stdc.locale;
import std.format;
import board;
import spots;

void main()
{
    setlocale(LC_CTYPE, ""); // ncurses locales are weird

    auto stdscr = initscr();
    scope (exit)
        endwin();
    cbreak(); // disables line buffering for input
    noecho(); // input doesn't display on-screen
    intrflush(stdscr, true);
    keypad(stdscr, true); // allows arrow keys to work
    scrollok(stdscr, true); // allows scrolling in up/down arrows
    nonl(); // "return" goes through input

    int x;
    int y;
    getmaxyx(stdscr, y, x);

    auto overlay = new OverlaySource(new RandomSource());
    auto board = new Board(overlay);
    auto disp = new BoardDisplay(board, stdscr);
    disp.print();
    int rocks = 0;
    disp.status = "0";
    refresh();

    outer: while (true)
    {
        auto c = getch();
        switch (c)
        {
        case ',':
        case KEY_UP:
            disp.up();
            break;
        case 'o':
        case KEY_DOWN:
            disp.down();
            break;
        case 'e':
        case KEY_RIGHT:
            disp.right();
            break;
        case 'a':
        case KEY_LEFT:
            disp.left();
            break;
        case KEY_RESIZE:
            disp.getdims();
            break;
        case ' ':
            if (overlay[disp.y, disp.x].isRock())
            {
                overlay[disp.y, disp.x] = Spot(' ');
                rocks++;
                disp.status = format("%d", rocks);
                disp.print();
            }
            else if (rocks > 0)
            {
                overlay[disp.y, disp.x] = Spot('*');
                rocks--;
                disp.status = format("%d", rocks);
                disp.print();
            }
            break;
        case 'q':
            break outer;
        default:
            continue;
        }
    }
}