Reputation: 483
Hello there i'm trying to make a Card War Game and what i want to do is select 1 image out of 3 images in total. The images are stored in the resource folder all numbers in same folder like All 2's all 3's so i can maybe Pick a card randomly if number = 2 so you don't always get 2 of hearts
I was thinking about this but the code will be very long.
Dim RC As Integer
if randomNumber = 2 then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen2
Else If RC = 2 Then
PictureBox1.Image = My.Resources.harten2
Else If RC = 3 Then
PictureBox1.Image = My.Resources.ruiten2
Else
PictureBox1.Image = My.Resources.klaveren2
End If
End If
If randomNumber = 3 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen3
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten3
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten3
Else
PictureBox1.Image = My.Resources.klaveren3
End If
End If
If randomNumber = 4 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen4
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten4
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten4
Else
PictureBox1.Image = My.Resources.klaveren4
End If
End If
If randomNumber = 5 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen5
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten5
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten5
Else
PictureBox1.Image = My.Resources.klaveren5
End If
End If
If randomNumber = 6 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen6
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten6
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten6
Else
PictureBox1.Image = My.Resources.klaveren6
End If
End If
If randomNumber = 7 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen7
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten7
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten7
Else
PictureBox1.Image = My.Resources.klaveren7
End If
End If
If randomNumber = 8 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen8
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten8
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten8
Else
PictureBox1.Image = My.Resources.klaveren8
End If
End If
If randomNumber = 9 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen9
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten9
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten9
Else
PictureBox1.Image = My.Resources.klaveren9
End If
End If
If randomNumber = 10 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen10
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten10
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten10
Else
PictureBox1.Image = My.Resources.klaveren10
End If
End If
If randomNumber = 11 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen11
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten11
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten11
Else
PictureBox1.Image = My.Resources.klaveren11
End If
End If
If randomNumber = 12 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen12
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten12
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten12
Else
PictureBox1.Image = My.Resources.klaveren12
End If
End If
If randomNumber = 13 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen13
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten13
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten13
Else
PictureBox1.Image = My.Resources.klaveren13
End If
End If
but when i have to repeat this code all the way to 13 then it will be to long. i saw on the internet that you can Grab a random file i thought maybe you can also do that with this
Thanks in Advance!
Upvotes: 0
Views: 134
Reputation:
Your code does Work fine tough. But its very long so what you can do is put your code inside a Private sub like this.
Private sub ChooseCard()
'Paste your code here
end sub
so you can just add this to your code to excute your script instead of pasting your code everywhere where you want it to be.
ChooseCard()
this will run your script inside the Private sub ChooseCard
Upvotes: 1
Reputation: 6385
You should make a storage lookup table for the cards. I will give you an example (the names in the example are german, but should be understandable). As a lookup I will use a Dictionary
object that itself holds a collection of dictionaries. That enables the lookup using two different keys. One for the card color and one for the card value. I use Enums to refer to the specific card values to make the code more readable afterwards. When using a variable of type Enum you can either assign an integer value or the name of the entry.
First, the complete code:
Public Class Form1
'Define the enums, example is for a 52 card game (Poker)
Private Enum CardColors
Herz = 1
Karo = 2
Pik = 3
Kreuz = 4
End Enum
Private Enum CardValues
Ass = 1
Zwei = 2
Drei = 3
Vier = 4
Fuenf = 5
Sechs = 6
Sieben = 7
Acht = 8
Neun = 9
Zehn = 10
Bube = 11
Dame = 12
Koenig = 13
End Enum
Private Sub MakeCards()
'Test function for creating a set of test cards, not escpecially relevant to this example
IO.Directory.CreateDirectory("C:\test\cards")
For i = 1 To 4
For y = 1 To 13
Dim bmp As New Bitmap(100, 100)
Using g As Graphics = Graphics.FromImage(bmp)
g.DrawString(CType(i, CardColors).ToString & " " & CType(y, CardValues).ToString, New Font("Arial", 8), Brushes.Black, New Point(10, 10))
End Using
bmp.Save("C:\test\cards\" & CType(i, CardColors).ToString & "_" & y.ToString & ".bmp")
bmp.Dispose()
Next
Next
End Sub
'This dictionary holds the cards
Private Cards As Dictionary(Of CardColors, Dictionary(Of CardValues, Bitmap))
Private Sub InitializeCards()
'The first dictionary contains 4 entries, assigned to the four colors
'The nested dictionaries hold the card values for each color
'We use the ResourceManager.GetObject() function to get the images from the resources
'You of course have to name them with a continuing index for this to work
'In this example: Herz_1, Herz_2, ..., Herz_13, Karo_1, Karo_2, ...
Cards = New Dictionary(Of CardColors, Dictionary(Of CardValues, Bitmap))
For CardColor = 1 To 4
Dim ThisCards As New Dictionary(Of CardValues, Bitmap)
Dim ThisColor As CardColors = CardColor 'This works because the enum is integer based
Dim ThisColorName As String = ThisColor.ToString
For Values = 1 To 13
ThisCards(Values) = My.Resources.ResourceManager.GetObject(ThisColorName & "_" & Values.ToString)
Next
Cards(CardColor) = ThisCards
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Load the cards into the lookup dictionary on startup
InitializeCards()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Show a specific picture in a picturebox
PictureBox1.Image = Cards(CardColors.Herz)(CardValues.Acht)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Select a random card from the range of available cards and show it
Dim rnd As New Random
PictureBox1.Image = Cards(rnd.Next(1, 4))(rnd.Next(1, 13))
End Sub
End Class
So you have to call InitializeCards only once. This loads the cards into the dictionaries. The Initialize function iterates all colors and values and selects the corresponding card from the ressources collection by name and adds it to the respective dictionary.
Afterwards you can very easily select the image of a specific card or a random card.
Upvotes: 0