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
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 #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 #include #include 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 #include
int Dice::rollDice() { sideRolled = (rand() % 6) + 1; //determine the side rolled return sideRolled; }
LCRdicegame.cpp
#include "stdafx.h" #include #include #include #include #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; }