
Modify the code below to create a "Dice game" program that is contained in 5 files:
- Dice_game.cpp
- Dice.cpp
- Die.cpp
- Dice.h
- Die.h
ou must include these features:
- Try-Throw-Catch
- Use .h and .cpp files for classes
- Must be your own code written now
In DrA’s dice game, you use two dice. On the first roll of the dice the player immediately wins when the roll is 7 or 11, and loses when the roll is 2, 3, or 12. (See below for code to doll a die.)
If 4, 5, 6, 8, 9, or 10 is rolled that number becomes the “key."
The player keeps rolling the dice until either 7 or the key is rolled. If the key is rolled first, then the player loses. If the player rolls a 7 first, then the player wins. (There is a loop here.) Write a program that plays the game once, using the rules stated above. There is no user input. Run it a few times watching the behavior in the debugger until you are convinced that it is playing by the rules. Instead of asking for a wager, the program should just determine if the player would win or lose. The program should simulate rolling the two dice and calculate the sum.
code to be modified:
Dice_Game.cpp
#include <iostream>
#include "Die.h"
using namespace std;
// a struct for game variables
struct GameState
{
int turn = 1;
int score = 0;
int score_this_turn = 0;
bool turn_over = false;
bool game_over = false;
Die die;
};
// declare functions
void display_rules();
void play_game(GameState&);
void take_turn(GameState&);
void roll_die(GameState&);
void hold_turn(GameState&);
int main()
{
display_rules();
GameState game;
play_game(game);
}
// define functions
void display_rules()
{
cout << "Dice Game Rules: \n"
<< "\n"
<< "* See how many turns it takes you to get to 20.\n"
<< "* Turn ends when you hold or roll a 1.\n"
<< "* If you roll a 1, you lose all points for the turn.\n"
<< "* If you hold, you save all points for the turn.\n\n";
}
void play_game(GameState& game)
{
while (!game.game_over)
{
take_turn(game);
}
cout << "Game over!\n";
}
void take_turn(GameState& game)
{
cout << "TURN " << game.turn << endl;
game.turn_over = false;
while (!game.turn_over)
{
char choice;
cout << "Roll or hold? (r/h): ";
cin >> choice;
if (choice == 'r')
roll_die(game);
else if (choice == 'h')
hold_turn(game);
else
cout << "Invalid choice. Try again.\n";
}
}
void roll_die(GameState& game)
{
game.die.roll();
cout << "Die: " << game.die.getValue() << endl;
if (game.die.getValue() == 1)
{
game.score_this_turn = 0;
game.turn += 1;
game.turn_over = true;
cout << "Turn over. No score.\n\n";
}
else
{
game.score_this_turn += game.die.getValue();
}
}
void hold_turn(GameState& game)
{
game.score += game.score_this_turn;
game.turn_over = true;
cout << "Score for turn: " << game.score_this_turn << endl;
cout << "Total score: " << game.score << "\n\n";
game.score_this_turn = 0;
if (game.score >= 20)
{
game.game_over = true;
cout << "You finished in " << game.turn << " turns!\n\n";
}
else
{
game.turn += 1;
}
}
Dice.cpp
#include "Dice.h"
Dice::Dice() {}
void Dice::add_die(Die die)
{
dice.push_back(die);
}
void Dice::roll_all()
{
for (Die& die : dice)
{
die.roll();
}
}
std::
{
return dice;
}
Die.cpp
#include <cstdlib>
#include <ctime>
#include "Die.h"
Die::Die()
{
srand(time(NULL)); // seed the rand() function
value = 1;
}
void Die::roll()
{
value = rand() % 6; // value is >= 0 and <= 5
++value; // value is >= 1 and <= 6
}
int Die::getValue()
{
return value;
}
Dice.h
ifndef DICE_H
#define DICE_H
#include <vector>
#include "Die.h"
class Dice
{
private:
std::vector<Die> dice;
public:
Dice();
void add_die(Die die);
void roll_all();
std::vector<Die> get_dice() const;
};
#endif // DICE_H
Die.h
#ifndef DIE_H
#define DIE_H
class Die
{
private:
int value;
public:
Die();
void roll();
int getValue();
};
#endif

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 3 images

