SlideShare a Scribd company logo
1 of 15
Download to read offline
C++
You will design a program to play a simplified version of war, using dice instead of cards. There
will be only one user, but 2 “players” for the game. The user will indicate the number of rounds
to play. The user will also specify for each “player” the number of sides on the dice used and if
that player is using regular or loaded dice. For example, one player could have a loaded 10-sided
die while the other has a 4-sided die. User should be able to determine how "loaded" the die is -
2/3/4 etc times mores likely to return a high number.
To play the game, for each round, you roll a die of the appropriate type for each player. The
higher result wins. If the results are equal, it is a draw. The winner of the game is the player who
won the most rounds. Your program will print out which player won to the user.
Die class: requires an input integer N which determines the number of sides on the individual
die. It includes a method to return a random integer between 1 and N as the result of rolling the
die for once.
LoadedDie class: it inherits the behavior and elements of Die, but the number it returns is biased
such that the average output of rolling it for several times will be higher than for a Die object.
You can determine how you want to realize the biased function.
Game class: it will implement the simple dice-rolling game. In the menu function, the user will
specify the die sides used by each player (the players can have dice with different number of
sides). The user will also indicate if either or both players are using loaded dice, and will enter
the number of rounds in the game. The Game class will create the necessary objects, play the
game, and display the results to the user. The output results should indicate the side and type
(loaded or not) of die used for each player, the number of rounds won by each player (in each
round, compare the rolling results, the larger number will win), and the final winner of the game
(the player that win more rounds).
Solution
//main.cpp
#include "Game.hpp"
#include
// Note: system("CLS") works with win / visual studio. Use "clear" for *nix...
// change value in Game.hpp too!
#define CLEAR_SCREEN "clear"
int main(){
int sides, // how many sides the dice should have.
rounds, // how many rounds to play in the game.
p1isLoaded, // menu choice for if player 1 is using a loaded die.
p2isLoaded; // menu choice for if player 2 is using a loaded die.
bool p1Loaded, // converted menu choice for if player 1 is using a loaded die.
p2Loaded; // converted menu choice for if player 2 is using a loaded die.
// main menu
system(CLEAR_SCREEN);
do {
std::cout << std::endl;
std::cout << "Welcome to a dice based game of War." << std::endl;
std::cout << "Please choose the number of sides each die should" << std::endl;
std::cout << "have for this game, or enter "5" to quit." << std::endl << std::endl;
std::cout << "1. Both dice will each have 4 sides." << std::endl;
std::cout << "2. Both dice will each have 6 sides." << std::endl;
std::cout << "3. Both dice will each have 12 sides." << std::endl;
std::cout << "4. Both dice will each have 20 sides." << std::endl;
std::cout << "5. to Qiut." << std::endl;
std::cin >> sides;
// validate main menu selection
while ((sides < 1) || (sides > 5)){
std::cout << "Please choose 1-5." << std::endl;
std::cin >> sides;
}
// if the user didn't Qiut.
if (sides != 5){
// convert the entered sides selection to a sides value.
if (sides == 1)
sides = 4;
else if (sides == 2)
sides = 6;
else if (sides == 3)
sides = 12;
else // (sides == 4)
sides = 20;
// get the amount of rounds to play for the game.
system(CLEAR_SCREEN);
std::cout << std::endl;
std::cout << "How many rounds should be played in this game?" << std::endl;
std::cout << "Please enter an integer value between 1 and 10000." << std::endl;
std::cin >> rounds;
// validate rounds
std::cout << std::endl;
while ((rounds < 1) || (rounds > 10000)){
std::cout << "Please enter an integer value between 1 and 10000." << std::endl;
std::cin >> rounds;
}
// get if the players are using normal or loaded dies.
// Player 1
system(CLEAR_SCREEN);
std::cout << std::endl;
std::cout << "Will Player 1 be using a standard or loaded die?" << std::endl;
std::cout << "(A loaded die rolls higher average values.)" << std::endl << std::endl;
std::cout << "1. Standard Die." << std::endl;
std::cout << "2. Loaded Die." << std::endl;
std::cin >> p1isLoaded;
// validate player 1 selection.
while ((p1isLoaded < 1) || (p1isLoaded > 2)){
std::cout << std::endl;
std::cout << "Please enter 1 or 2." << std::endl;
std::cin >> p1isLoaded;
}
// Player 2
std::cout << std::endl;
std::cout << "Will Player 2 be using a standard or loaded die?" << std::endl;
std::cout << "(A loaded die rolls higher average values.)" << std::endl << std::endl;
std::cout << "1. Standard Die." << std::endl;
std::cout << "2. Loaded Die." << std::endl;
std::cin >> p2isLoaded;
// validate player 2 selection.
while ((p2isLoaded < 1) || (p2isLoaded > 2)){
std::cout << std::endl;
std::cout << "Please enter 1 or 2." << std::endl;
std::cin >> p2isLoaded;
}
// convert player die selections to bool values.
if (p1isLoaded == 1)
p1Loaded = false;
else
p1Loaded = true;
if (p2isLoaded == 1)
p2Loaded = false;
else
p2Loaded = true;
system(CLEAR_SCREEN);
//create a game object which will run the game.
Game gameofwar(p1Loaded, p2Loaded, sides, rounds);
}
// If the user didn't quit return to the main menu.
} while (sides != 5);
return 0;
}
=====================================================================
=====
//Game.hpp
#ifndef GAME_H
#define GAME_H
// Note: system("CLS") works with win / visual studio.use "clear" for *nix...
// change value in main.cpp too!
#define CLEAR_SCREEN "clear"
#include
#include
#include
#include
#include "Die.hpp"
#include "LoadedDie.hpp"
class Game
{
private:
int dSides, // how many sides both dice have.
rounds, // how many rounds the game will run to determine the winner.
p1Rolled, // stores returned value of player 1's roll.
p2Rolled, // stores returned value of player 2's roll.
p1Total, // cumulative total of player 1's rolls for the game.
p2Total, // cumulative total of player 1's rolls for the game.
p1Points, // how many rounds player 1 has won.
p2Points; // how many rounds player 2 has won.
bool p1Loaded, // is player 1's die loaded?
p2Loaded; // is player 2's die loaded?
Die norm; // a Die object
LoadedDie load; // a loadedDie object
public:
Game(bool p1L, bool p2L, int s, int r);
void rollEm();
int compare(int p1, int p2);
// getters
int getDSides();
int getRounds();
int getP1Rolled();
int getP2Rolled();
int getP1Total();
int getP2Total();
int getP1Points();
int getP2Points();
bool getP1Loaded();
bool getP2Loaded();
//setters
void setDSides(int);
void setRounds(int);
void setP1Rolled(int);
void setP2Rolled(int);
void setP1Total(int);
void setP2Total(int);
void setP1Points(int);
void setP2Points(int);
void setP1Loaded(bool);
void setP2Loaded(bool);
};
#endif
============================================================
//Game.cpp
#include "Die.hpp"
#include "LoadedDie.hpp"
#include "Game.hpp"
#include
#include
Game::Game(bool p1L, bool p2L, int s, int r)
{
setP1Loaded(p1L);
setP2Loaded(p2L);
setDSides(s);
setRounds(r);
// set the seed for the die rolls.
srand(static_cast(time(0)));
// adjusts the dice sides to the user enterd value, and resets points and totals.
norm.setSides(s);
load.setSides(s);
p1Total = 0;
p2Total = 0;
p1Points = 0;
p2Points = 0;
// begins the game.
rollEm();
}
void Game::rollEm()
{
// for each round of the game...
for (int i = 0; i < rounds; i++){
if (p1Loaded == true)
p1Rolled = load.roll();
else
p1Rolled = norm.roll();
if (p2Loaded == true)
p2Rolled = load.roll();
else
p2Rolled = norm.roll();
// display who rolled what
std::cout << "Player 1 rolled: " << p1Rolled << std::endl;
std::cout << "Player 2 rolled: " << p2Rolled << std::endl;
// figure out who won the round, and adjust scores.
if (p1Rolled > p2Rolled){
std::cout << " - Player 1 wins the round." << std::endl << std::endl;
p1Points++;
}
else if (p2Rolled > p1Rolled){
std::cout << " - Player 2 wins the round." << std::endl << std::endl;
p2Points++;
}
else // p1Rolled == p2Rolled
std::cout << " - This round was a tie. No points awarded."
<< std::endl << std::endl;
// adjust totals...
p1Total += p1Rolled;
p2Total += p2Rolled;
}
// make it purdy to look at... :P
std::cout << " ********************************* " << std::endl << std::endl;
// different displays for the 3 conditions of Player 1 winning, Player2 winning,
// or a tie game.
switch (compare(p1Points, p2Points)){
case 1: // Player 1 wins.
std::cout << " Player 1 wins the game!!!" << std::endl;
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << " With a final score of " << p1Points << " - " << p2Points
<< std::endl << std::endl;
std::cout << "Player 1 averge roll: " << p1Total << "/" << rounds << " = "
<< (static_cast (p1Total) / rounds) << std::endl;
std::cout << "Player 2 averge roll: " << p2Total << "/" << rounds << " = "
<< (static_cast (p2Total) / rounds) << std::endl;
break;
case 2: // Player 2 wins.
std::cout << " Player 2 wins the game!!!" << std::endl;
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << " With a final score of " << p2Points << " - " << p1Points << std::endl
<< std::endl;
std::cout << "Player 2 averge roll: " << p2Total << "/" << rounds << " = "
<< (static_cast (p2Total) / rounds) << std::endl;
std::cout << "Player 1 averge roll: " << p1Total << "/" << rounds << " = "
<< (static_cast (p1Total) / rounds) << std::endl;
break;
case 3: // Tie game.
std::cout << " The game was a tie. Boo!!!" << std::endl;
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << " With a final score of " << p1Points << " - " << p2Points << std::endl
<< std::endl;
std::cout << "Player 1 averge roll: " << p1Total << "/" << rounds << " = "
<< (static_cast (p1Total) / rounds) << std::endl;
std::cout << "Player 2 averge roll: " << p2Total << "/" << rounds << " = "
<< (static_cast (p2Total) / rounds) << std::endl;
break;
}
// displays the die sides used and if either player used a loaded die.
std::cout << "Both Players used a " << dSides << " sided die." << std::endl;
if (p1Loaded)
std::cout << "Player 1's die was Loaded." << std::endl;
else
std::cout << "Player 1's die was Normal." << std::endl;
if (p2Loaded)
std::cout << "Player 2's die was Loaded." << std::endl;
else
std::cout << "Player 2's die was Normal." << std::endl;
// returns user to main menu after the game has ended.
std::cout << std::endl << "Press "enter" to return to the main menu.";
std::cin.ignore();
std::cin.get();
system(CLEAR_SCREEN);
}
int Game::compare(int p1, int p2)
{
if (p1 > p2)
return 1;
else if (p1 < p2)
return 2;
else // they're equal
return 3;
}
/***********************************************************
** getters...
************************************************************/
int Game::getDSides()
{
return dSides;
}
int Game::getRounds()
{
return rounds;
}
int Game::getP1Rolled()
{
return p1Rolled;
}
int Game::getP2Rolled()
{
return p2Rolled;
}
int Game::getP1Total()
{
return p1Total;
}
int Game::getP2Total()
{
return p2Total;
}
int Game::getP1Points()
{
return p1Points;
}
int Game::getP2Points()
{
return p2Points;
}
bool Game::getP1Loaded()
{
return p1Loaded;
}
bool Game::getP2Loaded()
{
return p2Loaded;
}
// end getters.
/***********************************************************
** setters...
************************************************************/
void Game::setDSides(int s)
{
dSides = s;
}
void Game::setRounds(int r)
{
rounds = r;
}
void Game::setP1Rolled(int p1R)
{
p1Rolled = p1R;
}
void Game::setP2Rolled(int p2R)
{
p2Rolled = p2R;
}
void Game::setP1Total(int p1T)
{
p1Total = p1T;
}
void Game::setP2Total(int p2T)
{
p2Total = p2T;
}
void Game::setP1Points(int p1P)
{
p1Points = p1P;
}
void Game::setP2Points(int p2P)
{
p2Points = p2P;
}
void Game::setP1Loaded(bool p1L)
{
p1Loaded = p1L;
}
void Game::setP2Loaded(bool p2L)
{
p2Loaded = p2L;
}
// end setters.
====================================================================
//Die.hpp
#ifndef DIE_H
#define DIE_H
class Die
{
protected:
int sides; // how many side the die has.
public:
// default constructor sets sides to 6.
Die();
// constructor with parameter to set the amount of sides for the die.
Die(int);
// sets the member variabe "sides".
void setSides(int);
// getter function to return the number of sides this die object has.
int getSides();
int roll();
};
#endif
======================================================
//Die.cpp
#include "Die.hpp"
#include
// default constructor sets sides to 6.
Die::Die()
{
setSides(6);
}
// constructor with parameter to set the amount of sides for the die.
Die::Die(int s)
{
setSides(s);
}
// sets the member variabe "sides".
void Die::setSides(int s)
{
sides = s;
}
// getter function to return the number of sides this die object has.
int Die::getSides()
{
return sides;
}
int Die::roll()
{
return std::rand() % sides + 1;
}
==================================================================
//LoadedDie.hpp
#ifndef LOADED_H
#define LOADED_H
#include"Die.hpp"
class LoadedDie : public Die
{
public:
// default constructor sets sides to 6.
LoadedDie();
// constructor with parameter to set the amount of sides for the die.
LoadedDie(int);
int roll();
};
#endif
===================================================================
//LoadedDie.cpp
#include "LoadedDie.hpp"
#include "Die.hpp"
#include
// default constructor sets sides to 6.
LoadedDie::LoadedDie()
{
setSides(6);
}
// constructor with parameter to set the amount of sides for the die.
LoadedDie::LoadedDie(int s)
{
setSides(s);
}
int LoadedDie::roll()
{
int result = (rand() % sides) + 1,
midpoint = (sides / 2);
if (result <= midpoint){
if (sides > 3)
result += (midpoint / 2);
else result++;
}
return result;
}
===================================================================

