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

C++ I\'m really lost in trying to complete this code. I need to get a working pr

ID: 3598490 • Letter: C

Question

C++

I'm really lost in trying to complete this code. I need to get a working program, but this stops once the text file displays. Can someone help me finish this program so that the code executes the game.

LCRrules.txt file

Develop a program that follows the rules of Left Center Right (LCR) as described.
On program start-up, it shall display the rules to the user as read from a text file submitted with the program.
The user can then set up the game by entering the number of players. Any number below three shall ask the user to add more players.
Once gameplay has started based on the game rules, there are a few main pieces to address.
Rolling the die should be performed by randomly generating the side of the die displayed for each of the three using a random number generator.
For this game, if the generated number is 1, that will be L. Additionally, 2 is R, 3 is C,
and 4–6 are dots that can be represented with the asterisk symbol *.
Be sure to check the current player’s number of chips before rolling. After each player’s roll,
calculate the number of chips for players based on the actions dictated by the dice.
Continue playing until only one player has chips. Display a message to the game winner.

Left Center Right (LCR) is a multiplayer dice game with a minimum of three players, but no upper limit on the number of participants.

The goal is to win all of the chips.

The Dice
- There are three dice rolled each turn. Each die has the letters L, C, and R on it along with dots on the remaining three sides.
- These dice determine where the player’s chips will go.
- For each L, the player must pass one chip to the player sitting to the left.
- For each R, the player must pass one chip to the player sitting to the right.
- For each C, the player must place one chip into the center pot and those chips are now out of play.
- Dots are neutral and require no action to be taken for that die.

The Chips
- Each player will start with three chips.
- If a player only has one chip, he/she rolls only one die.
- If a player has two chips left, he/she rolls two dice.
- Once a player is out of chips, he/she is still in the game (as he/she may get chips passed to him/her), but passes the dice to the next player.

Winning the Game
- The winner is the last player with chips.


Player.h

#pragma once
#include<iostream>
#include "Dice.h"

class Player
{

public:
int chips;
int numPlayers;
std::string name;

// Player();

void setName();
void setChips();
int checkChips();
static void directions();
};

Dice.h

#pragma once
class Dice
{
private:

int sideRolled;
public:
int rollDice();
};

Player.cpp

#include "stdafx.h"
#include "Player.h"
#include "Dice.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void Player::setName()
{
std::cin >> name;
}
void Player::setChips()
{
Dice obj;
switch (obj.rollDice()) //figure out how to get the dice outcome from RollDice() //CQG - replaced dice with rollDice since you want to know the result
{
case 1: chips -= 1;
  break;//TODO: code to subtract 1 chip from current player, and add 1 chip to player to the left
case 2: chips -= 1; break; //TODO: code to subtract 1 chip from current player
case 3: chips -= 1; break; //TODO: code to subtract 1 chip from current player, and add 1 chip to player to the right
case 4: break; //TODO: do no action
case 5: break; //TODO: do no action
case 6: break; //TODO: do no action
}
}
int Player::checkChips()
{
if (chips == 0)
{
  std::cout << "You don't have any chips. Your turn is skipped." << std::endl;
  //skip turn
  return 0;
}
else return chips;
}
void Player::directions() //display directions to player
{
string fileName = "LCRrules.txt"; //find the desired file
ifstream din(fileName); //create a stream reader object
string str; //System type string to display text in console
if (din.is_open())
{
  while (getline(din, str))
  {
   cout << str << ' ';
  }
  din.close();
}
else cout << "Unable to open file";
}

Dice.cpp

#include "stdafx.h"
#include "Dice.h"
#include <time.h>
#include <iostream>

int Dice::rollDice()
{
sideRolled = (rand() % 6) + 1; //determine the side rolled
return sideRolled;
}

LCRdicegame.cpp

#include "stdafx.h"
#include <iostream>
#include<conio.h>
#include <string>
#include <time.h>
#include "Dice.h"
#include "Player.h"
using namespace std;
int setPlayers() //receive input for number of players, display error message if number is less than 3
{
int numOfPlayers = 0;
cout << "Please enter number of players (3 or more): "<< endl;
cin >> numOfPlayers;
if (numOfPlayers <= 2)
{
  cout << "This game requires 3 or more players."<< endl;
  setPlayers();
}
return numOfPlayers;
}
int main()
{
int currentPlayer = 0;
srand((unsigned)time(NULL)); //CQG-added unsigned cast
const int numPlayers = setPlayers(); //set number of players
static Player* players = new Player[numPlayers]; //set up array to hold player objects
for (int i = 0; i < numPlayers; i++) //loop to set names and chips for each player
{
  cout << "Enter player name: " << endl;
  players[i].setName();
  players[i].chips = 3;
}
Player::directions(); //display game rules to player
_getch;
return 0;
}

Explanation / Answer

#include<iostream>
#include "Dice.h"

class Player
{

public:
int chips;
int numPlayers;
std::string name;

// Player();

void setName();
void setChips();
int checkChips();
static void directions();
};

Dice.h

#pragma once
class Dice
{
private:

int sideRolled;
public:
int rollDice();
};

Player.cpp

#include "stdafx.h"
#include "Player.h"
#include "Dice.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void Player::setName()
{
std::cin >> name;
}
void Player::setChips()
{
Dice obj;
switch (obj.rollDice()) //figure out how to get the dice outcome from RollDice() //CQG - replaced dice with rollDice since you want to know the result
{
case 1: chips -= 1;
break;//TODO: code to subtract 1 chip from current player, and add 1 chip to player to the left
case 2: chips -= 1; break; //TODO: code to subtract 1 chip from current player
case 3: chips -= 1; break; //TODO: code to subtract 1 chip from current player, and add 1 chip to player to the right
case 4: break; //TODO: do no action
case 5: break; //TODO: do no action
case 6: break; //TODO: do no action
}
}
int Player::checkChips()
{
if (chips == 0)
{
std::cout << "You don't have any chips. Your turn is skipped." << std::endl;
//skip turn
return 0;
}
else return chips;
}
void Player::directions() //display directions to player
{
string fileName = "LCRrules.txt"; //find the desired file
ifstream din(fileName); //create a stream reader object
string str; //System type string to display text in console
if (din.is_open())
{
while (getline(din, str))
{
   cout << str << ' ';
}
din.close();
}
else cout << "Unable to open file";
}

Dice.cpp

#include "stdafx.h"
#include "Dice.h"
#include <time.h>
#include <iostream>

int Dice::rollDice()
{
sideRolled = (rand() % 6) + 1; //determine the side rolled
return sideRolled;
}

LCRdicegame.cpp

#include "stdafx.h"
#include <iostream>
#include<conio.h>
#include <string>
#include <time.h>
#include "Dice.h"
#include "Player.h"
using namespace std;
int setPlayers() //receive input for number of players, display error message if number is less than 3
{
int numOfPlayers = 0;
cout << "Please enter number of players (3 or more): "<< endl;
cin >> numOfPlayers;
if (numOfPlayers <= 2)
{
cout << "This game requires 3 or more players."<< endl;
setPlayers();
}
return numOfPlayers;
}
int main()
{
int currentPlayer = 0;
srand((unsigned)time(NULL)); //CQG-added unsigned cast
const int numPlayers = setPlayers(); //set number of players
static Player* players = new Player[numPlayers]; //set up array to hold player objects
for (int i = 0; i < numPlayers; i++) //loop to set names and chips for each player
{
cout << "Enter player name: " << endl;
players[i].setName();
players[i].chips = 3;
}
Player::directions(); //display game rules to player
_getch;
return 0;
}