### Help with building this game

Hi all , I have been trying to make a game where 2 players pick from 4 cards but I am having trouble on assigning the randomly generated numbers into each card and making sure that the second card the player 2 choose wont be the same as player 1's. I am only allowed to use simple stuff like if else statements, switch statements, while loop, do while loop and for loops. (I googled and mostly seems to show how to do it with Arrays or call Functions or vectors which I cannot use).

1. Shuffle and randomly assigned numbers 1-4 to the cards
2. First player pick a card
3. Shuffle remaining cards and randomly assigned numbers 1-4 to the cards
4. Second player pick a card

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 #include #include #include #include #include using namespace std; int main() { srand((unsigned)time(0)); int num1, num2, num3, num4; num1 = rand()%4+1; num2=rand()%4+1; while (num2 == num1) num2 = rand()%4+1; num3=rand()%4+1; while (num3 == num1 || num3==num2) num3 = rand()%4+1; num4=rand()%4+1; while (num4==num1 || num4==num2 || num4==num3 ) num4 = rand()%4+1; cout << num1 << num2 << num3 <> choice; if (choice == card1) { num1 = rand()%3+1; num2=rand()%3+1; while (num2 == num1) num2 = rand()%3+1; num3=rand()%3+1; while (num3 == num1 || num3==num2) num3 = rand()%3+1; card2=num1 , card3=num2 , card4=num3; } else if (choice == card2) { num1 = rand()%3+1; num2=rand()%3+1; while (num2 == num1) num2 = rand()%3+1; num3=rand()%3+1; while (num3 == num1 || num3==num2) num3 = rand()%3+1; card1=num1, card3=num2, card4=num3; } else if (choice == card3) { num1 = rand()%3+1; num2=rand()%3+1; while (num2 == num1) num2 = rand()%3+1; num3=rand()%3+1; while (num3 == num1 || num3==num2) num3 = rand()%3+1; card1=num1, card2=num2, card4=num3; } else if (choice == card4) { num1 = rand()%3+1; num2=rand()%3+1; while (num2 == num1) num2 = rand()%3+1; num3=rand()%3+1; while (num3 == num1 || num3==num2) num3 = rand()%3+1; card1=num1, card2=num2, card3=num3; } else cout << "Invalid number - There are only 4 shuffled. Please select 1-4 : "; } while(choice!=1 && choice!=2 && choice!=3 && choice!=4 ); cout << num1 <> choice2; cout << "Player 1 chose card " << card1 << endl; cout << "Player 2 chose card " << card2 <

Last edited on
the best way to deal with that is to shuffle a container of cards.
eg standard deck, you have 52 or 54 (jokers) cards, so you have a container that holds one copy of each card. Randomly arrange them and deal off the cards to the players by simple iteration (0,1,2,... are dealt out). That way duplication is simply not possible.
the standard c++ language has random permutation and probably a couple of other ways to 'shuffle' the container for this purpose.

if you can't use any sort of container, then you pretty much are left with brute force where you create a random card and then next time you do that, you re-roll until it does not match any already created cards. This is terrible, but it will work. If you deal out more that 3/4 of the cards the create/check/reroll cycle will begin to eat a ton of time as it keeps creating duplicates until it gets lucky.. the last few cards will take quite some time (relatively).

 123456789101112131415161718192021 an efficient way to do that brute force (in terms of coding effort) //pseudo code, if its not obvious right away: int cc{}; //#cards allocated, "Card Count" or you can name it whatever. bool good{true}; while(cc != 10) { good = true; card = random card code stuff; switch(cc) { case 9: good &= (card != card9); case 8: good &= (card != card8); //exploiting switch fall through to check up to cc //so if cc is 5 if will do 5,4,3,2,1 because no breaks here. ... case 1 as above.. //no case 0. do nothing } cc+= good; //good is true if the card didn't match existing, so its 1, so you add a card. //if its false, add zero and loop again. //logic to assign next card = card generated goes here!! probably another switch statement. }

this is an unholy mess without at least being able to use an array. Teacher should be smacked repeatedly with a soft pillow or something.
Last edited on
First things first:
 Please learn to use code tags, they make reading and commenting on source code MUCH easier. How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/ There are other tags available. How to use tags: http://www.cplusplus.com/articles/z13hAqkS/ HINT: you can edit your post and add code tags. Some formatting & indentation would not hurt either

