Question

Use C++ 11 to write the program War Game Requirement Setting This is a 2-player game....

Use C++ 11 to write the program

War Game Requirement

Setting

This is a 2-player game. It is played through dice.

Rule for scoring

The player who rolls higher number gets one point. If both players roll the same number, it is considered a draw and no one gets a point.

Dice Specification

There are two kinds of dice:

  • normal die, represented by Die class.
  • loaded die, represented by the loadedDie class.

Classes

Die class

Die class has a member variable of an integer N that represents the number of sides on the individual die.

Die class also has a member function that returns a random integer between 1 and N as the result of rolling the die once.

LoadedDie class

LoadedDie class inherits the behavior and elements of Die class.

However, for the die rolling function, the number it returns is biased such that the average output of rolling it for several times is higher than that of a Die object with the same number of sides.

You can determine how you want to make the die loaded, as long as it fulfills the requirement above.

Note: A loaded Die can never roll a number that is higher than the number of sides it has. So, if a loaded die has 6 sides, it cannot roll a 10.

Game class:

This class implements the dice-rolling war game.

First, create a menu that first asks the user to select from the following 2 choices:

  • Play game
  • Exit game

If the user selects “play game”, ask the following questions at the start of the game:

  • How many rounds will be played
  • The type of die for each player
  • The number of sides for dice of both players

If the user selects “Exit game”, exit the game.

Note: The two dice can have different numbers of sides.

Note: You are allowed to add more choices for your menu as long as it makes sense.

Design the menu so it makes it easier for the user to read, and input selection.

After getting all the information from the user, the game then creates the necessary Die/LoadedDie objects, and play the game.

During the game, the game should output the detailed result of each round, including the side and type of die used for each player, the number each player rolls, and the score result. Afterwards, display the final score count after results of all rounds are printed, and the final winner of the game.

Note: You are allowed to have other classes, as long as you have the classes specified above, and the behavior satisfies the above requirements.

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

main.cpp


#include <iostream>
#include "Game.hpp"

using std::cout;
using std::endl;

//users will be prompted to choose from 6, 10, or 14 sided die as well as whether or not they would like to use
//loaded (biased towards higher number) die
int main() {

    Game game1;

    game1.gameMenu();

    return 0;
}

Die.cpp


#include "Die.hpp"
#include <cstdlib>

//function to set number of sides on Die
void Die::setNumSides(int number){
    numSides = number;
}

//function to get number of sides on Die
int Die::getNumSides(){
    return numSides;
}

//function to "roll" randomly generated number for Die
int Die::rollDie() {
    return rand() % getNumSides() + 1;
}

Die.hpp


#ifndef CS162LAB3_DIE_HPP
#define CS162LAB3_DIE_HPP

//base class
class Die{

private:

    int numSides;

public:

    void setNumSides(int number);
    int getNumSides();
    int rollDie();
};

#endif


Game.cpp


#include "Game.hpp"
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

//function to validate user input
int Game::inputValidate(int integer) {

    while(cin.fail()) { //input validation
        cout << "Please enter a valid integer." << endl;
        cin.clear();
        cin.ignore(256, '\n');
        cin >> integer;
    }
    cin.ignore(256, '\n');
    return integer;
}

//function to display game menu, includes play function
void Game::gameMenu() {

    Die * die1 = new Die;
    Die * die2 = new Die;

    LoadedDie * loadedDie1 = new LoadedDie;
    LoadedDie * loadedDie2 = new LoadedDie;

    cout << "Please input your choice below." << endl;
    cout << "1. Play Game" << endl;
    cout << "2. Exit Game" << endl;

    int choice;

    choice = getChoice();

    //continue to play game until the user decides to "quit"
    while (choice == 1) {

        cout << "How many rounds will be played?" << endl;
        cin >> numRounds;
        while (numRounds < 1) {
            cout << "Number of rounds must be greater than zero. Please enter a valid number of rounds." << endl;
            cin >> numRounds;
            numRounds = inputValidate(numRounds);
        }

        cout << "Player 1: What type of die would you like to use" << endl;
        cout << "1. Standard Die" << endl;
        cout << "2. Loaded Die" << endl;
        cin >> dieTypePlayer1;
        while (dieTypePlayer1 < 1 || dieTypePlayer1 > 2) {
            cout << "You must choose a Die Type of either 1 or 2." << endl;
            cin >> dieTypePlayer1;
            dieTypePlayer1 = inputValidate(dieTypePlayer1);
        }

        cout << "Player 2: What type of die would you like to use" << endl;
        cout << "1. Standard Die" << endl;
        cout << "2. Loaded Die" << endl;
        cin >> dieTypePlayer2;
        while (dieTypePlayer2 < 1 || dieTypePlayer2 > 2) {
            cout << "You must choose a Die Type of either 1 or 2." << endl;
            cin >> dieTypePlayer2;
            dieTypePlayer2 = inputValidate(dieTypePlayer2);
        }

        cout << "Player 1: How many sides would you like your Die to have?" << endl;
        cout << "6 sides" << endl;
        cout << "10 sides" << endl;
        cout << "14 sides" << endl;
        cin >> numSidesPlayer1;
        while (numSidesPlayer1 != 6 && numSidesPlayer1 != 10 && numSidesPlayer1 != 14) {
            cout << "Please choose a valid number of sides for your Die: 6, 10, or 14" << endl;
            cin >> numSidesPlayer1;
            numSidesPlayer1 = inputValidate(numSidesPlayer1);
        }

        die1->setNumSides(numSidesPlayer1);
        loadedDie1->setNumSides(numSidesPlayer1);

        cout << "Player 2: How many sides would you like your Die to have?" << endl;
        cout << "6 sides" << endl;
        cout << "10 sides" << endl;
        cout << "14 sides" << endl;
        cin >> numSidesPlayer2;
        while (numSidesPlayer2 != 6 && numSidesPlayer2 != 10 && numSidesPlayer2 != 14) {
            cout << "Please choose a valid number of sides for your Die: 6, 10, or 14" << endl;
            cin >> numSidesPlayer2;
            numSidesPlayer2 = inputValidate(numSidesPlayer2);
        }

        die2->setNumSides(numSidesPlayer2);
        loadedDie2->setNumSides(numSidesPlayer2);

        play(die1, die2, loadedDie1, loadedDie2);

        //prompt the user after the game if he/she would like to play again
        cout << "Play again? Please input your choice below." << endl;
        cout << "1. Play again?" << endl;
        cout << "2. Quit" << endl;
        choice = getChoice();
    }

    delete die1;
    delete die2;
    delete loadedDie1;
    delete loadedDie2;
}

//function to get menu choice from user
int Game::getChoice(){

    int choice;

    cin >> choice;
    choice = inputValidate(choice);

    while(choice != 1 && choice != 2){
        cout << "Please enter only a valid choice 1 or 2." << endl;
        cin.clear();
        cin.ignore(256, '\n');
        cin >> choice;
        choice = inputValidate(choice);
    }

    return choice;
}

//play function executes the gameplay of the war game
void Game::play(Die * DieObject1, Die * DieObject2, LoadedDie * LoadedDieObject1, LoadedDie * LoadedDieObject2) {


    int player1score = 0;
    int player2score = 0;

    if (dieTypePlayer1 == STANDARD && dieTypePlayer2 == STANDARD) {

        for (int round = 1; round <= numRounds; round++) {
            int currentRollPlayer1 = DieObject1->rollDie();
            int currentRollPlayer2 = DieObject2->rollDie();

            cout << "Round: " << round << endl;
            cout << "Player 1 rolled a " << currentRollPlayer1 << " using a Standard Die with "
                 << numSidesPlayer1 << " sides" << endl;
            cout << "Player 2 rolled a " << currentRollPlayer2 << " using a Standard Die with "
                 << numSidesPlayer2 << " sides" << endl;

            if(currentRollPlayer1 > currentRollPlayer2) {
                player1score++;
            }
            else if(currentRollPlayer1 < currentRollPlayer2) {
                player2score++;
            }

           cout << "Score: " << endl << "Player 1: " << player1score << " Player 2: " << player2score << endl << endl;
        }
    }

    else if(dieTypePlayer1 == STANDARD && dieTypePlayer2 == LOADED){

        for (int round = 1; round <= numRounds; round++) {
            int currentRollPlayer1 = DieObject1->rollDie();
            int currentRollPlayer2 = LoadedDieObject2->rollLoadedDie();

            cout << "Round: " << round << endl;
            cout << "Player 1 rolled a " << currentRollPlayer1 << " using a Standard Die with "
                 << numSidesPlayer1 << " sides" << endl;
            cout << "Player 2 rolled a " << currentRollPlayer2 << " using a Loaded Die with "
                 << numSidesPlayer2 << " sides" << endl;

            if(currentRollPlayer1 > currentRollPlayer2) {
                player1score++;
            }
            else if(currentRollPlayer1 < currentRollPlayer2) {
                player2score++;
            }

            cout << "Score: " << endl << "Player 1: " << player1score << " Player 2: " << player2score << endl << endl;
        }
    }

    else if(dieTypePlayer1 == LOADED && dieTypePlayer2 == LOADED){

        for (int round = 1; round <= numRounds; round++) {
            int currentRollPlayer1 = LoadedDieObject1->rollLoadedDie();
            int currentRollPlayer2 = LoadedDieObject2->rollLoadedDie();

            cout << "Round: " << round << endl;
            cout << "Player 1 rolled a " << currentRollPlayer1 << " using a Loaded Die with "
                 << numSidesPlayer1 << " sides" << endl;
            cout << "Player 2 rolled a " << currentRollPlayer2 << " using a Loaded Die with "
                 << numSidesPlayer2 << " sides" << endl;

            if(currentRollPlayer1 > currentRollPlayer2) {
                player1score++;
            }
            else if(currentRollPlayer1 < currentRollPlayer2) {
                player2score++;
            }

            cout << "Score: " << endl << "Player 1: " << player1score << " Player 2: " << player2score << endl << endl;
        }
    }

    else if(dieTypePlayer1 == LOADED && dieTypePlayer2 == STANDARD){

        for (int round = 1; round <= numRounds; round++) {
            int currentRollPlayer1 = LoadedDieObject1->rollLoadedDie();
            int currentRollPlayer2 = DieObject2->rollDie();

            cout << "Round: " << round << endl;
            cout << "Player 1 rolled a " << currentRollPlayer1 << " using a Loaded Die with "
                 << numSidesPlayer1 << " sides" << endl;
            cout << "Player 2 rolled a " << currentRollPlayer2 << " using a Standard Die with "
                 << numSidesPlayer2 << " sides" << endl;

            if(currentRollPlayer1 > currentRollPlayer2) {
                player1score++;
            }
            else if(currentRollPlayer1 < currentRollPlayer2) {
                player2score++;
            }

            cout << "Score: " << endl << "Player 1: " << player1score << " Player 2: " << player2score << endl << endl;
        }
    }

    if(player1score > player2score){
        cout << "Player 1 Wins!" << endl << endl;
    }
    else if(player2score > player1score){
        cout << "Player 2 Wins!" << endl << endl;
    }
    else if (player2score == player1score){
        cout << "It's a Draw!" << endl << endl;
    }
}

