Advertisement
Tooster

MapGenAlpha

Mar 3rd, 2015
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. //hello pastebin viewers ^^ it's my own algorithm 4 creating bool map... it's useless now XD if u want to use it - just do it, but give info about author :D
  2.  
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <ctime>
  6. #define _ ios_base::sync_with_stdio(0);cin.tie(0);
  7. #define AM 50 //amount of tiles
  8. //#define CorUP 5 //upper border
  9. //#define CorDOWN 1 //lower border
  10.  
  11. using namespace std;
  12.  
  13. void zeruj(bool map[][AM]){
  14.     for (int i = 0; i<AM; i++)
  15.         for (int j = 0; j<AM; j++)
  16.             map[i][j] = 0;
  17. }
  18. //==================================================================================================================================
  19.  
  20. int zlicz(bool map[][AM], int pF, int pS){
  21.     int population = 0;
  22.     if (map[pF - 1][pS - 1] == 1) population++;
  23.     if (map[pF - 1][pS] == 1) population++;
  24.     if (map[pF - 1][pS + 1] == 1) population++;
  25.     if (map[pF][pS - 1] == 1) population++;
  26.     if (map[pF][pS + 1] == 1) population++;
  27.     if (map[pF + 1][pS - 1] == 1) population++;
  28.     if (map[pF + 1][pS] == 1) population++;
  29.     if (map[pF + 1][pS + 1] == 1) population++;
  30.     return population;
  31. }
  32. //:::::::::::::::::::::::::::::::::::
  33. void ruch(bool map[][AM], int step, int dir, int &pF, int &pS, int &a){
  34.     for (int i = 0; i < step; i++){
  35.         switch (dir){ //0 = N    1 = E   2 = S   3 = W
  36.             case 0:
  37.                 pF--;
  38.                 break;
  39.             case 1:
  40.                 pS++;
  41.                 break;
  42.             case 2:
  43.                 pF++;
  44.                 break;
  45.             case 3:
  46.                 pS--;
  47.                 break;
  48.         }
  49.         if (
  50.             (pF == 0 && pS == 0) ||
  51.             (pF == 0 && pS == AM-1) ||
  52.             (pF == AM-1 && pS == 0 ) ||
  53.             (pF == AM-1 && pS == AM-1)
  54.             )a++; //sprawdza rogi mapy
  55.         if (pF < 0 || pF > AM-1 || pS < 0 || pS > AM-1) continue;
  56.  
  57.         int chance = zlicz(map, pF, pS);
  58.  
  59.         switch (chance){
  60.             case 0: //  1%
  61.                 if (rand() % 100 < 1) map[pF][pS] = 1;
  62.                 break;
  63.             case 1: //  35%
  64.                 if (rand() % 100 < 35) map[pF][pS] = 1;
  65.                 break;
  66.             case 2: //  50%
  67.                 if (rand() % 100 < 50) map[pF][pS] = 1;
  68.                 break;
  69.             case 3: //  75%
  70.                 if (rand() % 100 < 75) map[pF][pS] = 1;
  71.                 break;
  72.             default://  90%
  73.                 if (rand() % 100 < 90) map[pF][pS] = 1;
  74.                 break;
  75.         }
  76.  
  77.     }
  78. }
  79. //:::::::::::::::::::::::::::::::::::
  80. void zapelnij(bool map[AM][AM]){
  81.  
  82.     int rdF = rand() % AM;
  83.     int rdS = rand() % AM;
  84.  
  85.     int a = 0; //róg mapy
  86.  
  87.     int step = 1;
  88.     int dir = 0;
  89.     int mov = 0;
  90.  
  91.     map[rdF][rdS] = 1;
  92.  
  93. //  int pF = rdF; //wiersz
  94. //  int pS = rdS; //kolumna
  95.  
  96.     while (a!=4){
  97.  
  98.         ruch(map, step, dir, rdF, rdS, a);
  99.         dir++; dir %= 4; //zmiana kierunku;
  100.         mov++; //mówi który to był ruch
  101.         if (mov % 2 == 0){ mov %= 2; step++; } //zwiększa krok
  102.  
  103.     }
  104.  
  105. }
  106. //==================================================================================================================================
  107.  
  108. int popraw(bool map[][AM], int CorDOWN, int CorUP){
  109.     int count = 0;
  110.     for (int i = 0; i < AM; i++){
  111.         for (int j = 0; j < AM; j++){
  112.             int tmp = zlicz(map, i, j);
  113.             if (tmp >= CorUP){
  114.                 count++;
  115.                 map[i][j] = 1;
  116.             }
  117.             if (tmp <= CorDOWN){
  118.                 count++;
  119.                 map[i][j] = 0;
  120.             }
  121.  
  122.         }
  123.     }
  124.     return count;
  125. }
  126. void wypisz(bool map[][AM]){
  127.     for (int i = 0; i<AM; i++){
  128.         for (int j = 0; j<AM; j++){
  129.             if (map[i][j]==1) cout << "X" << " ";
  130.             else cout << "  ";
  131.         }
  132.         cout << "\n";
  133.     }
  134. }
  135. //==================================================================================================================================
  136.  
  137. int main(){
  138.     bool map[AM][AM];
  139.     srand(time(NULL));
  140.  
  141.     zeruj(map);
  142.     zapelnij(map);
  143.     wypisz(map);
  144.  
  145.     char doCorrect;
  146.         do{
  147.             cout << "\n======================================================================\n\n# popraw - Y / N - wyjdz \n> ";
  148.             cin >> doCorrect;
  149.             while (doCorrect != 'Y' && doCorrect != 'N'){
  150.                 cout << "# wpisz poprawne polecenie...\n#";
  151.                 cin >> doCorrect;
  152.             }
  153.  
  154.             if (doCorrect == 'Y'){
  155.                 cout << "\n# podaj zakres DOWN i UP z zakresu 0-8 oraz UP>=DOWN[ rekomendowane - DONW=1    UP=5 ]\n> ";
  156.                 short int DOWN, UP;
  157.                 do{
  158.                     cin >> DOWN >> UP;
  159.                     if (DOWN < 0 || DOWN> 8 || UP < 0 || UP>8 || DOWN > UP)
  160.                         cout << "\n# podaj poprawny zakres...\n> ";
  161.                 } while (DOWN < 0 || DOWN> 8 || UP < 0 || UP>8 || DOWN > UP);
  162.                     cout << "\n======================================================================\n#" << "\n# poprawiono:\n# " << popraw(map, DOWN, UP)<< " pól\n======================================================================\n";
  163.                 wypisz(map);
  164.             }
  165.         } while (doCorrect != 'N');
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement