Question

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 interesting as we progress through the course.

Zilch is a game played with 6 six-sided dice. Each die can take on the value of 1 to 6. Any number of people can play a game of Zilch, but for this assignment, you will need to handle 1 player. A player's turn consists of rolling the set of dice and calculating the resulting score. At the end of each turn, the player has the option of quitting or continuing to play. The game ends with the player's score reaches or exceeds 1,000 points. You should track two scores for the play: the points gained in the current round and the accumulated score of all prior rounds.

In this assignment, scoring will be simple:

  • For each 1 rolled, the player receives 100 points
  • For each 5 rolled, the player receives 50 points

A player's score is the sum of all his points

Building Your Program

Each object in your program will have its own class. Each class will have its own .CPP and .H file. In addition, you will have a .CPP file for the main function.

You will need to create a Die class that represents a single 6-sided die.

Output

  • The player's roll
  • The score of the player's roll
  • The accumulative score of the player
  • An announcement if the player wins by crossing the 1,000 point mark.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code of main.cpp

#include <iostream>
#include "Die.h"
using namespace std;

class Player{
private:
int score=0;
string name;
int score_board[20];
int round=0;
public:
// Default Constructor
Player(string n) {
name = n;
round = 0;
}
int getScore(){
return score;
}
void setScore(int s){
score = s;
}
string getName(){
return name;
}
int roll_dice(){
Die d;
int die = d.roll();
if (die == 1)
score = score + 100;
if (die == 5)
score = score + 50;
return die;
}
bool is_winner(){
if(score >= 1000)
return true;
return false;
}
void reset_score(){
score_board[round] = score;
score = 0;
}
void increase_round(){
round++;
}
int total_score() {
int total=0;
for(int i=0; i <=round; i++) {
total = total + score_board[i];
}
return total;
}
};


int main(){
string name;
char turn,ans;
string enter;
cout << "welcome to Zilch (Base Game)!\n";
cout << "Enter your first name: ";
cin >> name;
cout << "Hello " << name << endl;


Player p(name);
Player computer("Computer");
int round = 1;
for(;round != 0;) { // play the game in different round

while (true){ // play the game in a single round
cout << "Your turn. Press 'r' to roll the dice\n";
cin >> turn;
if( turn == 'r' ) {
cout << "You rolled :" << p.roll_dice() << endl;
cout << "Your score: " << p.getScore() << endl;
if (p.is_winner()){
cout << "Congratulations. You win the game!";
break;
}
}
cout << "Computer rolled: " << computer.roll_dice() << endl;
if (computer.is_winner()){
cout << "You lose. Computer won the game!";
break;
}
cout << "Want to quit?(y/n)\n";
cin >> ans;
if(ans == 'y'){
break;
}
}
cout << "Your score in this round is :"<< p.getScore() << endl;
cout << "Computer score in this round is :"<< computer.getScore() << endl;
p.reset_score();
computer.reset_score();
cout << "Your total score in this game is :"<< p.total_score() << endl;
cout << "Computer total score in this game is :"<< computer.total_score() << endl;

cout << "Want to play another round?(y/n)\n";
cin >> ans;
if(ans == 'n'){
round = 0;
} else {
// increase round counter
p.increase_round();
computer.increase_round();
round++;
}
}
cout << "Thanks for playing the game. Goodbye."<< endl;
}

-----------------------------------------

code of Die.cpp

#include <ctime>
#include <cstdlib>
#include "Die.h"

int Die :: roll(){
srand(static_cast<unsigned int>(clock()));
int random = rand()%6+1;
return random;
}

---------------------------

code of Die.h

#ifndef DIE_H
#define DIE_H
class Die{
public:
int roll();
};
#endif

Here is the program output:

welcome to Zilch (Base Game)!
Enter your first name: Robert
Hello Robert
Your turn. Press 'r' to roll the dice
r
You rolled :5
Your score: 50
Computer rolled: 6
Want to quit?(y/n)
n
Your turn. Press 'r' to roll the dice
r
You rolled :6
Your score: 50
Computer rolled: 3
Want to quit?(y/n)
y
Your score in this round is :50
Computer score in this round is :0
Your total score in this game is :50
Computer total score in this game is :0
Want to play another round?(y/n)
y
Your turn. Press 'r' to roll the dice
r
You rolled :4
Your score: 0
Computer rolled: 4
Want to quit?(y/n)
n
Your turn. Press 'r' to roll the dice
r
You rolled :2
Your score: 0
Computer rolled: 4
Want to quit?(y/n)
y
Your score in this round is :0
Computer score in this round is :0
Your total score in this game is :50
Computer total score in this game is :0
Want to play another round?(y/n)
n

Add a comment
Know the answer?
Add Answer to:
C++ Homework: This will be the first in a series of assignments to create a 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
  • #2 New Turn Rules A frustrating aspect of programming on a project with other programmers is the ...

    #2 New Turn Rules A frustrating aspect of programming on a project with other programmers is the fact that everyone solves problems in different ways. Just when you think you know which way things are headed, someone throws a snag in your design. Objects needed: Die Attributes Current value of the die Actions roll the die DiceSet Attributes An array of six Die The number of dice available to roll. When this number reaches 0, it is reset to 6....

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

  • For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two p...

    For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn,...

  • Create a Dice Game: PYTHON This game is between two people. They will roll a pair...

    Create a Dice Game: PYTHON This game is between two people. They will roll a pair of dice each 10 times. Each roll will be added to a total, and after 10 rolls the player with the highest total will be the Winner! You will use the RANDOM class RANDRANGE to create a random number from 1 to 6. You'll need a function for "rollDice" to roll a pair of dice and return a total of the dice. You must...

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

  • // This program creates a simulated player who takes one turn in the // Pig dice...

    // This program creates a simulated player who takes one turn in the // Pig dice game. The simulated player keeps rolling the die until // the total for the turn is 20 or greater, or until a 1 is rolled. // If a 1 is rolled, the player's score for the turn is 0. Otherwise // the player's score is the sum of the rolls for the turn. // ///////////////////////////////////////////////////////////////////// #include<iostream> #include<cstdlib> #include<time.h> using namespace std; int randNumGen(int upper,...

  • A dice game is played as follows: The "buy-in" to play a round costs $4. Then...

    A dice game is played as follows: The "buy-in" to play a round costs $4. Then a fair 6-sided die is rolled and the player receives an equal number of dollars as the number shown on the die (i.e. 1 earns $1, 2 earns $2, etc.). What is the player's expected profit from playing one round of the game? -$0.25 $0.25 O-$1.00 -$0.50 Page 4 of 34 Dronnie Dace Next Pare

  • please write the following program in Java PIG is one of a family of games called...

    please write the following program in Java PIG is one of a family of games called jeopardy dice games, since a player's main decision after each roll is whether to jeopardize previous gains by trying for potentially even greater gains. In PIG, all players start with ZERO points, and each turn involves rolling a die to earn points. The first player to earn 100 points or more total shouts "PIG!" and wins the game. On their turn, a player does...

  • Complete each problem separately and perform in python. 1. Create a script that will roll five...

    Complete each problem separately and perform in python. 1. Create a script that will roll five dice and print out their values. Allow the user to roll the dice three times. Prompt them to hit enter to roll. Ex: Hit enter to roll 1,4,3,6,6 Hit enter to roll 3,5,1,2,1 Hit enter to roll 4,3,4,2,6 2. Write a script that will roll five dice (just one time). The user's score will be the sum of the values on the dice. Print...

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

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
ADVERTISEMENT