Question

In need of some help with a LCR C++ game. The code will run, but I...

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 4–6 are dots that can be represented with the asterisk symbol *.

Be sure to check the current player’s number of chips before rolling. After each player’s 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 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.

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 << '\n';
       }

       //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();
};

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, since you are not asking for full code, i can provide you with some input.

1) To automate the player's turn, use a variable num=0;

now in each turn, take (num+1)%no_of_players, whatever this results, give the chance to that player and increment num by 1;

Example :

players = 3

so num = 0

First turn, (0+1)%3 = 1, so player 1's turn

Second turn, num was incremented in last tutn, so now num = 1

(1+1)%3 = 2, so Player 2 turn

Regarding your second pblm, the code is a little bit unreadable, but i think that problem might be in:

// check for winner
               if (totalChips - players[i].chips == 0)

Have you handled the case when total chips will become 0, then it might give wrong result.

Try these two changes on your own, or get back to me in comments, i will provide the working code. Rate please if you find it helpful

Add a comment
Know the answer?
Add Answer to:
In need of some help with a LCR C++ game. The code will run, but I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help figuring out how to code this in C++ in Visual Studio. Problem Statement:...

    I need help figuring out how to code this in C++ in Visual Studio. Problem Statement: Open the rule document for the game LCR Rules Dice Game. Develop the code, use commenting to describe your code and practice debugging if you run into errors. Submit source code. Use the document to write rules for the user on how to play the game in a text file. Then, using the information from the resource articles, create a program that will read...

  • I need some help creating C++ Left, Center, Right (LCR) dice game Pseudocode. Address the following : A. Analyze the giv...

    I need some help creating C++ Left, Center, Right (LCR) dice game Pseudocode. Address the following : A. Analyze the given problem statement. B. Break the problem down into distinct steps of pseudocode that will solve the problem. C. Create variables to track the various elements in the pseudocode. D. If applicable, determine any breakdown of pseudocode into functions and/or classes. E. Use natural language to work through the problems. Using three special dice and player pieces called chips. In...

  • I need this done in C++ Can someone help me get my code from crashing. The...

    I need this done in C++ Can someone help me get my code from crashing. The code compiles but it can be easily crashed with user input. The rubric and code below Instructions: Write a program that scores the following data about a soccer player in a structure:            Player’s Name            Player’s Number            Points Scored by Player      The program should keep an array of 12 of these structures. Each element is for a different player on a...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() {...

    can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() { return (rand() % 6+1); } void askYoNs(){ cout<<"Do you want to roll a dice (Y/N)?:"<<endl; } void printScores(int turnTotal,int humanTotal,int compTotal){ int player; int human; if(player==human){ cout<<"Your turn total is "<<turnTotal<<endl; } else{ cout<<"computer turn total is "<<turnTotal<<endl; } cout<<"computer: "<<compTotal<<endl; cout<<"human: "<<humanTotal<<endl; cout<<endl; } int human; int changePlayer(int player){ if(player==human) return 1; return human; } int process(int& turnTotal,int roll,int curr_player,int& humanTotal,int& computerTotal){ if(roll==2...

  • Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem...

    Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME             UNIFORM#    AVEPPG Mary                   15     10.50 Jack                   72      8.10 Elizabeth              41      7.50 Joseph                 32      6.20 Vince                  83      4.20 ​ My Code: #include <iostream>...

  • hey, struggling with this assignment, wondering if if I could get some help. Here is the...

    hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. The rollTwoDice function simulates rolling two dice objects and returns the sum of their values. The srand function requires including cstdlib. my class ------------------------------------ class Dice { public: Dice(); DIce(int numSides); virtual int rollDice()const; protected: int...

  • Need done in C++ Can not get my code to properly display output. Instructions below. The...

    Need done in C++ Can not get my code to properly display output. Instructions below. The code compiles but output is very off. Write a program that scores the following data about a soccer player in a structure:            Player’s Name            Player’s Number            Points Scored by Player      The program should keep an array of 12 of these structures. Each element is for a different player on a team. When the program runs, it should ask the user...

  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • can someone help me fix my jeopardy dice game I am having a hard time figuring...

    can someone help me fix my jeopardy dice game I am having a hard time figuring out what i am doing wrong #include #include using namespace std; //this function makes a number between 1 and 6 come out just like a dice int rollDie() { return (rand() % 6+1); } //this function asks the user with a printed statement if they want to roll the dice and gives instructions for yes and no (y/n) void askYoNs(){ cout<<"Do you want to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT