#include #include #define DO_TEST_PLAY typedef unsigned char byte; typedef unsigned char bool; #define TRUE 1 #define FALSE 0 #define MAX_X 10 #define MAX_Y 10 #define BOARD_T(B) byte B[MAX_X][MAX_Y] #define BOARD_T_P(B) byte B[MAX_X][MAX_Y] BOARD_T(board); byte max_x; byte max_y; byte nr_colors; #define FOR_ALL_X(X) for (X = 0; X < max_x; X++) #define FOR_ALL_Y(Y) for (Y = 0; Y < max_y; Y++) #define FOR_ALL_XY(X,Y) FOR_ALL_X(X) FOR_ALL_Y(Y) void print_board() { int x,y; for (y = max_y-1; y >= 0; y--) { FOR_ALL_X(x) printf("%c", "_123456789"[board[x][y]]); printf("\n"); } printf("\n"); } void fill_board_str_value(BOARD_T_P(board), char *value) { int x, y; FOR_ALL_XY(x,y) board[x][y] = (*value++) - '0'; } bool board_eq_str_value(BOARD_T_P(board), char *value) { int x, y; FOR_ALL_XY(x,y) if (board[x][y] != (*value++) - '0') return FALSE; return TRUE; } void fill_board_value(BOARD_T_P(board), long value) { int x, y; FOR_ALL_XY(x,y) { board[x][y] = (value % nr_colors) + 1; value = value / nr_colors; } } void fill_board_random(BOARD_T_P(board), int seed) { int x, y; srand(seed); FOR_ALL_XY(x,y) board[x][y] = (rand() % nr_colors) + 1; } int debug_play = 0; void play(BOARD_T_P(board), int at_x, int at_y) { int x,y; BOARD_T(m); bool more; int size; int max_x_m1 = max_x - 1; int max_y_m1 = max_y - 1; FOR_ALL_XY(x,y) m[x][y] = 0; m[at_x][at_y] = 1; size = 1; do { more = FALSE; FOR_ALL_XY(x,y) if (m[x][y]) { int color = board[x][y]; #define TRY(C,X,Y) if ((C) && board[x X][y Y] == color && m[x X][y Y] == 0) \ { m[x X][y Y] = 1; size++; more = TRUE;} TRY(x > 0, -1,+0 ) TRY(y > 0, +0, -1) TRY(x < max_x_m1, +1, +0) TRY(y < max_y_m1, +0, +1) } } while (more); if (size > 1) { int nx = 0; FOR_ALL_X(x) { int ny = 0; FOR_ALL_Y(y) if (m[x][y] == 0) { board[nx][ny] = board[x][y]; ny++; } if (ny > 0 && board[nx][0] > 0) { for (; ny < max_y; ny++) board[nx][ny] = 0; nx++; } } for (; nx < max_x; nx++) FOR_ALL_Y(y) board[nx][y] = 0; } } #ifdef DO_TEST_PLAY void test_play(int gmax_x, int gmax_y, char *before, int px, int py, char *result) { max_x = gmax_x; max_y = gmax_y; nr_colors = 10; fill_board_str_value(board, before); play(board,px,py); if (!board_eq_str_value(board, result)) { printf("test play %d,%d %s play %d,%d to %s failed\n", gmax_x, gmax_y, before, px,py, result); fill_board_str_value(board, before); print_board(); play(board,px,py); print_board(); fill_board_str_value(board, result); print_board(); exit(0); } } void do_test_play() { char *before, *after1, *after2, *after3, *after4; test_play(1,1, "1", 0,0, "1"); test_play(1,2, "12", 0,0, "12"); test_play(1,2, "11", 0,0, "00"); test_play(2,1, "12", 0,0, "12"); test_play(2,1, "11", 0,0, "00"); /* testing: 4444 2243 2133 1123 */ before = "1224112423443334"; after1 = "2240240023443334"; after2 = "1400114023443334"; after3 = "1224112424404000"; after4 = "1220112023003330"; #define TEST_PLAY_Y(Y,I,A,B,C,D) \ test_play(4,4, I, 0,Y, A);\ test_play(4,4, I, 1,Y, B);\ test_play(4,4, I, 2,Y, C);\ test_play(4,4, I, 3,Y, D); TEST_PLAY_Y(3, before, after4, after4, after4, after4) TEST_PLAY_Y(2, before, after2, after2, after4, after3) TEST_PLAY_Y(1, before, after2, after1, after3, after3) TEST_PLAY_Y(0, before, after1, after1, before, after3) test_play(4,4, "1000110014000000", 0,0, "4000000000000000"); test_play(4,4, "1000222020002000", 1,0, "1000000000000000"); test_play(4,4, "1000100040004400", 3,0, "1000100000000000"); printf("test play -- passed!\n"); } #else void do_test_play() { printf("test play -- skipped!\n"); } #endif main() { do_test_play(); }