The final project in my computer programming class is to create a simple game using C++. Unfortunately most of the semester was spent on a program called Alice instead of C++. This make me a beginner, and I really need some help.
My game is basically a simpler version of Yahtzee. Instead of adding in things like straights and full houses, it just adds up the number of repeated dice values you get after each roll. I tried to create two arrays, one for each player's set of dice, and have them generate random values. I then tried to print out said values, but that isn't working. I tried doing research and getting help from my older brother studying to be a computer engineer, but the internet searches weren't helpful and my brother is having trouble making time to help me.
This is what I have so far. All help will be appreciated.
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
usingnamespace std;
int main ()
{
int diceRoll1 [5];
int diceRoll2 [5];
int ctr1 = 0;
int ctr2 = 0;
string y;
string r;
cout << "Do you want to play? Type in Yes or No: ";
cin >> y;
while ( y != "No" )
{
if ( y != "No" )
{
cout << "-*-*-*-*-Welcome to Dice Poker!-*-*-*-*-" << endl;
cout << "The object of this game is similar to 5 card draw and Yahtzee:" << endl;
cout << "Try to get as many dice with the same" << endl;
cout << "number face up as you can. Type Roll to play: " << endl;
cin >> r;
}
if ( r == "Roll" )
{
for ( ctr1=0, ctr1 <=5, ctr1++;; )
{
srand ((unsigned)time(0));
diceRoll1[ctr1] = (rand()%6)+1;
}
cout << diceRoll1[0] << endl;
cout << diceRoll1[1] << endl;
cout << diceRoll1[2] << endl;
cout << diceRoll1[3] << endl;
cout << diceRoll1[4] << endl;
}
}
system("PAUSE");
return 0;
}
To start, your srand() is getting called more then once. You want to just seed the random function at the beginning of the program and leave it at that. Also your for loop syntax is slightly off. I fixed it for you, look at this.
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
usingnamespace std;
int main ()
{
srand ((unsigned)time(0));
int diceRoll1 [5];
int diceRoll2 [5];
int ctr1 = 0;
int ctr2 = 0;
string y;
string r;
cout << "Do you want to play? Type in Yes or No: ";
cin >> y;
while ( y != "No" )
{
if ( y != "No" )
{
cout << "-*-*-*-*-Welcome to Dice Poker!-*-*-*-*-" << endl;
cout << "The object of this game is similar to 5 card draw and Yahtzee:" << endl;
cout << "Try to get as many dice with the same" << endl;
cout << "number face up as you can. Type Roll to play: " << endl;
cin >> r;
}
if ( r == "Roll" )
{
for ( ctr1=0; ctr1 <=5; ctr1++ )
{
diceRoll1[ctr1] = (rand()%6)+1;
}
cout << diceRoll1[0] << endl;
cout << diceRoll1[1] << endl;
cout << diceRoll1[2] << endl;
cout << diceRoll1[3] << endl;
cout << diceRoll1[4] << endl;
}
}
system("PAUSE");
return 0;
}
Thanks kartracer, that fixed THAT problem...but now that I've continued, I'm having another. I guess I'm not too good at this.
I wrote some code that counts the number of similar dice values, and now I'm trying to get it to determine a winner. I wrote three if statements, one for if player one's doubles value is greater, one for if player two's doubles value is greater, and one for if they are equal. Because of some fault on my part (obviously), all three events happen every time. Help would be appreciated!
You have extra ';' after your other two if statements. Also, you should use an if, else if chain instead of a bunch of ifs there (so you don't waste time checking doubles < doubles2 if doubles was already > doubles2.
Two final problems I have are keeping score. The score for player one is done correctly, but player two's score goes up whether it wins or loses.
Also, I inserted a bit of code that is supposed to determine a winner once a total score of 5 is met. I don't understand why, but it declares a winner (and not always right either) after only one round. Here is the bit of code that my problems are centered around:
runs 6 times and writes data out of the array's boundary. Change it to < 5
1 2 3 4 5 6 7 8 9 10
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
if (diceRoll1[i] == diceRoll1[j] && i != j)
{
doubles++;
}
}
}
Runs more times than you need it to. Try this:
1 2 3 4 5 6 7 8
for(i = 0; i < 5 ; i++)
{
for(j = i + 1 ; j < 5 ; j++)
{
if (diceRoll1[i] == diceRoll1[j])
doubles++; //you don't need {} here since it's just a single statement
}
}
That won't compare the same index or any previous indexes already compared. You won't need to divide doubles by 2. Adjust player 2 similarly. Also, i don't believe you need the {} with the fors either, but to me it makes loops with complex single statements a little more readable.
Make sure you set both players' doubles to 0 each round (unless your game keeps a running total of doubles and awards a win based on total doubles.)