Game.hpp


#ifndef GAME_HPP
#define GAME_HPP
#include "Die.hpp"
#include "LoadedDie.hpp"

class Game{

private:

    int numRounds;

    enum dieTypes {STANDARD = 1, LOADED = 2};

    int dieTypePlayer1;
    int dieTypePlayer2;
    int numSidesPlayer1;
    int numSidesPlayer2;

public:

    void gameMenu();

    void play(Die * DieObject1, Die * DieObject2, LoadedDie * LoadedDieObject1, LoadedDie * LoadedDieObject2);

    int inputValidate(int integer);

    int getChoice();

};

#endif

LoadedDie.cpp


#include "LoadedDie.hpp"
#include <cstdlib>

//this function "rolls" a random number that is biased towards higher numbers
int LoadedDie::rollLoadedDie() {

    int randomNumber = rand() % 100 + 1;

    switch(getNumSides()){
        case 6:
            if(randomNumber <= 10)
                return 1;
            else if(randomNumber > 10 && randomNumber <= 22)
                return 2;
            else if(randomNumber > 22 && randomNumber <= 38)
                return 3;
            else if(randomNumber > 38 && randomNumber <= 56)
                return 4;
            else if(randomNumber > 56 && randomNumber <= 78)
                return 5;
            else if(randomNumber > 78 && randomNumber <= 100)
                return 6;
            break;

        case 10:
            if(randomNumber <= 5)
                return 1;
            else if(randomNumber > 5 && randomNumber <= 10)
                return 2;
            else if(randomNumber > 10 && randomNumber <= 16)
                return 3;
            else if(randomNumber > 16 && randomNumber <= 22)
                return 4;
            else if(randomNumber > 22 && randomNumber <= 29)
                return 5;
            else if(randomNumber > 29 && randomNumber <= 36)
                return 6;
            else if(randomNumber > 36 && randomNumber <= 46)
                return 7;
            else if(randomNumber > 46 && randomNumber <= 60)
                return 8;
            else if(randomNumber > 60 && randomNumber <= 80)
                return 9;
            else if(randomNumber > 80 && randomNumber <= 100)
                return 10;
            break;

        case 14:
            if(randomNumber <= 3)
                return 1;
            else if(randomNumber > 3 && randomNumber <= 6)
                return 2;
            else if(randomNumber > 6 && randomNumber <= 9)
                return 3;
            else if(randomNumber > 9 && randomNumber <= 13)
                return 4;
            else if(randomNumber > 13 && randomNumber <= 17)
                return 5;
            else if(randomNumber > 17 && randomNumber <= 21)
                return 6;
            else if(randomNumber > 21 && randomNumber <= 26)
                return 7;
            else if(randomNumber > 26 && randomNumber <= 31)
                return 8;
            else if(randomNumber > 31 && randomNumber <= 40)
                return 9;
            else if(randomNumber > 40 && randomNumber <= 50)
                return 10;
            else if(randomNumber > 50 && randomNumber <= 60)
                return 11;
            else if(randomNumber > 60 && randomNumber <= 70)
                return 12;
            else if(randomNumber > 70 && randomNumber <= 85)
                return 13;
            else if(randomNumber > 85 && randomNumber <= 100)
                return 14;
            break;

    }
}


LoadedDie.hpp


#ifndef LOADEDDIE_HPP
#define LOADEDDIE_HPP

#include "Die.hpp"

//LoadedDie Class is a derived class of Die Class
class LoadedDie: public Die {

public:

    int rollLoadedDie();
};

#endif

Add a comment
Know the answer?
Add Answer to:
Use C++ 11 to write the program War Game Requirement Setting This is a 2-player game....
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
  • The game of Pig is a simple two-player dice game in which the first player to...

    The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die: If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn. If the player rolls 2 through 6, then he or she can either ROLL AGAIN or HOLD:      At this point, the sum of all rolls...

  • 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...

  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

  • Two player Dice game In C++ The game of ancient game of horse, not the basketball...

    Two player Dice game In C++ The game of ancient game of horse, not the basketball version, is a two player game in which the first player to reach a score of 100 wins. Players take alternating turns. On each player’s turn he/she rolls a six-sided dice. After each roll: a. If the player rolls a 3-6 then he/she can either Roll again or Hold. If the player holds then the player gets all of the points summed up during...

  • Program 4: C++ The Game of War The game of war is a card game played by children and budding comp...

    Program 4: C++ The Game of War The game of war is a card game played by children and budding computer scientists. From Wikipedia: The objective of the game is to win all cards [Source: Wikipedia]. There are different interpretations on how to play The Game of War, so we will specify our SMU rules below: 1) 52 cards are shuffled and split evenly amongst two players (26 each) a. The 26 cards are placed into a “to play” pile...

  • Java programming Write a simulation of the Craps dice game. Craps is a dice game that...

    Java programming Write a simulation of the Craps dice game. Craps is a dice game that revolves around rolling two six-sided dice in an attempt to roll a particular number. Wins and losses are determined by rolling the dice. This assignment gives practice for: printing, loops, variables, if-statements or switch statements, generating random numbers, methods, and classes. Craps game rules: First roll: The first roll (“come-out roll”) wins if the total is a 7 or 11. The first roll loses...

  • PLEASE INCLUDE SAW-PROMPTS FOR 2 PLAYERS NAMES(VALIDATE NAMES). SHOW MENU (PLAYER MUST SELECT FROM MENU B4...

    PLEASE INCLUDE SAW-PROMPTS FOR 2 PLAYERS NAMES(VALIDATE NAMES). SHOW MENU (PLAYER MUST SELECT FROM MENU B4 THE GAME STARTS 1=PLAY GAME, 2=SHOW GAME RULES, 3=SHOW PLAYER STATISTICS, AND 4=EXIT GAME WITH A GOODBYE MESSAGE.) PLAYERS NEED OPTION TO SHOW STATS(IN A DIFFERNT WINDOW-FOR OPTION 3)-GAME SHOULD BE rock, paper, scissor and SAW!! PLEASE USE A JAVA GRAPHICAL USER INTERFACE. MUST HAVE ROCK, PAPER, SCISSORS, AND SAW PLEASE This project requires students to create a design for a “Rock, Paper, Scissors,...

  • 2. "Craps" is a game played by rolling two fair dice. To play one round of...

    2. "Craps" is a game played by rolling two fair dice. To play one round of this game, the player rolls the dice and the outcome is determined by the following rules: If the total number of dots is 7 or 11 (a "natural"), then the player wins. If the total number of dots is 2, 3, or 12 C'craps"), then the player loses. If the total number of dots is 4, 5, 6,8,9, or 10, then this number is...

  • I need to build the card game of War in C++. It will be a 2...

    I need to build the card game of War in C++. It will be a 2 player game. Each player will have their own deck of 52 cards. 2-10, Jack=11, Queen=12, King=13, Ace=14. Each player will draw one card from their deck. The player with the higher card wins both cards. If the card drawn is the same, then each player will draw 3 cards and on the 4th card drawn will show it. The player that shows the higher...

  • C++ Homework: This will be the first in a series of assignments to create a game...

    C++ Homework: This will be the first in a series of assignments to create a game called Zilch. In this assignment, you will create the basic structure of the program and demonstrate the ability to create programs with multiple files. It should serve as a refresher in the basics of C++ programming, especially in the handling of arrays. The initial assignments only establish a rough outline of the game. We will be adding the rules that make the game more...

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