Array confusion!

I need a bit of advice an array

Page 1 of 1

6 Replies - 1356 Views - Last Post: 27 March 2008 - 03:14 AM Rate Topic: -----

#1 Dave4M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-February 08

Array confusion!

Post icon  Posted 07 March 2008 - 03:45 PM

Hi everyone,

Firstly, I would like to thank you all for your very warm welcome. It was very much appreciated.

I am creating a card game for a Visual Basic course I am doing, however, I am a bit confused by one stipulation the brief contains.

It states that a single deck of 52 cards must be used and no card should be reused until the deck is shuffled (when there is less than 15 cards left).

I have set up the array

	Dim arrDeck(0 To 51) As Integer 


and have considered several options to solve this problem:

1. Having an extra array dimension to state whether the card has been used or not
2. Creating a new Dispose array in which to copy used cards then copy them back for the shuffle

But I can not help but feel there is an easier way. Have I missed something?

I would appreciate any guidance anyone can offer.

Thanks in advance!


Dave
Belfast

Is This A Good Question/Topic? 0
  • +

Replies To: Array confusion!

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Array confusion!

Posted 07 March 2008 - 05:18 PM

Perhaps you can keep track of your index value in the array? I am not sure how they are going to want you to draw cards. If it is at random, you could use a technique I cover on my blog about randomizing the values in the array and simply pulling values off the top. This approach is good because then you can keep track of the counter and when it reaches less than 15, call a shuffle function.

If they want the array to be in order but then you pull random values out of the array, that is a bit tricker but as long as you call a function to do that and keep a counter (not index value mind you) that will tell you how many cards have been chosen before needing a reshuffle that will work.

So for the examples... I am going to use simple integers here to demonstrate the point. My first approach for 1-5 would be to create a shuffled array...

0 = 1
1 = 4
2 = 2
3 = 5
4 = 3



With this you could use a loop to go down from 0 to 4 and pull off values thus generating 1, 4, 2, 5, 3. Since the array was randomly put together, going through it sequentially is also random. Once your index is greater than 37, then you could reshuffle (aka re-randomize your array and set the index back to zero).


The second approach would involve an ordered array (like the code above but that the right side would be in counting order) and to pull values out at random. You would setup a function that would do that for you (also determine if the number has been already chosen and if so, re-select) but that you will in essence keep track of the count.

Do until counter = 37 
     Dim cardpulled as Integer
     cardpulled = cardSelectFunction()
     ' Do whatever with card
     counter = counter + 1
Loop

' Reshuffle down here and then loop back around again.



The choice is really up to you how you want to do this. Personally I like approach one. I think it is slick and can work out nicely for what you want to do. The beauty of it is that you don't have to check if the card has already been pulled out and you can control the range of values (the card faces in the deck).

Hope that helps. Enjoy!

"At DIC we be card dealing, array smashing code ninjas!" :snap:

This post has been edited by Martyr2: 07 March 2008 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dave4M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-February 08

Re: Array confusion!

Posted 08 March 2008 - 09:54 AM

Thank you very much for your fast reply.

I am afraid that I may not have outlined my problem correctly, although I did find your response very interesting and informative.

I have most of the code organised in my design brief and I have now included a very simplified copy of my psuedocode.


Begin 

Set up Pack array

Repeat

		 Shuffle Pack array

		 While Not Pack array < 15

				  Repeat

							Deal cards
				  
							Process Stick/twist 

				  Unit player sticks

				  Calculate winners

				  Prompt for new game

				  If no then 

							   Exit

				  End if

		 End While

		 Reset counter

		 Shuffle Cards
 
Until Player chooses exit

End 




My major concern lies with how do I make sure that cards in the array are not rechoosen. So that if the King of Spades is dealt, it will not be redealt until the cards are shuffled.

Please accept my apologies if I have failed to understand a solution to this problem given in the first response below.

I have worked with arrays before (using COBOL) but I am a bit rusty.

Thank you in advance again.


View PostMartyr2, on 7 Mar, 2008 - 05:18 PM, said:

Perhaps you can keep track of your index value in the array? I am not sure how they are going to want you to draw cards. If it is at random, you could use a technique I cover on my blog about randomizing the values in the array and simply pulling values off the top. This approach is good because then you can keep track of the counter and when it reaches less than 15, call a shuffle function.

If they want the array to be in order but then you pull random values out of the array, that is a bit tricker but as long as you call a function to do that and keep a counter (not index value mind you) that will tell you how many cards have been chosen before needing a reshuffle that will work.

So for the examples... I am going to use simple integers here to demonstrate the point. My first approach for 1-5 would be to create a shuffled array...

0 = 1
1 = 4
2 = 2
3 = 5
4 = 3



With this you could use a loop to go down from 0 to 4 and pull off values thus generating 1, 4, 2, 5, 3. Since the array was randomly put together, going through it sequentially is also random. Once your index is greater than 37, then you could reshuffle (aka re-randomize your array and set the index back to zero).


The second approach would involve an ordered array (like the code above but that the right side would be in counting order) and to pull values out at random. You would setup a function that would do that for you (also determine if the number has been already chosen and if so, re-select) but that you will in essence keep track of the count.

