PDA

Click to See Complete Forum and Search --> : Removing array elements


scuzymoto
Dec 2nd, 1999, 12:52 AM
I am writing a computer version of a board game. I have an array of the game cards that should pop up when the user lands on the correct space. I want to be able to select any one of these cards at random but never twice in one game. What would be a good way to go about this (remembering that a game may last several weeks, so it needs to be retained on disk what cards have been picked).
I have designed graphically the card and now I just want the correct message to pop up within the graphical representation as well as a couple parameters to define how much money is taken from or added to the players bank account.
Any ideas? I have considerd an array of pointers that a random number is used to select from the array a single element but then after it has been used removing the element from the array and reitinizing and all that is a pain. Thanks for your help.

------------------
SCUZ

scuzymoto
Dec 2nd, 1999, 01:01 AM
Sounds like me and Rose have basically the same question.

Aaron Young
Dec 2nd, 1999, 01:11 AM
The easiest way is probably to use a Shuffling Method, whereby you just randomize the Array of Data, then Select each one from Start to Finish.

Doing this means you never have to worry about picking the same element twice as you are selecting them in an Orderly Fashion.

This way you could store the Contents of the Array and the Current Position in the Array, to save a Game, eg.

Private aCards(9) As String

Private Sub Form_Load()
aCards(0) = "Advance To Go!"
aCards(1) = "Go Back 3 Spaces"
aCards(2) = "Pay Tax of $200"
aCards(3) = "Advance to VB-World"
aCards(4) = "Ask Another Question"
aCards(5) = "Answer the Question"
aCards(6) = "Select a New Topic"
aCards(7) = "Edit Your Profile"
aCards(8) = "You Win a Prize!"
aCards(9) = "You Lose!"
End Sub

Private Sub Command1_Click()
Dim I As Integer
ShuffleDeck aCards
For I = 0 To 9
MsgBox aCards(I)
Next
End Sub

Private Sub ShuffleDeck(ByRef aArray As Variant)
Dim iTimes As Integer
Dim sTmp As String
Dim iElement As Integer
Dim iSwap As Integer

Randomize Timer
For iTimes = 0 To Int(Rnd * 90) + 10
For iElement = LBound(aArray) To UBound(aArray)
sTmp = aArray(iElement)
iSwap = Int(Rnd * (UBound(aArray) - LBound(aArray))) + LBound(aArray)
aArray(iElement) = aArray(iSwap)
aArray(iSwap) = sTmp
Next
Next
End Sub

If you don't want to save all the Card Data each time you save a game, create an Array to use as an Index and Shuffle that instead.

------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net

Rose
Dec 2nd, 1999, 01:40 AM
I got the code about a card shuffling and dealing simulation from the book According to the book, it says, "This shuffling algorithm could execute for an indefinitely long period of time if cards that have already been shuffled are repeatedly selected at random"

Option Explicit
Dim mSuit(1 To 4) As String, mFace(1 To 13) As String
Dim mDeck(1 To 4, 1 To 13) As Integer

Private Sub Form_Load()
mSuit(1) = "Hearts"
mSuit(2) = "Diamonds"
mSuit(3) = "Clubs"
mSuit(4) = "Spades"

mFace(1) = "Ace"
mFace(2) = "Deuce"
mFace(3) = "Three"
mFace(4) = "Four"
mFace(5) = "Five"
mFace(6) = "Six"
mFace(7) = "Seven"
mFace(8) = "Eight"
mFace(9) = "Nine"
mFace(10) = "Ten"
mFace(11) = "Jack"
mFace(12) = "Queen"
mFace(13) = "King"

Call Randomize
End Sub

Private Sub cmdShuffle_Click()
Call lstOutput.Clear
Call Shuffle
Call Deal
End Sub

Private Sub Shuffle()
Dim card As Integer, row As Integer, column As Integer

Call ZeroDeckArray

For card = 1 To 52
Do
row = 1 + Int(Rnd() * UBound(mSuit))
column = 1 + Int(Rnd() * UBound(mFace))
Loop While mDeck(row, column) <> 0

mDeck(row, column) = card
Next

End Sub

Private Sub Deal()
Dim card As Integer, row As Integer, column As Integer

For card = 1 To 52
For row = LBound(mSuit) To UBound(mSuit)
For column = LBound(mFace) To UBound(mFace)
If mDeck(row, column) = card Then
lstOutput.AddItem ( _
mFace(column) & " of " & mSuit(row))
End If
Next column
Next row
Next card

End Sub

Private Sub ZeroDeckArray()
Dim row As Integer, column As Integer

For row = LBound(mSuit) To UBound(mSuit)
For column = LBound(mFace) To UBound(mFace)
mDeck(row, column) = 0
Next column
Next row

End Sub


Hope this helps....


ROSE

Rose
Dec 2nd, 1999, 03:17 AM
Aaron Young

I used your code into my project and modified some.... There are no repeating words in my application!!!!

You make my day!!!!

Thank you alot!!! Thank you Thank you Thank you Thank you.

Thank all of others who helped me!!

ROSE