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

Can someone help me with this C++ LCR game My problem(s): I haven\'t been able t

ID: 3591063 • Letter: C

Question

Can someone help me with this C++ LCR game

My problem(s): I haven't been able to figure out how to set up my setChips()and checkChips()functions. The main problem seems to be with having multiple player objects and needing to manipulate more than one at a time (ie take one chip from one player and give it to another player.) I haven't completely addressed all problems yet such as getting the rollDice() function to roll as many dice as are needed.

·       Player Header File:-

#pragma once

#include "stdafx.h"

#include <iostream>

#include "DiceClass.h"

class Player

{

public:

        int chips;

        int numPlayers;

        std::string name = "";

        Player() = default;

        void setName();

        void setChips();

        int checkChips();

        static void directions();

};



Dice Header File

1

2

3

4

5

6

7

8

9

10

11

#pragma once

#include "stdafx.h"

class Dice

{

        private:

                static int sideRolled;

        public:

                static int rollDice();

};



Player .cpp file

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

#include "stdafx.h"

#include "PlayerClass.h"

#include "DiceClass.h"

#include <iostream>

#include <string>

#include <fstream>

using namespace System;

using namespace System::IO;

void Player::setName()

{

        std::cin >> name;

}

void Player::setChips()

{

        switch (Dice::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: break;//TODO: code to subtract 1 chip from current player, and add 1 chip to player to the left

        case 2: break; //TODO: code to subtract 1 chip from current player

        case 3: 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 = "LCR_Rules.txt"; //find the desired file

        StreamReader^ din = File::OpenText(fileName); //create a stream reader object

        String^ str; //System type string to display text in console

        while ((str = din->ReadLine()) != nullptr) //loop while there is still more to read in file

        {

                Console::WriteLine(str); //display text in console

        }

}

Dice .cpp file

1

2

3

4

5

6

7

8

9

10

11

#include "stdafx.h"

#include "DiceClass.h"

#include <time.h>

#include <iostream>

int Dice::rollDice()

{

        sideRolled = (rand() % 6) + 1; //determine the side rolled

        return sideRolled;

}

LCRDiceGame.cpp file

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

// LCRDiceGame.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <string>

#include <time.h>

#include "DiceClass.h"

#include "PlayerClass.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

return 0;

}

#pragma once

#include "stdafx.h"

#include <iostream>

#include "DiceClass.h"

class Player

{

public:

        int chips;

        int numPlayers;

        std::string name = "";

        Player() = default;

        void setName();

        void setChips();

        int checkChips();

        static void directions();

};

Explanation / Answer

Hi I have removed error from your code but there is one function:

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

}

}

Here you said to write code to subtract 1 chip from current player, and add 1 chip to player to the left

but this is not possible from here to that you have to write code in main file from main file you can add and subtract multiple player chip for example:

int main()
{

n=rollDice();

if(n==1)

{

player[1].setChip+=1;

player[2].setChip-=1;

} else if(n==2)

{

player[1].setChip+=1;

player[0].setChip-=1;

}

}

Here is your error free code:

LCRDiceGame.cpp:

#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;

}

Dice.cpp:


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

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

Player.cpp:

#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 = "LCR_Rules.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.h:

#pragma once

class Dice

{

private:

  

int sideRolled;

public:

int rollDice();

};

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();
};

Note:-You didnt provide any rules for this game or program so I only remove errors from it

I hope this solves your problem

Comment if you have any problem in above answer

And please give it a thumbs up if it solved your problem :)