Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

implement a looking-for-group queue for a game where players can be one of three roles (Defender, Hunter, and Bard) and each group consists of one

implement a looking-for-group queue for a game where players can be one of three roles (Defender, Hunter, and Bard) and each group consists of one Defender, one Hunter, and one Bard, and create a new C++ source file named lfgqueue.cpp that implements the LFGQueue class declared in lfgqueue.h such that lfqueue.cpp and the provided files compile into a program that runs with no failed tests.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

// (lfgqueue.h)

#ifndef LFGQUEUE_H #define LFGQUEUE_H #include "player.h" class LFGQueue { public: // Constructs a new empty queue LFGQueue(); // Returns the number of players in the queue. int size(); // Pushes a pointer to a player onto the back of the queue. void push_player(Player* p); // Returns a pointer to the frontmost player // with the specified role. // If no such player exists, returns nullptr. Player* front_player(Player::Role r); // Removes the frontmost player with the specified role. // If no such player exists, does nothing. void pop_player(Player::Role r); // Returns whether the queue contains a complete group // (a Defender, a Hunter, and a Bard). // // If the queue contains a complete group, the method // sets the first three elements of the array parameter // equal to the addresses of the frontmost: // 1. Defender (index 0) // 2. Hunter (index 1) // 3. Bard (index 2) bool front_group(Player** group); // Removes the frontmost Defender, Hunter, and Bard // from the queue. If some role has no player with that role, // then does nothing. void pop_group(); private: Player** players; int count; int capacity; }; #endif 

////////////////////////////////////////////////////////////////////////////////////////////////

// (player.h)

#ifndef PLAYER_H #define PLAYER_H #include  using namespace std; class Player { public: // This works like a custom type with just four values. // Outside of Player methods, reference them like: // "if (p->role == Player::Defender)", etc. enum Role {Defender, Hunter, Bard}; // Initializes a player with the given name and role Player(string name, Role role); // Returns the name of the player string name(); // Returns the role of the player Role role(); private: string _name; Role _role; }; #endif

/////////////////////////////////////////////////////////////////////////////////////////////

// (player.cpp)

#include "player.h" Player :: Player(string name, Role role) { _name = name; _role = role; } string Player :: name() { return _name; } Player::Role Player :: role() { return _role; }

//////////////////////////////////////////////////////////////////////////////////////////////////

// (main.cpp)

#include  #include  #include  #include "lfgqueue.h" using namespace std; inline void _test(const char* expression, const char* file, int line) { cerr << "test(" << expression << ") failed in file " << file; cerr << ", line " << line << "." << endl; abort(); } #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__)) int main() { // Variables used for testing Player* group[3]; Player daria("Daria", Player::Defender); Player daniela("Daniela", Player::Defender); Player hector("Hector", Player::Hunter); Player hugo("Hugo", Player::Hunter); Player berta("Berta", Player::Bard); Player bernardo("Bernardo", Player::Bard); // Test size(), add(), front_player(), pop_player() // on a small example. LFGQueue q; test(q.size() == 0); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == nullptr); test(q.front_player(Player::Bard) == nullptr); q.push_player(&daniela); test(q.size() == 1); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == nullptr); test(q.front_player(Player::Bard) == nullptr); q.push_player(&hector); test(q.size() == 2); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == nullptr); q.push_player(&berta); test(q.size() == 3); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &berta); q.push_player(&hugo); test(q.size() == 4); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &berta); q.push_player(&bernardo); test(q.size() == 5); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &berta); q.push_player(&daria); test(q.size() == 6); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &berta); // Order is now [Daniela, Hector, Berta, Hugo, Bernardo, Daria] q.pop_player(Player::Defender); test(q.size() == 5); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &berta); q.pop_player(Player::Hunter); test(q.size() == 4); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); q.pop_player(Player::Bard); test(q.size() == 3); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &bernardo); q.pop_player(Player::Bard); test(q.size() == 2); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == nullptr); q.pop_player(Player::Defender); test(q.size() == 1); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == nullptr); q.pop_player(Player::Hunter); test(q.size() == 0); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == nullptr); test(q.front_player(Player::Bard) == nullptr); // Test previous methods plus front_group(), pop_group() on a small example. q.push_player(&hugo); test(q.size() == 1); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == nullptr); test(!q.front_group(group)); q.pop_group(); test(q.size() == 1); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == nullptr); q.push_player(&hector); test(q.size() == 2); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == nullptr); test(!q.front_group(group)); q.pop_group(); test(q.size() == 2); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == nullptr); q.push_player(&berta); test(q.size() == 3); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); test(!q.front_group(group)); q.pop_group(); test(q.size() == 3); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); q.push_player(&bernardo); test(q.size() == 4); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); test(!q.front_group(group)); q.pop_group(); test(q.size() == 4); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); q.push_player(&daria); test(q.size() == 5); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); test(q.front_group(group)); q.push_player(&daniela); test(q.size() == 6); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); test(q.front_group(group)); // Order is now [Hugo, Hector, Berta, Bernardo, Daria, Daniela] group[0] = group[1] = group[2] = nullptr; test(q.front_group(group)); test(group[0] == &daria); test(group[1] == &hugo); test(group[2] == &berta); q.pop_group(); test(q.size() == 3); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &bernardo); group[0] = group[1] = group[2] = nullptr; test(q.front_group(group)); test(group[0] == &daniela); test(group[1] == &hector); test(group[2] == &bernardo); q.pop_group(); test(q.size() == 0); test(q.front_player(Player::Defender) == nullptr); test(q.front_player(Player::Hunter) == nullptr); test(q.front_player(Player::Bard) == nullptr); // Test a set of 999 players: 333 of each role in the following order: // 333 defenders, 1 hunter, 1 bard, 1 hunter, 1 bard... // Create the players Player** players = new Player*[999]; ostringstream oss; for (int i = 0; i < 333; ++i) { oss.str(""); oss << "Defender #" << i+1; players[i] = new Player(oss.str(), Player::Defender); } for (int i = 333; i < 999; i+=2) { oss.str(""); oss << "Hunter #" << (i-333)/2+1; players[i] = new Player(oss.str(), Player::Hunter); } for (int i = 334; i < 999; i+=2) { oss.str(""); oss << "Bard #" << (i-334)/2+1; players[i] = new Player(oss.str(), Player::Bard); } // Add them to the queue for (int i = 0; i < 999; ++i) q.push_player(players[i]); test(q.size() == 999); // Repeatedly check and remove the frontmost players. // Because of how we scrambled, this has a fixed order. for (int i = 0; i < 333; ++i) { group[0] = group[1] = group[2] = nullptr; test(q.front_group(group)); oss.str(""); oss << "Defender #" << i+1; test(group[0]->name() == oss.str()); oss.str(""); oss << "Hunter #" << i+1; test(group[1]->name() == oss.str()); oss.str(""); oss << "Bard #" << i+1; test(group[2]->name() == oss.str()); q.pop_group(); test(q.size() == 999 - 3 * (i+1)); } test(q.size() == 0); test(!q.front_group(group)); for (int i = 0; i < 999; ++i) delete players[i]; delete[] players; cout << "Assignment complete." << endl; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Eric Redmond ,Jim Wilson

1st Edition

1934356921, 978-1934356920

More Books

Students also viewed these Databases questions

Question

1. Define mass and mediated communication

Answered: 1 week ago