- Build Brothers company in C A civil engineering company called Build Brothers has approached you to write a program to help some of their employees with getting their job quicker specially when it comes to calculating different equations in their construction projects. Currently, they use the following PDF to perform all their calculations: http://www.madison-lake.k12.oh.us/userfiles/680/Classes/16192/IED-Review%20Engineering%20Formula%20Sheet.pdf They are willing to develop a small software to calculate all the functions and calculations in the above cheat sheet. They have made this decision to reduce inherent human errors during calculation and improve efficiency. They think that these can be achieved by an “advanced calculator” that allows a user to choose an equation, provide the inputs, and calculate the outputs. Before the company invests on the final product, the have agreed on developing a proof-of-concept first. The idea is that your solution will ‘prove’ that a better…arrow_forwardCreate a file named Question.py and then create a class on it named Question. A Question object will contain information about a trivia question. It should have the following: A constructor, with the following parameters: 1 string to be the question itself (for example: "When was Cypress College Founded?"). A list of four strings, each a possible answer to the question (only one of them should be correct) An integer named 'answer': the index in the list of the correct answer. Example: if the third answer in the answer list is the correct one, the client should pass 2 to this parameter to signify the correct answer is in index 2. Overload the __str__ method. It should return a string made up of the question and the four possible answers. A ‘guess’ method, with an integer parameter. If the 'answer' attribute matches the int passed to 'guess', return True. Otherwise, False On Main.py, in your main function, create a list and add four Question objects to it. You may give them with…arrow_forwardThe program will consist of two files Program.cs - the main application file; name the class appropriately Unique.cs - class for retrieving the unique values from the user The Unique class provides the following: Constructor Private member variable to store 5 unique values (hint: use an Array or a List) Public function to get 5 unique values from the user and store them in the member variable loop to get the numbers, if a number is already stored, ignore it and keep looping until you have 5 unique numbers if a number is out of range, don't store the value, throw an exception and handle it in such a way that you don't break the loop but do message the user that the value was out of range Public functions to return the following based on the stored values: Largest number Smallest number Sum of all numbers Average of the numbers entered Last number entered divided by the first number entered The main application does the following Creates an instance of the Unique class Calls…arrow_forward
- Write a class name FileDisplay with the following methods: Constructor: takes the name of a file as an argument displayHead: displays only the first five lines of the file’s contents. If the file contains less than 5 lines, it should display the file’s entire contents. displayContents: displays the entire contents of the file, the name of which was passed to the constructor. writeNew: displays the contents of a file, the name of which was passed to the constructor. Each line should be preceded with a line number followed by a colon. The line numbering should start at 1. As each line is displayed it should also be written to a new file, the name of which is passed in as a parameter. I have provided a test file called “testfile.txt”. I have also provided a driver for the application called “filedisplaytest.java”. This file is complete and should not be altered. Test File: This is line one.This is line two.This is line three.This is line four.This is line five.This is line…arrow_forwardfile operator code 1 // The FileOperator class that includes methods for file input and output 2 // Your name 3 import java.io.*; 4 public class FileOperator 5 { 6 // instance variables 7 private File m_file; 8 9 // constructor 10 // Do not make any changes to this method! 11 public FileOperator() 12 { 13 String fileName = "employeeData.txt"; 14 m_file = new File(fileName); 15 } 16 17 // This method writes an array of Employees into a physical file. 18 // Each line is in this format: "S-Optimus Prime-Computer Science-$1200" 19 // "S" represents StudentWorker ("F" represents Faculty) 20 public void writeFile(Employee[] employees) 21 { 22 // TODO: implement this method 23 } 24 }arrow_forwardin java #6 - program that reads the name data from the files named firstNames.txt and lastNames.txtand produces a list of 1000 random names randomPeople.txtone complete name (firstname lastname) per linearrow_forward
- Given Triangle struct (in files Triangle.h and Triangle.c), complete main() to read and set the base and height data members of triangle1 and of triangle2, determine which triangle's area is smaller, and output that triangle's info, making use of the relevant Triangle functions. Ex: If the input is: 3.0 4.0 4.0 5.0 where 3.0 is triangle1's base, 4.0 is triangle1's height, 4.0 is triangle2's base, and 5.0 is triangle2's height, the output is: Triangle with smaller area: Base: 3.00 Height: 4.00 Area: 6.00 412800.2778464.gx320/7 LAB ACTIVITY 1 #include 2 #include "Triangle.h" 3 4 void setBase (Triangle *triangle, double base) { 5 triangle->base = base; 6} 7 8 void setHeight (Triangle triangle, double height) { 9 triangle->height = height; 10} 11 12 void calculateArea(Triangle *triangle) { 13 14} 9.11.1: LAB: Triangle area comparison 24 25 26 27 28 29 15 16 void printTriangle(Triangle triangle) { 17 18 19 20 } 30 31 32 21 22 main() { 23 33 34 35 36 37 38 39 40 41 triangle->area = 0.5 *…arrow_forward// Declare data fields: a String named customerName, // an int named numItems, and // a double named totalCost.// Your code here... // Implement the default contructor.// Set the value of customerName to "no name"// and use zero for the other data fields.// Your code here... // Implement the overloaded constructor that// passes new values to all data fields.// Your code here... // Implement method getTotalCost to return the totalCost.// Your code here... // Implement method buyItem.//// Adds itemCost to the total cost and increments// (adds 1 to) the number of items in the cart.//// Parameter: a double itemCost indicating the cost of the item.public void buyItem(double itemCost){// Your code here... }// Implement method applyCoupon.//// Apply a coupon to the total cost of the cart.// - Normal coupon: the unit discount is subtracted ONCE// from the total cost.// - Bonus coupon: the unit discount is subtracted TWICE// from the total cost.// - HOWEVER, a bonus coupon only applies if the…arrow_forwardMake any necessary modifications to the RushJob class so that it can be sorted by job number. Modify the JobDemo3 application so the displayed orders have been sorted. Save the application as JobDemo4. An example of the program is shown below: Enter job number 22 Enter customer name Joe Enter description Powerwashing Enter estimated hours 4 Enter job number 6 Enter customer name Joey Enter description Painting Enter estimated hours 8 Enter job number 12 Enter customer name Joseph Enter description Carpet cleaning Enter estimated hours 5 Enter job number 9 Enter customer name Josefine Enter description Moving Enter estimated hours 12 Enter job number 21 Enter customer name Josefina Enter description Dog walking Enter estimated hours 2 Summary: RushJob 6 Joey Painting 8 hours @$45.00 per hour. Rush job adds 150 premium. Total price is $510.00 RushJob 9 Josefine Moving 12 hours @$45.00 per hour. Rush job adds 150 premium. Total price is $690.00 RushJob 12 Joseph Carpet cleaning 5 hours…arrow_forward
- file operator code 1 // The FileOperator class that includes methods for file input and output 2 // Maham Hameed 3 import java.io.*; 4 public class FileOperator 5 { 6 // instance variables 7 private File m_file; 8 9 // constructor 10 // Do not make any changes to this method! 11 public FileOperator() 12 { 13 String fileName = "employeeData.txt"; 14 m_file = new File(fileName); 15 } 16 17 // This method writes an array of Employees into a physical file. 18 // Each line is in this format: "S-Optimus Prime-Computer Science-$1200" 19 // "S" represents StudentWorker ("F" represents Faculty) 20 public void writeFile(Employee[] employees) 21 { 22 // TODO: implement this method 23 } 24 }arrow_forwardIn python and include doctring: First, write a class named Movie that has four data members: title, genre, director, and year. It should have: an init method that takes as arguments the title, genre, director, and year (in that order) and assigns them to the data members. The year is an integer and the others are strings. get methods for each of the data members (get_title, get_genre, get_director, and get_year). Next write a class named StreamingService that has two data members: name and catalog. the catalog is a dictionary of Movies, with the titles as the keys and the Movie objects as the corresponding values (you can assume there aren't any Movies with the same title). The StreamingService class should have: an init method that takes the name as an argument, and assigns it to the name data member. The catalog data member should be initialized to an empty dictionary. get methods for each of the data members (get_name and get_catalog). a method named add_movie that takes a Movie…arrow_forwardyou have been asked by a computer gaming company to create a role-playing game, commonly known as an rpg. the theme of this game will be of the user trying to get a treasure that is being guarded the user will choose a character from a list you create create a character class, in a file named character.h, which will: have the following variables: name race (chosen from a list that you create, like knight, wizard, elf, etc.) weapon (chosen from a list that you create) spells (true meaning has the power to cast spells on others) anything else you want to add the treasure will be hidden in one of the rooms in a castle create a castle class, in a file named castle.h, that will: have these variables: at least four rooms named room1, room2, etc. moat (which is a lagoon surrounding a castle) which boolean (not all castles have them) anything else you want to add create a default constructor for each class that initializes each argument to a blank or false value create a second…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





