• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
phil67rpg

shuffle array

12 posts in this topic

Well I am attempting to shuffle a deck of cards using the random_shuffle function out of the algorithm library in the STL. I am able to print out a sorted deck of cards. I am confused on how to use the random_shuffle function on the cards array.I have googled the random_shuffle function but I am still perplexed on how to integrate the cards array. I am trying to not use magic numbers and I am trying to write better code. By the way low_val is 2 and hi_val is 14. I don't need any code just a small explanation. I  will do more research on arrays.here is small snipet of code.

Also values is 15 and suits is 4.i am lost in the cards+low_val and cards+hi_val part of code.

 
 
int cards[suits][values]={0};
 
 
for(int i=low_val; i<=hi_val; i++)
{
cards[spades][i]=i;
cards[hearts][i]=i;
cards[diamonds][i]=i;
cards[clubs][i]=i;
}
random_shuffle(cards+low_val,cards+hi_val);

 

0

Share this post


Link to post
Share on other sites

random shuffle (and other stl algorithims) need a container of things being shuffled. In this case you'd want a 1D array of cards. But you don't have a 1D array of cards, you have a 2D array of integers. You could typecast the extra dimension away, but the better approach would be to just use a 1D array. You can use integers to represent cards if it suits you, but then you're not keeping track of the suit. Alternately, use an class or struct.

 

Also, the algorithm needs iterators to the beginning and end of the array. For a simple array that's the name of the array, and the name plus the size.

0

Share this post


Link to post
Share on other sites

I gather this is wasted but hey, I'm bored.

 

Your using the Wrong structures imo. You either want to use a std::Map or use 4 1D Arrays

 

E.g.

//EXAMPLE 1
std::Map<string, std::vector<int>> CardsBySuit;
CardsBySuit["Clubs"] = new vector<int>(...) //{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, }
CardsBySuit["Spades"] = new vector<int>(...) //{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, }
CardsBySuit["Diamonds"] = new vector<int>(...) //{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, }
CardsBySuit["Hearts"] = new vector<int>(...) // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, }
 
// OR Example 2
int Diamonds = new vector<int>(...);
int Hearts= new vector<int>(...);
int Spades= new vector<int>(...);
int Clubs= new vector<int>(...);

Then to shuffle you have in int[] stored foreach suit and just suffle like so:

//EXAMPLE 1
std::random_shuffle(CardBySuit["DIAMONDS"].begin(), _CardBySuit["DIAMONDS"].end());
 
//EXAMPLE 2
std::random_shuffle(Clubs.begin(), _Clubs.end());

However, there is a easier, and nicer Object Oriented way of Storing your data using ENUMS and classes you should look into.

Edited by Andy474
-3

Share this post


Link to post
Share on other sites

First, using a map or four 1D arrays for simulating cards is probably the worst way to do it. Second, if you feel the need to new any C++SL container (like a vector), you are probably doing something very wrong. Third, I highly doubt you can random_shuffle iterators into a container which has order as something intrinsic to its very being, like a map.

1

Share this post


Link to post
Share on other sites

Make a class or struct to represent your cards

 

struct card

{

int suit;

int value;

};

 

then use a container of cards and shuffle it.

0

Share this post


Link to post
Share on other sites

so I should use vectors instead of arrays according to andys post.

 
Not quite.  You should use container classes when appropriate, but that was not the key point of his post.

 
Much of the standard library is designed to work on a range. Look closer at his code:

std::random_shuffle(CardBySuit["DIAMONDS"].begin(), _CardBySuit["DIAMONDS"].end());

The parameters are x.begin() and x.end(). This is a range of objects. It starts at x[0] and ends at x[14] exclusive.

Compare it again with your code sample:

int cards[suits][values]={0};
...
random_shuffle(cards+low_val,cards+hi_val);

This is also a range of objects, but obviously not the correct range. This range starts at cards[2] and ends at cards[14].
 

 

In more general terms, a range operation takes two parameters, the starting address and the address one past the end of the range.  The developer can then use loops like for(T* ptr = begin; ptr < end; ptr++) and it will work correctly for any type T.

This type of range operation is ubiquitous in the language. If you are not comfortable with this kind of operation, I strongly suggest you go get a good book about the C++ language, such as Accelerated C++ by Koenig and Moo, and read it carefully.  

Edited by frob
2

Share this post


Link to post
Share on other sites

// OR Example 2
int Diamonds = new vector<int>(...);
int Hearts= new vector<int>(...);
int Spades= new vector<int>(...);
int Clubs= new vector<int>(...);

 

Um... Did you just assigned vector to an integer?

Edited by Edvinas Kilbauskas
0

Share this post


Link to post
Share on other sites

 


// OR Example 2
int Diamonds = new vector<int>(...);
int Hearts= new vector<int>(...);
int Spades= new vector<int>(...);
int Clubs= new vector<int>(...);

 

Um... Did you just assigned vector to an integer?

 

 

A pointer to a vector on top of that. or is this not c++? maybe java? laugh.png

0

Share this post


Link to post
Share on other sites

 

 


// OR Example 2
int Diamonds = new vector<int>(...);
int Hearts= new vector<int>(...);
int Spades= new vector<int>(...);
int Clubs= new vector<int>(...);

 

Um... Did you just assigned vector to an integer?

 

 

A pointer to a vector on top of that. or is this not c++? maybe java? laugh.png

 

You can't do that in java either :D

Maybe he just made his own programming language, haha

0

Share this post


Link to post
Share on other sites

 

 

 


// OR Example 2
int Diamonds = new vector<int>(...);
int Hearts= new vector<int>(...);
int Spades= new vector<int>(...);
int Clubs= new vector<int>(...);

 

Um... Did you just assigned vector to an integer?

 

 

A pointer to a vector on top of that. or is this not c++? maybe java? laugh.png

 

You can't do that in java either biggrin.png

Maybe he just made his own programming language, haha

 

:p

 

big mistake, typo ...

 

the [ code] tags on here keep eating code when i type so i have to retype, retype retype.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0