Originally Posted by
john.c
You need to post the entire program so we can run it ourselves.
No problem.
main.cpp
Code:
#include <iostream>
#include <string>
#include <stdio.h> // needed for reading from rules file
#include <cstdlib> // needed for using random number generator
#include <ctime> // needed to initilize random number generator
#include <fstream> // call the file management class
using namespace std;
void printRules();
int main(int argc, const char * argv[])
{
// wide scoped variables
string waiting; // delete after completing game code creation
bool winner = false;
std::string winnerName = "";
// set number of chips in pot
int potChips = 0;
// initialize dice roll generator
srand((unsigned)time(NULL));
// create player instances
Player p1(1, "", 3, false); // assigned player number, chip count, and winner status for player 1
Player p2(2, "", 3, false); // assigned player number, chip count, and winner status for player 2
Player p3(3, "", 3, false); // assigned player number, chip count, and winner status for player 3
// show players the rules
printRules();
// Get the names of the players and assign to playerName array
string playerNames[4];
for (int i = 1; i <= 3; i++)
{
cout << "What is player " << (i) << "'s name?" << endl;
std::cin >> playerNames[i];
}
// adding player names to player class instances
p1.pName = playerNames[0];
p2.pName = playerNames[1];
p3.pName = playerNames[2];
// Determines if there is a winner based on chip count
while (winner == false) {
for (int currentPlayer = 1; currentPlayer <= 3; currentPlayer++) {
cout << endl;
cout << "Current player is " << playerNames[currentPlayer] << endl << endl;
// derive dice values for each chip player has.
for (int RolledTheDice = 1; RolledTheDice <= 3; RolledTheDice++)
{
// create random number to represent rolled dice
int diceValue = (rand() % 6) + 1;
// chip assignment based on value in diceValues array
switch (diceValue) {
case 1: // move chips left
cout << "The roll was an 'L', moved one chip to the left " << endl;
// assign player chips to player on the left
if (currentPlayer == 1 and p1.pChips > 0) // case where current player is p1
{
// check if player is out of chips and skip turn
if (p1.pChips == 0) {
cout << p1.pName << " your turn has been skipped because you have no chips" << endl;
break;
}
p3.pChips = p3.pChips + 1; // moves chips to player three
p1.pChips = p1.pChips - 1; // removes the chips from player one
break;
}
else if (currentPlayer == 2 and p2.pChips > 0) // case where current player is p2
{
// check if player is out of chips and skip turn
if (p2.pChips == 0) {
cout << p2.pName << " your turn has been skipped because you have no chips" << endl;
break;
}
p1.pChips = p1.pChips + 1;
p2.pChips = p2.pChips - 1; // removes the chips from player two
break;
}
else if (currentPlayer == 3 and p3.pChips > 0) { // case where current player is p3
// check if player is out of chips and skip turn
if (p3.pChips == 0) {
cout << p3.pName << " your turn has been skipped because you have no chips" << endl;
break;
}
p2.pChips = p2.pChips + 1;
p3.pChips = p3.pChips - 1; // removes the chips from player three
break;
}
else
{
break;
}
case 2: // move chips pot
cout << "The roll was a 'C', moved one chip to the pot " << endl;
if (currentPlayer == 1 and p1.pChips > 0) // case where current player is p1
{
// check if player is out of chips and skip turn
if (p1.pChips == 0) {
cout << p1.pName << " your turn has been skipped because you have no chips" << endl;
break;
}
potChips = potChips + 1; // moves chips to player two
p1.pChips = p1.pChips - 1; // removes the chips from player one
break;
}
else if (currentPlayer == 2 and p2.pChips > 0) // case where current player is p2
{
// check if player is out of chips and skip turn
if (p2.pChips == 0) {
cout << p2.pName << " your turn has been skipped because you have no chips" << endl;
break;
}
potChips = potChips + 1; // moves chips to player three
p2.pChips = p2.pChips - 1; // removes the chips from player two
break;
}
else if (currentPlayer == 3 and p3.pChips > 0) { // case where current player is p3
// check if player is out of chips and skip turn
if (p3.pChips == 0) {
cout << p3.pName << " your turn has been skipped because you have no chips" << endl;
break;
}
potChips = potChips + 1; // moves chips to player 1
p3.pChips = p3.pChips - 1; // removes the chips from player three
break;
}
else {
break;
}
case 3: // move chips to right
cout << "The roll was an 'R', moved one chip to the right " << endl;
// assign player chips to player on the right
if (currentPlayer == 1 and p1.pChips > 0) // case where current player is p1
{
// check if player is out of chips and skip turn
if (p1.pChips == 0) {
cout << p1.pName << " your turn has been skipped because you have no chips" << endl;
break;
}
p2.pChips = p2.pChips + 1; // moves chips to player two
p1.pChips = p1.pChips - 1; // removes the chips from player one
break;
}
else if (currentPlayer == 2 and p2.pChips > 0) // case where current player is p2
{
// check if player is out of chips and skip turn
if (p2.pChips == 0) {
cout << p2.pName << " your turn has been skipped because you have no chips" << endl;
break;
}
p3.pChips = p3.pChips + 1; // moves chips to player three
p2.pChips = p2.pChips - 1; // removes the chips from player two
break;
}
else if (currentPlayer == 3 and p3.pChips > 0) { // case where current player is p3
// check if player is out of chips and skip turn
if (p2.pChips == 0) {
cout << p3.pName << " your turn has been skipped because you have no chips" << endl;
break;
}
p1.pChips = p1.pChips + 1; // moves chips to player 1
p3.pChips = p3.pChips - 1; // removes the chips from player three
break;
}
default:
cout << "The Roll was a '*'. No chips were moved" << endl;
}
// continually check patter for winner
};
if (p1.pChips == 0 and p2.pChips == 0)
{
winnerName = p3.pName;
cout << endl << winnerName << " wins" << endl;
p3.pWinner = true;
winner = true;
}
else if (p2.pChips == 0 and p3.pChips == 0) {
winnerName = p1.pName;
cout << endl << winnerName << " wins" << endl;
p1.pWinner = true;
winner = true;
}
else if (p1.pChips == 0 and p3.pChips == 0) {
winnerName = p2.pName;
cout << endl << winnerName << " wins" << endl;
p2.pWinner = true;
winner = true;
}
else {
system("pause");
}
};
cout << endl;
cout << endl;
cout << "Player one has " << p1.pChips << endl;
cout << "Player two has " << p2.pChips << endl;
cout << "Player three has " << p3.pChips << endl;
cout << "Chips in the pot are " << potChips << endl;
}
}
void printRules() {
for (size_t i = 0; i <= 1; i++)
{
ifstream rulesOfTheGame; // create a file stream
char ruleLine[255]; // declare output character string
rulesOfTheGame.open("lcr-rules-to-print.txt"); // open game rules file
// code to check for existence of file adapted from
// C++ Notes: Reading Text Files
if (!rulesOfTheGame) {
cerr << "Unable to open the rules file";
exit(1); // call system to stop
}
// reads the file in sequence to the end
while (!rulesOfTheGame.eof())
{
rulesOfTheGame.getline(ruleLine, 255); // get line and place in string
if (rulesOfTheGame) cout << ruleLine << endl; // prints string to console
}
rulesOfTheGame.clear();
rulesOfTheGame.close();
}
}
player.h
Code:
#pragma once#include <string>
class Player
{
private:
int Number;
std::string name;
int chips;
bool Winner;
public:
int pNumber = Number;
std::string pName = name;
int pChips = chips;
bool pWinner = Winner;
Player(int pNumber,std::string pName, int pChips, bool pWinner);
~Player();
};
player.cpp
Code:
#include "Player.h"#include <string>
using namespace std;
Player::Player(int pNumber, std::string pName, int pChips, bool pWinner) : name(pName), chips(pChips), Winner(pWinner)
{
}
Player::~Player()
{
}
lcr-rules-to-print.txt
Code:
Left Center Right (LCR) is a multi-player dice game with a minimum of three players, but no upper limit on the number of participants. The goal is to win all of the chips.
The Dice
* There are three dice rolled each turn. Each die has the letters L, C, and R on it along with dots on the remaining three sides. These dice determine where the player(s) chips will go.
* For each L, the player must pass one chip to the player sitting to the left.
* For each R, the player must pass one chip to the player sitting to the right.
* For each C, the player must place one chip into the center pot and those chips are now out of play.
* Dots are neutral and require no action to be taken for that die.
The Chips
* Each player will start with three chips.
* If a player only has one chip, he/she rolls only one die. If a player has two chips left, he/she rolls two dice. Once a player is out of chips, he/she is still in the game (as he/she may get chips passed to him/her), but passes the dice to the next player.
Winning the Game
* The winner is the last player with chips.