Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q. Replace any vectors with linked lists. here is my code that need modification

ID: 3807458 • Letter: Q

Question

Q. Replace any vectors with linked lists.

here is my code that need modification..

Player.h

#pragma once
#include <string>
using namespace std;

class Player
{
public:
   //constructors
   Player();
   Player(const string& new_name);

   //accessors
   string name() const;
   int wins() const;
   int sum() const;

   //mutators
   void name(const string& new_name);

//custom methods
   void recordWin();
   void addFace(int face);
private:
   string _name;
   int _wins;
   int _sum;
};

Player.cpp

#include <cassert>
#include "Player.h"

//constructors
Player::Player()
{
_name = "Nemo";
_wins = 0;
_sum = 0;
}

Player::Player(const string& new_name)
{
   _name = "Nemo";
   name(new_name);
   _wins = 0;
   _sum = 0;
}

//accessors
string Player::name() const
{
   return _name;
}
int Player::wins() const
{
   return _wins;
}
int Player::sum() const
{
   return _sum;
}

//mutators
void Player::name(const string& new_name)
{
   //trim leading and trailing spaces
   unsigned left = 0;
   while (left < new_name.length() && isspace(new_name[left]))
       left++;
   unsigned right = new_name.length();
   while (right > 0 && isspace(new_name[right - 1]))
       right--;
   //change _name only if new_name is a good name
   if (left <= right)
       _name = new_name.substr(left, right - left + 1);
}

// custom methods
void Player::recordWin()
{
   _wins++;
}
void Player::addFace(int face)
{
   assert(1 <= face && face <= 6);
   _sum += face;
}

source.cpp

#include <cassert>
#include <ctime>
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include "Player.h"

using namespace std;

int compSimpleWinner(vector<int> values, vector<int>& winners)
{
   winners.clear();
   int winnerIndex = 0;
   winners.push_back(winnerIndex);
   for (unsigned index = 1; index < values.size(); index++)
       if (values[index] > values[winnerIndex])
       {
           winners.clear();
           winnerIndex = index;
           winners.push_back(winnerIndex);
       }
       else if (values[index] == values[winnerIndex])
           winners.push_back(index);
   if (winners.size() == values.size())
   {
       winners.clear();
       return 0;
   }
   else
       return winners.size();
}
int compTossWinner(vector<int> faces, vector<int>& winners)
{
   return compSimpleWinner(faces, winners);
}
int compGameWinner(vector<int> winCounts, vector<int> sums, vector<int>& winners)
{
   int nwinners = compSimpleWinner(winCounts, winners);
   if (nwinners == 0) nwinners = compSimpleWinner(sums, winners);
   return nwinners;
}
int main()
{
   cout << "Please enter the number of players: ";
   int nPlayers;
   cin >> nPlayers;
   assert(cin.good() && nPlayers > 1);

   vector<Player> player(nPlayers);
   for (unsigned playerId = 0; playerId < player.size(); playerId++)
       player[playerId].name("p" + to_string(playerId));

   default_random_engine e(static_cast<unsigned>(time(nullptr)));
   uniform_int_distribution<int> u(1, 6);

   cout << "Please enter the number of tosses: ";
   int ntosses;
   cin >> ntosses;
   assert(cin.good() && ntosses > 0);

   vector<int> faces(nPlayers);
   vector<int> winners;
   for(int toss = 1; toss <= ntosses; toss++)
   {
       for (int playerId = 0; playerId < nPlayers; playerId++)
       {
           faces[playerId] = u(e);
           cout << player[playerId].name() << " tosses " << faces[playerId] << endl;
           player[playerId].addFace(faces[playerId]);
       }
       //who wins the this toss?
       if (compTossWinner(faces, winners) > 0)
       {
           for (unsigned winnerId = 0; winnerId < winners.size(); winnerId++)
           {
               player[winners[winnerId]].recordWin();
               cout << player[winners[winnerId]].name() << " wins toss " << toss << endl;
           }
       }
       else
           cout << "Toss " << toss << " is tied" << endl;
       cout << endl;
   }
   faces.clear();
  
   vector<int> wins(nPlayers);
   vector<int> sums(nPlayers);
   for (int playerId = 0; playerId < nPlayers; playerId++)
   {
       wins[playerId] = player[playerId].wins();
       sums[playerId] = player[playerId].sum();
   }
   //who wins the game?
   if (compGameWinner(wins, sums, winners) > 0)
   {
       for (unsigned winnerId = 0; winnerId < winners.size(); winnerId++)
           cout << player[winners[winnerId]].name() << " wins the game" << endl;
       winners.clear();
   }
   else      
       cout << "The game is tied" << endl;

   wins.clear();
   sums.clear();

   system("pause");
   return 0;
}

