|
-
Oct 5th, 2002, 06:25 PM
#1
Thread Starter
Hyperactive Member
Card game
i am making a card game with C++. have an array: cards[52].
i need it to go through and assign a number of 1 to 52 to cards[1] to cards[52]. I need each number to be used only once. So it simulates a shuffled deck of cards without jokers. Ex:
cards[1] = 25
cards[2] = 13
cards[3] = 17
cards[4] = 40
...
each one needs a different number 1 to 52.
-
Oct 6th, 2002, 12:10 AM
#2
Junior Member
Sounds like a job for rand() to me. Get your app to randomly generate a number between 1 and 52, then assign that number to card[x]. Have an if statment that recalls your random generator function if you get a repeat number.
Of course, you do realize that your array will be from 0 to 51, not 1 to 52 (unless this has changed from C to C++).
I might post some ugly code on this later, my brain is fried right now.
-
Oct 6th, 2002, 02:34 AM
#3
Hyperactive Member
Try the code below,
Courtesy of Jim McNamara, he is the one who taught me this.
PHP Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i,j,arr[52],tmp;
j=0;
//assign the numbers to the elements of the array
while(j<52)
{
arr[j]=j;
++j;
}
j=51;
srand((unsigned)time( NULL ));
//The code below re-shuffles them
while (j >= 0)
{
i = (rand()%51);
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
--j;
}
//The code below displays them
j=0;
while(j<52)
{
//You have to add 1 because your number starts from
//zero.
cout<<arr[j]+1<<endl;
++j;
}
return 0;
}
Last edited by transcendental; Oct 6th, 2002 at 02:38 AM.
-
Oct 6th, 2002, 04:27 AM
#4
Code:
#include <algorithm>
using std::random_shuffle;
int cards[52];
int i;
for(i=0;i<52;++i) {
cards[i] = i;
}
// shuffle them
random_shuffle(cards, cards+52);
// now shuffled
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 6th, 2002, 07:48 AM
#5
Thread Starter
Hyperactive Member
holy crap, i would never had thought there would be a command that "shuffles". And about the index starting with 0: cards[0], this is true that it sarts at cards[0], but i'm being lazy rightnow and to help my lazy brain i'm pretending that it starts at 1. But i'm going to have to be not lazy cause the code above starts at 0 and goes to 51
-
Oct 6th, 2002, 08:48 AM
#6
Hyperactive Member
Originally posted by CornedBee
Code:
#include <algorithm>
using std::random_shuffle;
int cards[52];
int i;
for(i=0;i<52;++i) {
cards[i] = i;
}
// shuffle them
random_shuffle(cards, cards+52);
// now shuffled
That's the problem with C programmers coming to C++ camp.
C programmers are too used to do everything on their own, when in fact, readily-made algorithms are there.
-
Oct 6th, 2002, 10:16 AM
#7
Junior Member
When i'm learning, i prefer to do it on my own. I would honestly prefer to write my own card shuffling algorithm the first time through.
Anytime after that, however, using pre-made algorithms that are prolly better than what i've made is just fine.
And yes, i'm a C programmer (who just happens to dip into C++ every so often).
-
Oct 6th, 2002, 01:32 PM
#8
Probably better. But there are some things I'm just too lazy to ever implement it, as long as I think I'd be capable of doing it.
And learning C++ is about as much learning the language basics (and, in succession, how to implement a shuffle-algorithm) as learning what the Standard Library offers you. The SL part is probably even bigger.
For example, modify the string class to store strings the same way as string does, but doing all compares case insensitive.
You wouldn't believe how easy it is once you know how.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 16th, 2002, 03:47 AM
#9
Fanatic Member
this code has same logic as given by transcendental. But here I added a generic swap function :-) Note that I initialized the card array as string, S for Spade, H for Hearts, D Diamonds, C Clubs
// to shuffle pack of cards
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
// generic swap function
void swap(void **x,void **y)
{
void *temp;
temp = *x;
*x = *y;
*y = temp;
}
void main(void)
{
// cards array
char* d[] = {"1C","2C","3C","4C","5C","6C","7C","8C","9C","10C","JC","QC","KC",
"1D","2D","3D","4D","5D","6D","7D","8D","9D","10D","JD","QD","KD",
"1H","2H","3H","4H","5H","6H","7H","8H","9H","10H","JH","QH","KH",
"1S","2S","3S","4S","5S","6S","7S","8S","9S","10S","JS","QS","KS"};
// size
int s = 51;
// to shuffle
int i,x;
// to randomize seed
srand(time(NULL));
// do shuffling
for(i=s;i>=0;i--)
{
x=rand()*i/RAND_MAX;
swap((void**)&d[i],(void**)&d[x]);
}
// print shuffled cards
cout << "shuffled cards are" << endl;
for(i=0;i<=s;i++)
cout << d[i] << " ";
}
Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.
-
Oct 16th, 2002, 01:32 PM
#10
Monday Morning Lunatic
Originally posted by CornedBee
Probably better. But there are some things I'm just too lazy to ever implement it, as long as I think I'd be capable of doing it.
And learning C++ is about as much learning the language basics (and, in succession, how to implement a shuffle-algorithm) as learning what the Standard Library offers you. The SL part is probably even bigger.
For example, modify the string class to store strings the same way as string does, but doing all compares case insensitive.
You wouldn't believe how easy it is once you know how.
Mmmm, char_traits
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 16th, 2002, 01:33 PM
#11
Monday Morning Lunatic
Originally posted by sbasak
this code has same logic as given by transcendental. But here I added a generic swap function :-) Note that I initialized the card array as string, S for Spade, H for Hearts, D Diamonds, C Clubs
// to shuffle pack of cards
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
// generic swap function
void swap(void **x,void **y)
{
void *temp;
temp = *x;
*x = *y;
*y = temp;
}
void main(void)
{
// cards array
char* d[] = {"1C","2C","3C","4C","5C","6C","7C","8C","9C","10C","JC","QC","KC",
"1D","2D","3D","4D","5D","6D","7D","8D","9D","10D","JD","QD","KD",
"1H","2H","3H","4H","5H","6H","7H","8H","9H","10H","JH","QH","KH",
"1S","2S","3S","4S","5S","6S","7S","8S","9S","10S","JS","QS","KS"};
// size
int s = 51;
// to shuffle
int i,x;
// to randomize seed
srand(time(NULL));
// do shuffling
for(i=s;i>=0;i--)
{
x=rand()*i/RAND_MAX;
swap((void**)&d[i],(void**)&d[x]);
}
// print shuffled cards
cout << "shuffled cards are" << endl;
for(i=0;i<=s;i++)
cout << d[i] << " ";
}
Just a few things.
1. iostream, not iostream.h
2. There's std::swap in <algorithm>
3. Do what CB did and use the Standard Library
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|