aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 09ca804eab25c7288db78929ce853cff261ca637 (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
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
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include "board.h"

void draw_board(BoardUnit* board, WINDOW* win) {
  wmove(win, 0, 0);
  for (int row = 0; row < BOARD_ROW_UNITS; row++) {
    for (int i = 0; i < BOARD_ROW_UNITS; i++) {
      int index = BOARD_ROW_UNITS * row + i;
      if (board[index].feature == EMPTY && is_center_index(index)) waddch(win, '*');
      else waddch(win, board[index].feature);
    }
  }
}

void draw_structures(BoardUnit* board, WINDOW* win) {
  wmove(win, 0, 0);
  for (int row = 0; row < BOARD_ROW_UNITS; row++) {
    for (int i = 0; i < BOARD_ROW_UNITS; i++) {
      int index = BOARD_ROW_UNITS * row + i;
      if (board[index].feature == EMPTY && is_center_index(index)) waddch(win, '*');
      else waddch(win, board[index].structure_group == 0 ? EMPTY : '0' + board[index].structure_group);
    }
  }
}

void preview_tile(Tile tile, WINDOW* win) {
  mvwaddch(win, 1, 1, tile.center);
  mvwaddch(win, 0, 1, tile.edges[0]);
  mvwaddch(win, 1, 2, tile.edges[1]);
  mvwaddch(win, 2, 1, tile.edges[2]);
  mvwaddch(win, 1, 0, tile.edges[3]);
}

int main() {
  /* initialize curses */
  initscr();
  cbreak();

  /* create board window */
  WINDOW* board_box = newwin(BOARD_ROW_UNITS + 2, BOARD_ROW_UNITS + 2, 0, 0);
  WINDOW* board_win = derwin(board_box, BOARD_ROW_UNITS, BOARD_ROW_UNITS, 1, 1);
  box(board_box, 0, 0);
  wrefresh(board_box);

  /* create structures window */
  WINDOW* structures_box = newwin(BOARD_ROW_UNITS + 2, BOARD_ROW_UNITS + 2, 0, BOARD_ROW_UNITS + 3);
  WINDOW* structures_win = derwin(structures_box, BOARD_ROW_UNITS, BOARD_ROW_UNITS, 1, 1);
  box(structures_box, 0, 0);
  wrefresh(structures_box);

  /* create input window */
  WINDOW* input_box = newwin(3, 22, BOARD_ROW_UNITS + 3, 0);
  WINDOW* input_win = derwin(input_box, 1, 20, 1, 1);
  box(input_box, 0, 0);
  wrefresh(input_box);

  /* create tile inspector window */
  WINDOW* tile_box = newwin(5, 5, BOARD_ROW_UNITS + 3, 60);
  WINDOW* tile_win = derwin(tile_box, 3, 3, 1, 1);
  box(tile_box, 0, 0);
  wrefresh(tile_box);

  /* create messages window */
  WINDOW* messages_box = newwin(40, 60, BOARD_ROW_UNITS + 6, 0);
  WINDOW* messages_win = derwin(messages_box, 40 - 2, 60 - 2, 1, 1);
  box(messages_box, 0, 0);
  wrefresh(messages_box);


  BoardUnit board[BOARD_UNITS];
  initialize_board(board);

  Tile tileset[3] = {
    { "RRRR", 'R', 0 },
    { "FCCC", 'C', 0 },
    { "FFFC", 'C', 0 }
  };

  Tile tile = { "FRCR", 'R', 0 };
  place_tile(tile, translate_coordinate(24), board, 1);

  /* main loop */
  char position[3];
  char rotation_input;
  while (1) {
    /* prepare */
    refresh_structure_groups(board);
    wclear(input_win);

    /* draw onto the screen */
    draw_board(board, board_win);
    draw_structures(board, structures_win);
    wrefresh(board_win);
    wrefresh(structures_win);

    /* tile pickup */
    tile = tileset[rand() % 3];

    while (1) {
      preview_tile(tile, tile_win);
      wrefresh(tile_win);

      rotation_input = wgetch(tile_win);
      if (rotation_input == 10) break; /* enter key */
      else if (rotation_input == 'j') rotate_tile(&tile, 1);
      else if (rotation_input == 'k') rotate_tile(&tile, 3);
    }

    /* position prompt */
    echo();
    box(input_box, 0, 0);
    mvwaddstr(input_box, 0, 0, "Enter position:");
    wrefresh(input_box);
    wgetnstr(input_win, position, 3);
    noecho();

    if (position[0] == 'q') break;

    int result = place_tile(tile, translate_coordinate(atoi(position)), board, 0);
    if (result) wprintw(messages_win, "Placed tile %s (%c) at position %i\n", tile.edges, tile.center, atoi(position));
    else wprintw(messages_win, "Could not place tile %s (%c) at position %i\n", tile.edges, tile.center, atoi(position));
    wrefresh(messages_win);
  }


  endwin();
  return 0;
}