More Related Content

Similar to C++ Dice War Game

Goal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdfGoal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdfaaicommunication34
 
COMP 220 Entire Course NEW
COMP 220 Entire Course NEWCOMP 220 Entire Course NEW
COMP 220 Entire Course NEWshyamuopeight
 
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdfPlease follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdfinfo382133
 
The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210Mahmoud Samir Fayed
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquacareser
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquapariwar
 
Please use java to write the program that simulates the card game!!! T (1).pdf
Please use java to write the program that simulates the card game!!! T (1).pdfPlease use java to write the program that simulates the card game!!! T (1).pdf
Please use java to write the program that simulates the card game!!! T (1).pdfadmin463580
 

Similar to C++ Dice War Game (8)

Goal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdfGoal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdf
 
COMP 220 Entire Course NEW
COMP 220 Entire Course NEWCOMP 220 Entire Course NEW
COMP 220 Entire Course NEW
 
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdfPlease follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
 
The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
RandomGuessingGame
RandomGuessingGameRandomGuessingGame
RandomGuessingGame
 
Please use java to write the program that simulates the card game!!! T (1).pdf
Please use java to write the program that simulates the card game!!! T (1).pdfPlease use java to write the program that simulates the card game!!! T (1).pdf
Please use java to write the program that simulates the card game!!! T (1).pdf
 

