I have a program here which returns how many shuffles in a deck it takes to find a card of each suit (hearts,diamonds etc..)
My problem is that, for some reason I keep getting numbers outputting . I am not sure why this is happening but I believe it is because of me seeding the randomizer. I would appreciate any feedback
cards.cpp
#include "cards.h"
#include <iostream>
#include <time.h>
#include <string>
int pickRandomCard() {
int v1 = rand() % Constants::CARD_COUNT;
std::cout << v1;
return v1;
}
// return an enum representing the Rank of the card index given.
Rank getRank(int cardIndex) {
Rank rankIndex = static_cast<Rank>(cardIndex);
return rankIndex;
}
// return an enum representing the Suit of the card index given.
Suit getSuit(int cardIndex) {
Suit suitIndex = static_cast<Suit>(cardIndex);
return suitIndex;
}
// return true if all the Booleans in the given array are true, note we have to pass in
//the length since our array “decays” to a pointer.
bool allSuitsPicked(const bool suitsPicked[], int length) {
bool checkIfAllTrue=true;
for (int i = 0; i < length; i++) {
if (suitsPicked[i] == false) {
checkIfAllTrue = false;
}
}
return checkIfAllTrue;
}
// Return an int representing the number of card picks it takes to cover 4 suits.
// If verbose is true, generate output to show cards picked and pick count.
// This is the function that does all the work behind solving the problem (including// sending output to the console).
// This function should use an array of Boolean values to represent the suits that
// have been picked already, and make use of allSuitsPicked() to test if complete.
int getPickCountNeededForFourSuits(bool verbose = true) {
//
int numberFoundWithSuit[4] = {};
int pickCount = 0;
//array of booleans values to represent the suits that have been already picked
bool suitsPickedAlready[4] = { false,false,false,false };
//initialzing our boolean array to use as a argument in our while loop
bool allpicked = allSuitsPicked(suitsPickedAlready,4);
//this should always run
while (allpicked == false) {
//we are getting a random number between 0-4, to represent our suit
int cardNumber = pickRandomCard()/13;
//we are getting a random number from 0-13 to represent our rank
int cardNumber2 = pickRandomCard()%13;
//now we are getting a random suit using our random number
Suit newSuitGenerated = getSuit(cardNumber);
Rank newRankGenerated = getRank(cardNumber2);
int valueOfSuit;
switch (newSuitGenerated) {
case Suit::Spades:
valueOfSuit = 0;
break;
case Suit::Hearts:
valueOfSuit = 1;
break;
case Suit::Diamonds:
valueOfSuit = 2;
break;
case Suit::Clubs:
valueOfSuit = 3;
break;
}
int valueOfRank;
//"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Jack", "Queen", "King"
switch (newRankGenerated) {
case Rank::Ace:
valueOfRank = 0;
break;
case Rank::Two:
valueOfRank = 1;
break;
case Rank::Three:
valueOfRank = 2;
break;
case Rank::Four:
valueOfRank = 3;
break;
case Rank::Five:
valueOfRank = 4;
break;
case Rank::Six:
valueOfRank = 5;
break;
case Rank::Seven:
valueOfRank = 6;
break;
case Rank::Eight:
valueOfRank = 7;
break;
case Rank::Nine:
valueOfRank = 8;
break;
case Rank::Ten:
valueOfRank = 9;
break;
case Rank::Jack:
valueOfRank = 10;
break;
case Rank::Queen:
valueOfRank = 11;
break;
case Rank::King:
valueOfRank = 12;
break;
}
//if that suit we generated has not already been found, we will add it to our array
if (suitsPickedAlready[valueOfSuit] == false) {
suitsPickedAlready[valueOfSuit] = true;
//adding that number to our array
numberFoundWithSuit[valueOfSuit] = valueOfRank;
std::cout <<Constants::SUITS[valueOfSuit] << " of ";
std::cout << Constants::RANKS[valueOfRank] << '\n';
}
//re calling the while loop test argument to see if we have a full array of suits found
pickCount++;
allpicked = allSuitsPicked(suitsPickedAlready, 4);
}
if (verbose == true) {
std::cout <<"Number of picks: ";
// If verbose is true, generate output to show cards picked and pick count.
return pickCount;
}
}
cards.h
#pragma once
#include <string.h>
#include <string>
#include <string>
enum class Suit{ Spades, Hearts, Diamonds, Clubs };// this will need to be filled out
enum class Rank{ Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine,Ten, Jack, Queen, King };// this will need to be filled out
namespace Constants {
constexpr int CARD_COUNT{ 52 };
constexpr int NUM_RANKS{ 13 };
constexpr int NUM_SUITS{ 4 };
// These string arrays will simplify the task of printing out the card values.
// We don’t need to use a switch statement to find the string representation
// of an enum. We can cast the enum as an int to index these arrays.
const std::string SUITS[NUM_SUITS] { "Spades", "Hearts", "Diamonds", "Clubs" };// initialize & match with enums
const std::string RANKS[NUM_RANKS] { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten","Jack", "Queen", "King" };// initialize & match with enums
}
// FUNCTION PROTOTYPES// pick a random card from the deck (return a random # between 0-51)
int pickRandomCard();
// return an enum representing the Rank of the card index given.
Rank getRank(int cardIndex);
// return an enum representing the Suit of the card index given.
Suit getSuit(int cardIndex);
// return true if all the Booleans in the given array are true, note we have to pass in
//the length since our array “decays” to a pointer.
bool allSuitsPicked(const bool suitsPicked[], int length);
// Return an int representing the number of card picks it takes to cover 4 suits.
// If verbose is true, generate output to show cards picked and pick count.
// This is the function that does all the work behind solving the problem (including// sending output to the console).
// This function should use an array of Boolean values to represent the suits that
// have been picked already, and make use of allSuitsPicked() to test if complete.
int getPickCountNeededForFourSuits(bool);
Source.cpp
#include<stdio.h>
#include<string>
#include <iostream>
#include "cards.cpp"
#include<time.h>
int main() {
srand(time(0));
srand(static_cast<unsigned int>(time(0)));
/*Write a single-file program (named main.cpp) that reads two separate integers from the user, adds them together, and then outputs the answer. The program should use three functions:
A function named “readNumber” should be used to get (and return) a single integer from the user.
A function named “writeAnswer” should be used to output the answer. This function should take a single parameter and have no return value.
A main() function should be used to glue the above functions together.*/
int x = getPickCountNeededForFourSuits(true);
std::cout << x;
}