Explanation / Answer

#include <cassert>
#include <ctime>
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include "Player.h"

using namespace std;

int compSimpleWinner(vector<int> values, vector<int>& winners)
{
   winners.clear();
   int winnerIndex = 0;
   winners.push_back(winnerIndex);
   for (unsigned index = 1; index < values.size(); index++)
       if (values[index] > values[winnerIndex])
       {
           winners.clear();
           winnerIndex = index;
           winners.push_back(winnerIndex);
       }
       else if (values[index] == values[winnerIndex])
           winners.push_back(index);
   if (winners.size() == values.size())
   {
       winners.clear();
       return 0;
   }
   else
       return winners.size();
}
int compTossWinner(vector<int> faces, vector<int>& winners)
{
   return compSimpleWinner(faces, winners);
}
int compGameWinner(vector<int> winCounts, vector<int> sums, vector<int>& winners)
{
   int nwinners = compSimpleWinner(winCounts, winners);
   if (nwinners == 0) nwinners = compSimpleWinner(sums, winners);
   return nwinners;
}
int main()
{
   cout << "Please enter the number of players: ";
   int nPlayers;
   cin >> nPlayers;
   assert(cin.good() && nPlayers > 1);

   vector<Player> player(nPlayers);
   for (unsigned playerId = 0; playerId < player.size(); playerId++)
       player[playerId].name("p" + to_string(playerId));

   default_random_engine e(static_cast<unsigned>(time(nullptr)));
   uniform_int_distribution<int> u(1, 6);

   cout << "Please enter the number of tosses: ";
   int ntosses;
   cin >> ntosses;
   assert(cin.good() && ntosses > 0);

   vector<int> faces(nPlayers);
   vector<int> winners;
   for(int toss = 1; toss <= ntosses; toss++)
   {
       for (int playerId = 0; playerId < nPlayers; playerId++)
       {
           faces[playerId] = u(e);
           cout << player[playerId].name() << " tosses " << faces[playerId] << endl;
           player[playerId].addFace(faces[playerId]);
       }
       //who wins the this toss?
       if (compTossWinner(faces, winners) > 0)
       {
           for (unsigned winnerId = 0; winnerId < winners.size(); winnerId++)
           {
               player[winners[winnerId]].recordWin();
               cout << player[winners[winnerId]].name() << " wins toss " << toss << endl;
           }
       }
       else
           cout << "Toss " << toss << " is tied" << endl;
       cout << endl;
   }
   faces.clear();
  
   vector<int> wins(nPlayers);
   vector<int> sums(nPlayers);
   for (int playerId = 0; playerId < nPlayers; playerId++)
   {
       wins[playerId] = player[playerId].wins();
       sums[playerId] = player[playerId].sum();
   }
   //who wins the game?
   if (compGameWinner(wins, sums, winners) > 0)
   {
       for (unsigned winnerId = 0; winnerId < winners.size(); winnerId++)
           cout << player[winners[winnerId]].name() << " wins the game" << endl;
       winners.clear();
   }
   else      
       cout << "The game is tied" << endl;

   wins.clear();
   sums.clear();

   system("pause");
   return 0;
}