More from ezzi97

Explain why multiple processes cannot share data easilySolution.pdf
Explain why multiple processes cannot share data easilySolution.pdfExplain why multiple processes cannot share data easilySolution.pdf
Explain why multiple processes cannot share data easilySolution.pdfezzi97
 
executive summary for law enforcement using dronesSolutionUse .pdf
executive summary for law enforcement using dronesSolutionUse .pdfexecutive summary for law enforcement using dronesSolutionUse .pdf
executive summary for law enforcement using dronesSolutionUse .pdfezzi97
 
Discuss how your life and experience with social mediaweb 2.0 compa.pdf
Discuss how your life and experience with social mediaweb 2.0 compa.pdfDiscuss how your life and experience with social mediaweb 2.0 compa.pdf
Discuss how your life and experience with social mediaweb 2.0 compa.pdfezzi97
 
create a narrative budget blueprint for a local unit of government. .pdf
create a narrative budget blueprint for a local unit of government. .pdfcreate a narrative budget blueprint for a local unit of government. .pdf
create a narrative budget blueprint for a local unit of government. .pdfezzi97
 
define three types of kernal-mode to user mode transfersSolution.pdf
define three types of kernal-mode to user mode transfersSolution.pdfdefine three types of kernal-mode to user mode transfersSolution.pdf
define three types of kernal-mode to user mode transfersSolution.pdfezzi97
 
Arrange the steps of DNA replication in the order that they occur. D.pdf
Arrange the steps of DNA replication in the order that they occur.  D.pdfArrange the steps of DNA replication in the order that they occur.  D.pdf
Arrange the steps of DNA replication in the order that they occur. D.pdfezzi97
 
(c) After conducting several measurements, the Beijing Municipal Env.pdf
(c) After conducting several measurements, the Beijing Municipal Env.pdf(c) After conducting several measurements, the Beijing Municipal Env.pdf
(c) After conducting several measurements, the Beijing Municipal Env.pdfezzi97
 
1. Which of the following statements would correctly print out t.pdf
1. Which of the following statements would correctly print out t.pdf1. Which of the following statements would correctly print out t.pdf
1. Which of the following statements would correctly print out t.pdfezzi97
 
2.How important is it to involve physicians in financial improvement.pdf
2.How important is it to involve physicians in financial improvement.pdf2.How important is it to involve physicians in financial improvement.pdf
2.How important is it to involve physicians in financial improvement.pdfezzi97
 
Write one page essay to explain how you relate signals and systems t.pdf
Write one page essay to explain how you relate signals and systems t.pdfWrite one page essay to explain how you relate signals and systems t.pdf
Write one page essay to explain how you relate signals and systems t.pdfezzi97
 
Why do IDPs & IDRs lack structure Lack a ligand or partner Denatu.pdf
Why do IDPs & IDRs lack structure  Lack a ligand or partner  Denatu.pdfWhy do IDPs & IDRs lack structure  Lack a ligand or partner  Denatu.pdf
Why do IDPs & IDRs lack structure Lack a ligand or partner Denatu.pdfezzi97
 
Write 2 to 3 paragraphs aboutONE DDoS attack that occurred in 2016.pdf
Write 2 to 3 paragraphs aboutONE DDoS attack that occurred in 2016.pdfWrite 2 to 3 paragraphs aboutONE DDoS attack that occurred in 2016.pdf
Write 2 to 3 paragraphs aboutONE DDoS attack that occurred in 2016.pdfezzi97
 
Why is methyl salicylate not appreciably soluble in waterSoluti.pdf
Why is methyl salicylate not appreciably soluble in waterSoluti.pdfWhy is methyl salicylate not appreciably soluble in waterSoluti.pdf
Why is methyl salicylate not appreciably soluble in waterSoluti.pdfezzi97
 
Who are stakeholders Define who they are and then please share what.pdf
Who are stakeholders Define who they are and then please share what.pdfWho are stakeholders Define who they are and then please share what.pdf
Who are stakeholders Define who they are and then please share what.pdfezzi97
 
What was the biggest problem with the Articles of Confederation They.pdf
What was the biggest problem with the Articles of Confederation They.pdfWhat was the biggest problem with the Articles of Confederation They.pdf
What was the biggest problem with the Articles of Confederation They.pdfezzi97
 
What is UWIN and what does it doSolutionUWin is a software .pdf
What is UWIN and what does it doSolutionUWin is a software .pdfWhat is UWIN and what does it doSolutionUWin is a software .pdf
What is UWIN and what does it doSolutionUWin is a software .pdfezzi97
 