A bit of overkill for this simple assignment, doing it the C++ way (one possibility):
 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 #include #include // std::default_random_engine, std::random_device #include // C++ dynamic container, std::deque #include // std::iota #include // std::shuffle int main() { // create an alias to represent a card // https://en.cppreference.com/w/cpp/language/type_alias using card = unsigned; // create an alias to represent a player using player = unsigned; // let's create a random engine // https://www.cplusplus.com/reference/random/default_random_engine/ // https://www.cplusplus.com/reference/random/random_device/ std::default_random_engine prng(std::random_device{}()); // let's create a vector to hold 4 cards // https://www.cplusplus.com/reference/deque/deque/ std::deque deck(4); // initialize the deck to 1 - 4 // https://www.cplusplus.com/reference/numeric/iota/ std::iota(deck.begin(), deck.end(), 1); // shuffle the deck // https://www.cplusplus.com/reference/algorithm/shuffle/ std::shuffle(deck.begin(), deck.end(), prng); // create 2 players player p1; player p2; // player one given first card p1 = deck[0]; // display player 1's card std::cout << "Player 1: " << p1 << '\n'; // display the remaining card using a range-based for loop // https://en.cppreference.com/w/cpp/language/range-for std::cout << "The deck contains:\n"; for (const auto& itr : deck) { std::cout << itr << ' '; } std::cout << "\n\n"; // reshuffle the deck give player two the first card std::shuffle(deck.begin(), deck.end(), prng); p2 = deck[0]; // display player 2's card std::cout << "Player 2: " << p2 << '\n'; std::cout << "The deck contains:\n"; for (const auto& itr : deck) { std::cout << itr << ' '; } std::cout << '\n'; }
 Player 1: 3 The deck contains: 3 2 1 4 Player 2: 4 The deck contains: 4 3 2 1

This requires at least C++11.
A more complex setup for a card game using C++ (with some testing output):
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 #include #include #include #include // std::default_random_engine, std::random_device #include // std::iota #include // std::shuffle int main() { // create & seed a C++ random engine // http://www.cplusplus.com/reference/random/ std::default_random_engine prng(std::random_device {} ()); // create an alias to represent a card using card = unsigned; // create a "standard" poker-style deck of 52 cards // http://www.cplusplus.com/reference/deque/deque/ std::deque deck(52); // populate the deck to represent all 52 cards // https://en.cppreference.com/w/cpp/algorithm/iota std::iota(deck.begin(), deck.end(), 0); // two lambdas to parse out suit and rank of a card // https://en.cppreference.com/w/cpp/language/lambda auto rank = [ ] (card c) { return "AKQJT98765432"[c % 13]; }; auto suit = [ ] (card c) { return "SHDC"[c / 13]; }; // casino black-jack uses more than a single deck // multiple decks combined are called a shoe std::deque shoe; // 3 decks for the shoe for (size_t i { }; i < 3; ++i) { shoe.insert(shoe.begin(), deck.begin(), deck.end()); } // display the original order of cards in the shoe std::cout << "Unshuffled shoe:\n"; // range-based for loop // https://en.cppreference.com/w/cpp/language/range-for for (card ccount { }; const card & c : shoe) { std::cout << rank(c) << suit(c) << ' '; ccount++; // if 13 cards have been displayed in a line, start a new line if (0 == (ccount % 13)) { std::cout << '\n'; } } std::cout << '\n'; // shuffle the shoe, using the random engine created earlier // https://en.cppreference.com/w/cpp/algorithm/random_shuffle std::shuffle(shoe.begin(), shoe.end(), prng); // display the shuffled shoe std::cout << "Shuffled shoe:\n"; for (card ccount { }; const card & c : shoe) { std::cout << rank(c) << suit(c) << ' '; ccount++; if (0 == (ccount % 13)) { std::cout << '\n'; } } std::cout << '\n'; // create 2 players and the dealer using vectors // http://www.cplusplus.com/reference/vector/vector/ std::vector p1; std::vector p2; std::vector d; // deal two cards to each player/dealer for (size_t itr { }; itr < 2; ++itr) { // begin() returns an iterator to the first element, like a pointer // need to retrieve the stored value using operator* // and add that value to the player's hand p1.push_back(*shoe.begin()); // remove the first element from the shoe shoe.pop_front(); // can access the first element using operator[] p2.push_back(shoe[0]); shoe.pop_front(); d.push_back(shoe[0]); shoe.pop_front(); } // display players' and dealer's hands // the 'hole' card is displayed with parentheses std::cout << "Player 1: "; std::cout << '(' << rank(p1[0]) << suit(p1[0]) << ") "; std::cout << rank(p1[1]) << suit(p1[1]) << '\n'; std::cout << "Player 2: "; std::cout << '(' << rank(p2[0]) << suit(p2[0]) << ") "; std::cout << rank(p2[1]) << suit(p2[1]) << '\n'; std::cout << "Dealer: "; std::cout << '(' << rank(d[0]) << suit(d[0]) << ") "; std::cout << rank(d[1]) << suit(d[1]) << '\n'; std::cout << '\n'; // display the shoe with already dealt cards std::cout << "Shoe after dealing cards to players:\n"; for (card ccount { }; const card & c : shoe) { std::cout << rank(c) << suit(c) << ' '; ccount++; if (0 == (ccount % 13)) { std::cout << '\n'; } } std::cout << '\n'; }
 Unshuffled shoe: AS KS QS JS TS 9S 8S 7S 6S 5S 4S 3S 2S AH KH QH JH TH 9H 8H 7H 6H 5H 4H 3H 2H AD KD QD JD TD 9D 8D 7D 6D 5D 4D 3D 2D AC KC QC JC TC 9C 8C 7C 6C 5C 4C 3C 2C AS KS QS JS TS 9S 8S 7S 6S 5S 4S 3S 2S AH KH QH JH TH 9H 8H 7H 6H 5H 4H 3H 2H AD KD QD JD TD 9D 8D 7D 6D 5D 4D 3D 2D AC KC QC JC TC 9C 8C 7C 6C 5C 4C 3C 2C AS KS QS JS TS 9S 8S 7S 6S 5S 4S 3S 2S AH KH QH JH TH 9H 8H 7H 6H 5H 4H 3H 2H AD KD QD JD TD 9D 8D 7D 6D 5D 4D 3D 2D AC KC QC JC TC 9C 8C 7C 6C 5C 4C 3C 2C Shuffled shoe: 5D TC 6D AS TH QC 6C 6C KH QS 7H 2D 7S 5H 9D TD 5S 7S AS 5D 4D 9S KH JS 9C 7H 7C 9H 4S 9C TS 6C 7C KC TD 5C 4C 8S QS AD 3H 6H 9C TS 8H KS JH 8H JD TD 3C 5H JS 2S 2D 7D AC JC 6S QC 3D KD 2C 7S QD 4C 3C 6H 6D QH JH 6H TC 9S 8C 3H 2C 4D 3C 5S 4D TH AC 4H TS QD KC AH 9H 2H 3D AS KS 4H 5H KC KD 3H 6S JD 2C 2H 8S 4H 2H 7C QS AD 3S 3S AD KH 3S 4S TH JD 4C 6D JS 5S 7D 8D KS QC 8H 9H 4S 9D JC QD 2S 2S 8D QH AC 2D 5C JC 9D 8S 8D 8C 9S 8C AH KD 5C QH 7H 3D 5D TC 7D 6S AH JH Player 1: (5D) AS Player 2: (TC) TH Dealer: (6D) QC Shoe after dealing cards to players: 6C 6C KH QS 7H 2D 7S 5H 9D TD 5S 7S AS 5D 4D 9S KH JS 9C 7H 7C 9H 4S 9C TS 6C 7C KC TD 5C 4C 8S QS AD 3H 6H 9C TS 8H KS JH 8H JD TD 3C 5H JS 2S 2D 7D AC JC 6S QC 3D KD 2C 7S QD 4C 3C 6H 6D QH JH 6H TC 9S 8C 3H 2C 4D 3C 5S 4D TH AC 4H TS QD KC AH 9H 2H 3D AS KS 4H 5H KC KD 3H 6S JD 2C 2H 8S 4H 2H 7C QS AD 3S 3S AD KH 3S 4S TH JD 4C 6D JS 5S 7D 8D KS QC 8H 9H 4S 9D JC QD 2S 2S 8D QH AC 2D 5C JC 9D 8S 8D 8C 9S 8C AH KD 5C QH 7H 3D 5D TC 7D 6S AH JH
 Teacher should be smacked repeatedly with a soft pillow or something.

