Ok, so this is my final project for school and I'm really struggling with it. I've got till Sunday to finish it up, so I'm here to ask for help finally.
The rules for the assignment are fairly simple. I have to create at least 2 classes, I have to have my program properly structured, ie using header files correctly, etc. And the program obviously has to execute correctly.
If you aren't familiar with the LCR dice game, I'll give a basic description. There are 3 or more players. Each player starts with 3 chips. A player will roll as many dice as he has chips ie 3 chips = 3 rolls of the dice, 2 chips = 2 rolls of the dice. If you have no dice, you turn is skipped. If you roll an L (1), you pass a chip to the player to the left, a C (2), put a chip in the center pile, a 3 (R), you pass a chip to the player to the right of you, and 4,5, and 6 there is no action necessary. The game goes on until there is only 1 player left with chips.
What I have so far: I decided to do a player class and a dice class. The dice class is very simple. It basically creates a dice object and then has a rollDice() function. The player class has checkChips() and setChips() functions. checkChips() should return the number of chips the current player has. If he has 0, it skips the current players turn and goes to the next, if it returns a different number, it should tell the rollDice() function how many times it needs to be called. The setChips() function is set up to use a switch statement to take the result of the rollDice() function to determine how to distribute chips. In the main file, outside of the main function, I created a setPlayers() function to take user input on how many players there will be. Inside the main function, setPlayers() is called, then the result is stored to a constant and then this constant is used to set the length of an array of Player objects. Then I used a loop to give each player a name and 3 chips.
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. This is partially because I want to at least get the basics down and then build on it as I go. I've spent the better part of a week trying to find a way to make these things work and haven't gotten any progress. Hopefully someone has some tips and ideas to help me out.
Player Header File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#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 50 51 52 53 54
|
// 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;
}
|