Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.02 KB | None | 0 0
  1. #include "winbgi2.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define KEY_LEFT 37
  7. #define KEY_RIGHT 39
  8. #define KEY_UP 38
  9. #define KEY_DOWN 40
  10.  
  11.  
  12. int find_min(int arr[], int length) { // funkcja do sprawdzania czy kafelek do którego chcemy wejść zawiera element najmniejszy
  13.     int min = arr[0];
  14.     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...
  15.         for (int i = 1; i < length; i++) {
  16.             if (arr[i] < min) { min = arr[i]; ind = i; }
  17.         }
  18.         return ind;
  19.  
  20. }
  21.  
  22.  
  23. void main() {
  24.  
  25.     int width = 600;
  26.     int height = 600;
  27.     int square_w = 500; // wymiary prostokąta
  28.     int square_h = 400; // wymiary prostokąta
  29.     const int x = 5; // ilość małych kwadratów w rzędzie
  30.     const int y = 4; // ilość małych kwadratów w kolumnie
  31.     int dist = square_w / x; // odległość między liniami, aby zbudować naszą siatkę w poziomie
  32.     graphics(width, height);
  33.     for (int i = 1; i < x; i++) { // w tej pętli układamy linie aby utworzyły małe kwadraciki
  34.         line(dist * i, 0, dist * i, square_h);
  35.         line(0, dist * i, square_w, dist * i);
  36.     }
  37.     rectangle(0, 0, square_w, square_h); // obramowanie naszego prostokąta
  38.     setbkcolor(0);
  39.     int temp_c = dist / 2;
  40.     int temp_r = dist / 2;
  41.     time_t t;
  42.     int arr[x*y];
  43.     for (int i = 0; i < x*y; i++) {
  44.         arr[i] = 101; //wypełniamy ją liczbami z poza zakresu
  45.     }
  46.     srand((unsigned)time(&t));
  47.     char result[3];
  48.     int index = 0;
  49.     int pos_p_x; // stara pozycja czerwonego kwadratu - x, potrzebne, aby potem zmienić na czarny
  50.     int pos_p_y; // stara pozycja czerwonego kwadratu - y
  51.     int pos_a_x; // j.w tylko to jest aktualna pozycja czerwonego kwadratu
  52.     int pos_a_y; // j.w
  53.     for (int i = 0; i < x - 1; i++) { // losowo wstawiamy liczby z zakresu 1...100 w kwadraty
  54.         for (int i = 0; i <= y; i++) {
  55.             if (temp_r == square_w - (dist / 2) && temp_c == square_h - (dist / 2)) {
  56.                 pos_p_x = temp_r;
  57.                 pos_p_y = temp_c;
  58.                 pos_a_x = temp_r;
  59.                 pos_a_y = temp_c;
  60.                 setcolor(4); // kolor czerwony
  61.                 floodfill(temp_r, temp_c, getmaxcolor());
  62.             }
  63.             else {
  64.                 if (index == 0 || index == 7 || index == 9 || index == 10 || index == 12) {
  65.                     int r = (rand() % 101)+1; // zakres od 1...100
  66.                     _snprintf(result, 3, "%d",r); // castujemy int na char
  67.                     outtextxy(temp_r, temp_c, result);
  68.                     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
  69.                 }
  70.             }
  71.             temp_r = temp_r + dist;
  72.             index++;
  73.         }
  74.         temp_c = temp_c + dist;
  75.         temp_r = dist / 2;
  76.     }
  77.     int check = 0; // licznik ile już odchaczyliśmy dobrych kafelków z liczbami
  78.     index--;
  79.     while(true){
  80.         if (check >= 5) {
  81.             outtextxy(0, 0, "Wygrałeś! \n Naciśnij przycisk aby zakończyć");
  82.             getch();
  83.             exit(0);
  84.         }
  85.     char c;
  86.     c = (char)getch();
  87.     switch (c) {
  88.     case KEY_UP: {
  89.         if (pos_p_y - dist > 0) {
  90.             int in = find_min(arr, x*y); // nasz ind, który obecnie zawiera elem. najmniejszy
  91.             if (index-x == 0 || index-x == 7 || index-x == 9 || index-x == 10 || index-x == 12) {
  92.                 if (index-x == in || arr[index-x]>100) {
  93.                     setcolor(0); // kolor czarny
  94.                     floodfill(pos_p_x, pos_p_y, getmaxcolor()); // wcześniejszy kafelek zmieniamy na czarny
  95.                     arr[in] = 101; // ustawiamy teraz ten elem na taki, żeby więcej na niego nie wpaść czyli poza zakres bo zakres to 1...100  
  96.                     check++;
  97.                     pos_a_x = pos_p_x;
  98.                     pos_a_y = pos_p_y - dist;
  99.                     setcolor(0);
  100.                     _snprintf(result, 3, "%d", arr[in]); // castujemy int na char
  101.                     outtextxy(pos_a_x, pos_a_y, result);
  102.                     pos_p_x = pos_a_x;
  103.                     pos_p_y = pos_a_y;
  104.                     setcolor(4);
  105.                     floodfill(pos_a_x, pos_a_y, getmaxcolor());
  106.                     index = index - x;
  107.                 }
  108.             }
  109.             else {
  110.                 setcolor(0); // kolor czarny
  111.                 floodfill(pos_p_x, pos_p_y, getmaxcolor());
  112.                 pos_a_x = pos_p_x;
  113.                 pos_a_y = pos_p_y - dist;
  114.                 pos_p_x = pos_a_x;
  115.                 pos_p_y = pos_a_y;
  116.                 setcolor(4);
  117.                 floodfill(pos_a_x, pos_a_y, getmaxcolor());
  118.                 index = index - x;
  119.             }
  120.         }
  121.  
  122.     } break;
  123.     case KEY_RIGHT: {
  124.         if (pos_p_x + dist < square_w) {
  125.             int in = find_min(arr, x*y);
  126.             if (index+1 == 0 || index+1 == 7 || index+1 == 9 || index+1 == 10 || index+1 == 12) {
  127.                 if (index+1 == in || arr[index+1]>100) {
  128.                     setcolor(0); // kolor czarny
  129.                     floodfill(pos_p_x, pos_p_y, getmaxcolor());
  130.                     check++;
  131.                     arr[in] = 101;
  132.                     pos_a_x = pos_p_x + dist;
  133.                     pos_a_y = pos_p_y;
  134.                     setcolor(0);
  135.                     _snprintf(result, 3, "%d", arr[in]); // castujemy int na char
  136.                     outtextxy(pos_a_x, pos_a_y, result);
  137.                     pos_p_x = pos_a_x;
  138.                     pos_p_y = pos_a_y;
  139.                     setcolor(4);
  140.                     floodfill(pos_a_x, pos_a_y, getmaxcolor());
  141.                     index = index + 1;
  142.                 }
  143.             }
  144.             else {
  145.                 setcolor(0); // kolor czarny
  146.                 floodfill(pos_p_x, pos_p_y, getmaxcolor());
  147.                 pos_a_x = pos_p_x + dist;
  148.                 pos_a_y = pos_p_y;
  149.                 pos_p_x = pos_a_x;
  150.                 pos_p_y = pos_a_y;
  151.                 setcolor(4);
  152.                 floodfill(pos_a_x, pos_a_y, getmaxcolor());
  153.                 index = index + 1;
  154.             }
  155.         }
  156.     } break;
  157.     case KEY_LEFT: {
  158.         if (pos_p_x - dist > 0) {
  159.             int in = find_min(arr, x*y);
  160.             if (index-1 == 0 || index-1 == 7 || index-1 == 9 || index-1 == 10 || index-1 == 12) {
  161.                 if (index-1 == in || arr[index-1]>100) {
  162.                     setcolor(0); // kolor czarny
  163.                     floodfill(pos_p_x, pos_p_y, getmaxcolor());
  164.                     check++;
  165.                     arr[in] = 101;
  166.                     pos_a_x = pos_p_x - dist;
  167.                     pos_a_y = pos_p_y;
  168.                     setcolor(0);
  169.                     _snprintf(result, 3, "%d", arr[in]); // castujemy int na char
  170.                     outtextxy(pos_a_x, pos_a_y, result);
  171.                     pos_p_x = pos_a_x;
  172.                     pos_p_y = pos_a_y;
  173.                     setcolor(4);
  174.                     floodfill(pos_a_x, pos_a_y, getmaxcolor());
  175.                     index = index - 1;
  176.                 }
  177.             }
  178.             else {
  179.                 setcolor(0); // kolor czarny
  180.                 floodfill(pos_p_x, pos_p_y, getmaxcolor());
  181.                 pos_a_x = pos_p_x - dist;
  182.                 pos_a_y = pos_p_y;
  183.                 pos_p_x = pos_a_x;
  184.                 pos_p_y = pos_a_y;
  185.                 setcolor(4);
  186.                 floodfill(pos_a_x, pos_a_y, getmaxcolor());
  187.                 index = index - 1;
  188.             }
  189.         }
  190.     } break;
  191.     case KEY_DOWN: {
  192.         if (pos_p_y + dist < square_h) {
  193.             int in = find_min(arr, x*y);
  194.             if (index+x == 0 || index+x == 7 || index+x == 9 || index+x == 10 || index+x == 12) {
  195.                 if (index+x == in || arr[index+x]>100) {
  196.                     setcolor(0); // kolor czarny
  197.                     floodfill(pos_p_x, pos_p_y, getmaxcolor());
  198.                     check++;
  199.                     arr[in] = 101;
  200.                     pos_a_x = pos_p_x;
  201.                     pos_a_y = pos_p_y + dist;
  202.                     setcolor(0);
  203.                     _snprintf(result, 3, "%d", arr[in]); // castujemy int na char
  204.                     outtextxy(pos_a_x, pos_a_y, result);
  205.                     pos_p_x = pos_a_x;
  206.                     pos_p_y = pos_a_y;
  207.                     setcolor(4);
  208.                     floodfill(pos_a_x, pos_a_y, getmaxcolor());
  209.                     index = index + x;
  210.                 }
  211.             }
  212.             else {
  213.                 setcolor(0); // kolor czarny
  214.                 floodfill(pos_p_x, pos_p_y, getmaxcolor());
  215.                 pos_a_x = pos_p_x;
  216.                 pos_a_y = pos_p_y + dist;
  217.                 pos_p_x = pos_a_x;
  218.                 pos_p_y = pos_a_y;
  219.                 setcolor(4);
  220.                 floodfill(pos_a_x, pos_a_y, getmaxcolor());
  221.                 index = index + x;
  222.             }
  223.         }
  224.     } break;
  225.    
  226.     }
  227. }
  228.  
  229.     wait();
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement