C++ Help Please!

Everything todo with programming goes HERE.
Post Reply
Marshmellow
Posts: 6
Joined: Mon Apr 06, 2009 2:46 am

C++ Help Please!

Post by Marshmellow »

I'm trying to make a game, here are the directions:

The game of 50/50 is a simple two person dice game in which the first player to reach 50 or more points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll:
• If the player rolls a 2- 6 then he can either:
o Roll again or
o Hold (at this point the sum of all rolls made this turn are added to the players total score and it becomes the others players turn)
• If the player rolls a 1 then the player loses his turn. He/She gets no new points and it becomes the opponent’s turn.
If the player reaches 50 or more points after holding then the player wins.

Write a program that plays the game of 50/50, where one player is a human and the other is the computer. Allow the human to roll first. Allow the human to input “r” to roll again or “h” to hold.

The computer should play according to the following rule: keep rolling on its turn until it has accumulated 15 or more points, then hold. Of course, if the computer rolls a 1 then the turn ends immediately.

Write your program with at least two functions:
int humanTurn( );
int computerTurn( );

The functions should perform the necessary logic to handle a single turn for either the computer or the human. The functions should return the turn total to be added to the total score upon competition of the turn. For example if the human rolls a 3 and 6 and then holds, then humanTurn should return 9. However if the human rolls a 3 and 6 and 1 then the function should return 0.
.
Requirements

The program will require the following parts:

• Utilize the random number generator to generate random dice value in each roll
• Display the dice value after each roll and display the total points after each turn
• When game is over, display who is the winner and the total points of the winner.
• Handling of any invalid human input data
• Commented code including your name and a description of the program






I think I made the random number generator (which I will post below) but where do I go from there?

Random Number Generator:
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
srand((unsigned)time(0));

int i;
i = (rand()%6)+1;

cout << i << "\n";

}
User avatar
Z-Man
God & Project Admin
Posts: 11717
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

Re: C++ Help Please!

Post by Z-Man »

This smells like homework. So:
Write the required functions
int humanTurn( );
int computerTurn( );
and glue them together in the main function.

It would be unethical (and, in the long run, unhelpful) to give you any more help at the point you're at now. Come back and post code you got, we can help with compilation errors or problems you can't handle yourself ("oh, that's easy, you have an extra semicolon after your for() loop statement").

One right now: rand() % 6 gives bad random numbers, the lower bits of rand() can repeat quickly. You want rand()/((RAND_MAX/6)+1) instead. Not that it matters much.
Marshmellow
Posts: 6
Joined: Mon Apr 06, 2009 2:46 am

Re: C++ Help Please!

Post by Marshmellow »

It's actually not homework since I don't even have a programming class, lol
Im just trying to teach myself C++ and I figure the best way to learn is to actually do it.
I don't have any program to try this out either which kinda sucks.
I tried getting Visual Studio for my Mac, but it just came up as garble in Text Edit form :/
Fair enough, though. I'll try it on my own and post what I figured out :)

FOR COMPUTER:

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main() {
int computerTurn() {

srand(time(0));

int DieRoll;
DieRoll = rand()/((RAND_MAX/6)+1);
cout << DieRoll << endl;

while (DieRoll > 1) {
cout << DieRoll << endl;
}
else {
cout << 0 << endl;
}

if (DieRoll < 15) {
DieRoll++;
}
else {
break;
}

}
system ("PAUSE");
return 0;
}

This way, I have it so the rolls add up per turn, but how do I make it so the turns add up? Do I have to put in a function?
User avatar
Z-Man
God & Project Admin
Posts: 11717
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

Re: C++ Help Please!

Post by Z-Man »

Ah, ok. In that case, you should probably start with easier problems, not complete games. A program that prints all square numbers between 0 and 1000. A program that prints all prime numbers in the same range.
Marshmellow wrote:I tried getting Visual Studio for my Mac, but it just came up as garble in Text Edit form :/
Well, it's a Windows program, so what do you expect? You'll either have to use it on Windows or stick with Mac native programs such as the default development system XCode. Anyway, let's see what we got:
int main() {
int computerTurn() {
That won't work, you can't declare functions inside other functions in C(++). You need

Code: Select all

int computerTurn() {
 ... stuff ...
}

int main() { 
 ... other stuff using computerTurn() ...
}
instead. The code you have so far would go into computerTurn().
while (DieRoll > 1) {
cout << DieRoll << endl;
}
Since DieRoll is a variable you assigned a single value to earlier, this will either not execute the print line or execute it in an infinite loop. You'd need to reassign DieRoll a new value every iteration.
else {
cout << 0 << endl;
}
Last time I checked, while loops had no else clause. I sometimes wish they did.
if (DieRoll < 15) {
DieRoll++;
}
else {
break;
}
DieRoll++ increases the DieRoll by just 1, that's not what should happen any time. Instead, you want to rethrow the die. Also, the break clause only is valid inside the while loop from earlier (or other loops), and the AI abort criterion (DieRoll < 15) should also go inside the die roll loop.
but how do I make it so the turns add up? Do I have to put in a function?
You need more variables for that. DieRoll should be used exclusively to hold the result of the last roll, you need at least three more:
- the total score so far of the human player (global or, better, in main())
- the total score so far of the AI player (likewise)
- the score accumulated in the current turn (can be local to computer/humanTurn())

Code: Select all

system ("PAUSE");
Umm, that's probably from an old DOS handbook. Just leave it out, the output of your program will stay visible even after it exits.
Post Reply