The document describes a simplified dice game of war simulation to be programmed in C++. There will be two simulated players, each with their own die that can have a different number of sides as specified by the user. The user will also specify if either die is loaded, meaning it is biased to roll higher on average. The program will simulate rounds of the two players rolling their dice, compare the results, track scores, and report the overall winner. Classes like Die, LoadedDie, and Game will be defined to represent the dice and game logic.
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()
{
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;
}
===================================================================