I'm trying to shuffle a deck of card in C ++, with out having the same card twice. And I'm supposed to give each player 5 cards. And if they want, they can replace 3 cards. Right now I can assign 5 cards to each player, but some of the cards repeat themselces (ex. I have 3 Ace of spades, or 2 king of clubs, etc). Can anyone help me fix this problem. Here is what I have so far. -
#include <iostream>
-
#include <ctime>
-
#include <cstdlib>
-
#include <cmath>
-
-
-
using namespace std;
-
using std::cout;
-
using std::strncpy;
-
-
//int random;
-
-
int main()
-
{
-
srand((unsigned)time(0));
-
-
string heart[] = {"A\3","2\3","3\3","4\3","5\3","6\3","7\3","8\3","9\3","10\3","J\3","Q\3","K\3"};
-
string diamond[] = {"A\4","2\4","3\4","4\4","5\4","6\4","7\4","8\4","9\4","10\4","J\4","Q\4","K\4"};
-
string club[] = {"A\5","2\5","3\5","4\5","5\6","6\5","7\5","8\5","9\5","10\5","J\5","Q\5","K\5"};
-
string spade[] = {"A\6","2\6","3\6","4\6","5\6","6\6","7\6","8\6","9\6","10\6","J\6","Q\6","K\6"};
-
-
-
string general[52] ; //= new string[52];
-
int k=0;
-
-
for(int j = 0; j <= 12; j++)
-
{
-
general [k++] = heart[rand()% 12];
-
general [k++] = diamond[rand()% 12];
-
general [k++] = club[rand()% 12];
-
general [k++] = spade[rand()% 12];
-
}
-
-
-
int id1, id2;
-
string temp;
-
for (int i = 0; i < 100; i++ )
-
{
-
id1 = rand()%52;
-
id2 = rand()%52;
-
if(id1!=id2)
-
{
-
temp = general[id1];
-
general[id1] = general[id2];
-
general[id2] = temp;
-
}
-
-
}
-
for(int i=0; i<52; i++)
-
{
-
-
cout<< general[i]<<" ";
-
}
-
cout << endl;
-
-
-
int currentId = 1;
-
string player1 [5];
-
string player2 [5];
-
int currentindex =0;
-
for (int p = 0; p < 5; p++)
-
{
-
-
player1 [p] = general [currentindex]; // picks the 0th,2nd,4th,6th,8th elements
-
-
player2 [p] = general [currentId]; // picks the 1st,3rd,5th,7th,9th elements
-
currentindex = currentindex + 2;
-
currentId = currentId +2;
-
}
-
-
for (int z = 0;z < 5;z++)
-
{
-
cout << player1[z] <<endl;
-
}
-
cout <<"-------------------------------"<<endl;
-
-
for (int t = 0; t< 5; t++)
-
cout <<player2[t]<<endl;
-
-
-
-
system("pause");
-
-
return 0;
-
}
3 3061 DeMan 1,806
Top Contributor
There are many approaches to this problem.....
One would be to "empty" an arrays value once it has been chosen, and set a loop around the random number generator which only exits when it finds an non-empty spot. This can, however be a little tedious and expensive.
ANother idea, might be to have a list where you keep all of the cards in some order (point to the array entry you want from the element in the list). As you choose a random card, remove it from the list, and alter your random number generator to choose a random number over the size of the reamining list eg:
if I have 10 elements in a list
Then rand % 10 and remove the element.
I now have nine elements in the list so rand % 9
etc.
Ganon11 3,652
Recognized Expert Specialist
If you can use classes, I would recommend making a Card class to hold the suit and value, and a Deck class to hold 52 cards. You can write functions in the Deck class to select a card from the top of the deck and remove that card from the current list of cards - this will guarantee that you on't get a repeat of a card.
Savage 1,764
Recognized Expert Top Contributor
I'm trying to shuffle a deck of card in C ++, with out having the same card twice. And I'm supposed to give each player 5 cards. And if they want, they can replace 3 cards. Right now I can assign 5 cards to each player, but some of the cards repeat themselces (ex. I have 3 Ace of spades, or 2 king of clubs, etc). Can anyone help me fix this problem. Here is what I have so far. -
#include <iostream>
-
#include <ctime>
-
#include <cstdlib>
-
#include <cmath>
-
-
-
using namespace std;
-
using std::cout;
-
using std::strncpy;
-
-
//int random;
-
-
int main()
-
{
-
srand((unsigned)time(0));
-
-
string heart[] = {"A\3","2\3","3\3","4\3","5\3","6\3","7\3","8\3","9\3","10\3","J\3","Q\3","K\3"};
-
string diamond[] = {"A\4","2\4","3\4","4\4","5\4","6\4","7\4","8\4","9\4","10\4","J\4","Q\4","K\4"};
-
string club[] = {"A\5","2\5","3\5","4\5","5\6","6\5","7\5","8\5","9\5","10\5","J\5","Q\5","K\5"};
-
string spade[] = {"A\6","2\6","3\6","4\6","5\6","6\6","7\6","8\6","9\6","10\6","J\6","Q\6","K\6"};
-
-
-
string general[52] ; //= new string[52];
-
int k=0;
-
-
for(int j = 0; j <= 12; j++)
-
{
-
general [k++] = heart[rand()% 12];
-
general [k++] = diamond[rand()% 12];
-
general [k++] = club[rand()% 12];
-
general [k++] = spade[rand()% 12];
-
}
-
-
-
int id1, id2;
-
string temp;
-
for (int i = 0; i < 100; i++ )
-
{
-
id1 = rand()%52;
-
id2 = rand()%52;
-
if(id1!=id2)
-
{
-
temp = general[id1];
-
general[id1] = general[id2];
-
general[id2] = temp;
-
}
-
-
}
-
for(int i=0; i<52; i++)
-
{
-
-
cout<< general[i]<<" ";
-
}
-
cout << endl;
-
-
-
int currentId = 1;
-
string player1 [5];
-
string player2 [5];
-
int currentindex =0;
-
for (int p = 0; p < 5; p++)
-
{
-
-
player1 [p] = general [currentindex]; // picks the 0th,2nd,4th,6th,8th elements
-
-
player2 [p] = general [currentId]; // picks the 1st,3rd,5th,7th,9th elements
-
currentindex = currentindex + 2;
-
currentId = currentId +2;
-
}
-
-
for (int z = 0;z < 5;z++)
-
{
-
cout << player1[z] <<endl;
-
}
-
cout <<"-------------------------------"<<endl;
-
-
for (int t = 0; t< 5; t++)
-
cout <<player2[t]<<endl;
-
-
-
-
system("pause");
-
-
return 0;
-
}
You need to implement a do while loop in your for loop for generarting cards.the loop shoul serch your's arrays and reapet it self if the same card is generated
It should be like: - int card_type[12];
-
do{
-
k=0;
-
for(int j = 0; j <= 12; j++)
-
{
-
card_type[j] = rand()%12;
-
general [k++] = heart[card_type[j]];
-
general [k++] = diamond[card_type[j]];
-
general [k++] = club[card_type[j]];
-
general [k++] = spade[card_type[j]];
-
}
-
-
}while( card_type[0]==card_type[1] ............);
Savage
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: JC |
last post by:
I am very new to programming and learning on my own.
Why do I keep getting duplicate values using this code? I want to shuffle a
deck of 52 cards. The logic seems right to me.
Randomize
For C = 0 To 1000
C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
|
by: glowfire |
last post by:
Please, somebody help me with this program!
We have a deck of 52 cards and we shuffle the deck by choosing a
random card out of the deck placing it back in the deck at some random
place. Repeat it 500 times and then consider the deck shuffled. Choose
a hand of 5 cards from the top of the deck. Count the number of hands
which have three-of-a-kind but don't count hands which have
four-of-a-kind. Continue to draw new hands until the deck...
|
by: CaseyB |
last post by:
If I wanted to create a game like Solitaire that would first randomly
shuffle a deck of cards, I figured out that all I had to use is the Random()
class or rnd and make sure I use the Randomize function so as not to get the
same card twice. BUT I noticed that some Solitaire games allow you to
select one of the 4,294,967,296 possibilities in a 52 card deck. I want to
do something similar, after I shuffle the deck I want to show the number...
|
by: ericvw |
last post by:
How would I shuffle a static array of 52 cards that you input an
integer, n, into a function and it takes the first n cards as the left
segment and the remaining as the right. Then it shuffles this deck
starting from the first of the right segment, then first of the left,
second of the right, second of the left. Once one side is exhausted it
fills in the rest of the shuffle with the other remaining segment. Any
suggestions?
|
by: greenflame |
last post by:
I would like to make a function that takes a list, more specificaly a
list of strings, and shuffles its elements, like a pile of cards. The
following is a script I tryed to make that implements pile shuffling.
----------
testdeck = list('qwertyuiop')
def pileshuffle(DECK, NUMPILES):
"""Split the deck given into NUMPILES piles. Then also put the
piles""" \
| |
by: Arun Nair |
last post by:
Can any one help me with this im not getting it even after reading
books because there is not much of discussion anywhere
a>
Implement a calss that represents a playing card. The class should
implement the following methods:
_ _ init _ _ (self, rank, suit) Creates a card.
rank is an integer in range of 1 to 13 (Ace:1, King 13), suit is a
character in set {'h','d','c','s'}
getRank(self) Returns the rank of the card
|
by: Pratik |
last post by:
For the time being, I'm doing a simple swap method for my deck of
cards using the random number generator in cstdlib. I've created a
dynamic array of a type class Card. When I go to shuffle it and then
later print the shuffled deck, some of the values overlap -- I can't
figure out why. Here's my code:
void shuffle()
{
for (counter = 0; counter < 120; counter++)
{
|
by: garyrowell |
last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received.
The program
This week you are going to write three classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below.
Card.Java
This is a simple class that represents a playing card.
Card has two attributes:
• rank which is a String that represents the value...
|
by: Vneha |
last post by:
import java.util.*;
public class Deck {
public static int numSuits = 4;
public static int numRanks = 13;
public static int numCards = numSuits * numRanks;
private Card cards;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |