Not sure if it's (rand() % 6 + 1) or my if and else statements. Could someone help me with this.
Also is there any way to attach an image of the output im getting?
#include <iostream>
#include<string>
#include<iomanip>
#include<conio.h>
usingnamespace std;
int Player1Dice1;// declare player 1 first dice
int Player1Dice2;//declare player 1 second dice
int Player1Sum;//declare player 1 sum of both dice
int Player1Wins;// declare Player 1 number of wins
int Player2Dice1;//declare player 2 first dice
int Player2Dice2;//declare player 2 second dice
int Player2Sum;//declare player 2 sum of both dice
int Player2Wins;//declare Player 2 number of wins
int Round = 1;
int main(){
cout << "Welcome to a C++ port of Akeems Dice game simulator" << endl << endl;
while (Round >= 0 && Round <= 10) //while loop to keep game running for 10 rounds
{
cout << "**************** ROUND" << Round << " ****************" << endl;
cout << "Player 1 press any key to roll dice" << endl << endl;
system("PAUSE");//Waits for user button input to continue
Player1Dice1 = (rand() % 6 + 1);// first random dice roll
Player1Dice2 = (rand() % 6 + 1);// second random dice roll
Player1Sum = Player1Dice1 + Player1Dice2; // sum of player ones dice rolls
cout << "Player 1 rolled " << Player1Dice1 << " & a " << Player1Dice2 << endl;
if (Player1Dice1 == Player1Dice2)
{
cout << "PLAYER 1 ROLLED DOUBLE. PLAYER 1 WINS!!!" << endl;
Player1Wins = Player1Wins + 1;
}
elseif (Player1Dice1 == 1 && Player1Dice2 == 1)// SNAKE EYES IF STATEMENT IF DICE BOTH 1
{
cout << "SNAKE EYES!!! PLAYER 1 HAS LOST ! " << endl;
Player2Wins = Player2Wins + 1;
}
//pLAYER 2 TURN
else {
cout << "Player 2 press any key to roll dice" << endl << endl;
system("PAUSE");//Waits for user button input to continue
Player2Dice1 = (rand() % 6 + 1);// first random dice roll
Player2Dice2 = (rand() % 6 + 1);// second random dice roll
Player2Sum = Player2Dice1 + Player2Dice2; // sum of player 2 dice rolls
cout << "Player 2 rolled " << Player2Dice1 << " & a " << Player2Dice2 << endl;
}
if (Player2Dice1 == Player2Dice2)
{
cout << "PLAYER 2 ROLLED DOUBLE. PLAYER 2 WINS!!!" << endl;
Player2Wins = Player2Wins + 1;
}
elseif (Player2Dice1 == 1 && Player2Dice2 == 1)//SNAKE EYES IF STATEMENT IF DICE BOTH 1
{
cout << "SNAKE EYES!!! PLAYER 1 HAS LOST ! " << endl;
Player1Wins = Player1Wins + 1;
}
Round = Round + 1;
}// end of while loop
//Display results and winner
cout << "**********************************************" << endl;
cout << "* RESULTS *" << endl;
cout << "**********************************************" << endl;
cout << "PLAYER 1 WON " << Player1Wins << "OUT OF 10 ROUNDS" << endl;
cout << "PLAYER 2 WON " << Player2Wins << "OUT OF 10 ROUNDS" << endl;
if (Player1Wins < Player2Wins)//if player 1 has less wins then player 2 player one wins
{
cout << " . . ." << endl;
cout << " . o o ." << endl;
cout << " . \_/ ." << endl;
cout << " . . ." << endl;
cout << "PLAYER 2 WINS!!!!" << endl;
}
elseif (Player1Wins > Player2Wins)//if player 1 has more wins then player 1 wins
{
cout << " . . ." << endl;
cout << " . o o ." << endl;
cout << " . \_/ ." << endl;
cout << " . . ." << endl;
cout << "PLAYER 1 WINS!!!!" << endl;
}
cout << "The game has ended" << endl;
cout << "Press any key to continue..." << endl;
system("PAUSE");
}
Line 74: Your cout is incorrect. It should be player 2 has lost.
Lines 93,101: You have an invalid escape sequence. If you want to use a \ in a quoted string, you need to escape the \ as \\.
Lines 67-77: You probably want these lines within the else for player 2 turn (54-65). You're executing 67-77 even if player 2 did not roll.
You're still not handling a tie.
As Zhuge mentioned, you're not calling srand() to initialize the random number generator. Without it, you're going to get the same sequence of rolls every time. srand() should be called only once at the beginning of main.
Lines 31-35 and 59-61: These lines are prefect candidates for a function.
This site is great. Thanks guys!. I'll have to wait till tomorrow but ill make the corrections and see where i get. Thanks again. Also i will be more descriptive next time.
So I realized I had to right most of the code over to add Tie handling. Also making the task harder without functions. (Dont ask). I took all suggestions and redid the code but I am still having problems.
The program should simulate and display Player 1 and two Dice rolls. Store them and them go through all the possible outcomes.
Tie , Snake eyes , P1 win, or P2 wins. It will do this for 10 rounds then tally it all and display the winner and stats on screen.
A couple of problems I noticed is I am not getting RANDOM numbers and my else if are all messed up I think. The sums aren't adding up and snake eyes from a 6 & 6 ....
And here's The output .
**************** ROUND 1 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 0 & a 0
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 0 & a 0
Both players 1 & 2 rolled doubles
Its a tie! Restart Round 1
**************** ROUND 1 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 6 & a 6
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 5 & a 5
Player 2 Rolled SNAKE EYES!!! //?????????? HUH
Player 1 WINS!!
Player 2 Loses
**************** ROUND 2 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 6 & a 5
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 1 & a 1
Player 1 Rolled a Double! //???? HUH
Player 1 WINS
**************** ROUND 3 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 3 & a 3
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 6 & a 6
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 4 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 4 & a 4
Player 2 press any key to roll dice //4AND 4 IS 8
Press any key to continue . . .
Player 2 rolled 2 & a 6
Player 1 rolled the sum of 5& Player 2 rolled the sum of = 5
Its a tie! Restart Round 4
**************** ROUND 4 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 2 & a 3
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 4 & a 1
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 5 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 1 & a 1
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 3 & a 4
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 6 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 5 & a 5
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 4 & a 3
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 7 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 6 & a 6
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 6 & a 1
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 8 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 1 & a 1
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 4 & a 5
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 9 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 2 & a 2
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 2 & a 1
Player 1 Rolled a Double!
Player 1 WINS
**************** ROUND 10 ****************
Player 1 press any key to roll dice
Press any key to continue . . .
Player 1 rolled 4 & a 4
Player 2 press any key to roll dice
Press any key to continue . . .
Player 2 rolled 3 & a 4
Player 1 Rolled a Double!
Player 1 WINS
**********************************************
* RESULTS *
**********************************************
PLAYER 1 WON 10OUT OF 10 ROUNDS
PLAYER 2 WON 0OUT OF 10 ROUNDS
. . .
. o o .
. \_/ .
. . .
PLAYER 1 WINS!!!!
The game has ended
Press any key to continue...
Press any key to continue . . .
Lines 8-16: The variables don't need to be global by the looks of it (although if they do please ignore this).
Line 22: You need to seed your random, or it will produce the same values each time.
Line 26: Since you're setting round to one yourself, and it only increases, you don't need to check the lower bound.
Lines 33 & 42: Here are your big problems. In these lines, you are printing out the roles for player 1 and player two respectively. However, it is not until 35 & 45 respectively that you roll the dice, so the values you're outputting for the current round are actually from the previous round, but the message you display will be for the current round.
Line 66: You're checking whether the got the same sum before checking whether either of them got doubles. This may be intended, but if it's not, you will be saying it is a tie in the case of Player 1 (2, 4) Player 2 (3, 3). That else if should probably be moved to line 123 to remedy the situation.
Other than that, a good thing to know to shorten your code a bit is that, instead of writing
round = round + 1 (or anything of the format x = x + 1), you could write round++ (or ++round) which has the same functionality.
#include <iostream>
#include <stdlib.h>
#include <ctime>
usingnamespace std;
int cast (int); // Proto type your function
int main()
{
srand(time(0)); // Make random seed with off computer time
// Any time you need a die cast, you can do this...
cout << "Die cast is " << cast(6) << endl;
cout << "Die cast is " << cast(20) << endl;
cout << "Die cast is " << cast(12) << endl;
cout << "Die cast is " << cast(100) << endl;
cout << "Die cast is " << cast(75) << "\n\n\n" <<endl;
// If you need it returned to a variable, you can do this...
int rolled = cast(6);
cout << "You rolled a " << rolled << endl;
return 0;
}
int cast(int num){ // Your integer function
int number = rand()% num +1; // You only need this once
return number; // send your number back
}