Advertisement
Guest User

OOKOKO

a guest
Apr 2nd, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <cmath>
  4. #include <string>
  5. #include <sstream>
  6. #include <Windows.h>
  7. #include <vector>
  8. #include <time.h>
  9.  
  10. using namespace std;
  11.  
  12. string converttostr(unsigned int a)
  13. {
  14.         stringstream ss;
  15.         if(ss << a)
  16.         {
  17.                 return ss.str();
  18.         }else
  19.         {
  20.                 return "Fail converting integer type to string type";
  21.         }
  22. }
  23.  
  24. string converttostr(int a)
  25. {
  26.         stringstream ss;
  27.         if(ss << a)
  28.         {
  29.                 return ss.str();
  30.         }else
  31.         {
  32.                 return "Fail converting integer type to string type";
  33.         }
  34. }
  35.  
  36. int converttoint(string a)
  37. {
  38.         stringstream ss;
  39.         ss << a;
  40.         int b;
  41.         if(ss >> b)
  42.         {
  43.                 return b;
  44.         }else
  45.         {
  46.                 return -1;
  47.         }
  48. }
  49.  
  50. int isum(vector<int> a)
  51. {
  52.         int x = 0;
  53.         for(int i = 0; i < a.size(); i++)
  54.         {
  55.                 x += a.at(i);
  56.         }
  57.         return x;
  58. }
  59.  
  60. const double G = (6.67e-11);
  61.  
  62.  
  63.  
  64. class obj
  65. {
  66.        
  67.         unsigned int mass;
  68.         int Vx, Vy;
  69.         int x, y;
  70. public:
  71.         vector<int> rpath;
  72.         vector<int> dpath;
  73.         void accelerate(double r, unsigned int M)
  74.         {
  75.                 M = M*pow(10.0,12.0);
  76.                 if(mass >= 1)
  77.                 {
  78.                         cout << "a = " << ((G*M*1000)/(pow(r,2.0))) + 0.5 << endl;
  79.                         Vx += ((G*M*1000)/pow(r,2.0)) + 0.5;
  80.                         cout << Vx << endl;
  81.                 }else
  82.                 {
  83.                         Vx = 0;
  84.                 }
  85.         }
  86.         void setmass(unsigned int a)
  87.         {
  88.                 mass += a;
  89.         }
  90.         string getmass()
  91.         {
  92.                 return ((mass >= 1) ? converttostr(mass) : " ");
  93.         }
  94.         int* getcoords()
  95.         {
  96.                 int temp[2] = {x, y};
  97.                 return (temp);
  98.         }
  99.         void clearpath()
  100.         {
  101.                 rpath.clear();
  102.                 dpath.clear();
  103.         }
  104.         void clearall()
  105.         {
  106.                 mass = 0;
  107.                 rpath.clear();
  108.                 dpath.clear();
  109.                 Vx = 0;
  110.                 Vy = 0;
  111.         }
  112.  
  113.  
  114.         void operator= (const obj& other)
  115.         {
  116.                 mass = other.mass;
  117.                 rpath = other.rpath;
  118.                 dpath = other.dpath;
  119.                 Vx = other.Vx;
  120.                 Vy = other.Vy;
  121.         }
  122.  
  123.         obj(unsigned int xco = 0, unsigned int yco = 0, unsigned int m = 0, unsigned int sx = 0, unsigned sy = 0)
  124.         {
  125.                 x = xco;
  126.                 y = yco;
  127.                 mass = m;
  128.                 Vx = sx;
  129.                 Vy = sy;
  130.         }
  131. };
  132.  
  133. void drawfirstgrid(vector<vector<obj>>& ogrid);
  134.  
  135.  
  136. void tick(vector<vector<obj>>& ogrid)
  137. {
  138.         //cout << "tick" << endl;
  139.         vector<obj> list;
  140.         for(int i = 0; i < ogrid.size(); i++)
  141.         {
  142.                 for(int j = 0; j < ogrid[i].size(); j++)
  143.                 {
  144.                         if((converttoint(ogrid[i][j].getmass())) >= 1)
  145.                         {
  146.                                
  147.                                 list.push_back(ogrid[i][j]);
  148.                                 ogrid[i][j].clearall();
  149.                         }
  150.                 }
  151.         }
  152.         for(int i = 0; i < list.size(); i++)
  153.         {
  154.                 int x = (list[i].getcoords()[0]);
  155.                 x += isum(list[i].rpath);
  156.                 if(x >= ogrid[0].size())
  157.                 {
  158.                         x = ogrid[0].size()-1;
  159.                 }
  160.                 //cout << "this x: " << x << endl;
  161.                 int y = (list[i].getcoords()[1]);
  162.                 y += isum(list[i].dpath);
  163.                 if(y >= ogrid.size())
  164.                 {
  165.                         y = ogrid.size()-1;
  166.                 }
  167.                 //cout << "this y: " << y << endl;
  168.                 if(converttoint(ogrid[y][x].getmass()) == 0)
  169.                 {
  170.                         ogrid[y][x] = list[i];
  171.                 }else
  172.                 {
  173.                         ogrid[y][x].setmass(converttoint(list[i].getmass()));
  174.                 }
  175.                 ogrid[y][x].clearpath();
  176.         }
  177.         drawfirstgrid(ogrid);
  178. }
  179.  
  180. void pathfind(int Ax, int Ay, int Bx, int By, obj& curobj)
  181. {
  182.         /*cout << "pathfind" << endl;
  183.         cout << "Obj(" << Ax << "," << Ay << ")" << "has found Obj(" << Bx << "," << By << ")" << endl;*/
  184.        
  185.         int rightvector = -(Ax - Bx);
  186.         int downvector = -(Ay - By);
  187.         if(rightvector != 0)
  188.         {
  189.                 curobj.rpath.push_back(rightvector/abs(rightvector));
  190.         }else
  191.         {
  192.                 curobj.rpath.push_back(0);
  193.         }
  194.         if(downvector != 0)
  195.         {
  196.                 curobj.dpath.push_back(downvector/abs(downvector));
  197.         }else
  198.         {
  199.                 curobj.dpath.push_back(0);
  200.         }
  201. }
  202.  
  203. void interact(obj& curobj, vector<vector<obj>>& ogrid)
  204. {
  205.         //cout << "interact" << endl;
  206.         for(int i = 0; i < ogrid.size(); i++)
  207.         {
  208.                 for(int j = 0; j < ogrid[i].size(); j++)
  209.                 {
  210.                        
  211.                         if((converttoint(ogrid[i][j].getmass())) >= 1)
  212.                         {
  213.                                 int Ax = curobj.getcoords()[0];
  214.                                 int Ay = curobj.getcoords()[1];
  215.                                 int Bx = ogrid[i][j].getcoords()[0];
  216.                                 int By = ogrid[i][j].getcoords()[1];
  217.                                 if( (Ax - Bx != 0) || (Ay - By != 0) )
  218.                                 {
  219.                                         pathfind(Ax, Ay, Bx, By, curobj);
  220.                                 }
  221.  
  222.                         }
  223.                 }
  224.         }
  225.        
  226.        
  227. }
  228.  
  229. void scangrid(vector<vector<obj>>& ogrid)
  230. {
  231.         //cout << "scangrid" << endl;
  232.         for(int i = 0; i < ogrid.size(); i++)
  233.         {
  234.                 for(int j = 0; j < ogrid[i].size(); j++)
  235.                 {
  236.                         if(converttoint(ogrid[i][j].getmass()) >= 1)
  237.                         {
  238.                                 interact(ogrid[i][j], ogrid);
  239.                         }
  240.                 }
  241.         }
  242.         bool flag = false;
  243.         for(int i = 0; i < ogrid.size(); i++)
  244.         {
  245.                 for(int j = 0; j < ogrid[i].size(); j++)
  246.                 {
  247.                         if(converttoint(ogrid[i][j].getmass()) >= 1)
  248.                         {
  249.                                 /*cout << "rs for (" << ogrid[i][j].getcoords()[0] << ", " << ogrid[i][j].getcoords()[1] << ") :";
  250.  
  251.                                 cout << isum(ogrid[i][j].rpath) << endl;
  252.                                 cout << "ds for (" << ogrid[i][j].getcoords()[0] << ", " << ogrid[i][j].getcoords()[1] << ") :";
  253.                                 cout << isum(ogrid[i][j].dpath) << endl;*/
  254.                                 //flag = true;
  255.                         }
  256.                         if(flag){break;}
  257.                 }
  258.                 if(flag){break;}
  259.         }
  260.         //system("pause");
  261.         tick(ogrid);
  262. }
  263.  
  264. void drawfirstgrid(vector<vector<obj>>& ogrid)
  265. {
  266.         //cout << "drawfgrid" << endl;
  267.        
  268.         for(int i = 0; i < ogrid.size(); i++)
  269.         {
  270.                 for(int j = 0; j < ogrid[i].size(); j++)
  271.                 {
  272.                         cout << ogrid[i][j].getmass() << " ";
  273.                 }
  274.                 cout << endl;
  275.         }
  276.         cout << endl;
  277.         string a;
  278.         getline(cin, a);
  279.         scangrid(ogrid);
  280. }
  281.  
  282. void setgrid(unsigned int grids[2])
  283. {
  284.         //cout << "setgrid" << endl;
  285.         srand(time(NULL));
  286.         vector<vector<obj>> ogrid;
  287.         ogrid.resize(grids[0], vector<obj> (grids[1]));
  288.         for(int i = 0; i < grids[0]; i++)
  289.         {
  290.                 for(int j = 0; j < grids[1]; j++)
  291.                 {
  292.                         unsigned int rnd = rand() % 100;
  293.                         if(rnd % 19 == 0)
  294.                         {
  295.                                 ogrid[i][j].obj::obj(j, i, 1);
  296.                         }else
  297.                         {
  298.                                 ogrid[i][j].obj::obj(j, i , 0);
  299.                         }
  300.                 }
  301.         }
  302.         drawfirstgrid(ogrid);
  303. }
  304.  
  305. int main()
  306. {
  307.         cout << "mass is measured in kg*10^12, time in 1000s. Distance is in meters, G is defined as 6.67e-11" << endl;
  308.         while(true)
  309.         {
  310.                 unsigned int gridsize[] = {0,0};
  311.                 string getsize;
  312.                 try
  313.                 {
  314.                         cout << "Enter grid dimensions, x (space) y : ";
  315.                         if(getline(cin, getsize))
  316.                         {
  317.  
  318.                                 for(int i = 0; i <= 1; i++)
  319.                                 {
  320.                                         string temp;
  321.                                         temp = (i == 0) ? getsize.substr(0, getsize.find(' ')) : getsize;
  322.                                         (converttoint(temp) != -1) ? gridsize[i] = converttoint(temp) : throw 0;
  323.                                         getsize.erase(0, getsize.find(' '));
  324.                                        
  325.                                 }
  326.                                        
  327.  
  328.                                 cout << "calling setgrid with " << gridsize[0] << "," << gridsize[1] << endl;
  329.                                 setgrid(gridsize);
  330.                                 cout << endl << "--End--" << endl;
  331.                                 //break;
  332.                         }
  333.                         throw 0;
  334.                 }
  335.                 catch(int a)
  336.                 {
  337.                         switch(a)
  338.                         {
  339.                         case 0:
  340.                                 cout << "input must be a number!" << endl;
  341.                                 cin.clear();
  342.                                 cin.sync();
  343.                                 break;
  344.                         case 1:
  345.                                 cout << "grid sizes greater than 20 not allowed" << endl;
  346.                                 break;
  347.                         }
  348.                 }
  349.         }
  350.         system("pause");
  351.         return 0;
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement