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
|
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <time.h>
#include "board.h"
int get_player_color(int player) {
return player ? COLOR_PAIR(player + 2) : COLOR_PAIR(0);
}
void draw_board(BoardUnit* board, int* meeple_map, 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
| get_player_color(get_structure_dominator(board[index].structure_group, meeple_map))
);
}
}
wrefresh(win);
}
int main() {
/* initialize curses */
initscr();
cbreak();
use_default_colors();
srand(time(NULL));
/* colors */
start_color();
for (int i = 1; i < 6; i++) init_pair(i, COLOR_BLACK + i, COLOR_BLACK);
/* 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);
mvwaddstr(board_box, 0, 1, "Board");
wrefresh(board_box);
/* create messages window */
WINDOW* messages_box = newwin(BOARD_ROW_UNITS + 2, 60, 0, BOARD_ROW_UNITS + 3);
WINDOW* messages_win = derwin(messages_box, BOARD_ROW_UNITS, 60 - 2, 1, 1);
box(messages_box, 0, 0);
mvwaddstr(messages_box, 0, 1, "Log");
wrefresh(messages_box);
int meeple_map[MAX_STRUCTURES * PLAYERS];
BoardUnit board[BOARD_UNITS];
initialize_board(board);
Tile tile = { "FRCR", 'R', 0 };
place_tile(tile, translate_coordinate(BOARD_WIDTH * BOARD_WIDTH / 2), board, 1);
Tile tileset[19] = {
/* cities */
{ "FFFF", 'F', 0 },
{ "CFFF", 'F', 0 },
{ "FCFC", 'C', 0 },
{ "FCFC", 'F', 0 },
{ "CCFF", 'C', 0 },
{ "CCFF", 'F', 0 },
{ "CCCF", 'C', 0 },
{ "CCCC", 'C', 0 },
/* roads */
{ "FFFR", 'R', 0 },
{ "FRFR", 'R', 0 },
{ "FFRR", 'R', 0 },
{ "FRRR", '+', 0 },
{ "RRRR", '+', 0 },
/* mix */
{ "CRFR", 'R', 0 },
{ "CFRR", 'R', 0 },
{ "CRRF", 'R', 0 },
{ "CRRR", '+', 0 },
{ "CCRR", '*', 0 },
{ "CCCR", 'C', 0 },
};
/* main loop */
char input_key;
for (int move = 0; ; move++) {
/* board */
refresh_structure_groups(board);
refresh_meeple_map(board, meeple_map);
draw_board(board, meeple_map, board_win);
/* tile placement */
tile = tileset[rand() % 19];
int position = 0;
BoardUnit board_preview[BOARD_UNITS];
while (1) {
for (int i = 0; i < BOARD_UNITS; i++) {
board_preview[i].feature = board[i].feature;
board_preview[i].structure_group = board[i].structure_group;
}
place_tile(tile, translate_coordinate(position), board_preview, 1);
int is_allowed = is_allowed_placement(tile, translate_coordinate(position), board);
if (is_allowed) wattron(board_win, COLOR_PAIR(2));
draw_board(board_preview, meeple_map, board_win);
wattroff(board_win, COLOR_PAIR(2));
input_key = wgetch(board_win);
if (input_key == 10 && is_allowed) break; /* enter key */
else if (input_key == 'l') position += 1;
else if (input_key == 'h') position -= 1;
else if (input_key == 'j') position += BOARD_WIDTH;
else if (input_key == 'k') position -= BOARD_WIDTH;
else if (input_key == 'r') rotate_tile(&tile, 3);
else if (input_key == 'q') {
endwin();
return 0;
}
}
int result = place_tile(tile, translate_coordinate(position), board, 0);
if (result) wprintw(messages_win, "Placed tile %s (%c) at position %i\n", tile.edges, tile.center, position);
else wprintw(messages_win, "Could not place tile %s (%c) at position %i\n", tile.edges, tile.center, position);
refresh_structure_groups(board);
refresh_meeple_map(board, meeple_map);
/* meeple placement */
input_key = wgetch(board_win);
int meeple_index = translate_coordinate(position);
if (input_key == 'l') meeple_index += 1;
else if (input_key == 'h') meeple_index -= 1;
else if (input_key == 'j') meeple_index += BOARD_ROW_UNITS;
else if (input_key == 'k') meeple_index -= BOARD_ROW_UNITS;
else if (input_key != 10) meeple_index = -1;
if (meeple_index >= 0) {
int meeple = (move % PLAYERS) + 1;
if (is_allowed_meeple(meeple, meeple_index, board, meeple_map)) {
board[meeple_index].meeple = meeple;
wprintw(messages_win, "Placed meeple #%i at index %i\n", meeple, meeple_index);
} else wprintw(messages_win, "Could not place meeple #%i at index %i\n", meeple, meeple_index);
}
wrefresh(messages_win);
}
endwin();
return 0;
}
|