Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. class Game{  
  2. public:  
  3.     Game( int heaps_, int players_ ){  
  4.         numHeaps = heaps_;  
  5.         numPlayers = players_;  
  6.         heaps = new int[numHeaps];  
  7.         players = new Player*[numPlayers];  
  8.         heapIndex = 0;  
  9.         playerIndex = 0;  
  10.     }  
  11.     ~Game(){  
  12.         delete[] heaps;  
  13.         delete[] players;  
  14.     }  
  15.  
  16.     void addHeap( int coins ) throw( logic_error ){  
  17.         if( heapIndex >= numHeaps ){  
  18.             throw( logic_error("Exceeded heap count") );  
  19.         }  
  20.         if( coins < 0 ){  
  21.             throw( logic_error("Invalid coins") );  
  22.         }  
  23.         heaps[heapIndex] = coins;  
  24.         heapIndex++;  
  25.     }  
  26.     void addPlayer( Player* player ) throw( logic_error ){  
  27.         if( playerIndex >= numPlayers ){  
  28.             throw( logic_error("Exceeded player count") );  
  29.         }  
  30.         players[playerIndex] = player;  
  31.         playerIndex++;  
  32.     }  
  33.     void play( ostream& out ) throw( logic_error ){  
  34.         int turn = 0;  
  35.         State s( numHeaps, heaps );  
  36.         while( !s.winning() ){  
  37.             out << "State: " << s << '\n';  
  38.             out << *players[turn%numPlayers] << " " << players[turn%numPlayers]->play(s) << '\n';  
  39.             s.next(players[turn%numPlayers]->play(s));  
  40.             turn++;  
  41.         }  
  42.         turn--;  
  43.         out << "State: " << s << '\n' << *players[turn%numPlayers] << " wins" << '\n';  
  44.     }  
  45.  
  46. private:  
  47.     int numHeaps;  
  48.     int numPlayers;  
  49.     int* heaps;  
  50.     Player** players;  
  51.     int heapIndex;  
  52.     int playerIndex;  
  53. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement