implement a looking-for-group queue for a game where players can be one of three
ID: 3918615 • Letter: I
Question
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)
////////////////////////////////////////////////////////////////////////////////////////////////
// (player.h)
/////////////////////////////////////////////////////////////////////////////////////////////
// (player.cpp)
//////////////////////////////////////////////////////////////////////////////////////////////////
// (main.cpp)
Explanation / Answer
lfgqueue.cpp
--------------------------------------------------------------------------
#include "lfgqueue.h"
#include <iostream>
LFGQueue::LFGQueue() {
count = 0;
capacity = 20;
players = new Player*[capacity];
}
int LFGQueue::size() {
return count;
}
void LFGQueue::push_player(Player* p) {
players[count++] = p;
}
Player* LFGQueue::front_player(Player::Role r) {
for(int i=0; i<count; i++) {
if (players[i]->role() == r)
return players[i];
}
return nullptr;
}
void LFGQueue::pop_player(Player::Role r) {
int removed = false;
for(int i=0; i<count; i++) {
if (players[i]->role() == r || removed) {
players[i] = players[i + 1];
removed = true;
}
}
if(removed)
count--;
}
bool LFGQueue::front_group(Player** group) {
int cnt = 0;
for(int i=0; i<count && i<3; i++) {
if(players[i] == group[i])
cnt++;
if(cnt == 3)
return true;
}
return false;
}
void LFGQueue::pop_group() {
pop_player(Player::Defender);
pop_player(Player::Hunter);
pop_player(Player::Bard);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.