Clear screen in the end.
[funnysort.git] / funnysort.c
blob6f24f76f2d1d4a638111f121cb118d98ac22de0f
1 /* funnysort.c */
3 /* remove the next line to enable asserts */
4 #define NDEBUG
6 #include <stdio.h>
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <pthread.h>
10 #include <unistd.h>
11 #include <stdbool.h>
12 #include <signal.h>
14 #include "congl.h"
16 #define WIDTH 50
17 #define HEIGHT 30
18 #define NCYCLES 1000000
19 #define SLEEP_TIME 0
20 #define SORTER_MOVE_TRIES 100
21 #define FREE_PER_ITEM 50
22 #define NSORTERS 5
24 typedef enum {A=0, EMPTY, SORTER} item_t;
26 struct board_s {
27 unsigned int width;
28 unsigned int height;
29 bool size_valid;
30 item_t **surface;
31 pthread_mutex_t **lock;
32 bool surface_valid;
34 typedef struct board_s board_t;
36 struct sorter_s {
37 unsigned int column;
38 unsigned int line;
40 typedef struct sorter_s sorter_t;
42 void count_item (board_t *, item_t *, unsigned int *);
43 void drop_item (unsigned int, unsigned int, board_t *, item_t *);
44 void erase_board (board_t *);
45 void fetch_item(unsigned int, unsigned int, item_t *);
46 void finish_board(board_t *);
47 void move_sorter (board_t *, sorter_t *);
48 void peek_item (unsigned int, unsigned int, board_t *, item_t *);
49 void pick_item (unsigned int, unsigned int, board_t *, item_t *);
50 void poke_item (unsigned int, unsigned int, board_t *, item_t);
51 void print_board (board_t *);
52 void print_item (unsigned int, unsigned int, item_t);
53 void *launch_sorter (void *);
54 void setup_board(board_t *);
56 /* Erase the output medium. */
57 void erase_board (board_t *board)
59 conglReset();
60 conglClearScreen();
61 conglMoveCursor(1, 1);
64 void move_sorter(board_t *board, sorter_t *sorter)
66 /* sorter current position was locked before calling this function */
68 assert(sorter != NULL);
69 assert(sorter->column < WIDTH);
70 assert(sorter->line < HEIGHT);
72 int h, v;
73 unsigned int newline, newcolumn;
74 unsigned int ncycles;
75 item_t item, sorter_item;
77 ncycles = 0;
78 do {
79 /* Select random displacement. */
80 h = random()%3 - 1;
81 v = random()%3 - 1;
83 newline = (sorter->line + (h+HEIGHT)) % HEIGHT;
84 newcolumn = (sorter->column + (v+WIDTH)) % WIDTH;
86 /* lock position to where the sorter may move */
87 pthread_mutex_lock((board->lock)[newline]+newcolumn);
88 peek_item(newline, newcolumn, board, &item);
89 if (item != EMPTY) {
90 /* unlock if position if not available */
91 pthread_mutex_unlock((board->lock)[newline]+newcolumn);
93 ++ncycles;
94 } while ((item != EMPTY) && (ncycles < SORTER_MOVE_TRIES));
96 /* If sorter failed to move, respawn in random position */
97 while (item != EMPTY) {
98 newline = random()%HEIGHT;
99 newcolumn = random()%WIDTH;
101 /* lock position to where the sorter may move */
102 pthread_mutex_lock((board->lock)[newline]+newcolumn);
103 peek_item(newline, newcolumn, board, &item);
104 if (item != EMPTY) {
105 /* unlock if position if not available */
106 pthread_mutex_unlock((board->lock)[newline]+newcolumn);
110 /* Update board. */
112 /* Don't reset the item if sorter just droped one. */
113 /* If an item was droped, it will be different from SORTER. */
114 peek_item(sorter->line, sorter->column, board, &sorter_item);
115 if (sorter_item == SORTER) {
116 poke_item (sorter->line, sorter->column, board, EMPTY);
118 poke_item(newline, newcolumn, board, SORTER);
119 sorter->line = newline;
120 sorter->column = newcolumn;
122 pthread_mutex_unlock((board->lock)[newline]+newcolumn);
124 assert(sorter->column < WIDTH);
125 assert(sorter->line < HEIGHT);
128 void *launch_sorter (void *board)
130 int itera;
131 item_t item;
132 unsigned int line;
133 unsigned int column;
134 sorter_t sorter;
136 /* Place sorter in some free position. */
137 do {
138 line = random()%HEIGHT;
139 column = random()%WIDTH;
140 peek_item(line, column, board, &item);
141 } while (item != EMPTY);
142 poke_item(line, column, board, SORTER);
143 sorter.line = line;
144 sorter.column = column;
146 for (itera = 0; itera <= NCYCLES; ++itera) {
147 //for (itera = 0; 0==0; ++itera) {
148 do {
149 move_sorter(board, &sorter);
150 pick_item(sorter.line, sorter.column, board, &item);
152 usleep(SLEEP_TIME);
153 } while (item == EMPTY);
155 /* because the sorter allways moves, if it drops the item it will not
156 * remain on the top of it */
157 while (item != EMPTY) {
158 line = sorter.line;
159 column = sorter.column;
161 // Because the sorter may drop an item, it's current position must
162 // be locked until after it moves.
163 pthread_mutex_lock((((board_t *)board)->lock)[line]+column);
164 drop_item(sorter.line, sorter.column, board, &item);
165 move_sorter(board, &sorter);
166 pthread_mutex_unlock((((board_t *)board)->lock)[line]+column);
168 usleep(SLEEP_TIME);
171 /* leave the premisses */
172 //for (r = 0; (r < 10); ++r) {
173 // move(board, sorter);
177 return NULL;
180 /* Try to pick an item. Don't pick other sorters :) */
181 void pick_item (unsigned int line, unsigned int column, board_t *board, item_t *item)
183 assert (board != NULL);
184 assert (board->size_valid);
185 assert (board->surface_valid);
186 assert (line < board->height);
187 assert (column < board->width);
188 assert (item != NULL);
189 assert (*item == EMPTY);
191 int newline, newcolumn;
192 item_t newitem;
193 int h, v; /* h: horizontal; v: vertical */
195 for (h = -1; (h <= 1) && (*item == EMPTY); ++h) {
196 for (v = -1; (v <= 1) && (*item == EMPTY); ++v) {
197 /* newline and newcolumn need to be allawys positive */
198 newline = (line+(h+HEIGHT))%HEIGHT;
199 newcolumn = (column+(v+WIDTH))%WIDTH;
201 pthread_mutex_lock((board->lock)[newline]+newcolumn);
202 peek_item(newline, newcolumn, board, &newitem);
203 if((newitem != EMPTY) && (newitem != SORTER)) {
204 *item = newitem;
205 poke_item(newline, newcolumn, board, EMPTY);
206 /* we have the item */
208 pthread_mutex_unlock((board->lock)[newline]+newcolumn);
212 /* we either pick an item or leave it as it is */
213 assert ((*item == newitem) || (*item == EMPTY));
214 assert (*item != SORTER);
217 /* Try to drop one item. The item needs to be droped in a position adjacent to
218 * a similar item.
220 void drop_item (unsigned int line, unsigned int column, board_t *board, item_t *item)
222 assert (board != NULL);
223 assert (board->size_valid);
224 assert (board->surface_valid);
225 assert (line < board->height);
226 assert (column < board->width);
227 assert (item != NULL);
228 assert (*item != EMPTY);
229 assert (*item != SORTER);
231 int newline, newcolumn;
232 int h, v; /* h: horizontal, v: vertical */
233 item_t newitem;
235 for (h = -1; (h <= 1) && (*item != EMPTY); ++h) {
236 for (v = -1; (v <= 1) && (*item != EMPTY); ++v) {
237 /* newline and newcolumn need to be allawys positive */
238 newline = (line+h+HEIGHT)%HEIGHT;
239 newcolumn = (column+v+WIDTH)%WIDTH;
241 pthread_mutex_lock((board->lock)[newline]+newcolumn);
242 peek_item(newline, newcolumn, board, &newitem);
243 if(newitem == *item) {
244 poke_item(line, column, board, newitem);
245 *item = EMPTY;
246 /* and the item is down */
248 pthread_mutex_unlock((board->lock)[newline]+newcolumn);
253 /* Print the base of the board, without any items. */
254 void print_board(board_t *board)
256 assert(board->size_valid);
257 assert(board->surface_valid);
259 conglClearScreen();
260 conglMoveCursor(1, 1);
263 void print_item(unsigned int line, unsigned int column, item_t item)
265 struct properties_s {
266 conglColor_t bg;
267 conglColor_t fg;
268 char symbol;
270 typedef struct properties_s properties_t;
272 properties_t properties[] = {
273 {congl_magenta, congl_white, ' '},
274 {congl_green, congl_white, ' '},
275 {congl_black, congl_white, ' '}
276 //{'i'}, {'f'}, {'s'}
279 properties_t p;
281 p = properties[item];
282 conglDrawCharFull(line+1, column+1, p.symbol, p.fg, p.bg);
284 //printf("%c: %d,%d\n", p.symbol, column, line);
287 /* Read an item from the board. */
288 void peek_item (unsigned int line, unsigned int column, board_t *board, item_t *item)
290 assert(board != NULL);
291 assert(board->size_valid);
292 assert(board->surface_valid);
293 assert(line < board->height);
294 assert(column < board->width);
295 assert(item != NULL);
297 *item = board->surface[line][column];
299 assert(*item == board->surface[line][column]);
302 /* Write an item in position (column,line) of the board. At the same time update
303 * any output medium.
305 void poke_item (unsigned int line, unsigned int column, board_t *board, item_t item)
307 assert(board != NULL);
308 assert(board->size_valid);
309 assert(board->surface_valid);
310 assert(line < board->height);
311 assert(column < board->width);
313 board->surface[line][column] = item;
315 print_item (line, column, item);
317 assert(board->surface[line][column] == item);
320 /* Fetch an item from somewhere. The purpose of this functin is to allow the
321 * user to load one pattern every time the program runs. For now all items are
322 * random (sort of).
324 void fetch_item(unsigned int line, unsigned int column, item_t *item)
326 assert(item != NULL);
328 int number;
330 /* generate one block in every FREE_PER_ITEM board cells */
331 number = random()%FREE_PER_ITEM;
332 switch (number) {
333 case 0:
334 *item = A;
335 break;
336 default:
337 *item = EMPTY;
338 break;
341 void setup_board(board_t *board)
343 assert (board->size_valid);
344 assert (!board->surface_valid);
346 unsigned int column, line;
347 item_t item;
349 //printf("Preparing board.\n");
351 /* allocate space for the board */
352 board->surface = (item_t **)calloc (board->height, sizeof(board->surface));
353 board->lock = (pthread_mutex_t **)calloc (board->height, sizeof(board->lock));
354 if (board->surface == NULL) {
355 perror("board_setup");
356 return;
358 if (board->lock == NULL) {
359 perror("board_setup");
360 return;
362 for (line = 0; line < board->height; ++line) {
363 board->surface[line] = (item_t *)calloc (board->width, sizeof(item_t));
364 board->lock[line] = (pthread_mutex_t *)calloc (board->width, sizeof(pthread_mutex_t));
365 if (board->surface[line] == NULL) {
366 perror("board_setup");
367 return;
369 if (board->lock[line] == NULL) {
370 perror("board_setup");
371 return;
374 board->surface_valid = true;
376 //printf("Printing board.\n");
377 print_board(board);
378 //printf("Board printed.\n");
380 /* fill the board with initial values */
381 for (line = 0; line < board->height; ++line) {
382 for (column = 0; column < board->width; ++column) {
383 fetch_item(line, column, &item);
384 poke_item(line, column, board, item);
385 pthread_mutex_init((board->lock)[line]+column, NULL);
389 assert (board->surface_valid);
392 void finish_board (board_t *board)
394 assert (board->surface_valid);
396 int line;
398 for (line = 0; line < board->height; ++line) {
399 free (board->surface[line]);
400 free (board->lock[line]);
402 free (board->surface);
403 free (board->lock);
404 board->surface_valid = false;
406 erase_board(board);
408 assert (!board->surface_valid);
411 void count_items(board_t *board, item_t *item, unsigned int *nitems)
413 assert (board != NULL);
414 assert (item != NULL);
415 assert (nitems != NULL);
417 unsigned int line, column;
418 item_t this_item;
419 unsigned int count;
421 count = 0;
422 for (line = 0; line < board->height; ++line) {
423 for (column = 0; column < board->width; ++column) {
424 peek_item(line, column, board, &this_item);
425 if (this_item == *item) count++;
429 *nitems = count;
432 int main (int argc, char *argv[])
434 board_t board;
435 unsigned int nitems_begin, nitems_finish;
436 pthread_attr_t thread_attributes;
437 pthread_t sorter[NSORTERS];
438 item_t item;
439 unsigned int n;
441 board.width = WIDTH;
442 board.height = HEIGHT;
443 board.size_valid = true;
444 board.surface_valid = false;
446 /* intialize random number generator */
447 srandom((unsigned int)time(NULL));
449 setup_board (&board);
450 sleep(1);
452 item = A;
453 count_items(&board, &item, &nitems_begin);
455 /* launch sorters */
456 pthread_attr_init(&thread_attributes);
457 for (n = 0; n < NSORTERS; ++n) {
458 pthread_create(sorter+n, &thread_attributes, launch_sorter, (void *)(&board));
461 /* wait for keypress (return) */
462 getchar();
463 printf("THE END");
465 count_items(&board, &item, &nitems_finish);
467 /* make shure the sorters didn't ate items (they can have one with
468 * themselves) */
469 assert((nitems_begin - nitems_finish) <= NSORTERS);
471 for (n = 0; n < NSORTERS; ++n) {
472 pthread_cancel(sorter[n]);
475 finish_board (&board);
477 printf("All good.\n");
479 exit(0);