That is too kind for giving an exercise like this that requires such a code mess to make it work when the C++ solution is so much simpler. Something involving boiling oil possibly...

@sandbox007 - whatever solution you finally code within the given constraints, please remember that this is NOT how it should be coded in C++ and that you are NOT being taught good C++ practice.

Given the restraints, I'm not even going to think about re-factoring any of this code mess.
Last edited on
This will generate 4 unique numbers (num1, num2, num3, num4) in the range 1 - 4 based upon C random number functions (as per original code) and within the given constraints:

 123456789101112131415161718192021222324252627282930 #include #include #include int main() { std::srand(static_cast(std::time(nullptr))); unsigned used {}; unsigned num1 {}, num2 {}, num3 {}, num4 {}; num1 = std::rand() % 4 + 1; used |= 1 << num1; do num2 = std::rand() % 4 + 1; while (used & (1 << num2)); used |= 1 << num2; do num3 = std::rand() % 4 + 1; while (used & (1 << num3)); used |= 1 << num3; do num4 = std::rand() % 4 + 1; while (used & (1 << num4)); used |= 1 << num4; std::cout << num1 << '\t' << num2 << '\t' << num3 << '\t' << num4 << '\n'; }

 3 1 2 4

Which code?
Forget it, seeplus, Hison is an edit-the-post spammer. Bork him!
FYI, anyone foolish enough to click on spammer Hison's links deserves the grief they get. Don't come complaining to me afterwards.
Topic archived. No new replies allowed.