home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter09 / game_lobby.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-10-21  |  3.0 KB  |  163 lines

  1. //Game Lobby
  2. //Simulates a game lobby where players wait
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. class Player
  10. {
  11. public:  
  12.     Player(const string& name = "");
  13.     string GetName() const;
  14.     Player* GetNext() const;
  15.     void SetNext(Player* next);
  16.     
  17. private:
  18.     string m_Name;
  19.     Player* m_pNext;  //Pointer to next player in list
  20. };
  21.  
  22. Player::Player(const string& name): 
  23.     m_Name(name), 
  24.     m_pNext(0) 
  25. {}
  26.  
  27. string Player::GetName() const
  28. {
  29.     return m_Name;
  30. }
  31.  
  32. Player* Player::GetNext() const
  33. {
  34.     return m_pNext;
  35. }
  36.  
  37. void Player::SetNext(Player* next)
  38. {
  39.     m_pNext = next;
  40. }
  41.  
  42. class Lobby
  43. {
  44.     friend ostream& operator<<(ostream& os, const Lobby& aLobby);
  45.     
  46. public:
  47.     Lobby();
  48.     ~Lobby();
  49.     void AddPlayer();
  50.     void RemovePlayer();
  51.     void Clear();
  52.     
  53. private:
  54.     Player* m_pHead;  
  55. };
  56.  
  57. Lobby::Lobby():
  58.     m_pHead(0)
  59. {}
  60.  
  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.  
  74.     //if list is empty, make head of list this new player
  75.     if (m_pHead == 0)
  76.     {
  77.         m_pHead = pNewPlayer;
  78.     }
  79.     //otherwise find the end of the list and add the player there
  80.     else
  81.     {
  82.         Player* pIter = m_pHead;
  83.         while (pIter->GetNext() != 0)
  84.         {
  85.             pIter = pIter->GetNext();       
  86.         }
  87.         pIter->SetNext(pNewPlayer);
  88.     }
  89. }
  90.  
  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;
  100.         m_pHead = m_pHead->GetNext();
  101.         delete pTemp;
  102.     }
  103. }
  104.  
  105. void Lobby::Clear()
  106. {
  107.     while (m_pHead != 0)
  108.     {
  109.         RemovePlayer();
  110.     }
  111. }
  112.  
  113. ostream& operator<<(ostream& os, const Lobby& aLobby)
  114. {
  115.     Player* pIter = aLobby.m_pHead;
  116.  
  117.     os << "\nHere's who's in the game lobby:\n";
  118.     if (pIter == 0)
  119.     {
  120.         os << "The lobby is empty.\n";
  121.     }
  122.     else
  123.     {
  124.         while (pIter != 0)
  125.         {   
  126.             os << pIter->GetName() << endl;
  127.             pIter = pIter->GetNext();
  128.         }
  129.     }
  130.  
  131.     return os;
  132. }
  133.  
  134. int main()
  135. {
  136.     Lobby myLobby;
  137.     int choice;
  138.     
  139.     do
  140.     {
  141.         cout << myLobby;
  142.         cout << "\nGAME LOBBY\n";
  143.         cout << "0 - Exit the program.\n";
  144.         cout << "1 - Add a player to the lobby.\n";
  145.         cout << "2 - Remove a player from the lobby.\n";
  146.         cout << "3 - Clear the lobby.\n";
  147.         cout << endl << "Enter choice: ";
  148.         cin >> choice;
  149.  
  150.         switch (choice)
  151.         {
  152.             case 0: cout << "Good-bye.\n"; break;
  153.             case 1: myLobby.AddPlayer(); break;  
  154.             case 2: myLobby.RemovePlayer(); break;
  155.             case 3: myLobby.Clear(); break;
  156.             default: cout << "That was not a valid choice.\n";
  157.         }
  158.     }
  159.     while (choice != 0);
  160.     
  161.     return 0;
  162. }
  163.