What sort of archaeological remains have been discovered on the site.pdf
What sort of archaeological remains have been discovered on the site.pdfWhat sort of archaeological remains have been discovered on the site.pdf
What sort of archaeological remains have been discovered on the site.pdfezzi97
 
Link to assignment that I need help with is below httpweb.cse..pdf
Link to assignment that I need help with is below httpweb.cse..pdfLink to assignment that I need help with is below httpweb.cse..pdf
Link to assignment that I need help with is below httpweb.cse..pdfezzi97
 
The Food Stamp Program is Americas first line of defense against hu.pdf
The Food Stamp Program is Americas first line of defense against hu.pdfThe Food Stamp Program is Americas first line of defense against hu.pdf
The Food Stamp Program is Americas first line of defense against hu.pdfezzi97
 
The following code snipet contains some unfamiliar text#include .pdf
The following code snipet contains some unfamiliar text#include .pdfThe following code snipet contains some unfamiliar text#include .pdf
The following code snipet contains some unfamiliar text#include .pdfezzi97
 

More from ezzi97 (20)

Explain why multiple processes cannot share data easilySolution.pdf
Explain why multiple processes cannot share data easilySolution.pdfExplain why multiple processes cannot share data easilySolution.pdf
Explain why multiple processes cannot share data easilySolution.pdf
 
executive summary for law enforcement using dronesSolutionUse .pdf
executive summary for law enforcement using dronesSolutionUse .pdfexecutive summary for law enforcement using dronesSolutionUse .pdf
executive summary for law enforcement using dronesSolutionUse .pdf
 
Discuss how your life and experience with social mediaweb 2.0 compa.pdf
Discuss how your life and experience with social mediaweb 2.0 compa.pdfDiscuss how your life and experience with social mediaweb 2.0 compa.pdf
Discuss how your life and experience with social mediaweb 2.0 compa.pdf
 
create a narrative budget blueprint for a local unit of government. .pdf
create a narrative budget blueprint for a local unit of government. .pdfcreate a narrative budget blueprint for a local unit of government. .pdf
create a narrative budget blueprint for a local unit of government. .pdf
 
define three types of kernal-mode to user mode transfersSolution.pdf
define three types of kernal-mode to user mode transfersSolution.pdfdefine three types of kernal-mode to user mode transfersSolution.pdf
define three types of kernal-mode to user mode transfersSolution.pdf
 
Arrange the steps of DNA replication in the order that they occur. D.pdf
Arrange the steps of DNA replication in the order that they occur.  D.pdfArrange the steps of DNA replication in the order that they occur.  D.pdf
Arrange the steps of DNA replication in the order that they occur. D.pdf
 
(c) After conducting several measurements, the Beijing Municipal Env.pdf
(c) After conducting several measurements, the Beijing Municipal Env.pdf(c) After conducting several measurements, the Beijing Municipal Env.pdf
(c) After conducting several measurements, the Beijing Municipal Env.pdf
 
1. Which of the following statements would correctly print out t.pdf
1. Which of the following statements would correctly print out t.pdf1. Which of the following statements would correctly print out t.pdf
1. Which of the following statements would correctly print out t.pdf
 
2.How important is it to involve physicians in financial improvement.pdf
2.How important is it to involve physicians in financial improvement.pdf2.How important is it to involve physicians in financial improvement.pdf
2.How important is it to involve physicians in financial improvement.pdf
 
Write one page essay to explain how you relate signals and systems t.pdf
Write one page essay to explain how you relate signals and systems t.pdfWrite one page essay to explain how you relate signals and systems t.pdf
Write one page essay to explain how you relate signals and systems t.pdf
 
Why do IDPs & IDRs lack structure Lack a ligand or partner Denatu.pdf
Why do IDPs & IDRs lack structure  Lack a ligand or partner  Denatu.pdfWhy do IDPs & IDRs lack structure  Lack a ligand or partner  Denatu.pdf
Why do IDPs & IDRs lack structure Lack a ligand or partner Denatu.pdf
 
Write 2 to 3 paragraphs aboutONE DDoS attack that occurred in 2016.pdf
Write 2 to 3 paragraphs aboutONE DDoS attack that occurred in 2016.pdfWrite 2 to 3 paragraphs aboutONE DDoS attack that occurred in 2016.pdf
Write 2 to 3 paragraphs aboutONE DDoS attack that occurred in 2016.pdf
 
Why is methyl salicylate not appreciably soluble in waterSoluti.pdf
Why is methyl salicylate not appreciably soluble in waterSoluti.pdfWhy is methyl salicylate not appreciably soluble in waterSoluti.pdf
Why is methyl salicylate not appreciably soluble in waterSoluti.pdf
 
Who are stakeholders Define who they are and then please share what.pdf
Who are stakeholders Define who they are and then please share what.pdfWho are stakeholders Define who they are and then please share what.pdf
Who are stakeholders Define who they are and then please share what.pdf
 
