summaryrefslogtreecommitdiff
path: root/src/user/ttt/main.c
blob: 655bf8e096cb61d9ce5042ace4572051f5d8a909 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <pland/syscall.h>
#include <popups/info.h>
#include <knob/rand.h>

#define SPACE_SIZE 20
#define BORDER_SIZE 4
#define WIDTH (SPACE_SIZE * 3 + BORDER_SIZE * 4)

#define BG ((_pixel_t){.r = 0xd6, .g = 0xd6, .b = 0xd6})
#define FG ((_pixel_t){.r = 0x33, .g = 0x33, .b = 0x33})
#define XC ((_pixel_t){.r = 0x60, .g = 0x06, .b = 0x43})
#define OC ((_pixel_t){.r = 0x2c, .g = 0x77, .b = 0x30})
#define SL ((_pixel_t){.r = 0x9d, .g = 0x99, .b = 0xc2})

static const bool x_pic[] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,1,1,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

typedef enum {B, X, O} trit;

static const bool *pics[] = {b_pic, x_pic, o_pic};
static const _pixel_t colors[] = {FG, XC, OC};

static _window_handle_t w;
static _pixel_t pbuf[WIDTH * WIDTH];
static trit board_state[9];
static uint8_t cursor;
static trit playing_as;
static uint8_t filled_squares;

__attribute__ ((__pure__))
static bool is_win(uint8_t hint) {
  const uint8_t c = hint % 3;
  if ((board_state[c] == board_state[c + 3]) && (board_state[c] == board_state[c + 6]))
    return true;
  const uint8_t r = hint - c;
  if ((board_state[r] == board_state[r + 1]) && (board_state[r] == board_state[r + 2]))
    return true;
  return board_state[4] && (
    ((board_state[0] == board_state[4]) && (board_state[0] == board_state[8])) ||
    ((board_state[2] == board_state[4]) && (board_state[2] == board_state[6]))
  );
}

static void draw_rect(uint8_t y, uint8_t x, uint8_t h, uint8_t w, _pixel_t color) {
  for (uint8_t yy = y; yy < y + h; ++yy)
    for (uint8_t xx = x; xx < x + w; ++xx)
      pbuf[yy * WIDTH + xx] = color;
}

static void draw_spot(uint8_t n) {
  const _pixel_t bg_c = n == cursor ? SL : BG;
  const _pixel_t fg_c = colors[board_state[n]];
  const bool *const pic = pics[board_state[n]];

  const uint8_t ys = BORDER_SIZE + (n / 3) * (BORDER_SIZE + SPACE_SIZE);
  const uint8_t xs = BORDER_SIZE + (n % 3) * (BORDER_SIZE + SPACE_SIZE);
  for (uint8_t y = 0; y < SPACE_SIZE; ++y)
    for (uint8_t x = 0; x < SPACE_SIZE; ++x)
      pbuf[(ys + y) * WIDTH + xs + x] = pic[y * SPACE_SIZE + x] ? fg_c : bg_c;
}

static void ai_turn();

static void reset_board() {
  cursor = 4;
  playing_as = gen_rand() % 2 + 1;
  filled_squares = 0;
  for (uint8_t i = 0; i < 9; ++i) {
    board_state[i] = B;
    draw_spot(i);
  }

  if (gen_rand() % 2)
    ai_turn();
}

static uint32_t start_time;

static void on_win() {
  struct popup p;
  info_popupf(&p, "You win! Time: %us\nPress escape to play again.", FG, BG, _get_timestamp() - start_time);
  make_modal(&p);
  reset_board();
  _paint_window(w);
  start_time = _get_timestamp();
}

static void on_tie() {
  struct popup p;
  info_popupf(&p, "Tie! Time: %us\nPress escape to play again.", FG, BG, _get_timestamp() - start_time);
  make_modal(&p);
  reset_board();
  _paint_window(w);
  start_time = _get_timestamp();
}

static void ai_turn() {
  //super easy mode
  uint8_t n;
  do
    n = gen_rand() % 9;
  while (board_state[n]);
  board_state[n] = playing_as == O ? X : O;
  draw_spot(n);
  _paint_window(w);
  if (is_win(n)) {
    struct popup p;
    info_popupf(&p, "You lose. Time: %us\nPress escape to play again.", FG, BG, _get_timestamp() - start_time);
    make_modal(&p);
    reset_board();
    _paint_window(w);
    start_time = _get_timestamp();
  }
  else if (++filled_squares == 9)
    on_tie();
}

void main() {
  struct popup controls;
  info_popup(&controls,
    "Tic-Tac-Toe Controls:\n\n"
    "Arrow keys: move selection\n"
    "Spacebar:   confirm selection\n"
    "Escape:     quit\n\n"
    "Press escape to start.", FG, BG);
  make_modal(&controls);

  for (uint16_t i = 0; i < WIDTH * WIDTH; ++i)
    pbuf[i] = BG;

  draw_rect(SPACE_SIZE + BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, WIDTH - 2 * BORDER_SIZE, FG);
  draw_rect(BORDER_SIZE, SPACE_SIZE + BORDER_SIZE, WIDTH - 2 * BORDER_SIZE, BORDER_SIZE, FG);
  draw_rect(SPACE_SIZE * 2 + BORDER_SIZE * 2, BORDER_SIZE, BORDER_SIZE, WIDTH - 2 * BORDER_SIZE, FG);
  draw_rect(BORDER_SIZE, SPACE_SIZE * 2 + BORDER_SIZE * 2, WIDTH - 2 * BORDER_SIZE, BORDER_SIZE, FG);

  reset_board();

  w = _new_window(WIDTH, WIDTH, pbuf);
  start_time = _get_timestamp();

  while (1) {
    struct window_action wa;
    _get_win_action(w, &wa);
    if (!wa.action_type) {
      _wait_for_action();
      _yield_task();
      continue;
    }

    if (wa.action_type != KEY_DOWN)
      continue;

    switch (wa.as_key.key_id) {
    case KEY_UP_ARROW:
      if (cursor >= 3) {
        cursor -= 3;
        draw_spot(cursor + 3);
        draw_spot(cursor);
        _paint_window(w);
      }
      break;
    case KEY_DOWN_ARROW:
      if (cursor < 6) {
        cursor += 3;
        draw_spot(cursor - 3);
        draw_spot(cursor);
        _paint_window(w);
      }
      break;
    case KEY_LEFT_ARROW:
      if (cursor % 3) {
        --cursor;
        draw_spot(cursor + 1);
        draw_spot(cursor);
        _paint_window(w);
      }
      break;
    case KEY_RIGHT_ARROW:
      if (cursor % 3 != 2) {
        ++cursor;
        draw_spot(cursor - 1);
        draw_spot(cursor);
        _paint_window(w);
      }
      break;
    case KEY_ESCAPE:
      return;
    case KEY_SPACE:
      if (!board_state[cursor]) {
        board_state[cursor] = playing_as;
        draw_spot(cursor);
        _paint_window(w);
        if (is_win(cursor))
          on_win();
        else if (++filled_squares == 9)
          on_tie();
        else
          ai_turn();
      }
      break;
    default:
      break;
    }
  }
}