Do until counter = 37 
     Dim cardpulled as Integer
     cardpulled = cardSelectFunction()
     ' Do whatever with card
     counter = counter + 1
Loop

' Reshuffle down here and then loop back around again.



The choice is really up to you how you want to do this. Personally I like approach one. I think it is slick and can work out nicely for what you want to do. The beauty of it is that you don't have to check if the card has already been pulled out and you can control the range of values (the card faces in the deck).

Hope that helps. Enjoy!

"At DIC we be card dealing, array smashing code ninjas!" :snap:

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Array confusion!

Posted 08 March 2008 - 10:35 AM

Re-read approach number 1 I gave you. This is the method you would use for your non-repeating until reshuffle situation. Here is the pseudocode...

1) Create an array to hold all cards
2) fill array sequentially with all possible values (all card faces in the deck)
3) Switch them around in random order (shuffle them around)
4) Start at the top of the array and sequentially go through the array.
5) When we hit the 37th card, call reshuffle and repeat step 4 onwards.

For example...

element 0 = AH (ace of hearts)
element 1 = 2H (2 of hearts)
element 2 = 3H (3 of hearts... you get the picture)
...
element 51 = KS (king of spades)



Here we have all the cards of the deck in an array of 52 cards. Great, they are in sequential order. Write a swapping algorithm that just swaps them around at random (pick two spots randomly and swap their values).

Result is...

element 0 = 2D (2 of Diamonds)
element 1 = 7C (7 of Clubs)
element 2 = JH (jack of hearts)
...
element 51 = 3D (3 of Diamonds)



So now we have an array that has all 52 cards and is shuffled around. (You create a shuffle method for the project, so just use that to shuffle them around initially).

Since you do a random shuffle of the cards, they are now shuffled randomly in their spots. So you can start pulling them right off the top.

Pick element 0, then element 1, then element 2....incrementing the index each time. When you reach index 37, you know you have 15 cards left to choose... you can trigger a reshuffle of the same array. Then you can start right back at element 0 and do it all again.

I hope I have made it all clear now. ;)

This post has been edited by Martyr2: 08 March 2008 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

#5 Dave4M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-February 08

Re: Array confusion!

Posted 08 March 2008 - 10:58 AM

Martyr2

Thank you for your patience.

Your first answer was very clear, unfortunately, I had managed to confuse myself so much with reading so much on arrays and sorting integers and stuff that I was blind to the answer.

Thanks again, I appreciate the pseudocode as I would like to construct the code myself.
Was This Post Helpful? 0
  • +
  • -

#6 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Array confusion!

Posted 08 March 2008 - 11:00 AM

View PostDave4M, on 8 Mar, 2008 - 11:28 PM, said:

I would like to construct the code myself.

That's the spirit :)!
Was This Post Helpful? 0
  • +
  • -

#7 Dave4M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-February 08

Re: Array confusion!

Posted 27 March 2008 - 03:14 AM

Right,

I would like to bring a bit of closure to this subject.

If you read the earlier posts in this thread you will know that I have been doing a course on Visual Basic and have been presented with a Black Jack program to write.

Coming from a qBasic/COBOL background I use to be very familiar with arrays but the many years of database development has dulled my understanding of them.

My main problem was that I kept thinking that the array in terms of a COBOL update process, that is I would need to copy the cards from one array to a temp array then take one out and copy them back to the original array. Dont ask me why!

However, after some careful guidance from Martyr2, we prevailed (Martyr2, if you are ever in Belfast, give me a shout and I will sponsor a beer for you) and hopefully arrays are stuck in my tiny mind for good.

For anybody else suffering my same fate, I offer some advice:

Plan your project - even if you do not use Jackson Structured Programming (JSP), SSADM or UML, put something down on paper. This is an area where I am neurotic (maybe I will try to make it into a tutorial at a later date). It is essential that you write a simple design to ensure that you are heading towards your goal and it will act as a map if you have to update it.

Modulate your code and annotate it AS YOU WRITE IT - I am in the process of debugging and streamlining my code and have found my obsession with subdividing my code into subs and functions (functions return a value, subs do not - for those who were afraid to ask) invaluable for finding my way to problem areas. Unfortunately, my annotating skills are nowhere near as developed, so although I was able to find the problem, I was not always able to remember what the hell I was talking about. I sometimes make the mistake of thinking I can do it at the end - WRONG! By the time I get to the end (end is such a flexible word in the world of programming) I have forgotten how and why I am using this bit of code or this counter, but it often turns out that these are essential to the code I was writing.

SAVE PRINT SAVE - I cannot express this strongly enough! The number of times I have lost code because I have went to grab a coffee and my girlfriend (who I really do love, most of the time) decides that she wants to check her email and "all the funny windows" confuse her. I have taught her to minimize (I have even put instructions on the wall next to the monitor) but Close is so much easier and (unlike Jesus) she never saves. So now I save as often as I remember and then print at the end of the session.

These are just observations I learned (painfully) and after all the kind support I thought I would try to give something back.

After all, the best lessons are learnt from another (wo)mans mistakes!

Take care and thanks for reading!

Oh, and PM/email me if you like the idea of a tutorial on unorthodox design!

This post has been edited by Dave4M: 27 March 2008 - 05:00 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1