Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "winbgi2.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define KEY_LEFT 37
- #define KEY_RIGHT 39
- #define KEY_UP 38
- #define KEY_DOWN 40
- int find_min(int arr[], int length) { // funkcja do sprawdzania czy kafelek do którego chcemy wejść zawiera element najmniejszy
- int min = arr[0];
- int ind = 0; // indeks znalezionego minimalnego elementu, dzięki temu dla 0 wiemy że chodzi o 1 kafelek licząc od lewo góra, jeśli ind = 1 to chodzi o ten drugi kafelek czyli o index = 8 itd...
- for (int i = 1; i < length; i++) {
- if (arr[i] < min) { min = arr[i]; ind = i; }
- }
- return ind;
- }
- void main() {
- int width = 600;
- int height = 600;
- int square_w = 500; // wymiary prostokąta
- int square_h = 400; // wymiary prostokąta
- const int x = 5; // ilość małych kwadratów w rzędzie
- const int y = 4; // ilość małych kwadratów w kolumnie
- int dist = square_w / x; // odległość między liniami, aby zbudować naszą siatkę w poziomie
- graphics(width, height);
- for (int i = 1; i < x; i++) { // w tej pętli układamy linie aby utworzyły małe kwadraciki
- line(dist * i, 0, dist * i, square_h);
- line(0, dist * i, square_w, dist * i);
- }
- rectangle(0, 0, square_w, square_h); // obramowanie naszego prostokąta
- setbkcolor(0);
- int temp_c = dist / 2;
- int temp_r = dist / 2;
- time_t t;
- int arr[x*y];
- for (int i = 0; i < x*y; i++) {
- arr[i] = 101; //wypełniamy ją liczbami z poza zakresu
- }
- srand((unsigned)time(&t));
- char result[3];
- int index = 0;
- int pos_p_x; // stara pozycja czerwonego kwadratu - x, potrzebne, aby potem zmienić na czarny
- int pos_p_y; // stara pozycja czerwonego kwadratu - y
- int pos_a_x; // j.w tylko to jest aktualna pozycja czerwonego kwadratu
- int pos_a_y; // j.w
- for (int i = 0; i < x - 1; i++) { // losowo wstawiamy liczby z zakresu 1...100 w kwadraty
- for (int i = 0; i <= y; i++) {
- if (temp_r == square_w - (dist / 2) && temp_c == square_h - (dist / 2)) {
- pos_p_x = temp_r;
- pos_p_y = temp_c;
- pos_a_x = temp_r;
- pos_a_y = temp_c;
- setcolor(4); // kolor czerwony
- floodfill(temp_r, temp_c, getmaxcolor());
- }
- else {
- if (index == 0 || index == 7 || index == 9 || index == 10 || index == 12) {
- int r = (rand() % 101)+1; // zakres od 1...100
- _snprintf(result, 3, "%d",r); // castujemy int na char
- outtextxy(temp_r, temp_c, result);
- arr[index] = r; // dzięki temu będziemy wiedzieli, na której pozycji są jakie liczby. Indeks liczony od 0 od lewego górnego rogu
- }
- }
- temp_r = temp_r + dist;
- index++;
- }
- temp_c = temp_c + dist;
- temp_r = dist / 2;
- }
- int check = 0; // licznik ile już odchaczyliśmy dobrych kafelków z liczbami
- index--;
- while(true){
- if (check >= 5) {
- outtextxy(0, 0, "Wygrałeś! \n Naciśnij przycisk aby zakończyć");
- getch();
- exit(0);
- }
- char c;
- c = (char)getch();
- switch (c) {
- case KEY_UP: {
- if (pos_p_y - dist > 0) {
- int in = find_min(arr, x*y); // nasz ind, który obecnie zawiera elem. najmniejszy
- if (index-x == 0 || index-x == 7 || index-x == 9 || index-x == 10 || index-x == 12) {
- if (index-x == in || arr[index-x]>100) {
- setcolor(0); // kolor czarny
- floodfill(pos_p_x, pos_p_y, getmaxcolor()); // wcześniejszy kafelek zmieniamy na czarny
- arr[in] = 101; // ustawiamy teraz ten elem na taki, żeby więcej na niego nie wpaść czyli poza zakres bo zakres to 1...100
- check++;
- pos_a_x = pos_p_x;
- pos_a_y = pos_p_y - dist;
- setcolor(0);
- _snprintf(result, 3, "%d", arr[in]); // castujemy int na char
- outtextxy(pos_a_x, pos_a_y, result);
- pos_p_x = pos_a_x;
- pos_p_y = pos_a_y;
- setcolor(4);
- floodfill(pos_a_x, pos_a_y, getmaxcolor());
- index = index - x;
- }
- }
- else {
- setcolor(0); // kolor czarny
- floodfill(pos_p_x, pos_p_y, getmaxcolor());
- pos_a_x = pos_p_x;
- pos_a_y = pos_p_y - dist;
- pos_p_x = pos_a_x;
- pos_p_y = pos_a_y;
- setcolor(4);
- floodfill(pos_a_x, pos_a_y, getmaxcolor());
- index = index - x;
- }
- }
- } break;
- case KEY_RIGHT: {
- if (pos_p_x + dist < square_w) {
- int in = find_min(arr, x*y);
- if (index+1 == 0 || index+1 == 7 || index+1 == 9 || index+1 == 10 || index+1 == 12) {
- if (index+1 == in || arr[index+1]>100) {
- setcolor(0); // kolor czarny
- floodfill(pos_p_x, pos_p_y, getmaxcolor());
- check++;
- arr[in] = 101;
- pos_a_x = pos_p_x + dist;
- pos_a_y = pos_p_y;
- setcolor(0);
- _snprintf(result, 3, "%d", arr[in]); // castujemy int na char
- outtextxy(pos_a_x, pos_a_y, result);
- pos_p_x = pos_a_x;
- pos_p_y = pos_a_y;
- setcolor(4);
- floodfill(pos_a_x, pos_a_y, getmaxcolor());
- index = index + 1;
- }
- }
- else {
- setcolor(0); // kolor czarny
- floodfill(pos_p_x, pos_p_y, getmaxcolor());
- pos_a_x = pos_p_x + dist;
- pos_a_y = pos_p_y;
- pos_p_x = pos_a_x;
- pos_p_y = pos_a_y;
- setcolor(4);
- floodfill(pos_a_x, pos_a_y, getmaxcolor());
- index = index + 1;
- }
- }
- } break;
- case KEY_LEFT: {
- if (pos_p_x - dist > 0) {
- int in = find_min(arr, x*y);
- if (index-1 == 0 || index-1 == 7 || index-1 == 9 || index-1 == 10 || index-1 == 12) {
- if (index-1 == in || arr[index-1]>100) {
- setcolor(0); // kolor czarny
- floodfill(pos_p_x, pos_p_y, getmaxcolor());
- check++;
- arr[in] = 101;
- pos_a_x = pos_p_x - dist;
- pos_a_y = pos_p_y;
- setcolor(0);
- _snprintf(result, 3, "%d", arr[in]); // castujemy int na char
- outtextxy(pos_a_x, pos_a_y, result);
- pos_p_x = pos_a_x;
- pos_p_y = pos_a_y;
- setcolor(4);
- floodfill(pos_a_x, pos_a_y, getmaxcolor());
- index = index - 1;
- }
- }
- else {
- setcolor(0); // kolor czarny
- floodfill(pos_p_x, pos_p_y, getmaxcolor());
- pos_a_x = pos_p_x - dist;
- pos_a_y = pos_p_y;
- pos_p_x = pos_a_x;
- pos_p_y = pos_a_y;
- setcolor(4);
- floodfill(pos_a_x, pos_a_y, getmaxcolor());
- index = index - 1;
- }
- }
- } break;
- case KEY_DOWN: {
- if (pos_p_y + dist < square_h) {
- int in = find_min(arr, x*y);
- if (index+x == 0 || index+x == 7 || index+x == 9 || index+x == 10 || index+x == 12) {
- if (index+x == in || arr[index+x]>100) {
- setcolor(0); // kolor czarny
- floodfill(pos_p_x, pos_p_y, getmaxcolor());
- check++;
- arr[in] = 101;
- pos_a_x = pos_p_x;
- pos_a_y = pos_p_y + dist;
- setcolor(0);
- _snprintf(result, 3, "%d", arr[in]); // castujemy int na char
- outtextxy(pos_a_x, pos_a_y, result);
- pos_p_x = pos_a_x;
- pos_p_y = pos_a_y;
- setcolor(4);
- floodfill(pos_a_x, pos_a_y, getmaxcolor());
- index = index + x;
- }
- }
- else {
- setcolor(0); // kolor czarny
- floodfill(pos_p_x, pos_p_y, getmaxcolor());
- pos_a_x = pos_p_x;
- pos_a_y = pos_p_y + dist;
- pos_p_x = pos_a_x;
- pos_p_y = pos_a_y;
- setcolor(4);
- floodfill(pos_a_x, pos_a_y, getmaxcolor());
- index = index + x;
- }
- }
- } break;
- }
- }
- wait();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement