Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- //Game Lobby
- //Simulates a game lobby where players wait
- #include <iostream>
- #include <string>
- using namespace std;
- class Player
- {
- public:
- Player(const string& name = "");
- string GetName() const;
- Player* GetNext() const;
- void SetNext(Player* next);
- private:
- string m_Name;
- Player* m_pNext; //Pointer to next player in list
- };
- //The Lobby Class
- class Lobby
- {
- 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.
- public:
- Lobby();
- ~Lobby();
- void AddPlayer(); //nstantiates a Player object on the heap and adds it to the end of the list
- void RemovePlayer(); //removes the first Player object in the list, freeing the allocated memory.
- void Clear();
- private:
- 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
- };
- //Player constructors:
- Player::Player(const string& name) :
- m_Name(name),
- m_pNext(0)
- {}
- string Player::GetName() const
- {
- return m_Name;
- }
- Player* Player::GetNext() const
- {
- return m_pNext;
- }
- void Player::SetNext(Player* next)
- {
- m_pNext = next;
- }
- //Lobby constructor: //simply initializes the data member m_pHead to 0, making it a null pointer
- Lobby::Lobby() :
- m_pHead(0)
- {}
- //Lobby destructor
- Lobby::~Lobby()
- {
- Clear();
- }
- void Lobby::AddPlayer()
- {
- //create a new player node
- cout << "Please enter the name of the new player: ";
- string name;
- cin >> name;
- Player* pNewPlayer = new Player(name);
- //if list is empty, make head of list this new player
- if (m_pHead == 0)
- {
- m_pHead = pNewPlayer;
- }
- //otherwise find the end of the list and add the player there
- else
- {
- Player* pIter = m_pHead;
- while (pIter->GetNext() != 0)
- {
- pIter = pIter->GetNext();
- }
- pIter->SetNext(pNewPlayer);
- }
- }
- //member function removes the player at the head of the line.
- void Lobby::RemovePlayer()
- {
- if (m_pHead == 0)
- {
- cout << "The game lobby is empty. No one to remove!\n";
- }
- else
- {
- Player* pTemp = m_pHead; //Create a pointer and point to the first player of the list
- m_pHead = m_pHead->GetNext(); //Sets m_pHead to do the next thing in the list either the next plater obj or 0
- delete pTemp;
- }
- }
- //removes all of the players from the lobby
- void Lobby::Clear()
- {
- while (m_pHead != 0)
- {
- RemovePlayer();
- }
- }
- //The operator<<() member function overloads the << operator so I can
- //display a Lobby object by sending it to cout
- ostream& operator<<(ostream& os, const Lobby& aLobby)
- {
- Player* pIter = aLobby.m_pHead;
- os << "\nHere’s who’s in the game lobby:\n";
- if (pIter == 0)
- {
- os << "The lobby is empty.\n";
- }
- else
- {
- while (pIter != 0)
- {
- os << pIter -> GetName() << endl;
- pIter = pIter -> GetNext();
- }
- }
- return os;
- }
- //If the lobby is empty, the appropriate message is sent to the output stream.
- //Otherwise, the function cycles through all of the players in the list, sending
- //their names to the output stream, using pIter to move through the list.*/
- int main()
- {
- Lobby myLobby;
- int choice;
- do
- {
- cout << myLobby;
- cout << "\nGAME LOBBY\n";
- cout << "0 - Exit the program.\n";
- cout << "1 - Add a player to the lobby.\n";
- cout << "2 - Remove a player from the lobby.\n";
- cout << "3 - Clear the lobby.\n";
- cout << endl << "Enter choice: ";
- cin >> choice;
- switch (choice)
- {
- case 0: cout << "Good-bye.\n"; break;
- case 1: myLobby.AddPlayer(); break;
- case 2: myLobby.RemovePlayer(); break;
- case 3: myLobby.Clear(); break;
- default: cout << "That was not a valid choice.\n";
- }
- } while (choice != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment