Question
In need of some help with a LCR C++ game. The code will run, but I need it to cycle through the turns automatically so
In need of some help with a LCR C++ game. The code will run, but I need it to cycle through the turns automatically so that the only input from the user is the number of players. It's also showing the winner as the person with 0 chips which is incorrect and I can't figure out why. Any help is greatly appreciated as this is already late.
Game rules:
Develop a program that follows the rules of Left Center Right (LCR) as described. On program start-up, it shall display the rules to the user as read from a text file submitted with the program. The user can then set up the game by entering the number of players. Any number below three shall ask the user to add more players. Once gameplay has started based on the game rules, there are a few main pieces to address. Rolling the die should be performed by randomly generating the side of the die displayed for each of the three using a random number generator. For this game, if the generated number is 1, that will be L. Additionally, 2 is R, 3 is C, and 46 are dots that can be represented with the asterisk symbol *.
Be sure to check the current players number of chips before rolling. After each players roll, calculate the number of chips for players based on the actions dictated by the dice. Continue playing until only one player has chips. Display a message to the game winner. Left Center Right (LCR) is a multiplayer 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 players 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.
LCRGame.cpp
#include "pch.h" #include #include #include #include "Dice.h" #include "Player.h" #include
bool winner = false; int totalChips = 0;
using namespace std;
int setPlayers() {
int numPlayers = 0; cout << "How many players? " << endl; cin >> numPlayers;
if (numPlayers < 3) { cout << "This game requires at least 3 players." << endl; setPlayers(); }
return numPlayers; }
int main() { Player::directions(); int currentPlayer = 0; srand((unsigned)time(NULL));
const int numPlayers = setPlayers();
static Player*players = new Player[numPlayers];
for (int i = 0; i < numPlayers; i++) { players[i].chips = 3; } 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 << "Player has no chips & must skip this turn" << endl; ++i; } cin.ignore(); cout << " You have " << players[i].chips << " chips " << " press enter to roll the dice: "; 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 you win " << players[i].chips << " chips!"; winner = true; return 0; } } } }
}
Player.cpp
#include "pch.h" #include "Player.h" #include "Dice.h" #include #include #include
using namespace std;
int Player::chips = 0;
Player::Player() { chips = 0; }
int Player::setChips() { int l = 0; switch (Dice::rollDice()) { case 1: cout << endl << "You rolled L "; // subtract 1 chip from player and add one to left
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
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() { string line; //Reading rules from specified file ifstream file("LCRGameRules.txt");
//Loop to continue reading lines until the end of the file is reached if (file.is_open()) { //Return data for all available lines while (getline(file, line)) { cout << line << ' '; }
//Close file after reading all data file.close(); }
else { //Error if unable to open file cout << "Cannot open file"; } }
Dice.cpp
#include "pch.h" #include #include "Dice.h" #include
int Dice::rollDice() { return (rand() % 6) + 1; }
Player.h
#pragma once #include "pch.h" #include #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(); };
Dice.h
#pragma once #include "pch.h" #include "Player.h"
class Dice { public: Dice(); static int rollDice(); };
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started