Hello,I am trying to design a very basic bowling design game ,practicing it from the scratch for machine coding round
Can you please let me know ,when I am trying to getscores of P1,P2,P3 it is returning 0 but it is giving correct output of declareWinner() function
As far as I can understand,I am probably creating a copy of my object and it is changing that which is not being reflected in the original variable
Any help would be really great
Also any additional inputs or suggestions to better the design will be equally welcomed
Thankyou
enum GameStatus{NotStarted,InProgress,Completed};
enum AlleyStatus{vacant,occupied};
static int NoOfPlayers=0,NoOfGames=0;
class Player
{
public:
Player();
Player(string name);
string Getname() { return name; }
int Getscore() { return score; }
void Setscore(int val) { score = val; }
int GetPlayerID() { return PlayerID; }
void SetPlayerID(int val) { PlayerID = val; }
int *Getrolls() { return rolls; }
bool operator==(Player Player);
void updateScore(int new_score);
protected:
private:
string name;
int score;
int PlayerID;
int *rolls=new int[21];
};class GameSession
{
public:
GameSession() {}
GameSession(int AlleyNo);
vector<Player> GetPlayersList() { return PlayersList; }
void SetPlayersList(vector<Player> val) { PlayersList = val; }
int GetGameID() { return GameID; }
void SetGameID(int val) { GameID = val; }
int GetAlleyNo() { return AlleyNo; }
void SetAlleyNo(int val) { AlleyNo = val; }
int declareWinner();
void addPlayer(Player p1);
void Roll(Player P1,int Hit);
protected:
private:
vector<Player> PlayersList;
int GameID;
int AlleyNo;
GameStatus GameStatus;
AlleyStatus AlleyStatus;
};class Game
{
public:
unordered_map<int,GameSession> GetGameSessions() { return GameSessions; }
void SetGameSessions(unordered_map<int,GameSession> val) { GameSessions = val; }
vector<int> Getalleys() { return alleys; }
void Setalleys(vector<int> val) { alleys = val; }
protected:
private:
unordered_map<int,GameSession> GameSessions;
vector<int> alleys;
}; Player::Player(){
this->name="";
this->PlayerID=NoOfPlayers;
NoOfPlayers++;
this->score=0;
}
Player::Player(string name){
this->name=name;
this->PlayerID=NoOfPlayers;
NoOfPlayers++;
this->score=0;
}
GameSession::GameSession(int AlleyNo){
this->GameID=NoOfGames++;
this->AlleyNo=AlleyNo;
this->GameStatus=InProgress;
this->AlleyStatus=occupied;
}
int GameSession::declareWinner()
{
vector <Player> templist=GetPlayersList();
int maxscore=templist[0].Getscore();
int winnerId=templist[0].GetPlayerID();
for(auto i:templist){
//maxscore=max(maxscore,);
int currPlayerScore=i.Getscore();
if(maxscore<currPlayerScore){
maxscore=currPlayerScore;
winnerId=i.GetPlayerID();
}
}
cout<<"maxscore is"<<maxscore<<endl;
return winnerId;
}
inline bool Player:: operator==(Player Player){
return this->GetPlayerID()==Player.GetPlayerID();
}
void GameSession::addPlayer(Player p1)
{
PlayersList.push_back(p1);
}
void Player::updateScore(int new_score)
{
this->score=new_score;
}
void GameSession::Roll(Player P1, int Hit)
{
vector <Player> templist=GetPlayersList();
vector<Player> ans;
for(auto i:templist){
if(i==P1){
P1.updateScore(i.Getscore()+Hit);
ans.push_back(P1);
}
else{
ans.push_back(i);
}
}
SetPlayersList(ans);
}int main()
{
Player P1("Alan");
Player P2("Charlie");
Player P3("Jake");
GameSession GS1(1);
GS1.addPlayer(P1);
GS1.addPlayer(P2);
GS1.addPlayer(P3);
GS1.Roll(P1,3);
cout<<P1.Getscore()<<endl;
cout<<P2.Getscore()<<endl;
cout<<P3.Getscore()<<endl;
GS1.Roll(P2,1);
GS1.Roll(P3,9);
GS1.Roll(P3,19);
cout<<GS1.declareWinner();
return 0;
}