Write your question here.
create a craps game 3 files ( main cpp. dice.cpp dice.h files) You will need to create a Dice or Die class to play. Use "rand() % 6 + 1" to produce a random number between 1 and 6. Create two instances (objects) of the Die/Dice class to conduct each roll and call a function called roll to produce two int between 1 and 6 and add the results together for a roll.
Craps has two phases: the first roll and the next rolls.
If the first roll is 7 or 11 the players wins. Print out (cout) you win!
If the first roll is 2 or 12 the player loses. Print out you lose!
Any other roll becomes the target (point) and the game continues.
On consecutive rolls:
If the roll = the target (points) the player wins. Print out you win!
If the roll = 7 the player loses. Print out you lose.
Keeps rolling until the game is over (player wins or loses).
int main()
{ for (int i = 0; i < 1000; i++)
{ int die1, die2, roll1, roll2, point,;
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
point = die1 + die2;
cout << "You rolled: " << point << endl;
if( point == 7 || point == 11 )
{cout << "You win! Would you like to play again? "; }
else if( point == 2 || point == 3 || point == 12 )
{cout >> "Sorry you lose. Do you want to play agin? ";
}
return 0;
}
You're not following the instructions directing you to create a class. You need to write a class called "die" which has a method called "roll". That class should be in its own file with its own header file. Then create a main file where you include the "die" class so you can use it to instantiate two dies that will be used in the craps game itself.
You also need to know the rules of craps. If you don't win or crap out on your roll you have to keep going and try and hit your target number. See here: http://en.wikipedia.org/wiki/Craps
Write your die class first so you can test it on its own before moving on to the main program logic that implements the rules of craps.
I try to write the code as the instruction, but I got errors:
1>c:\users\naif\documents\visual studio 2010\projects\craps_game\craps_game\main.cpp(11): error C2039: 'crap' : is not a member of 'Craps' 1> c:\users\naif\documents\visual studio 2010\projects\craps_game\craps_game\craps.h(6) : see declaration of 'Craps'
I get no errors when I compile the above code with VS2010.
You've created a class to represent the game, but you're still not following the instructions to create a class to represent a die.
You also not modeling the game of craps correctly. If the first roll is not 1,7,2 or 12, the game is not over. See the instructions regarding consecutive rolls.
The instructions are your program! Just take each part of the instructions and restate them using the C++ statements:
Create a Dice or Die class to play.
In the class you created, include a value and a function "roll," using "rand() % 6 + 1" to set the class's value to a random number between 1 and 6
In main():
Create two instances (objects) of the Die/Dice class to conduct each roll
For each play of the game, do the following:
Starting out each game there is no point to compare:
Starting out each game you will continue to throw dice until you either win or lose:
//This starts the still playing loop:
Call the "roll" function in each object (die) you created, to set the value of each die to a random number;
add the value from each object to get your initial roll value.
//Craps has two phases: the first roll and the next rolls.
Phase 1 - if there is no point established:
If the first roll is 7 or 11
The player wins.
Print out (cout) you win!
The game ends.
If the first roll is 2 or 12
The player loses.
Print out you lose!
The game ends
Any other roll becomes the target (point)
The game continues.
Phase 2 - else the point has been set -- you didn't win or lose on the first roll, so do this until you either win or lose:
If the roll = the target (points)
The player wins.
Print out you win!
The game ends.
If the roll = 7
The player loses.
Print out you lose.
The game ends.
if you won or lost,
Repeat from the beginning of the game,
otherwise
Go back to the beginning of the "still playing" loop and roll again.