C++ Can someone help me to determine what I\'m missing. Below is the code for my
ID: 3595597 • Letter: C
Question
C++
Can someone help me to determine what I'm missing. Below is the code for my Left Center Right dice game. The code compiles and runs, but exits once the game rules are displayed. It doesn't actually play the game. I'm need some help to determine what I'm missing here after the Game rules are displayed the game should start. Please help
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
Here is where you're failing. And is explained why. But sorry, the problem is not solved.
//#include "stdafx.h"
#include <iostream>
//#include<conio.h>
#include <string>
#include <time.h>
#include "Dice.cpp"
#include "Player.cpp"
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
//This is where you're missing the actual code.
//The before code will just initialize the players, and will print the directions.
//But after that, you didn't wrote anything to be done, and therefore, the code is ending.
//You also didn't provided any directions for the game, i.e., the file LCRrules.txt
//is not given. If you had given that, I would had tried to develop some more instructions
//to run the code. Anyways, as you asked where you're failing, I would like to inform
//you that this is where you're failing. Please write the actual code to play the game here.
// _getch;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.