What was the biggest problem with the Articles of Confederation They.pdf
What was the biggest problem with the Articles of Confederation They.pdfWhat was the biggest problem with the Articles of Confederation They.pdf
What was the biggest problem with the Articles of Confederation They.pdf
 
What is UWIN and what does it doSolutionUWin is a software .pdf
What is UWIN and what does it doSolutionUWin is a software .pdfWhat is UWIN and what does it doSolutionUWin is a software .pdf
What is UWIN and what does it doSolutionUWin is a software .pdf
 
What sort of archaeological remains have been discovered on the site.pdf
What sort of archaeological remains have been discovered on the site.pdfWhat sort of archaeological remains have been discovered on the site.pdf
What sort of archaeological remains have been discovered on the site.pdf
 
Link to assignment that I need help with is below httpweb.cse..pdf
Link to assignment that I need help with is below httpweb.cse..pdfLink to assignment that I need help with is below httpweb.cse..pdf
Link to assignment that I need help with is below httpweb.cse..pdf
 
The Food Stamp Program is Americas first line of defense against hu.pdf
The Food Stamp Program is Americas first line of defense against hu.pdfThe Food Stamp Program is Americas first line of defense against hu.pdf
The Food Stamp Program is Americas first line of defense against hu.pdf
 
The following code snipet contains some unfamiliar text#include .pdf
The following code snipet contains some unfamiliar text#include .pdfThe following code snipet contains some unfamiliar text#include .pdf
The following code snipet contains some unfamiliar text#include .pdf
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

