Commit 3f0b4378 authored by Cameron Mahan's avatar Cameron Mahan

Trying to commit changes

parent ea5b5f7e
#include "Lobby.h"
#include "Player.h"
#include <iostream>
using namespace std;
Lobby::Lobby(){
capacity = 6;
Player players[capacity];
size = 0;
rankSum = 0;
}
Lobby::Lobby(unsigned int cap)
{
capacity = cap;
Player players[capacity];
size = 0;
rankSum = 0;
}
int Lobby::getAverage() {
if (size == 0) {
return 0;
}
rankSum = 0;
for (int i = 0; i < size; i++) {
rankSum += players[i].getRank();
}
return rankSum / size;
}
void Lobby::addPlayer(Player p){
cout<<"start add"<<endl;
cout<<"player rank: "<<p.getRank()<<endl;
players[size] = p;
cout<<"increase size"<<endl;
size++;
}
void Lobby::display(){
for (unsigned int i = 0; i < size; i++) {
cout<<"Player "<< i+1<<endl;
players[i].displayName();
cout<<players[i].getRank()<<endl;
}
}
#ifndef LOBBY_H
#define LOBBY_H
#include <iostream>
#include "Player.h"
class Lobby
{
public:
Lobby();
Lobby(unsigned int cap);
int getAverage();
unsigned int getCapacity() { return capacity; }
unsigned int getSize() { return size; }
void addPlayer(Player p);
void display();
private:
unsigned int capacity;
unsigned int size;
unsigned int rankSum;
Player players[];
};
#endif //LOBBY_H
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
Player::Player(const std::string& username, unsigned rank) Player::Player(const std::string& username, int rank)
{ {
_username = username; _username = username;
_rank = rank; _rank = rank;
...@@ -13,7 +13,7 @@ void Player::displayName() ...@@ -13,7 +13,7 @@ void Player::displayName()
cout<<_username<<endl; cout<<_username<<endl;
} }
unsigned Player::getRank() int Player::getRank()
{ {
return _rank; return _rank;
} }
...@@ -8,14 +8,14 @@ ...@@ -8,14 +8,14 @@
class Player class Player
{ {
public: public:
Player(const std::string& username = "CPU", unsigned rank = 1); Player(const std::string& username = "CPU", int rank = 1);
void displayName(); // print name void displayName(); // print name
unsigned getRank(); // returns rank int getRank(); // returns rank
private: private:
//player info //player info
std::string _username; std::string _username;
unsigned _rank; int _rank;
}; };
......
...@@ -2,11 +2,27 @@ ...@@ -2,11 +2,27 @@
using namespace std; using namespace std;
#include "Player.h" #include "Player.h"
#include "Lobby.h"
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
int main() int main()
{ {
//Create lobbies and add to array
unsigned maxLobbies = 4;
unsigned maxPlayers = 6;
Lobby lobbies[maxLobbies];
Lobby l1(maxPlayers);
Lobby l2(maxPlayers);
Lobby l3(maxPlayers);
Lobby l4(maxPlayers);
lobbies[0] = l1;
lobbies[1] = l2;
lobbies[2] = l3;
lobbies[3] = l4;
//open file and read lines //open file and read lines
ifstream myfile("PlayersList.txt"); ifstream myfile("PlayersList.txt");
if(myfile.is_open()) if(myfile.is_open())
...@@ -15,7 +31,7 @@ int main() ...@@ -15,7 +31,7 @@ int main()
{ {
string username; string username;
string rankStr; string rankStr;
unsigned rank; int rank;
//store player data into strings //store player data into strings
getline(myfile, username, '-'); getline(myfile, username, '-');
getline(myfile, rankStr, '\n'); getline(myfile, rankStr, '\n');
...@@ -27,6 +43,27 @@ int main() ...@@ -27,6 +43,27 @@ int main()
p.displayName(); p.displayName();
cout<<p.getRank()<<endl; cout<<p.getRank()<<endl;
//enqueue and repeat with next player //enqueue and repeat with next player
bool sorted = false;
for (unsigned i = 0; i < maxLobbies && sorted == false; i++) {
if (lobbies[i].getAverage() == 0) {
lobbies[i].addPlayer(p);
sorted = true;
}
else if (p.getRank() <= lobbies[i].getAverage() +5 &&
p.getRank() >= lobbies[i].getAverage() -5 ) {
lobbies[i].addPlayer(p);
sorted = true;
}
}
if (sorted == false) {
//Place player in waiting queue
cout<<"Player placed in wait queue"<<endl;
}
//Display lobbies
for (unsigned i = 0; i > maxLobbies && sorted == false; i++) {
cout<<"Displaying Lobby "<<i+1<<":"<<endl;
lobbies[i].display();
}
} }
} }
else cout<<"file cannot open"; else cout<<"file cannot open";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment