I am working on a problem for which i have already developed a solution, but i am new to OPP prgramming and started learning recently.
Would request OPP Gurus to provide suggestions for the better designs, keeping scalability, extendability of (course readability) in mind.
Also,I would like to extend the the current program for mutiple random players and winner is decided based on max number of votes.
But there are rules:
1)Currently i have 6 players, out of which any number of players might contest(taken from std::input or cmd args)
2)Contestants are not aware of other contestants.
3)Each contestant has to send a message to other players,other players will give support to the contestant only if the sent message has that particular player symbol in it.( as done in findSymbol function).
4)Contestants are not allowed to give support , and other players will give support on FCFS basis.
5)Currently i have six players,if 3 pplayers wants to contest then three players will send 5 messages to other players, so 3*5 = 15 messages total.
6)There will a umpire who picks 6 messages(because currently only 6 players) and gives to all players based on message(because message has player name in it, can be found in createSecretMessage)
7)In case of two/more players get same votes then contest should be held for those players, till one gets majority or no one gets majority.
As message is choosen randomly there is a possibility that no one gets majority,then there wont be any winner, thats possible.
Its a mid size prgram , request to bear with me and provide your valueable suggestions, thanks in advance..
The below program does above steps but for only one player, so there wont be any ties etc..
#ifndef __PLAYER__
#define __PLAYER__
#include <string>
#include <iostream>
class Winner;
class Player{
private:
std::string placeOfPlayer;
std::string symbolName;
std::string nameOfPlayer;
std::string WSI;//Winner,Supporter,Idle
public:
std::string getPlaceOfPlayer() const;
std::string getSymbolOfPlayer() const;
std::string getNameOfPlayer() const;
std::string getStatusOfPlayer() const;
void setPlaceOfPlayer(const std::string&);
void setSymbolOfPlayer(const std::string&);
void setNameOfPlayer(const std::string&);
void setStatusOfPlayer(const std::string&);
bool operator <(const Player&) const;
Player();
~Player();
Player(std::string place,std::string symbol,std::string name,std::string state);
friend std::ostream & operator << (std::ostream &out, const Player &p);
};
#endif
#include <iostream>
#include <map>
#include <vector>
#include "winner.h"
#include "player.h"
Player::Player()
{
// std::cout<<"Default Ctor invoked"<<std::endl;
// numOfPlayers++;
}
Player::Player(std::string place,std::string symbol,std::string name,std::string state):
placeOfPlayer(place),symbolName(symbol),nameOfPlayer(name),WSI(state)
{
// std::cout<<"Param Ctor invoked"<<std::endl;
// numOfPlayers++;
}
Player::~Player()
{
//std::cout<<"Dtor invoked for "<<nameOfPlayer<<numOfPlayers<<std::endl;
// numOfPlayers--;
}
void Display_Mapper(std::map<std::string,Player> Mapper)
{
std::cout<<"Mapper"<<std::endl;
for(auto &m:Mapper) {
std::cout<<"{"<<m.first<<","<<m.second<<"},"<<std::endl;
}
}
void Display_Player_Vector(std::vector<Player> aPlayer)
{
std::cout<<"aPlayer vecor"<<std::endl;
for(auto &k:aPlayer) {
std::cout<<k<<std::endl;
}
}
std::ostream &operator << (std::ostream &out,const Player &p)
{
std::cout <<p.placeOfPlayer<<" "<<p.symbolName<<" "<<p.nameOfPlayer<<" "<<p.WSI;
}
std::string Player::getNameOfPlayer() const {
return this->nameOfPlayer;
}
std::string Player:: getSymbolOfPlayer() const {
return this->symbolName;
}
std::string Player::getStatusOfPlayer() const{
return this->WSI;
}
std::string Player::getPlaceOfPlayer() const{
return this->placeOfPlayer;
}
void Player::setStatusOfPlayer(const std::string& status){
this->WSI = status;
}
bool Player:: operator <(const Player& g) const{
return (g.nameOfPlayer > this->nameOfPlayer);
}
#include "player.h"
#include <string>
#include <map>
#include <vector>
class Player;
class Winner{
private:
std::string playername;
int numOfvotes;
public:
void createSecretMessage(std::vector<Player>);
void StoreMessage(std::string msg,Player receiver);
void DisplayStoredMessages();
void ProcessMessageForVotes();
int checkSymbolIntheMessage(std::string msg,std::string symbol);
int check_for_symbol( int base_message_count[], std::string symbol);
int check_is_vote_ok(std::string msg,std::string symbol);
void GetWinner();
void GetSupporters();
bool findSymbol( std::string string_to_be_made, std::string given_chars );
Winner();
~Winner();
};
#include "player.h"
#include <string>
#include <map>
#include <vector>
class Player;
#include "msgs.h"
#include "winner.h"
#include <map>
#include <cctype>
#include <cstring>
#include <limits>
std::map<Player,std::string> Game;
// Map key is always constant, so modifying the values is not possible.
Winner::Winner()
{
playername = "MrC";
numOfvotes = 0;
}
Winner::~Winner(){
}
int getARandomNumber()
{
//std::cout<<"N Messages:"<<Messages.size();
return ( rand() % Messages.size());
}
void Winner::createSecretMessage(std::vector<Player> player){
for(auto &p:player){
if(p.getPlaceOfPlayer() != "CCC") {
std::string msg = p.getPlaceOfPlayer() +","+Messages.at(getARandomNumber());
StoreMessage(msg,p);
//std::cout<<msg<<std::endl;
}
}
}
void Winner::StoreMessage(std::string msg,Player player)
{
Game.insert(std::make_pair(player,msg));
}
void Winner::DisplayStoredMessages()
{
std::cout<<"stored messages"<<std::endl;
for(auto &m:Game)
std::cout<<"{"<<m.first<<" "<<m.second<<"}"<<std::endl;
std::cout<<"numOfvotes:"<<numOfvotes<<std::endl;
}
void Winner::ProcessMessageForVotes(){
bool retVal = false;
std::string Ally("S");
decltype(Game) m;
for (std::pair<Player, std::string> p: Game) {
if(p.first.getStatusOfPlayer() == "C")
std::cout<<"Receiver is already a contender"<<std::endl;
else if(p.first.getStatusOfPlayer() == "A")
std::cout<<"Receiver is already an Ally"<<std::endl;
else {
retVal = checkSymbolIntheMessage(p.second,p.first.getSymbolOfPlayer());
if(retVal == true){
p.first.setStatusOfPlayer(Ally);
m.insert(p);
numOfvotes++;
}else{
m.insert(p);
}
}
}
Game = m;
}
int Winner ::checkSymbolIntheMessage(std::string msg,std::string symbol)
{
std::string message {0};
unsigned int i{0};
for(char c:msg){
if (c == ',')
break;
i++;
}
message = msg.substr(i+1,msg.size()-(i+1));
// std::cout<<"mess ="<<message<<std::endl;
if(findSymbol(symbol,msg) == true)
{
return true;
}
return false;
}
bool Winner :: findSymbol( std::string symbol, std::string message )
{
constexpr std::size_t MAX_DISTINCT_CHAR_VALUES = std::numeric_limits<unsigned char>::max() + 1 ;
std::size_t freq_count[MAX_DISTINCT_CHAR_VALUES] = {0} ; // initialise to all zeroes
for( unsigned char u : message )
++freq_count[tolower(u)] ;
for( unsigned char u : symbol ) // for each character
{
if( freq_count[tolower(u)] > 0 )
--freq_count[tolower(u)] ;
else
return false ;
}
return true ;
}
void Winner::GetWinner()
{
if(numOfvotes >= 2)
std::cout<<"Winner "<<playername<<std::endl;
else
std::cout<<"none"<<std::endl;
}
void Winner::GetSupporters()
{
if(numOfvotes >= 2) {
for(auto &m:Game) {
if(m.first.getStatusOfPlayer() == "S")
std::cout<<m.first.getNameOfPlayer()<<std::endl;
}
}else{
std::cout<<"None"<<std::endl;
}
}
class Winner{
private:
std::string playername;
int numOfvotes;
public:
void createSecretMessage(std::vector<Player>);
void StoreMessage(std::string msg,Player receiver);
void DisplayStoredMessages();
void ProcessMessageForVotes();
int checkSymbolIntheMessage(std::string msg,std::string symbol);
int check_for_symbol( int base_message_count[], std::string symbol);
int check_is_vote_ok(std::string msg,std::string symbol);
void GetWinner();
void GetSupporters();
bool findSymbol( std::string string_to_be_made, std::string given_chars );
Winner();
~Winner();
};
msgs.h
#include<vector>
#include<string>
std::vector<std::string> Messages = {
"DuraineggfruitfrakelBerrydefghijaklbananacherryapplelbbnopqrstunvwoxyz",
"DuraineggfruitfrakelBerrydefghijaklbananacherryapplelbbnopqrstunvwoxyz",
"DuraineggfruitfrakelBerrydefghijaklbananacherryapplelbbnopqrstunvwoxyz",
"abnbobacdefghijklbbpoqrstuvwxyz",
"DuraineggfruitfrakelBerryabnopdefghijklbbqorstuvwxyz",
"cdefghijklbbbnabaopoqrstuvwxyz",
"cdefgDuraineggfruitfrakelBerryadefghijklbbbnopqorstuvwxyzhijklbbbnl",
"efgDuraineggfruitfrakelBerrytoctbpadefgushijkoclpsubbLetjkadolwrl",
"DuraineggfruitfrakelBerrydabefghijklbbnopqrstouvwxyz",
"DuraineggfruitfrakelBerrydeafghijklbnobabpqrstuovwoxyz:",
"GoDuraineggfruitfrakelBerrydefghijklabbbnopqrsltuvwoxyz.",
"DuraineggfruitfrakelBerrydefghijklbbabnoyopqrstuvwxz.",
"DuraineggfruitfrakelBerrydefghijklbbbanopqrstuvwxyzo.",
"fghijklbbboocpalndaaatusspaireowl",
"fghijklbbboocpstluandaaapaireowl!",
"DuraineggfruitfrakelBerrydefghijklbbblnaopqrstuvwxyz.o",
"defghijkolbbbalbcnoapqrstuvwxyzes.",
"DuraineggfruitfrakelBerrydefghijkollbbbnoapqrstuvwxyz.",
"DuraineggfruitfrakelBerrydefghijkolbbblnoapqrstuvwxyz",
};
#include "Player.h"
#include "Winner.h"
#include <time.h>
std::vector<Player> ListOfPlayer;
int main()
{
srand(time(0));
Player AAA("AAA","Apple","MrA","I");
Player BBB("BBB","Banana","MrB","I");
Player CCC("CCC","Cherry","MrC","I");
Player DDD("DDD","Durain","MrD","I");
Player EEE("EEE","Eggfruit","MrE","I");
Player FFF("FFF","FarkelBerry","MrF","I");
ListOfPlayer.push_back(AAA);
ListOfPlayer.push_back(BBB);
ListOfPlayer.push_back(CCC);
ListOfPlayer.push_back(DDD);
ListOfPlayer.push_back(EEE);
ListOfPlayer.push_back(FFF);
Winner Gamer;
Gamer.createSecretMessage(ListOfPlayer);
Gamer.DisplayStoredMessages();
Gamer.ProcessMessageForVotes();
Gamer.DisplayStoredMessages();
std::cout<<std::endl;
std::cout<<"Winner of Game?"<<std::endl;
Gamer.GetWinner();
std::cout<<std::endl;
std::cout<<"Supporters of Player?"<<std::endl;
Gamer.GetSupporters();
return 0;
}