C++ Dice War Game

  • 1. C++ You will design a program to play a simplified version of war, using dice instead of cards. There will be only one user, but 2 “players” for the game. The user will indicate the number of rounds to play. The user will also specify for each “player” the number of sides on the dice used and if that player is using regular or loaded dice. For example, one player could have a loaded 10-sided die while the other has a 4-sided die. User should be able to determine how "loaded" the die is - 2/3/4 etc times mores likely to return a high number. To play the game, for each round, you roll a die of the appropriate type for each player. The higher result wins. If the results are equal, it is a draw. The winner of the game is the player who won the most rounds. Your program will print out which player won to the user. Die class: requires an input integer N which determines the number of sides on the individual die. It includes a method to return a random integer between 1 and N as the result of rolling the die for once. LoadedDie class: it inherits the behavior and elements of Die, but the number it returns is biased such that the average output of rolling it for several times will be higher than for a Die object. You can determine how you want to realize the biased function. Game class: it will implement the simple dice-rolling game. In the menu function, the user will specify the die sides used by each player (the players can have dice with different number of sides). The user will also indicate if either or both players are using loaded dice, and will enter the number of rounds in the game. The Game class will create the necessary objects, play the game, and display the results to the user. The output results should indicate the side and type (loaded or not) of die used for each player, the number of rounds won by each player (in each round, compare the rolling results, the larger number will win), and the final winner of the game (the player that win more rounds). Solution //main.cpp #include "Game.hpp" #include // Note: system("CLS") works with win / visual studio. Use "clear" for *nix... // change value in Game.hpp too! #define CLEAR_SCREEN "clear"
  • 2. int main(){ int sides, // how many sides the dice should have. rounds, // how many rounds to play in the game. p1isLoaded, // menu choice for if player 1 is using a loaded die. p2isLoaded; // menu choice for if player 2 is using a loaded die. bool p1Loaded, // converted menu choice for if player 1 is using a loaded die. p2Loaded; // converted menu choice for if player 2 is using a loaded die. // main menu system(CLEAR_SCREEN); do { std::cout << std::endl; std::cout << "Welcome to a dice based game of War." << std::endl; std::cout << "Please choose the number of sides each die should" << std::endl; std::cout << "have for this game, or enter "5" to quit." << std::endl << std::endl; std::cout << "1. Both dice will each have 4 sides." << std::endl; std::cout << "2. Both dice will each have 6 sides." << std::endl; std::cout << "3. Both dice will each have 12 sides." << std::endl; std::cout << "4. Both dice will each have 20 sides." << std::endl; std::cout << "5. to Qiut." << std::endl; std::cin >> sides; // validate main menu selection while ((sides < 1) || (sides > 5)){ std::cout << "Please choose 1-5." << std::endl; std::cin >> sides; } // if the user didn't Qiut. if (sides != 5){ // convert the entered sides selection to a sides value. if (sides == 1) sides = 4; else if (sides == 2) sides = 6; else if (sides == 3) sides = 12;
  • 3. else // (sides == 4) sides = 20; // get the amount of rounds to play for the game. system(CLEAR_SCREEN); std::cout << std::endl; std::cout << "How many rounds should be played in this game?" << std::endl; std::cout << "Please enter an integer value between 1 and 10000." << std::endl; std::cin >> rounds; // validate rounds std::cout << std::endl; while ((rounds < 1) || (rounds > 10000)){ std::cout << "Please enter an integer value between 1 and 10000." << std::endl; std::cin >> rounds; } // get if the players are using normal or loaded dies. // Player 1 system(CLEAR_SCREEN); std::cout << std::endl; std::cout << "Will Player 1 be using a standard or loaded die?" << std::endl; std::cout << "(A loaded die rolls higher average values.)" << std::endl << std::endl; std::cout << "1. Standard Die." << std::endl; std::cout << "2. Loaded Die." << std::endl; std::cin >> p1isLoaded; // validate player 1 selection. while ((p1isLoaded < 1) || (p1isLoaded > 2)){ std::cout << std::endl; std::cout << "Please enter 1 or 2." << std::endl; std::cin >> p1isLoaded; } // Player 2 std::cout << std::endl; std::cout << "Will Player 2 be using a standard or loaded die?" << std::endl; std::cout << "(A loaded die rolls higher average values.)" << std::endl << std::endl; std::cout << "1. Standard Die." << std::endl;
  • 4. std::cout << "2. Loaded Die." << std::endl; std::cin >> p2isLoaded; // validate player 2 selection. while ((p2isLoaded < 1) || (p2isLoaded > 2)){ std::cout << std::endl; std::cout << "Please enter 1 or 2." << std::endl; std::cin >> p2isLoaded; } // convert player die selections to bool values. if (p1isLoaded == 1) p1Loaded = false; else p1Loaded = true; if (p2isLoaded == 1) p2Loaded = false; else p2Loaded = true; system(CLEAR_SCREEN); //create a game object which will run the game. Game gameofwar(p1Loaded, p2Loaded, sides, rounds); } // If the user didn't quit return to the main menu. } while (sides != 5); return 0; } ===================================================================== ===== //Game.hpp #ifndef GAME_H #define GAME_H // Note: system("CLS") works with win / visual studio.use "clear" for *nix... // change value in main.cpp too! #define CLEAR_SCREEN "clear" #include
  • 5. #include #include #include #include "Die.hpp" #include "LoadedDie.hpp" class Game { private: int dSides, // how many sides both dice have. rounds, // how many rounds the game will run to determine the winner. p1Rolled, // stores returned value of player 1's roll. p2Rolled, // stores returned value of player 2's roll. p1Total, // cumulative total of player 1's rolls for the game. p2Total, // cumulative total of player 1's rolls for the game. p1Points, // how many rounds player 1 has won. p2Points; // how many rounds player 2 has won. bool p1Loaded, // is player 1's die loaded? p2Loaded; // is player 2's die loaded? Die norm; // a Die object LoadedDie load; // a loadedDie object public: Game(bool p1L, bool p2L, int s, int r); void rollEm(); int compare(int p1, int p2); // getters int getDSides(); int getRounds(); int getP1Rolled(); int getP2Rolled(); int getP1Total(); int getP2Total(); int getP1Points(); int getP2Points(); bool getP1Loaded();
  • 6. bool getP2Loaded(); //setters void setDSides(int); void setRounds(int); void setP1Rolled(int); void setP2Rolled(int); void setP1Total(int); void setP2Total(int); void setP1Points(int); void setP2Points(int); void setP1Loaded(bool); void setP2Loaded(bool); }; #endif ============================================================ //Game.cpp #include "Die.hpp" #include "LoadedDie.hpp" #include "Game.hpp" #include #include Game::Game(bool p1L, bool p2L, int s, int r) { setP1Loaded(p1L); setP2Loaded(p2L); setDSides(s); setRounds(r); // set the seed for the die rolls. srand(static_cast(time(0))); // adjusts the dice sides to the user enterd value, and resets points and totals. norm.setSides(s); load.setSides(s); p1Total = 0; p2Total = 0;
  • 7. p1Points = 0; p2Points = 0; // begins the game. rollEm(); } void Game::rollEm() { // for each round of the game... for (int i = 0; i < rounds; i++){ if (p1Loaded == true) p1Rolled = load.roll(); else p1Rolled = norm.roll(); if (p2Loaded == true) p2Rolled = load.roll(); else p2Rolled = norm.roll(); // display who rolled what std::cout << "Player 1 rolled: " << p1Rolled << std::endl; std::cout << "Player 2 rolled: " << p2Rolled << std::endl; // figure out who won the round, and adjust scores. if (p1Rolled > p2Rolled){ std::cout << " - Player 1 wins the round." << std::endl << std::endl; p1Points++; } else if (p2Rolled > p1Rolled){ std::cout << " - Player 2 wins the round." << std::endl << std::endl; p2Points++; } else // p1Rolled == p2Rolled std::cout << " - This round was a tie. No points awarded." << std::endl << std::endl; // adjust totals...
  • 8. p1Total += p1Rolled; p2Total += p2Rolled; } // make it purdy to look at... :P std::cout << " ********************************* " << std::endl << std::endl; // different displays for the 3 conditions of Player 1 winning, Player2 winning, // or a tie game. switch (compare(p1Points, p2Points)){ case 1: // Player 1 wins. std::cout << " Player 1 wins the game!!!" << std::endl; std::cout << std::fixed << std::showpoint << std::setprecision(2); std::cout << " With a final score of " << p1Points << " - " << p2Points << std::endl << std::endl; std::cout << "Player 1 averge roll: " << p1Total << "/" << rounds << " = " << (static_cast (p1Total) / rounds) << std::endl; std::cout << "Player 2 averge roll: " << p2Total << "/" << rounds << " = " << (static_cast (p2Total) / rounds) << std::endl; break; case 2: // Player 2 wins. std::cout << " Player 2 wins the game!!!" << std::endl; std::cout << std::fixed << std::showpoint << std::setprecision(2); std::cout << " With a final score of " << p2Points << " - " << p1Points << std::endl << std::endl; std::cout << "Player 2 averge roll: " << p2Total << "/" << rounds << " = " << (static_cast (p2Total) / rounds) << std::endl; std::cout << "Player 1 averge roll: " << p1Total << "/" << rounds << " = " << (static_cast (p1Total) / rounds) << std::endl; break; case 3: // Tie game. std::cout << " The game was a tie. Boo!!!" << std::endl; std::cout << std::fixed << std::showpoint << std::setprecision(2); std::cout << " With a final score of " << p1Points << " - " << p2Points << std::endl << std::endl; std::cout << "Player 1 averge roll: " << p1Total << "/" << rounds << " = "
  • 9. << (static_cast (p1Total) / rounds) << std::endl; std::cout << "Player 2 averge roll: " << p2Total << "/" << rounds << " = " << (static_cast (p2Total) / rounds) << std::endl; break; } // displays the die sides used and if either player used a loaded die. std::cout << "Both Players used a " << dSides << " sided die." << std::endl; if (p1Loaded) std::cout << "Player 1's die was Loaded." << std::endl; else std::cout << "Player 1's die was Normal." << std::endl; if (p2Loaded) std::cout << "Player 2's die was Loaded." << std::endl; else std::cout << "Player 2's die was Normal." << std::endl; // returns user to main menu after the game has ended. std::cout << std::endl << "Press "enter" to return to the main menu."; std::cin.ignore(); std::cin.get(); system(CLEAR_SCREEN); } int Game::compare(int p1, int p2) { if (p1 > p2) return 1; else if (p1 < p2) return 2; else // they're equal return 3; } /*********************************************************** ** getters... ************************************************************/
  • 10. int Game::getDSides() { return dSides; } int Game::getRounds() { return rounds; } int Game::getP1Rolled() { return p1Rolled; } int Game::getP2Rolled() { return p2Rolled; } int Game::getP1Total() { return p1Total; } int Game::getP2Total() { return p2Total; } int Game::getP1Points() { return p1Points; } int Game::getP2Points() {
  • 11. return p2Points; } bool Game::getP1Loaded() { return p1Loaded; } bool Game::getP2Loaded() { return p2Loaded; } // end getters. /*********************************************************** ** setters... ************************************************************/ void Game::setDSides(int s) { dSides = s; } void Game::setRounds(int r) { rounds = r; } void Game::setP1Rolled(int p1R) { p1Rolled = p1R; } void Game::setP2Rolled(int p2R) { p2Rolled = p2R; }
  • 12. void Game::setP1Total(int p1T) { p1Total = p1T; } void Game::setP2Total(int p2T) { p2Total = p2T; } void Game::setP1Points(int p1P) { p1Points = p1P; } void Game::setP2Points(int p2P) { p2Points = p2P; } void Game::setP1Loaded(bool p1L) { p1Loaded = p1L; } void Game::setP2Loaded(bool p2L) { p2Loaded = p2L; } // end setters. ==================================================================== //Die.hpp #ifndef DIE_H #define DIE_H
  • 13. class Die { protected: int sides; // how many side the die has. public: // default constructor sets sides to 6. Die(); // constructor with parameter to set the amount of sides for the die. Die(int); // sets the member variabe "sides". void setSides(int); // getter function to return the number of sides this die object has. int getSides(); int roll(); }; #endif ====================================================== //Die.cpp #include "Die.hpp" #include // default constructor sets sides to 6. Die::Die() { setSides(6); } // constructor with parameter to set the amount of sides for the die. Die::Die(int s) { setSides(s); } // sets the member variabe "sides". void Die::setSides(int s)
  • 14. { sides = s; } // getter function to return the number of sides this die object has. int Die::getSides() { return sides; } int Die::roll() { return std::rand() % sides + 1; } ================================================================== //LoadedDie.hpp #ifndef LOADED_H #define LOADED_H #include"Die.hpp" class LoadedDie : public Die { public: // default constructor sets sides to 6. LoadedDie(); // constructor with parameter to set the amount of sides for the die. LoadedDie(int); int roll(); }; #endif =================================================================== //LoadedDie.cpp #include "LoadedDie.hpp" #include "Die.hpp" #include // default constructor sets sides to 6.
  • 15. LoadedDie::LoadedDie() { setSides(6); } // constructor with parameter to set the amount of sides for the die. LoadedDie::LoadedDie(int s) { setSides(s); } int LoadedDie::roll() { int result = (rand() % sides) + 1, midpoint = (sides / 2); if (result <= midpoint){ if (sides > 3) result += (midpoint / 2); else result++; } return result; } ===================================================================