Advertisement
Frost2312

POO Final 2012

Dec 20th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. class Exception {
  9.     string _message;
  10. public:
  11.     Exception(string message) : _message(message) { }
  12.  
  13.     string message() const
  14.     {
  15.         return _message;
  16.     }
  17. };
  18.  
  19.  
  20. class Player {
  21.     string _description;
  22.     float _M;
  23.  
  24. public:
  25.     Player(string description, float montant)
  26.     {
  27.         if (montant < 0)
  28.             throw Exception("The amount should be positive");
  29.  
  30.         _M = montant;
  31.         _description = description;
  32.     }
  33.  
  34.     float montant() const
  35.     {
  36.         return _M;
  37.     }
  38.  
  39.     void add(float x)
  40.     {   if (x < 0)
  41.             throw Exception("You can only add positive numbers");
  42.  
  43.         _M += x;
  44.     }
  45.  
  46.     void sub(float x)
  47.     {
  48.         if (x > _M)
  49.             throw Exception("The amount you're trying to remove is greater than the player's money");
  50.         else if (x < 0)
  51.             throw Exception("You can only substract positive numbers");
  52.  
  53.         _M -= x;
  54.     }
  55. };
  56.  
  57.  
  58. class Property {
  59. protected:
  60.     string _nom;
  61.  
  62. public:
  63.     Property(string nom) : _nom(nom) { }
  64.  
  65.     virtual void passThrough(Player *x) { }
  66.  
  67.     virtual void land(Player *x) = 0;
  68.  
  69.     virtual ~Property() { }
  70. };
  71.  
  72. class Go : public Property {
  73.     float pass_amount = 200;
  74.     float land_amount = 400;
  75.  
  76. public:
  77.     Go() : Property("Go") { }
  78.  
  79.     void passThrough(Player *x)
  80.     {
  81.         x->add(pass_amount);
  82.     }
  83.  
  84.     void land(Player *x)
  85.     {
  86.         x->add(land_amount);
  87.     }
  88.  
  89.     ~Go() { }
  90. };
  91.  
  92. class Land : public Property {
  93.     float _price;
  94.     Player *_owner = 0;
  95.  
  96. public:
  97.     Land(string nom, float price) : Property(nom)
  98.     {
  99.         if (price < 0)
  100.             throw Exception("Price must be a positive value");
  101.  
  102.         _price = price;
  103.     }
  104.  
  105.     void land(Player *x)
  106.     {
  107.         if (_owner == 0)
  108.         {
  109.             try
  110.             {
  111.               x->sub(_price);
  112.             }
  113.             catch (Exception e)
  114.             {
  115.                 throw e;
  116.             }
  117.  
  118.             _owner = x;
  119.         }
  120.  
  121.         else if (_owner != x)
  122.         {
  123.             try
  124.             {
  125.               x->sub(_price / 10);
  126.             }
  127.             catch (Exception e)
  128.             {
  129.                 throw e;
  130.             }
  131.  
  132.             _owner->add(_price / 10);
  133.         }
  134.  
  135.     }
  136.  
  137. };
  138.  
  139.  
  140. class Monopoly {
  141.     Player* players[6];
  142.     Property* properties[36];
  143.     int position[6];
  144.     int playerCount = 0;
  145.  
  146. public:
  147.     Monopoly()
  148.     {
  149.         for (int i = 0; i < 6; i++)
  150.         {
  151.             players[i] = 0;
  152.             position[i] = 0;
  153.         }
  154.  
  155.         properties[0] = new Go;
  156.  
  157.         stringstream s;
  158.  
  159.         for (int i = 1; i < 36; i++)
  160.         {
  161.             s << "L" << i;
  162.             properties[i] = new Land(s.str(), 25);
  163.             s.clear();
  164.         }
  165.     }
  166.  
  167.     Monopoly(Monopoly const &M)
  168.     {
  169.         for (int i = 0; i < 6; i++)
  170.         {
  171.             players[i] = M.players[i];
  172.             position[i] = M.position[i];
  173.         }
  174.  
  175.         properties[0] = new Go;
  176.  
  177.         for (int i = 1; i < 36; i++)
  178.         {
  179.             properties[i] = M.properties[i];
  180.         }
  181.     }
  182.  
  183.     virtual ~Monopoly()
  184.     {
  185.         for (int i = 0; i < 36; i++)
  186.         {
  187.             delete properties[i];
  188.         }
  189.     }
  190.  
  191.     Monopoly operator=(Monopoly const &M)
  192.     {
  193.         for (int i = 0; i < 6; i++)
  194.         {
  195.             players[i] = M.players[i];
  196.             position[i] = M.position[i];
  197.         }
  198.  
  199.  
  200.         for (int i = 1; i < 36; i++)
  201.         {
  202.             delete properties[i];
  203.             properties[i] = M.properties[i];
  204.         }
  205.     }
  206.  
  207.     void addPlayer(Player *p)
  208.     {
  209.         if (playerCount == 6)
  210.             throw Exception("Maximum number of players reached");
  211.  
  212.         players[playerCount++] = p;
  213.     }
  214.  
  215.     void movePlayer(Player *p, int n)
  216.     {
  217.         int index;
  218.         for (int i = 0; i < playerCount; i++)
  219.         {
  220.             if (players[i] == p)
  221.             {
  222.                 index = i;
  223.                 break;
  224.             }
  225.         }
  226.  
  227.         for (int i = position[index]; i < position[index] + n - 1; i++)
  228.         {
  229.             properties[i % 36] -> passThrough(p);
  230.         }
  231.  
  232.         position[index] = (position[index] + n) % 36;
  233.  
  234.         try
  235.         {
  236.             properties[position[index]] -> land(p);
  237.         }
  238.  
  239.         catch (Exception e)
  240.         {
  241.             if (e.message() == "The amount you're trying to remove is greater than the player's money")
  242.             {
  243.                 cout << "You don't have enough money. You lost" << endl;
  244.  
  245.  
  246.                 for (int i = index; i < playerCount - 1; i++)
  247.                 {
  248.                     players[i] = players[i+1];
  249.                     position[i] = position[i+1];
  250.                 }
  251.  
  252.                 playerCount -= 1;
  253.  
  254.                 players[playerCount] = 0;
  255.                 position[playerCount] = 0;
  256.  
  257.             }
  258.         }
  259.     }
  260.  
  261.     void movePlayer(Player *p, Property *l)
  262.     {
  263.         int indexl;
  264.         for (int i = 0; i < 36; i++)
  265.         {
  266.             if (properties[i] == l)
  267.             {
  268.                 indexl = i;
  269.                 break;
  270.             }
  271.         }
  272.  
  273.         int indexp;
  274.         for (int i = 0; i < playerCount; i++)
  275.         {
  276.             if (players[i] == p)
  277.             {
  278.                 indexp = i;
  279.                 break;
  280.             }
  281.         }
  282.  
  283.         indexp < indexl ? movePlayer(p, indexl - indexp) : movePlayer(p, 36 + indexl - indexp);
  284.     }
  285.  
  286. };
  287.  
  288.  
  289. bool diff(string file1, string file2)
  290. {
  291.     ifstream f1(file1, ios::binary);
  292.     ifstream f2(file2, ios::binary);
  293.  
  294.     if (f1.fail())
  295.         throw Exception("File not found " + file1);
  296.     else if(f2.fail())
  297.         throw Exception("File not found " + file2);
  298.  
  299.     char b1;
  300.     char b2;
  301.  
  302.     while (!f1.eof() && !f1.eof())
  303.     {
  304.         f1.read(&b1, 1);
  305.         f2.read(&b2, 1);
  306.  
  307.         if(b1 != b2)
  308.         {
  309.  
  310.             f1.close();
  311.             f2.close();
  312.  
  313.             return true;
  314.         }
  315.     }
  316.  
  317.     f1.close();
  318.     f2.close();
  319.  
  320.     return false;
  321. }
  322.  
  323. int main()
  324. {
  325.     Monopoly game;
  326.     Player *p1 = new Player("Chris", 2000);
  327.     Player *p2 = new Player("Brian", 100);
  328.  
  329.     game.addPlayer(p1);
  330.     game.addPlayer(p2);
  331.     game.movePlayer(p1, 10);
  332.     game.movePlayer(p2, 46);
  333.     cout << p1->montant() << endl;
  334.     cout << p2->montant() << endl;
  335.  
  336.     delete p1;
  337.     delete p2;
  338.     return 0;
  339.  
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement