Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Game{
- public:
- Game( int heaps_, int players_ ){
- numHeaps = heaps_;
- numPlayers = players_;
- heaps = new int[numHeaps];
- players = new Player*[numPlayers];
- heapIndex = 0;
- playerIndex = 0;
- }
- ~Game(){
- delete[] heaps;
- delete[] players;
- }
- void addHeap( int coins ) throw( logic_error ){
- if( heapIndex >= numHeaps ){
- throw( logic_error("Exceeded heap count") );
- }
- if( coins < 0 ){
- throw( logic_error("Invalid coins") );
- }
- heaps[heapIndex] = coins;
- heapIndex++;
- }
- void addPlayer( Player* player ) throw( logic_error ){
- if( playerIndex >= numPlayers ){
- throw( logic_error("Exceeded player count") );
- }
- players[playerIndex] = player;
- playerIndex++;
- }
- void play( ostream& out ) throw( logic_error ){
- int turn = 0;
- State s( numHeaps, heaps );
- while( !s.winning() ){
- out << "State: " << s << '\n';
- out << *players[turn%numPlayers] << " " << players[turn%numPlayers]->play(s) << '\n';
- s.next(players[turn%numPlayers]->play(s));
- turn++;
- }
- turn--;
- out << "State: " << s << '\n' << *players[turn%numPlayers] << " wins" << '\n';
- }
- private:
- int numHeaps;
- int numPlayers;
- int* heaps;
- Player** players;
- int heapIndex;
- int playerIndex;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement