Advertisement
Guest User

JeuKslanTN.cpp

a guest
Feb 20th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <math.h>
  5. #include <algorithm>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. class JeuKslanTN {
  11. public :
  12.     JeuKslanTN(vector<vector<double>> Board)
  13.     {
  14.         board = Board;
  15.         str_max = "";
  16.         str_min = "";
  17.         calculs();
  18.         afficher();
  19.     }
  20. private :
  21.     vector<vector<double>> board;
  22.     vector<double> resultats;
  23.     vector<double> resultats_ranges;
  24.     vector<string> paths;
  25.     string str_max;
  26.     double max;
  27.     string str_min;
  28.     double min;
  29.     double moyenne;
  30.     double mediane;
  31.    
  32.    
  33.     void calculs()
  34.     {
  35.         int compteur = 0;
  36.         for (unsigned int i = 0 ; i < board.size(); i++) {
  37.             for (unsigned int k = 0; k < board[i].size(); k++) {
  38.                 compteur++;
  39.                 cout << "case : " << i << " " << k << endl;
  40.                 chemins(board, i, k,"", 1);
  41.             }
  42.         }
  43.         cout << endl;
  44.         ranger();
  45.         min = resultats_ranges[0];
  46.         max = resultats_ranges.back();
  47.  
  48.         int i = 0;
  49.         while (str_min == "" || str_max == "") { // à opti
  50.             if(resultats[i] == max && (str_max == "" || str_max.size() > paths[i].size())) {
  51.                 str_max = paths[i];
  52.             }
  53.             if(resultats[i] == min && (str_min == "" || str_min.size() > paths[i].size())) {
  54.                 str_min = paths[i];
  55.             }
  56.             i++;
  57.         }      
  58.         double somme_scores = somme();
  59.         moyenne = somme_scores / resultats.size();
  60.         if(resultats.size() % 2 == 0) {
  61.             mediane = ((resultats_ranges[resultats.size()/2-1]) + (resultats_ranges[resultats.size()/2]))/2.;
  62.         }
  63.         else {
  64.             mediane = resultats_ranges[ceil((resultats.size()-1)/2)];
  65.         }
  66.     }
  67.    
  68.     void chemins(vector<vector<double>> tab, int x, int y,string str, double score)
  69.     {
  70.         unsigned int mX = 0; unsigned int MX = 0; unsigned int mY = 0; unsigned int MY = 0;
  71.         score *= tab[x][y];
  72.         resultats.push_back(score);
  73.         str += " -> " + to_string(tab[x][y]);
  74.         paths.push_back(str);
  75.         tab[x][y] = -1;
  76.         if (score > 0) {
  77.             vector<int> retour = limites(x,y,mX,MX,mY,MY);
  78.             mX = retour[0]; MX = retour[1]; mY = retour[2]; MY = retour[3];
  79.             for(unsigned int i = x - 1 + mX ; i <= x + 1 - MX ; i++) {
  80.                 if(tab[i][y] != -1) chemins(tab,i,y,str,score);
  81.             }
  82.             for(unsigned int k = y - 1 + mY ; k <= y + 1 - MY ; k++) {
  83.                 if(tab[x][k] != -1) chemins(tab,x,k,str,score);
  84.             }
  85.         }
  86.     }
  87.    
  88.     vector<int> limites(unsigned int x,unsigned int y,int mX,int MX,int mY,int MY)
  89.     {
  90.         if (x == 0)
  91.             mX = 1;
  92.         else
  93.             mX = 0;
  94.  
  95.         if (x == board.size() -1)
  96.             MX = 1;
  97.         else
  98.             MX = 0;
  99.            
  100.         if (y == 0)
  101.             mY = 1;
  102.         else
  103.             mY = 0;
  104.  
  105.         if (y == board[x].size() -1)
  106.             MY = 1;
  107.         else
  108.             MY = 0;
  109.         vector<int> retour = {mX,MX,mY,MY};
  110.         return retour;
  111.     }
  112.    
  113.     void afficher()
  114.     {
  115.         cout << "****************************************************************************\n\n";
  116.         cout << "Nombres de possibilites : " << resultats.size() << endl << endl;
  117.         cout << "min = " << min << endl;
  118.         cout << str_min << endl << endl;
  119.         cout << "max = " << max << endl;
  120.         cout << str_max << endl << endl;
  121.         cout << "moyenne = " << moyenne << endl;
  122.         cout << "mediane = " << mediane << endl;
  123.         cout << "\n****************************************************************************";
  124.     }
  125.    
  126.     void ranger()
  127.     {
  128.         resultats_ranges = resultats;
  129.         sort(resultats_ranges.begin(),resultats_ranges.end()); 
  130.     }
  131.    
  132.     double somme()
  133.     {
  134.         double s = 0;
  135.         for (unsigned int i = 0 ; i < resultats.size() ; i++) {
  136.             s += resultats[i];
  137.         }
  138.         return s;
  139.     }
  140. };
  141.  
  142. int main()
  143. {
  144.     cout << "";
  145.     vector<vector<double>> Board =  {
  146. {0.0 , 2.0 , 2.5 , 1.5 , 2.5 , 1.0},
  147. {3.0 , 1.5 , 1.0 , 2.5 , 0.0 , 2.0},
  148. {1.5 , 0.0 , 1.0 , 2.5 , 1.5 , 1.0},
  149. {2.0 , 1.5 , 5.0 , 5.0 , 2.0 , 0.0},
  150. {1.0 , 2.0 , 0.0 , 3.0 , 1.5 , 3.0}
  151.     };
  152.     JeuKslanTN jeu(Board);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement