I am working on an LCR dice game in c++ for scgool that is due this Sunday. I ha
ID: 3907508 • Letter: I
Question
I am working on an LCR dice game in c++ for scgool that is due this Sunday. I have the main program working fine. I am stuck on the setChips function in the Player class in Player.cpp. I have commented out what I want to do and tried but no luck. I only need to complete the switch cases 1-3 and the project is complete. Here are my files:
Player.h:
#pragma once
#include "stdafx.h"
#include <iostream>
#include "Dice.h"
class Player
{
class main;
public:
int chips;
int numPlayers;
std::string name = "";
Player() = default;
void setName();
static void setChips ();
static void directions();
};
#pragma once
#include "stdafx.h"
#include <iostream>
#include "Dice.h"
class Player
{
class main;
public:
int chips;
int numPlayers;
std::string name = "";
Player() = default;
void setName();
static void setChips ();
static void directions();
};
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()
{
switch (Dice::rollDice()) // roll the dice
{
case 1:
cout << endl << "You rolled <L> "; // subtract 1 chip from player and add one to left
//if (i = numPlayers) {
//++players[i].chips;
// }
break;
case 2:
cout << endl << "You rolled <C> "; // subtract 1 chip from player
//--players[i].chips;
break;
case 3:
cout << endl << "You rolled <R> "; //subrtact 1 chip from player and give to right
//--players[i].chips;
//if (i = 1) {
// ++players[numPlayers].chips;
break;
case 4:
cout << endl << "You rolled <*> ";
break; //do nothing
case 5:
cout << endl << "You rolled <*> ";
break; // do nothing
case 6:
cout << endl << "You rolled <*> ";
break; //do nothing
}
}
void Player::directions() //display directions to player
{
string line; // variable to hold line being read from txt file
ifstream LCRRules("LCR_Rules.txt"); // open txt file
if (LCRRules.is_open()) //start loop if file is opened
{
while (getline(LCRRules, line)) //read txt file line by line and store it in var line while there are lines to read
{
cout << line << ' '; // print var line to screen
}
LCRRules.close(); // close txt file
}
else cout << "Unable to open file"; // catch error of not reading file
}
LCR Game.cpp:
// LCR Game.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
#include "Dice.h"
#include "Player.h"
bool winner = false;
int totalChips = 0;
using namespace std;
int setPlayers() //get number of players, check if 3 or more
{
int numOfPlayers = 0;
cout << endl << "Please enter number of players: " << endl;
cin >> numOfPlayers;
if (numOfPlayers <= 2)
{
cout << "Must be 3 or more." << endl;
setPlayers();
}
return numOfPlayers;
}
int main()
{
// Display Game Rules
Player::directions();
// Get # of Players
int currentPlayer = 0;
srand((unsigned)time(NULL));
const int numPlayers = setPlayers(); //set number of players
static Player* players = new Player[numPlayers]; //set up array
for (int i = 0; i < numPlayers; i++) //set names and chips for each player
{
cout << endl << "Enter player number " << (i + 1) << "'s name: " << endl;
players[i].setName();
players[i].chips = 3;
}
// start game
cout << endl << "OK Let's play!" << endl;
while (winner == false) {
for (int i = 0; i < numPlayers; i++) {
// check if player has chips. if not ++i and skip turn
if (players[i].chips == 0) {
cout << endl << "Sorry " << players[i].name << " you have no chips, you must skip this turn" << endl;
++i;
}
cout << endl << players[i].name << " You have " << players[i].chips << " chips " << " press enter to roll the dice" << endl;
std::cin.ignore(); //wait for keypress
for (int j = 0; j < players[i].chips || j < 3; j++) { // player rolls dice up to 3 times or max chips
Player::setChips(); // call setChips. roll dice & move chips
cout << players[i].chips;
// check for winner after each diceroll
totalChips = 0; // reset chip counter
for (int k = 0; k < numPlayers; k++) { //add all chips on table
totalChips = totalChips + players[i].chips;
}
// check for winner
if (totalChips - players[i].chips == 0) {
cout << endl << "Congratulations " << players[i].name << " you win " << players[i].chips << " chips!";
winner = true;
return 0;
}
}
}
}
return 0;
}
Dice.h:
#pragma once
#include "stdafx.h"
#include "Player.h"
class Dice
{
public:
Dice() = default;
static int rollDice();
};
Dice.cpp:
#include "stdafx.h"
#include "Dice.h"
#include "Player.h"
#include <time.h>
#include <iostream>
int Dice::rollDice()
{
return 1 + rand() % 6;
}
Explanation / Answer
Dice.h
#pragma once
//#include "stdafx.h"
#include "Player.h"
class Dice
{
public:
Dice();
static int rollDice();
};
Dice.cpp
//#include "stdafx.h"
#include "Dice.h"
#include "Player.h"
#include <time.h>
#include <iostream>
int Dice::rollDice()
{
return 1 + rand() % 6;
}
Player.h
#pragma once
//#include "stdafx.h"
#include <iostream>
#include "Dice.h"
class Player
{
class main;
public:
static int chips;
int numPlayers;
std::string name ;
Player();
void setName();
static int setChips ();
static void directions();
};
Player.cpp
#include "Player.h"
#include "Dice.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int Player::chips = 0;
Player::Player()
{
name="";
chips=0;
}
void Player::setName()
{
std::cin >> name;
}
int Player::setChips()
{
int l=0;
switch (Dice::rollDice()) // roll the dice
{
case 1:
cout << endl << "You rolled <L> "; // subtract 1 chip from player and add one to left
//if (i = numPlayers) {
//++players[i].chips;
// }
chips--;
l=-1;
break;
case 2:
cout << endl << "You rolled <C> "; // subtract 1 chip from player
//--players[i].chips;
chips--;
l=0;
break;
case 3:
cout << endl << "You rolled <R> "; //subrtact 1 chip from player and give to right
//--players[i].chips;
//if (i = 1) {
// ++players[numPlayers].chips;
chips--;
l=1;
break;
case 4:
cout << endl << "You rolled <*> ";
break; //do nothing
case 5:
cout << endl << "You rolled <*> ";
break; // do nothing
case 6:
cout << endl << "You rolled <*> ";
break; //do nothing
}
return l;
}
void Player::directions() //display directions to player
{
string line; // variable to hold line being read from txt file
ifstream LCRRules("LCR_Rules.txt"); // open txt file
if (LCRRules.is_open()) //start loop if file is opened
{
while (getline(LCRRules, line)) //read txt file line by line and store it in var line while there are lines to read
{
cout << line << ' '; // print var line to screen
}
LCRRules.close(); // close txt file
}
else cout << "Unable to open file"; // catch error of not reading file
}
LCRGame.cpp
//#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
#include "Dice.h"
#include "Player.h"
bool winner = false;
int totalChips = 0;
using namespace std;
int setPlayers() //get number of players, check if 3 or more
{
int numOfPlayers = 0;
cout << endl << "Please enter number of players: " << endl;
cin >> numOfPlayers;
if (numOfPlayers <= 2)
{
cout << "Must be 3 or more." << endl;
setPlayers();
}
return numOfPlayers;
}
int main()
{
// Display Game Rules
Player::directions();
// Get # of Players
int currentPlayer = 0;
srand((unsigned)time(NULL));
const int numPlayers = setPlayers(); //set number of players
static Player* players = new Player[numPlayers]; //set up array
for (int i = 0; i < numPlayers; i++) //set names and chips for each player
{
cout << endl << "Enter player number " << (i + 1) << "'s name: " << endl;
players[i].setName();
players[i].chips = 3;
}
// start game
cout << endl << "OK Let's play!" << endl;
while (winner == false) {
for (int i = 0; i < numPlayers; i++) {
// check if player has chips. if not ++i and skip turn
if (players[i].chips == 0) {
cout << endl << "Sorry " << players[i].name << " you have no chips, you must skip this turn" << endl;
++i;
}
cin.ignore();
cout << players[i].name << " You have " << players[i].chips << " chips " << " press enter to roll the dice: ";
std::cin.ignore(); //wait for keypress
for (int j = 0; j < players[i].chips || j < 3; j++) { // player rolls dice up to 3 times or max chips
int l=Player::setChips(); // call setChips. roll dice & move chips
if(l==1)
{
players[i+1].chips++;
}
else if(l==-1)
{
players[i-1].chips++;
}
cout << "Number of chips remaining: "<<players[i].chips;
// check for winner after each diceroll
totalChips = 0; // reset chip counter
for (int k = 0; k < numPlayers; k++) { //add all chips on table
totalChips = totalChips + players[i].chips;
}
// check for winner
if (totalChips - players[i].chips == 0) {
cout << endl << "Congratulations " << players[i].name << " you win " << players[i].chips << " chips!";
winner = true;
return 0;
}
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.