I am writing a yahtzee game for my c++ programming class. One of my difficulties I have ran into is the scoring system for different categories. I think I have figured out how to do it for adding 1s, 2s etc but I do not know how to have the program determine when a 3 of a kind, 4 of a kind, etc has been rolled. Here is my code so far.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Declare variables
int players;
int turn = 1;
vector<string> names;
string playerName;
int dice[5];
int finalScore = 0;
char reroll[5];
char rollDice;
int tries = 1;
const int DICE = 5;
int roll[DICE];
int scorecard;
int scoreThisTurn(int scorecard);
int turnScore = 0;
//Introduction, get number of players.
cout << "Hello, welcome to Yahtzee! How many players are there?" << endl;
cin >> players;
if (players > 4) {
cout << "Sorry, the maximum number of players is 4." << endl;
cout << "How many players are there?" << endl;
cin >> players;
}
//Get player names
string getNames();
for (int i = 0; i < players; i++) {
cout << "Hello player " << i + 1 << ", please enter your name" << endl;
cin >> playerName;
names.push_back(playerName);
}
srand(time(NULL)); //random seed
cout << "Welcome to Yahtzee!\n";
while (turn <= 13) { //roll dice
cout << "Press 'r' to roll" << endl;
cin >> rollDice;
if (rollDice == 'r') {
for (int i = 0; i < DICE; i++) {
roll[i] = rand() % 6 + 1;
}
}
cout << "You rolled: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
cout << "Type y to reroll or n to keep. For example yynnn would keep the first three dice" << endl;
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
for (int i = 0; i < DICE; i++) {
if (reroll[i] == 'y') {
roll[i] = rand() % 6 + 1;
}
else if (reroll[i] == 'n') {
roll[i];
}
else cout << "Sorry you entered an invalid letter." << endl;
}
cout << "Your second roll is: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
cout << "Type y to reroll or n to keep. For example yynnn would keep the first three dice" << endl;
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
for (int i = 0; i < DICE; i++) {
if (reroll[i] == 'y') {
roll[i] = rand() % 6 + 1;
}
else if (reroll[i] == 'n') {
roll[i];
}
else cout << "Sorry you entered an invalid letter." << endl;
}
cout << "Your third roll is: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
//displays scorecard categories
cout << "Which category would you like to score this in" << endl;
cout << "1 - ones: " << endl;
cout << "2 - twos: " << endl;
cout << "3 - threes: " << endl;
cout << "4 - fours: " << endl;
cout << "5 - fives: " << endl;
cout << "6 - sixes: " << endl;
cout << "7 - 3 of a kind: " << endl;
cout << "8 - 4 of a kind: " << endl;
cout << "9 - small straight: " << endl;
cout << "10 - large straight: " << endl;
cout << "11 - full house: " << endl;
cout << "12 - yahtzee: " << endl;
cout << "13 - chance: " << endl;
//asks player to choose where to score
cout << "\nEnter 1-14 to choose a category." << endl;
cin >> scorecard;
//assigns points
for (int i = 0; i < DICE; i++) {
turnScore = 0;
if (scorecard == 1) {
if (roll[i] == 1) {
turnScore = turnScore + 1;
}
}
if (scorecard == 2) {
if (roll[i] == 2) {
turnScore = turnScore + 2;
}
}
if (scorecard == 3) {
if (roll[i] == 3) {
turnScore = turnScore + 3;
}
}
if (scorecard == 4) {
if (roll[i] == 4) {
turnScore = turnScore + 4;
}
}
if (scorecard == 5) {
if (roll[i] == 5) {
turnScore = turnScore + 5;
}
}
if (scorecard == 6) {
if (roll[i] == 6) {
turnScore = turnScore + 6;
}
if (scorecard == 7) {
if (roll[i] == 2) {
turnScore = turnScore + 2;
}
}
}
cout << scorecard << endl;
turn++;
}
system("pause");
return 0;
}
As you can see I've set up the scoring for the first 6 categories but don't know how to proceed.
I do not know how to have the program determine when a 3 of a kind, 4 of a kind, etc has been rolled.
Create a variable to help you keep track of the number of dice that have a given number.
int diceCount[DICE] = {0};
and fill up the array with:
for (int i = 0; i < DICE; i++) {
diceCount[roll[i-1]]++
}
Create helper functions to determine whether five, four, or three of a kind have been thrown.
int getNOfAKind(int diceCount[], int N)
{
// This will need moving DICE out of `main`
// making it a global variable.
for ( int i = 0; i < DICE; ++i )
{
if (diceCount[i] == N )
{
return i+1;
}
}
return -1;
}
int getFiveOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 5);
}
int getFourOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 4);
}
int getThreeOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 3);
}
and use it as:
int fiveCount = getFiveOfAKind(diceCount);
if ( fiveCount != -1 )
{
}
int fourCount = getFourOfAKind(diceCount);
if ( fourCount != -1 )
{
}
int threeCount = getThreeOfAKind(diceCount);
if ( threeCount != -1 )
{
}
Related
Good day, I'm having difficulty on the last two parts of my program where it's supposed to only output players who got maximum/minimum scores, I need help on how to do it because I'm really confused. If it's also alright to provide some explanations I'd really appreciate it.
I tried this approach:
#include <iostream>
using namespace std;
int main() {
double lrgst, lrgst2, lrgst3;
int numbers[5];
lrgst = lrgst2 = lrgst3;
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
}
for (int i = 0; i < 5; i++) {
if (numbers[i] > lrgst) {
lrgst3 = lrgst2;
lrgst2 = lrgst;
lrgst = numbers[i];
} else if (numbers[i] > lrgst2) {
lrgst3 = lrgst2;
lrgst2 = numbers[i];
} else if (numbers[i] > lrgst3) {
lrgst3 = numbers[i];
}
}
cout << "largest are: " << lrgst << " " << lrgst2 << " " << lrgst3;
}
this is my actual code:
#include <iostream>
using namespace std;
struct playerdata {
char name[50];
int age, score1, score2;
double average;
};
int main() {
int choice, i = 1, j = 1, z = 1, backtomain2;
char backtomain;
playerdata p1[10];
do {
for (int a = 0; a < 47; a++) {
cout << "=";
}
cout << "\n";
for (int b = 0; b < 22; b++) {
cout << " ";
if (b == 21) {
cout << "MENU \n";
}
}
for (int c = 0; c < 47; c++) {
ocut << "=";
}
cout << " "
"\n1. Add record\n"
"2. View players records\n"
"3. Compute for the average\n"
"4. Show the player(s) who gets the max average.\n"
"5. Show the player(s) who gets the min average.\n"
"6. Exit\n"
"Enter your choice:";
cin >> choice;
if (choice == 1) {
cout << "Add player data" << endl;
do {
cout << "Enter player " << i << " nickname:";
cin >> p1[i].name;
cout << "Enter player " << i << " age:";
cin >> p1[i].age;
cout << "Enter player " << i << " score 1:";
cin >> p1[i].score1;
cout << "Enter player " << i << " score 2:";
cin >> p1[i].score2;
cout << "Enter again? (Y/N)";
cin >> backtomain;
i++;
}
while (backtomain != 'N' && backtomain != 'n' && i < 7);
if (choice == 2) {
cout << "Player records" << endl;
cout << "Player nickname "
<< "Player age "
<< " player score 1"
<< "
player score 2\n ";
for (z = 1; z <= i - 1; z++) {
cout << p1[z].name << " " << p1[z].age << "" << p1[z].score1 << ""
<< p1[z].score2 << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 3) {
cout << "Computing for average...\n";
for (int d = 1; d <= i - 1; d++) {
p1[d].average = (p1[d].score1 + p1[d].score2) / 2.0;
cout << "\n" << p1[d].average << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 4) {
cout << "Player(s) who got the max average:\n";
cout << "\nPress 1 to go back to main menu";
cin >> backtomain;
}
if (choice == 5) {
cout << "player(s) who got the min average: \n";
cout << "Press 1 to go back to main menu";
cin >> backtomain;
}
}
while (choice != 6);
}
You can simply sort the array of players for that
int n = sizeof(p1)/ sizeof(p1[0]);
sort(p1, p1+n, compPlayer);
//larget at pl[0]
//smallest at pl[9]
where
bool compPlayer(playerdata p1, playerdata p2) {
return (p1.score1+p1.score2) > (p2.score1+p2.score2);
//use score incase average has not been calculated for all players yet
}
I'm trying to output the score for each addition, subtraction, multiplication, division in the end test function. and the total of the question asked.
**Any help would be appreciated
BTW this is only using local functions and is passing the scores by reference. IT's supposed to output the number of answers that you answer correctly.
Also is there a way where you can prevent the program from letting you select the choices more than once?
here's what I got so far:
#include < iostream >
#include < iomanip >
#include < stdlib.h >
#include < time.h >
#include < stdlib.h >
using namespace std;
int addition(int addscore);
int subtraction(int subscore);
int multiplication(int multiscore);
int division(int divscore);
int endtest(int & addscore, int & subscore, int & multiscore, int & divscore);
main() {
int end_final = 0;
do {
int addscore, subscore, multiscore, divscore;
char choice;
cout << "A- " << "Addition\n";
cout << "B- " << "Subtraction\n";
cout << "C- " << "Multiplication\n";
cout << "D- " << "Division\n";
cout << "E- " << "End of Test\n";
cin >> choice;
cout << endl << endl;
switch (choice) {
case 'A':
case 'a':
addition(addscore);
break;
case 'B':
case 'b':
subtraction(subscore);
break;
case 'C':
case 'c':
multiplication(multiscore);
break;
case 'D':
case 'd':
division(divscore);
break;
case 'E':
case 'e':
endtest(addscore, subscore, multiscore, divscore);
break;
}
}
while (end_final != 1);
return 0;
}
int addition(int addscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
int total = 0;
for (int i = 1; i <= 5; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
addscore++;
} else {
cout << "Incorrect \n";
}
}
}
int subtraction(int subscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen subtraction\n";
int randnum1, randnum2;
int total = 0;
while (total != 5) {
randnum1 = rand() % 20 + 1;
randnum2 = rand() % 20 + 1;
if (randnum1 >= randnum2) {
cout << randnum1 << " " << "-" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 - randnum2) {
cout << endl;
cout << "Correct!\n ";
subscore++;
} else {
cout << "Incorrect\n ";
}
}
}
}
int multiplication(int multiscore) {
int iRandom;
int total = 0;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen Multiplication\n";
int randnum1, randnum2;
for (int i = 1; i <= 5; i++) {
randnum1 = rand() % 20 + 1;
randnum2 = rand() % 20 + 1;
cout << randnum1 << " " << "x" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 * randnum2) {
cout << endl;
cout << "Correct! \n";
multiscore++;
} else {
cout << "Incorrect\n ";
}
}
}
int division(int divscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen Division\n";
int randnum1, randnum2;
int total = 0;
while (total != 5) {
randnum1 = rand() % 13 + 1;
randnum2 = rand() % 13 + 1;
if (randnum1 % randnum2 == 0) {
cout << randnum1 << " " << "/" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 / randnum2) {
cout << endl;
cout << "Correct! \n";
divscore++;
} else {
cout << "Incorrect\n ";
}
}
}
}
int endtest(int & addscore, int & subscore, int & multiscore, int & divscore) {
int total = 0;
cout << endl << endl;
cout << "Addition" << " " << addscore++ << endl;
cout << "Subtraction" << " " << subscore << endl;
cout << "Multiplication" << " " << multiscore << endl;
cout << "Division" << " " << divscore << endl;
cout << "Total" << " " << total << endl;
}
To clarify the code, declare an enum
enum { ADDITION=0, SUBTRACTION, MULTIPLICATION, DIVISION };
You could have global arrays
int used[] = { 0,0,0,0 }; // not used
int total[] = { 0,0,0,0 }; // total questions per operation
int score[] = { 0,0,0,0 }; // score per operation
Then, doing it for 'addition', in the do {
if ( ! used[ADDITION]) cout << "A- " << "Addition\n";
then in the switch
case 'A':
case 'a':
used[ADDITION] = 1; // tells addition was used
addition(addscore);
break;
The addition code becomes
int addition(int addscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
total[ADDITION] = 0; // should not be
score[ADDITION] = 0; // ...necessary (but in case you call it again)
for (int i = 0; i < 5 ; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total[ADDITION]++; // total is incremented for Addition
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
score[ADDITION]++; // score is incremented
} else {
cout << "Incorrect \n";
}
}
}
endtest becomes
int endtest() {
cout << endl << endl;
if (used[ADDITION]) cout << "Addition" << " " << score[ADDITION] << " / " << total[ADDITION] << endl;
// same for SUBTRACTION ...
int grandtotal = total[ADDITION] + ....;
cout << "Grand Total" << " " << grandtotal << endl;
}
endtest is declared, and called like that
int endtest();
...
endtest();
Since this is C++, you could also make a class Operation, then one subclass per operation, holding the name of the operation and overriding a 'execute' method that does the specific operation, then declare an array of the parent class that holds an instance of each operation ...
Based on OP's comments, after the do {
// initialize variables to zero
int addscore=0, subscore=0, multiscore=0, divscore=0;
in the switch
case 'A':
case 'a':
used[ADDITION] = 1; // tells addition was used
addition( &addscore ); // <== gives the pointer to that variable
break;
then in addition code
int addition(int *addscore) { // <== gets a pointer
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
total[ADDITION] = 0; //
score[ADDITION] = 0; // using your way, this is not necessary anymore
for (int i = 0; i < 5 ; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total[ADDITION]++; // total is incremented for Addition
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
// score[ADDITION]++; // score is incremented (see above)
*addscore++; // <== increment variable pointed to by pointer
} else {
cout << "Incorrect \n";
}
}
}
My assignment is to create a guessing game where the computer guesses my number and one where I guess the computer's number. I have my code written out but It's incomplete. I want to create a menu so the user can choose which game he wants to play, and also i am having trouble joining the two programs into one so they both will run. Can someone please assist me. The first game works fine but from there I really don't know what I'm doing.
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
const int min = 1;
const int max = 100;
int num = 2 && 3 && 4;
int prevMin = 0;
int prevMax = 0;
int tries = 0;
int guess = 0;
int userNum = 0;
int userGuess = 0;
int systemNum = 0;
int systemGuess = 0;
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{
cout << "Welcome to Game 1!" << endl;
cout << "User, enter a random number between " << min << " and " << max << " : "; cin >> userNum;
}
srand(time(0));
systemGuess = rand() % 100 + 1;
do
{
cout << "System, guess the user's number between " << min << " and " << max << ": " << systemGuess << endl;
cin.get();
++tries;
if (systemGuess > max || systemGuess<min)
{
cout << "I said guess a number between " << min << " and " << max << " stupid." << endl;
}
if (systemGuess > userNum)
{
cout << "Too high. Guess lower." << endl;
prevMax = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
else if (systemGuess < userNum)
{
cout << "Too low. Guess higher." << endl;
prevMin = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
} while (systemGuess != userNum);
cout << systemGuess << endl;
cout << " I guessed it right! It took me " << tries << " guess(es). " << endl;
srand(time(0));
systemNum = rand()% 100 + 1;
//Beginning of second game
do
{
if (num == 3)
{
cout << "Welcome to Game 2!" << endl;
cout << "User, try to guess the computer's number that's between " << min << " and " << max << " . " << endl;
}
cout << "Enter your guess." << endl;
cin >> userGuess;
tries++;
if (userGuess > systemNum)
{
cout << "Too high. Guess lower." << endl;
cin >> userGuess;
}
else if (userGuess < systemNum)
{
cout << "Too low. Guess higher." << endl;
cin >> userGuess;
}
else
{
cout << "Correct! It took you long enough! Lol... " << endl;
cin >> userGuess;
}
while (userGuess != systemNum);
}
system("pause");
return 0;
}
Make the two games two distinct functions (each with its own local variables), create a third one that is a loop that prompts for the games (and break when "quit" is selected), and call the corresponding function, then call the third one from main.
You can use if...else :
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{//game1
}
else if(num==3)
{//game2
}
else if(num==4)
{return 0;
}else
{
cout<<"Invalid choice!";
system("pause");
return 1;
}
//print highscores
system("pause");
return 0;
Your whole code:
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
const int min = 1;
const int max = 100;
int num = 2 && 3 && 4;
int prevMin = 0;
int prevMax = 0;
int tries = 0;
int guess = 0;
int userNum = 0;
int userGuess = 0;
int systemNum = 0;
int systemGuess = 0;
srand(time(0));
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{
cout << "Welcome to Game 1!" << endl;
cout << "User, enter a random number between " << min << " and " << max << " : "; cin >> userNum;
systemGuess = rand() % 100 + 1;
do
{
cout << "System, guess the user's number between " << min << " and " << max << ": " << systemGuess << endl;
cin.get();
++tries;
if (systemGuess > max || systemGuess<min)
{
cout << "I said guess a number between " << min << " and " << max << " stupid." << endl;
}
if (systemGuess > userNum)
{
cout << "Too high. Guess lower." << endl;
prevMax = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
else if (systemGuess < userNum)
{
cout << "Too low. Guess higher." << endl;
prevMin = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
} while (systemGuess != userNum);
cout << systemGuess << endl;
cout << " I guessed it right! It took me " << tries << " guess(es). " << endl;
}
systemNum = rand()% 100 + 1;
//Beginning of second game
if (num == 3)
{
do
{
cout << "Welcome to Game 2!" << endl;
cout << "User, try to guess the computer's number that's between " << min << " and " << max << " . " << endl;
cout << "Enter your guess." << endl;
cin >> userGuess;
tries++;
if (userGuess > systemNum)
{
cout << "Too high. Guess lower." << endl;
cin >> userGuess;
}
else if (userGuess < systemNum)
{
cout << "Too low. Guess higher." << endl;
cin >> userGuess;
}
else
{
cout << "Correct! It took you long enough! Lol... " << endl;
cin >> userGuess;
}}
while (userGuess != systemNum);
}else if(num==4)return 0;
else{
cout<<"Invalid choice!";
system("pause");
return 1;
}
//print highscores
system("pause");
return 0;
}
okay here is my code for a game called pig. I having trouble because the code for the computers turn will only got through one time and once the second turn is reached the first line is only printed. is there any soluction to this or am I just screwed?
`int main () {
int humanTurn=0;
int humanTotalScore=0;
int computerTotalScore=0;
int computerTurn=3;
float diceRoll = 0;
int score = 0;
int computerScore = 0;
int answer = 0;
int humanScore = 0;
int pause = 0;
float computerDiceRoll = 0;
srand(time(0));
cout << "Welcome to the game of pig, the first player to reach to 100 wins."<< endl;
cout << "if you roll a one no score will be recorded and your turn will go the computer"<< endl;
cout << " If you choose to hold, the total score accumilated will be recorded" << endl;
while ((humanTotalScore < 100) && (computerTotalScore < 100)) {
cout << endl;
cout << endl;
cout << "press 1 to roll or 2 to save your score: ";
cin >> answer;
for (int i=0; i<1; i++) {
diceRoll = rand() % 6 + 1;
}
if ((diceRoll > 1) && ( answer == 1)){
cout << endl;
cout << endl;
cout << "your current roll is: " << diceRoll << endl;
cout << endl;
cout << endl;
score += diceRoll;
cout << "Score for this turn is: " << score << endl;
cout << "your total score is: " << humanTotalScore << endl;
cout << endl;
cout << endl;
}
else if (answer == 2){
cout << "your score of: " << score << " will be saved" << endl;
humanTotalScore += score;
score = 0;
}
else {
cout << endl;
cout << endl;
cout << "you rolled a ONE. You lose your turn and any points that were with it" <<endl;
cout << endl;
cout << endl;
score = 0;
computerTurn = score;
}
if ((computerTurn == 0) || (answer == 2)){
computerDiceRoll = rand() % 6 + 1;
here is code that is giving me loop problems.
while ((computerDiceRoll > 1)&& (computerScore <= 20)){
if (computerDiceRoll == 1){
computerScore = 0;
computerTurn = 1;
cout << "Computer rolled a ONE, it is now your turn." << endl;
}
computerDiceRoll = rand() % 6 + 1;
cout << endl;
cout << endl;
cout << "computers roll is : " << computerDiceRoll << endl;
cout << endl;
cout << endl;
computerScore += computerDiceRoll;
if (computerScore >20){
computerTotalScore += computerScore;
computerTurn = 1;
}
cout << "computer current score is : " << computerScore << endl;
cout << "computer total score is: " << computerTotalScore << endl;
cout << endl;
cout << endl;
}
}
}
} `
I have been coding a blackjack game that is almost done it's first stage of development. I am almost done, the only problem is that the random numbers that are generated in this part:
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
}
else {
if (x.userShowCardFour == 0) {
x.userShowCardFour = 1 + (rand()%11);
int dealerHit2 = x.userShowCardFour;
x.userTotal += dealerHit2;
x.cardCount++;
}
else {
if (x.userShowCardFive ==0) {
x.userShowCardFive = 1 + (rand()%11);
int dealerHit3 = x.userShowCardFive;
x.userTotal += dealerHit3;
x.cardCount++;
}
}
}
is not the same as the numbers generated in the final part:
cout << "You had a total of: " << x.userTotal << endl;
as i keep getting different numbers. I will paste my entire code below.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;
void setRand() {
srand(time(0));
}
class Game {
public:
int userCards[11] = {1,2,3,4,5,6,7,8,9,10,11};
int dealerCards[11] = {1,2,3,4,5,6,7,8,9,10,11};
int userShowCardOne = userCards[1 + (rand()%11)];
int userShowCardTwo = userCards[1 + (rand()%11)];
int dealerShowCard = dealerCards[1 + (rand()%11)];
int dealerHiddenCard = dealerCards[1 + (rand()%11)];
int userShowCards[5] = {userShowCardOne, userShowCardTwo, userShowCardThree, userShowCardFour, userShowCardFive};
int userShowCardThree = 0;
int userShowCardFour = 0;
int userShowCardFive = 0;
int fresh = 1;
int beginningInput;
int hit = 1;
int dealerTotal = dealerShowCard + dealerHiddenCard;
int userTotal = userShowCardOne + userShowCardTwo;
int cardCount = 2;
int runGame = 1;
private:
};
// int a = 1 + rand()%11;
/*int b = 1 + rand()%11;
int c = 1 + rand()%11;
int d = 1 + rand()%11;
int e = 1 + rand()%11;
int f = 1 + rand()%11;
int g = 1 + rand()%11;
int h = 1 + rand()%11;
*/
int startGame();
int main() {
srand(time(0));
Game x;
cout << "Welcome to BlackJack 1.0.0" << endl;
cout << "Press: " <<endl;
cout << "1 ----- New Game" << endl;
cout << "2 ----- Help" << endl;
cin >> x.beginningInput;
if(x.beginningInput == 1){
startGame();
cout << "The dealer had: " << endl;
cout << x.dealerHiddenCard << endl;
cout << x.dealerShowCard << endl;
while(x.dealerTotal <= 16) {
cout << "The dealer has decided to hit" << endl;
int dealerHit = 1 + (rand()%11);
cout << "The dealer has gotten " << dealerHit << endl;
x.dealerTotal += dealerHit;
}
cout << "The dealer has decided to stay" << endl;
cout << "The dealer had a total of " << x.dealerTotal << endl;
cout << "You had a total of: " << x.userTotal << endl;
if (x.dealerTotal > 21 && x.userTotal <= 21) {
cout << "The dealer busted. You won!" << endl;
}
else if (x.userTotal > 21 && x.dealerTotal <= 21) {
cout << "You busted. The Dealer Won" << endl;
}
else if (x.userTotal > 21 && x.dealerTotal > 21) {
cout << "You and the dealer both busted. Tie" << endl;
}
else {
if (x.dealerTotal > x.userTotal) {
cout << "Sorry, you lost to the dealer" << endl;
}
else if (x.dealerTotal < x.userTotal) {
cout << "Congrats, you won!" << endl;
}
else {
cout << "You and the dealer tied. " << endl;
}
}
cout << "Would you like to play again? 1 to play again, 0 to quit" << endl;
cin >> x.beginningInput;
if (x.beginningInput == 1){
startGame();
}
else if (x.beginningInput == 0){
return 0;
}
}
else if (x.beginningInput == 0) {
cout << "Thanks for playing!" << endl;
}
else {
cout << "Here's the help section" << endl;
}
}
int startGame() {
Game x;
srand(time(0));
if (x.fresh != 1){
cout << "NEW GAME\n \n " << endl;
}
while (x.runGame == 1) {
cout << "Dealer: \n" << endl;
cout << "X" << endl;
cout << x.dealerShowCard << endl;
cout << "You: \n" << endl;
cout << x.userShowCardOne << endl;
cout << x.userShowCardTwo << endl;
x.userTotal = x.userShowCardOne + x.userShowCardTwo;
if (x.userShowCardThree != 0) {
cout << x.userShowCardThree << endl;
}
if (x.userShowCardFour != 0) {
cout << x.userShowCardFour << endl;
cout << "You can only hit one more time!" << endl;
}
if (x.userShowCardFive != 0) {
cout << x.userShowCardFive << endl;
}
if(x.cardCount > 5) {
cout << "sorry, there is a 5 card limit.";
}
cout << "Would you like to hit or stay? (1 for hit or 2 for stay)" << endl;
cin >> x.hit;
x.fresh = 2;
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
}
else {
if (x.userShowCardFour == 0) {
x.userShowCardFour = 1 + (rand()%11);
int dealerHit2 = x.userShowCardFour;
x.userTotal += dealerHit2;
x.cardCount++;
}
else {
if (x.userShowCardFive ==0) {
x.userShowCardFive = 1 + (rand()%11);
int dealerHit3 = x.userShowCardFive;
x.userTotal += dealerHit3;
x.cardCount++;
}
}
}
}
if (x.hit == 2) {
x.runGame = 2;
}
}
return 0;
}
Looks like you have 2 instances of the Game object and both are named x
int main() {
srand(time(0));
Game x;
...
cout << "You had a total of: " << x.userTotal << endl;
then in the startGame function
int startGame() {
Game x;
...
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
The x in main() is not the same as x in startGame() as both are separate Game objects.