void move_tile_2_position(int target_row, int target_col) { while (target_col + 1 > empty_col) move_tile(right); while (target_row < empty_row) move_tile(up); while (target_row > empty_row) move_tile(down); while (target_col + 1 < empty_col) move_tile(left); }
void fix_bottom_corner(int iteration) { int larger_tile = d * (d - 1) + iteration + 1; int smaller_tile = larger_tile - d; int tile_row, tile_col; int target_row, target_col; look_4_tile(larger_tile, &tile_row, &tile_col); move_blank_2_position(tile_row, tile_col); // Move larger tile to position [d - 2][iteration + 1] target_row = d - 2; target_col = iteration + 1; #if DEBUG sprintf(str, "Moving tile %d to position [d - 2][iteration + 1]\n",larger_tile); god_mode_debug(str); #endif move_tile_2_position(target_row, target_col); // If smaller_tile is in position [d - 2][iteration], reorder tiles if (board[d - 2][iteration] == smaller_tile) { #if DEBUG sprintf(str, "Tile %d in position [d - 2][iteration]\n",smaller_tile); god_mode_debug(str); sprintf(str, "Reordering tiles\n"); god_mode_debug(str); #endif // Reorder tiles move_blank(left); rotate_blank(NE, 3, true); move_blank(right); move_blank(right); rotate_blank(NE, 2, true); move_blank(left); rotate_blank(SW, 3, true); rotate_blank(SW, 2, false); #if DEBUG sprintf(str, "Reordering complete\n"); god_mode_debug(str); #endif } else // Else move larger tile to position [d - 2][iteration] { #if DEBUG sprintf(str, "Moving tile %d to position [d - 2][iteration]\n",larger_tile); god_mode_debug(str); #endif // Move larger tile to position [d - 2][iteration] move_tile(left); #if DEBUG sprintf(str, "Moving tile %d to position [d - 2][iteration + 1]\n",smaller_tile); god_mode_debug(str); #endif // Move smaller tile to position [d - 2][iteration + 1] look_4_tile(smaller_tile, &tile_row, &tile_col); move_blank_2_position(tile_row, tile_col); target_col = iteration + 1; move_tile_2_position(target_row, target_col); } #if DEBUG sprintf(str, "Rotating corner tiles\n"); god_mode_debug(str); #endif // Rotate tiles to fix corner rotate_blank(NE, 2, true); rotate_blank(SE, 3, true); }
void fix_top_corner(int iteration) { int larger_tile = (iteration + 1) * d; int smaller_tile = larger_tile - 1; int tile_row, tile_col; int target_row, target_col; look_4_tile(larger_tile, &tile_row, &tile_col); move_blank_2_position(tile_row, tile_col); // If smaller_tile is in position [iteration][d - 2], reorder tiles if (board[iteration][d - 2] == smaller_tile) { #if DEBUG sprintf(str, "Tile %d in position [iteration][d - 2]\n",smaller_tile); god_mode_debug(str); #endif // Move larger tile to position [iteration + 2][d - 2] target_row = iteration + 2; target_col = d - 2; #if DEBUG sprintf(str, "Moving tile %d to position [iteration + 2][d - 2]\n",larger_tile); god_mode_debug(str); #endif move_tile_2_position(target_row, target_col); #if DEBUG sprintf(str, "Reordering tiles\n"); god_mode_debug(str); #endif // Reorder tiles move_blank_2_position(iteration, d - 2); rotate_blank(NE, 3, false); move_blank_2_position(target_row, target_col); move_tile(up); rotate_blank(SE, 4, false); #if DEBUG sprintf(str, "Reordering complete\n"); god_mode_debug(str); #endif } else // Else move larger tile to position [iteration][d - 2] { #if DEBUG sprintf(str, "Moving tile %d to position [iteration][d - 2]\n",larger_tile); god_mode_debug(str); #endif // Move larger tile to position [iteration][d - 2] target_row = iteration; target_col = d - 2; move_tile_2_position(target_row, target_col); #if DEBUG sprintf(str, "Moving tile %d to position [iteration + 1][d - 2]\n",smaller_tile); god_mode_debug(str); #endif // Move smaller tile to position [iteration + 1][d - 2] look_4_tile(smaller_tile, &tile_row, &tile_col); move_blank_2_position(tile_row, tile_col); target_row = iteration + 1; move_tile_2_position(target_row, target_col); } #if DEBUG sprintf(str, "Rotating corner tiles\n"); god_mode_debug(str); #endif // Rotate tiles to fix corner rotate_blank(SE, 3, false); }
int main(int argn, char** argv) { int xsize, ysize; char c, c2; int i, i2; int x, y; WINDOW* wnd; char buf[50]; tile* temp_tile; /* Inicjalizacja generatora liczb losowych */ srand(time(0)); wnd = initscr(); start_color(); if(has_colors()) { enable_tile_colors(); use_default_colors(); /* Przyporządkuj każdej płytce kolor */ init_pair(1, COLOR_YELLOW, -1); init_pair(2, COLOR_GREEN, -1); init_pair(3, COLOR_RED, -1); init_pair(4, COLOR_YELLOW, -1); init_pair(5, COLOR_WHITE, -1); init_pair(6, COLOR_RED, -1); init_pair(7, COLOR_GREEN, -1); init_pair(8, COLOR_BLUE, -1); init_pair(9, COLOR_CYAN, -1); init_pair(10, COLOR_MAGENTA, -1); } else { disable_tile_colors(); } cbreak(); echo(); nonl(); intrflush(stdscr, FALSE); keypad(stdscr, TRUE); getmaxyx(wnd, ysize, xsize); /* Pobierz docelowy rozmiar */ while(1) { werase(wnd); draw_frame(wnd); wmove(wnd, ysize/2-(MAX_SIZE-MIN_SIZE+2), 20); wprintw(wnd, "Available pool size:"); for(i=MIN_SIZE;i<=MAX_SIZE;i++) { wmove(wnd, ysize/2-(MAX_SIZE-MIN_SIZE+2)+(i-MIN_SIZE+1), 20); attron(COLOR_PAIR(5) | A_BOLD); wprintw(wnd, "%d", i); attroff(COLOR_PAIR(5) | A_BOLD); wprintw(wnd, " for %dx%d", i, i); } wmove(wnd, ysize-2, 2); wprintw(wnd, "Saephir (c), 2010"); wmove(wnd, ysize/2+1, 20); wprintw(wnd, "Type the desired number: "); c = getch(); buf[0] = c; buf[1] = 0; i2 = atoi(buf); if(i2 >= MIN_SIZE && i2 <= MAX_SIZE) { TILES_COUNT = i2; init(); break; } } while(1) { werase(wnd); draw_frame(wnd); /* Wygrana gra */ if(verify()) { print_game(wnd); wmove(wnd, ysize/2, 40); wprintw(wnd, "Well done!"); wmove(wnd, ysize/2+1, 40); wprintw(wnd, "Press ENTER to exit..."); getch(); break; } print_game(wnd); print_pool(wnd); wmove(wnd, ysize-2, 1); wgetnstr(wnd, buf, 49); buf[49] = 0; if(strcmp(buf, "exit") == 0 || strcmp(buf, "quit") == 0 || strcmp(buf, "q") == 0) { break; } else if(strcmp(buf, "help") == 0 || strcmp(buf, "?") == 0) { wmove(wnd, ysize-2, 1); wclrtoeol(wnd); draw_frame(wnd); wmove(wnd, ysize-2, 1); wprintw(wnd, HELP_LINE); getch(); wmove(wnd, ysize-2, 1); wclrtoeol(wnd); draw_frame(wnd); wmove(wnd, ysize-2, 1); wprintw(wnd, HELP_LINE2); getch(); } else if(strcmp(buf, "<<") == 0) { push_left(); } else if(strcmp(buf, ">>") == 0) { push_right(); } else if(strcmp(buf, "^^") == 0) { push_top(); } else if(strcmp(buf, "vv") == 0) { push_bottom(); } else if(sscanf(buf, "%c%d<%c%d", &c, &i, &c2, &i2) == 4) { i--; i2--; if(c == c2 && i == i2) continue; if(c-'a' < 0 || c-'a' >= 2*TILES_COUNT) continue; if(c2-'a' < 0 || c2-'a' >= 2*TILES_COUNT) continue; if(i < 0 || i >= TILES_COUNT) continue; if(i2 < 0 || i2 >= TILES_COUNT) continue; /* Przesuwamy na pole gry */ if(c-'a' < TILES_COUNT) { temp_tile = &game[i][c-'a']; /* Miejsce musi być puste */ if(!is_empty(temp_tile)) continue; /* przesuwamy z puli do gry */ if(c2-'a' >= TILES_COUNT) temp_tile = &pool[i2][c2-'a'-TILES_COUNT]; /* przesuwamy z gry do gry */ else temp_tile = &game[i2][c2-'a']; /* Żródło nie może być puste */ if(is_empty(temp_tile)) continue; move_tile(&game[i][c-'a'], temp_tile); } /* Przesuwamy do puli */ else { temp_tile = &pool[i][c-'a'-TILES_COUNT]; /* Miejsce musi być puste */ if(!is_empty(temp_tile)) continue; /* Przesuwamy z pola gry do puli */ if(c2-'a' < TILES_COUNT) temp_tile = &game[i2][c2-'a']; /* Przesuwamy z puli do puli */ else temp_tile = &pool[i2][c2-'a'-TILES_COUNT]; /* Żródło nie może być puste */ if(is_empty(temp_tile)) continue; move_tile(&pool[i][c-'a'-TILES_COUNT], temp_tile); } } else if(sscanf(buf, "%c%d>%c%d", &c, &i, &c2, &i2) == 4) { i--; i2--; if(c == c2 && i == i2) continue; if(c-'a' < 0 || c-'a' >= 2*TILES_COUNT) continue; if(c2-'a' < 0 || c2-'a' >= 2*TILES_COUNT) continue; if(i < 0 || i >= TILES_COUNT) continue; if(i2 < 0 || i2 >= TILES_COUNT) continue; /* Przesuwamy do puli */ if(c2-'a' >= TILES_COUNT) { temp_tile = &pool[i2][c2-'a'-TILES_COUNT]; /* Miejsce musi być puste */ if(!is_empty(temp_tile)) continue; /* Przesuwamy z pola gry */ if(c-'a' < TILES_COUNT) temp_tile = &game[i][c-'a']; /* Przesuwamy z puli */ else temp_tile = &pool[i][c-'a'-TILES_COUNT]; /* Żródło nie może być puste */ if(is_empty(temp_tile)) continue; move_tile(&pool[i2][c2-'a'-TILES_COUNT], temp_tile); } /* Przesuwamy na pole gry */ else { temp_tile = &game[i2][c2-'a']; /* Miejsce musi być puste */ if(!is_empty(temp_tile)) continue; /* Przesuwamy z puli na pole gry */ if(c-'a' >= TILES_COUNT) temp_tile = &pool[i][c-'a'-TILES_COUNT]; /* Przesuwamy z pola gry na pole gry */ else temp_tile = &game[i][c-'a']; /* Żródło nie może być puste */ if(is_empty(temp_tile)) continue; move_tile(&game[i2][c2-'a'], temp_tile); } } else if(sscanf(buf, "%c%d>pool", &c, &i) == 2 || sscanf(buf, "pool<%c%d", &c, &i) == 2) { i--; if(strcmp(&buf[2], ">pool") != 0) { buf[5] = 0; if(strcmp(buf, "pool<") != 0) continue; } if(c-'a' < 0 || c-'a' >= TILES_COUNT) continue; if(i < 0 || i >= TILES_COUNT) continue; /* Żródło nie może być puste */ if(is_empty(&game[i][c-'a'])) continue; /* Jeżeli źródło nie jest puste, to istnieją wolne miejsca w puli */ /* znajdujemy więc pierwsze wolne miejsce */ for(y=0;y<TILES_COUNT;y++) { for(x=0;x<TILES_COUNT;x++) { if(is_empty(&pool[x][y])) { move_tile(&pool[x][y], &game[i][c-'a']); break; } } } } } endwin(); return 0; }