Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: Use objects and functions Use header file Use classes Background: Cr

ID: 3811251 • Letter: O

Question

Objectives: Use objects and functions Use header file Use classes Background: Craps is a dice game in which the players make wagers on the outcome of the roll, or series of rolls, of a pair of dice^1. To start a round, the shooter makes a "come-out" roll. A come-out roll of 2, 3 or 12 is called "craps" and is an immediate loss. A come-out roll of 7 or 11 is a "natural", and is an immediate win. The other possible rolls are called the "point" numbers: 4, 5, 6, 8, 9, and 10. If the shooter rolls one of these numbers on the come-out roll, this establishes the "point", and the point number must be rolled again before a seven on subsequent rolls in order to win. Assignment: You will create this game that you did in hw03b and hw04b, but this time using objects. Give the user 5 chips initially. Subtract 1 for a loss and add one for a win. Stop when the user is out of chips or has 10 chips. Output the number of chips at the end of each game. Your program must do the following: You must create a dev-c++ PROJECT for everything to run correctly. The project will contain the 3 files below Create and save a header file named Craps.h with private data members and public functions prototypes. Create a .cpp file named Game.cpp to keep the function definitions. Create the main program named hw07.cpp as the main program to declare objects and call functions. The number of functions you decide to use may vary, but you should have functions for making a roll, getting the total, incrementing/decrementing chips, and functions for displaying the needed variables. Declare at least these private variables: int roll1, roll2, total, point, chips; int maxChips, minChips;

Explanation / Answer

/*********************************/
/************craps.h**************/
/*********************************/

#ifndef CRAPS_H
#define CRAPS_H

class Craps{
private:
int roll1, roll2, total, point, chips;
int maxChips, minChips;

public:
//declare function prototypes for private data memebers, setters
int setRoll1();
int setRoll2();
int setTotal();
int setPoint();
int setChips(int n);
int setMaxChips(int n);
int setMinChips(int n);

//declare function prototypes for private data memebers, getters
int getRoll1();
int getRoll2();
int getTotal();
int getPoint();
int getChips();
int getMaxChips();
int getMinChips();

int increaseChips();
int decreaseChips();
}


/**********************************/
/*************game.cpp**********/
/**********************************/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "craps.h"

//define setters
Craps::setChips(int n){
chips = n;
}

Craps::setRoll1(){
int temp = rand() % 12 + 1; //generates a random between 1 and 12
roll1 = temp;
}

Craps::setRoll2(){
int temp = rand() % 12 + 1;
roll2 = temp;
}

Craps::setTotal(){
total = roll1 + roll2;
}

Craps::setPoint(){
switch(roll1){
case 2:
case 3:
case 12:
case 7:
case 11: break;
case 4:
case 5:
case 6:
case 9:
case 9:
case 10: point = roll1; break;
}
}

Craps::setMaxChips(int n){
maxChips = n;
}

Craps::setMinChips(int n){
minChips = n;
}


//define getters
Craps::getChips(){
return chips;
}

Craps::getRoll1(){
return roll1;
}

Craps::getRoll2(){
return roll2;
}

Craps::getTotal(){
return total;
}

Craps::getPoint(){
return point;
}

Craps::getMaxChips(){
return maxChips;
}

Craps::getMinChips(){
return minChips;
}


//define other functions
Craps::increaseChips(){
chips += 1;
}

Craps::decreaseChips(){
chips -= 1;
}


/***********************************/
/*********** hw07.cpp *************/
/***********************************/
#include <iostream>
#include "game.cpp"

using namespace std;

int main(){
Craps g1; //object of class Craps


g1.setMaxChips(10); //max number of chips
g1.setMinChips(1); // min number of chips
g1.setChips(5); //give user 5 chips initially

while( g1.getChips() != g1.getMaxChips() || g1.getChips() != g1.getMinChips() ) //run until chips are 10 or 1
{
//roll both dices & set total
g1.setRoll1();
g1.setRoll2();
g1.setTotal();

//get total
int temp = g1.getTotal();
if(temp == 2 || temp == 3 || temp == 12)
{
cout<<"You loose. Dice total: "<<g1.getTotal();
g1.decreaseChips();
}else if(temp == 7 || temp == 11){
cout<<"You win. Dice total: "<<g1.getTotal();
g1.increaseChips();
}else{
g1.setPoint();

while( g1.getTotal() != g1.getPoint() )
{
if( g1.getTotal() == 7 )
{
g1.decreaseChips();
cout<<"You loose. Dice total: "<<g1.getTotal();
break;
}

if( g1.getTotal() == g1.getPoint() )
{
g1.increaseChips();
cout<<"You win. Dice total: "<<g1.getTotal();
break;
}

//roll both dices again and set total
g1.setRoll1();
g1.setRoll2();
g1.setTotal();

}
}//enf if-else

cout<<"You now have "<<g1.getChips()<<" chips.";

}//end while

}