| | |
Game of Crap
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
Hello, I am beginner with c++ programming and I need help with this source. I tried to find a way for getPrintDetails to not print a infomation.
//Course: 4002-208
//Author: Joseph Dalpra
//Topic: Functions
//Name: project4.cpp
//Purpose: Using functions to shorten repeated code
// Play the game of craps for a number of games input by user
// Print the results of the game if the user chooses to
// Allow a random seed or the user to input a seed
//----------------
// Include Section
//----------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
//---------------------------
// Function Prototype Section
//---------------------------
int roll();
int roll2Dice();
int getNumGames();
int setRand();
bool playCraps(int, bool);
int getPrintDetails();
// ------ M A I N P R O G R A M ------
int main ()
{
int numGames = 0; //number of games to play
int point = 0; //value for the roll of the first 2 dice
int seed = 0; //value for the seed
int sum = roll2Dice();
int numWins = 0; //number of wins
int numLosses = 0; //number of losses
double winPercent = 0.0; //value for the percent of wins
int numLongWins = 0; //value for the longest win streak
int numLongLosses = 0; //value for the longest streak of losses
int nextRoll = roll2Dice(); //sequential roll if no win on first roll
bool print;
//call function setRand to get the seed for the random number generator
seed = setRand();
cout<<endl;
//call function getNumGames to get the number of games to be played
numGames =getNumGames();
cout<<endl;
//call getPrintDetails function
getPrintDetails();
//call craps function
//play craps for while k <= number of games
for (int k=1; k<=numGames; k++)
{
if(playCraps(k,print))
numWins++;
}
//calculate the longest streaks of wins
if (point ==7 || point ==11)
{
numLongWins++;
}
int numlose;
numlose = numGames - numWins;
//calculate the win percentage
winPercent= ((double)numWins/(double)numGames)*100;
//print the percentage of games won
//print longest win streak
//print longest loss streak
cout<<"The number of games won out of "<< numGames << " is " << numWins <<endl;
cout << "the percent of games won " << fixed << setprecision(4)<< winPercent <<endl;
cout<<"The number of games lose out of "<< numGames << " is " << numlose <<endl;
//cout<<"Longest run of wins is " << numLongWins <<endl;
cout<<"Longest run of losses is " << numLongLosses <<endl;
//freeze the window
//halt program
system ("Pause");
return 0;
}
//Name:roll
//Purpose: generate a random number 1-6 to simulate a die roll
//
//Parameters: none
//Return: generate a random number 1 - 6 inclusively
//---------------------------------------------------------------
int roll(void)
{
return ( 1 + rand()%6);
}
//Name:roll2Dice
//Purpose: roll 2 dice and add them together to give the player their point
// return value of 2 die rolls
//Parameters: none
//Return: sum
//---------------------------------------------------------------
int roll2Dice(void)
{
int sum = roll() + roll();
return sum;
}
//Name:getNumGames
//Purpose: prompt the user to input the number of games
// validate the input to make sure it is >0
//
//Parameters: none
//Return: numGames
//---------------------------------------------------------------
int getNumGames()
{
int numGames = 0; //number of games to be played
cout<<"Enter number of games to be played: ";
cin>>numGames;
while (numGames <=0)
{
cout<<"Invalid number of games - must be > 1"<<endl;
cout<<"Enter number of games to be played: ";
cin>>numGames;
}
return numGames;
}
//Name:setRand
//Purpose: prompt user for input of a seed
// 1-50 start the seed at input value
// 0 is a random seed
//Parameters: none
//Return: none
//---------------------------------------------------------------
int setRand()
{
int seed; //value for seed for random generator
cout<<"Enter the seed 1..50 or 0 for random seed: ";
cin>>seed;
if (seed == 0)
{
srand(seed);
}
while (seed <0 || seed > 50)
{
cout<<"Invalid seed - must be 0..50"<<endl;
cout<<"Enter seed: ";
cin>>seed;
}
srand(seed);
}
//Name:getPrintDetails
//Purpose:
//
//
//Parameters: none
//Return: none
//---------------------------------------------------------------
int getPrintDetails()
{
int gameNum;
int point;
bool print = ' ';
cout<<"Do you wish to print details <Y/N>?: ";
cin>>print;
print=toupper(print);
if (print == 'Y')
{
return true;
}
else
{
return false;
}
}
//---------------------------------------------------------------
//Name:playCraps
//Purpose: play a game of craps
//
//
//Parameters: none
//Return: none
//---------------------------------------------------------------
bool playCraps(int gameNum, bool print)
{
int point = roll2Dice();
int nextRoll;
int numLosses = 0;
int numWins = 0;
cout << "Game " << gameNum << ": " << point;
if (point ==2 || point==3 ||point==12)
{
cout<<" loss "<<endl;
return false;
}
else if (point ==7 || point ==11)
{
cout<<" win "<<endl;
return true;
}
else
{
do
{
nextRoll=roll2Dice();
cout<<" " << nextRoll;
}while(nextRoll !=7 && nextRoll !=point);
if(nextRoll == 7)
{
cout<<" loss "<<endl;
return false;
}
else
{
cout<<" win "<<endl;
return true;
}
}
return print;
}
//Course: 4002-208
//Author: Joseph Dalpra
//Topic: Functions
//Name: project4.cpp
//Purpose: Using functions to shorten repeated code
// Play the game of craps for a number of games input by user
// Print the results of the game if the user chooses to
// Allow a random seed or the user to input a seed
//----------------
// Include Section
//----------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
//---------------------------
// Function Prototype Section
//---------------------------
int roll();
int roll2Dice();
int getNumGames();
int setRand();
bool playCraps(int, bool);
int getPrintDetails();
// ------ M A I N P R O G R A M ------
int main ()
{
int numGames = 0; //number of games to play
int point = 0; //value for the roll of the first 2 dice
int seed = 0; //value for the seed
int sum = roll2Dice();
int numWins = 0; //number of wins
int numLosses = 0; //number of losses
double winPercent = 0.0; //value for the percent of wins
int numLongWins = 0; //value for the longest win streak
int numLongLosses = 0; //value for the longest streak of losses
int nextRoll = roll2Dice(); //sequential roll if no win on first roll
bool print;
//call function setRand to get the seed for the random number generator
seed = setRand();
cout<<endl;
//call function getNumGames to get the number of games to be played
numGames =getNumGames();
cout<<endl;
//call getPrintDetails function
getPrintDetails();
//call craps function
//play craps for while k <= number of games
for (int k=1; k<=numGames; k++)
{
if(playCraps(k,print))
numWins++;
}
//calculate the longest streaks of wins
if (point ==7 || point ==11)
{
numLongWins++;
}
int numlose;
numlose = numGames - numWins;
//calculate the win percentage
winPercent= ((double)numWins/(double)numGames)*100;
//print the percentage of games won
//print longest win streak
//print longest loss streak
cout<<"The number of games won out of "<< numGames << " is " << numWins <<endl;
cout << "the percent of games won " << fixed << setprecision(4)<< winPercent <<endl;
cout<<"The number of games lose out of "<< numGames << " is " << numlose <<endl;
//cout<<"Longest run of wins is " << numLongWins <<endl;
cout<<"Longest run of losses is " << numLongLosses <<endl;
//freeze the window
//halt program
system ("Pause");
return 0;
}
//Name:roll
//Purpose: generate a random number 1-6 to simulate a die roll
//
//Parameters: none
//Return: generate a random number 1 - 6 inclusively
//---------------------------------------------------------------
int roll(void)
{
return ( 1 + rand()%6);
}
//Name:roll2Dice
//Purpose: roll 2 dice and add them together to give the player their point
// return value of 2 die rolls
//Parameters: none
//Return: sum
//---------------------------------------------------------------
int roll2Dice(void)
{
int sum = roll() + roll();
return sum;
}
//Name:getNumGames
//Purpose: prompt the user to input the number of games
// validate the input to make sure it is >0
//
//Parameters: none
//Return: numGames
//---------------------------------------------------------------
int getNumGames()
{
int numGames = 0; //number of games to be played
cout<<"Enter number of games to be played: ";
cin>>numGames;
while (numGames <=0)
{
cout<<"Invalid number of games - must be > 1"<<endl;
cout<<"Enter number of games to be played: ";
cin>>numGames;
}
return numGames;
}
//Name:setRand
//Purpose: prompt user for input of a seed
// 1-50 start the seed at input value
// 0 is a random seed
//Parameters: none
//Return: none
//---------------------------------------------------------------
int setRand()
{
int seed; //value for seed for random generator
cout<<"Enter the seed 1..50 or 0 for random seed: ";
cin>>seed;
if (seed == 0)
{
srand(seed);
}
while (seed <0 || seed > 50)
{
cout<<"Invalid seed - must be 0..50"<<endl;
cout<<"Enter seed: ";
cin>>seed;
}
srand(seed);
}
//Name:getPrintDetails
//Purpose:
//
//
//Parameters: none
//Return: none
//---------------------------------------------------------------
int getPrintDetails()
{
int gameNum;
int point;
bool print = ' ';
cout<<"Do you wish to print details <Y/N>?: ";
cin>>print;
print=toupper(print);
if (print == 'Y')
{
return true;
}
else
{
return false;
}
}
//---------------------------------------------------------------
//Name:playCraps
//Purpose: play a game of craps
//
//
//Parameters: none
//Return: none
//---------------------------------------------------------------
bool playCraps(int gameNum, bool print)
{
int point = roll2Dice();
int nextRoll;
int numLosses = 0;
int numWins = 0;
cout << "Game " << gameNum << ": " << point;
if (point ==2 || point==3 ||point==12)
{
cout<<" loss "<<endl;
return false;
}
else if (point ==7 || point ==11)
{
cout<<" win "<<endl;
return true;
}
else
{
do
{
nextRoll=roll2Dice();
cout<<" " << nextRoll;
}while(nextRoll !=7 && nextRoll !=point);
if(nextRoll == 7)
{
cout<<" loss "<<endl;
return false;
}
else
{
cout<<" win "<<endl;
return true;
}
}
return print;
}
0
#2 Oct 31st, 2009
C++ Syntax (Toggle Plain Text)
int getPrintDetails() { int gameNum; int point; char print = 0; cout<<"Do you wish to print details <Y/N>?: "; cin >> print; return (print == 'Y'); }
0
#7 Oct 31st, 2009
If I understand you correctly, something like this:
C++ Syntax (Toggle Plain Text)
int getPrintDetails() { int gameNum = 0; int point = 0; char print = 0; cout<<"Do you wish to print details <Y/N>?: "; cin >> print; return playCraps(gameNum , print == 'Y'); }
Я из Молдавии. Говорю на Русском.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need some help with if statements
- Next Thread: How do I find the total sale for my program??
| Thread Tools | Search this Thread |
Tag cloud for c++, game
add api array arrays assignment background based beginner binary bitmap c# c++ card char char* class code community compile console database design development download dwmapi ect egg embed encryption error exam file fstream function functions game gamer games gauntanamo gdi+ graph helpwithhomework html input int intellectualproperty java jni jygame kioti16 math microsoft msn multiple newbie news nintendo node number objects output playstation pointers popular practice prime problem program programmer programming project ps3 python read recursion recursive rpg samples search sony sticky string strings struct studio subclass symbian system template templates testing text tree url visual wii win32 win32api xbox xbox360







