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.
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 #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 #include #include
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 #include
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 #include #include #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; } |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started