Caminhoneiro

Lobby

Apr 22nd, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. #include "stdafx.h"
  2. //Game Lobby
  3. //Simulates a game lobby where players wait
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7. class Player
  8. {
  9.     public:
  10.         Player(const string& name = "");
  11.         string GetName() const;
  12.         Player* GetNext() const;
  13.         void SetNext(Player* next);
  14.     private:
  15.         string m_Name;
  16.         Player* m_pNext; //Pointer to next player in list
  17. };
  18.  
  19. //The Lobby Class
  20. class Lobby
  21. {
  22.     friend ostream& operator<<(ostream& os, const Lobby& aLobby);   //I declare the function operator<<() a friend of Lobby so that I can send a Lobby object to cout using the << operator.
  23.     public:
  24.         Lobby();
  25.         ~Lobby();
  26.         void AddPlayer(); //nstantiates a Player object on the heap and adds it to the end of the list
  27.         void RemovePlayer(); //removes the first Player object in the list, freeing the allocated memory.
  28.         void Clear();
  29.     private:
  30.         Player* m_pHead;//pointer that points to a Player object, which represents the first person in line.m_pHead represents the head of the line
  31. };
  32.  
  33. //Player constructors:
  34. Player::Player(const string& name) :
  35.     m_Name(name),
  36.     m_pNext(0)
  37. {}
  38.  
  39. string Player::GetName() const
  40. {
  41.     return m_Name;
  42. }
  43.  
  44. Player* Player::GetNext() const
  45. {
  46.     return m_pNext;
  47. }
  48.  
  49. void Player::SetNext(Player* next)
  50. {
  51.     m_pNext = next;
  52. }
  53.  
  54.  
  55. //Lobby constructor:    //simply initializes the data member m_pHead to 0, making it a null pointer
  56. Lobby::Lobby() :
  57.     m_pHead(0)
  58. {}
  59.  
  60. //Lobby destructor
  61. Lobby::~Lobby()
  62. {
  63.     Clear();
  64. }
  65.  
  66. void Lobby::AddPlayer()
  67. {
  68.     //create a new player node
  69.     cout << "Please enter the name of the new player: ";
  70.     string name;
  71.     cin >> name;
  72.     Player* pNewPlayer = new Player(name);
  73.     //if list is empty, make head of list this new player
  74.     if (m_pHead == 0)
  75.     {
  76.         m_pHead = pNewPlayer;
  77.     }
  78.     //otherwise find the end of the list and add the player there
  79.     else
  80.     {
  81.         Player* pIter = m_pHead;
  82.         while (pIter->GetNext() != 0)
  83.         {
  84.             pIter = pIter->GetNext();
  85.         }
  86.         pIter->SetNext(pNewPlayer);
  87.     }
  88. }
  89.  
  90. //member function removes the player at the head of the line.
  91. void Lobby::RemovePlayer()
  92. {
  93.     if (m_pHead == 0)
  94.     {
  95.         cout << "The game lobby is empty. No one to remove!\n";
  96.     }
  97.     else
  98.     {
  99.         Player* pTemp = m_pHead; //Create a pointer and point to the first player of the list
  100.         m_pHead = m_pHead->GetNext(); //Sets m_pHead to do the next thing in the list either the next plater obj or 0
  101.         delete pTemp;
  102.     }
  103. }
  104.  
  105. //removes all of the players from the lobby
  106. void Lobby::Clear()
  107. {
  108.     while (m_pHead != 0)
  109.     {
  110.         RemovePlayer();
  111.     }
  112. }
  113.  
  114. //The operator<<() member function overloads the << operator so I can
  115. //display a Lobby object by sending it to cout
  116. ostream& operator<<(ostream& os, const Lobby& aLobby)
  117. {
  118.     Player* pIter = aLobby.m_pHead;
  119.     os << "\nHere’s who’s in the game lobby:\n";
  120.     if (pIter == 0)
  121.     {
  122.         os << "The lobby is empty.\n";
  123.     }
  124.     else
  125.     {
  126.         while (pIter != 0)
  127.         {
  128.             os << pIter -> GetName() << endl;
  129.             pIter = pIter -> GetNext();
  130.         }
  131.     }
  132.     return os;
  133. }
  134. //If the lobby is empty, the appropriate message is sent to the output stream.
  135. //Otherwise, the function cycles through all of the players in the list, sending
  136. //their names to the output stream, using pIter to move through the list.*/
  137.  
  138. int main()
  139. {
  140.     Lobby myLobby;
  141.     int choice;
  142.     do
  143.     {
  144.         cout << myLobby;
  145.         cout << "\nGAME LOBBY\n";
  146.         cout << "0 - Exit the program.\n";
  147.         cout << "1 - Add a player to the lobby.\n";
  148.         cout << "2 - Remove a player from the lobby.\n";
  149.         cout << "3 - Clear the lobby.\n";
  150.         cout << endl << "Enter choice: ";
  151.         cin >> choice;
  152.         switch (choice)
  153.         {
  154.         case 0: cout << "Good-bye.\n"; break;
  155.         case 1: myLobby.AddPlayer(); break;
  156.         case 2: myLobby.RemovePlayer(); break;
  157.         case 3: myLobby.Clear(); break;
  158.         default: cout << "That was not a valid choice.\n";
  159.         }
  160.     } while (choice != 0);
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment