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


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
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 <<num4;
	int card1=num1 , card2=num2,card3=num3,card4=num4;
	
	cout << setw(50) << "CARD SHUFFLING COMPLETED \n" <<endl;
	cout << card1 << "\t" <<card2 << "\t" <<card3 << "\t" << card4  <<endl;

	
	
	do
	{
	cout << endl << " (Player One), please pick a number from 1-4 to draw your card : \n" <<endl; 
	cin >> 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 <<num2 << num3 ;
	cout << "Cards reshuffled. /n" ;
	cout << "Dear Player 2, please select your shuffled card from 1-4 : " <<endl;
	cin >> choice2;
	
	cout << "Player 1 chose card " << card1 << endl;
	cout << "Player 2 chose card " << card2 <<endl;

}

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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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):
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
#include <iostream>
#include <random>    // std::default_random_engine, std::random_device
#include <deque>     // C++ dynamic container, std::deque
#include <numeric>   // std::iota
#include <algorithm> // 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<card> 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):
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <deque>
#include <vector>
#include <random>    // std::default_random_engine, std::random_device
#include <numeric>   // std::iota
#include <algorithm> // 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<card> 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<card> 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<card> p1;
   std::vector<card> p2;
   std::vector<card> 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:

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
#include <iostream>
#include <ctime>
#include <cstdlib>

int main() {
	std::srand(static_cast<unsigned>(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.