Dice Game.
Mar 5, 2016 at 2:18pm UTC
Hey guys, I'm trying to make a console dice game, but I've got some problems.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <time.h>
int main(){
std::cout << "Click 1 to start the game, 2 to exit!" ;
int startOr;
std::cin >> startOr;
while (startOr != 2){
int compScore = 0;
int playerScore = 0;
srand(time(NULL));
std::cout << "Click 1 to Roll the dice! Click 2 to EXIT the game." << std::endl; //This statement is ALWAYS displayed.
int n;
std::cin >> n;
if (n == 1){
int m;
m = rand() % 6;
std::cout << m << std::endl;
std::cout << "Computer is rolling the dice!" << std::endl;
int o;
o = rand() % 6;
std::cout << o << std::endl;
if (o > m){
std::cout << "Computer wins!" << std::endl;
compScore = compScore + 1;
}
else if (m > o){
std::cout << "You win!" << std::endl;
playerScore = playerScore + 1;
}
else if (m == o){
std::cout << "Draw!" << std::endl;
}
}
else if (n == 2){
std::cout << "Scoreboard." ;
std::cout << "Computer Score = :" << compScore << std::endl;
std::cout << "Player Score = :" << playerScore << std::endl;
std::cout << "Goodbye!" ;
}
}
} //scoreboard always shows 0 for both
Also, if there's a way to make the code more organized, I'd appreciate the help , thank you :)
Last edited on Mar 5, 2016 at 3:24pm UTC
Mar 5, 2016 at 2:29pm UTC
but I've got some problems.
It always helps to say what they are.
I can see a problem, but it may not be the one you have in mind.
Line 15:
srand(time(NULL));
Don't use srand repeatedly inside a loop like this. Call it just once at the start of the program.
Mar 5, 2016 at 3:05pm UTC
Thank you! , but what about the scoreboard problem?
Mar 5, 2016 at 4:12pm UTC
13 14
int compScore = 0;
int playerScore = 0;
These variables are declared inside the while loop, and are initialised to zero each time.
Then there are two possibilities:
The dice game is played, one of the variables may be incremented (except in the case of a draw)
or
the games is exited and the scores are printed.
If you want to keep score of all the games, move the compScore and playerScore declaration/initialisation outside the while loop, (before the loop begins).
Another slight usability problem is that the user is prompted twice at the very beginning, and also the game does not exit properly once it has begun.
One way to rearrange the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <iostream>
#include <ctime>
int main()
{
srand(time(NULL));
int compScore = 0;
int playerScore = 0;
std::cout << "Click 1 to start the game, 2 to exit! " ;
int n;
std::cin >> n;
while (n != 2)
{
if (n == 1)
{
int m = rand() % 6;
std::cout << m << std::endl;
std::cout << "Computer is rolling the dice!" << std::endl;
int o = rand() % 6;
std::cout << o << std::endl;
if (o > m)
{
std::cout << "Computer wins!" << std::endl;
compScore++;
}
else if (m > o)
{
std::cout << "You win!" << std::endl;
playerScore++;
}
else
std::cout << "Draw!" << std::endl;
}
std::cout << "Click 1 to Roll the dice! Click 2 to EXIT the game." << std::endl;
std::cin >> n;
if (n == 2)
{
std::cout << "Scoreboard\n" ;
std::cout << "Computer Score = " << compScore << std::endl;
std::cout << "Player Score = " << playerScore << std::endl;
std::cout << "Goodbye!" ;
}
}
}
Mar 5, 2016 at 4:19pm UTC
Thanks so much! :)
Topic archived. No new replies allowed.