Question
C++ Question: In many multiplayer online games, theres a looking for group feature for being automatically matched with other players. Such a feature behaves like
C++ Question:
In many multiplayer online games, theres a looking for group feature for being automatically matched with other players. Such a feature behaves like a queue: players join the queue and removed in complete groups, and players who join the queue first are the first to be grouped. Here youll 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.
The following files have been given to you (Do Not Alter These Files): 1. A C++ header file (lfgqueue.h) declaring the LFGQueue class. 2. A C++ header file (player.h) declaring the Player class. 3. A C++ header file (player.cpp) implementing the Player class. 4. A C++ source file (main.cpp) containing a main() function with tests.
Create new C++ source file named lfgqueue.cpp that implements the classes declared in lfgqueue.h so that lfgqueue.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 with capacity for 100 players LFGQueue();
// Returns the number of players in the queue. int size();
// Adds a (pointer to a) player to the back of the queue. // If there is no room for the new player it doubles the // capacity of the queue before adding the player 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 // no players are removed. void pop_group();
private: Player** players; // Pointer to the queue (dynamic array) int count; // Total quantity of players in the queue int capacity; // Current capacity of the queue };
#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
using namespace std;
inline void _test(const char* expression, const char* file, int line) { cerr
#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.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.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.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.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
// Repeatedly check and remove the frontmost players. // Because of how we scrambled, this has a fixed order. for (int i = 0; i
oss.str(""); oss name() == oss.str());
oss.str(""); oss name() == oss.str());
oss.str(""); oss 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 Player** A capacity count Players araw M Player* array "Berta" Hunter "Hector" Bard "Daria" Defender Player objects Player** A capacity count Players araw M Player* array "Berta" Hunter "Hector" Bard "Daria" Defender Player objects
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started