VB Classic Poker. Detecting a winning hand using modulo - vb.net

I need to create a poker game in vb as work for my class using mod13 for each suite to evaluate the winning hand... but I am at lost here. I just can't get how to use modulo.
I really need an hint how to do it. (this is what I have coded so far....)
Public Class Form1
Dim Cards(4) As PictureBox
Dim Hand(4) As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Creat_poker()
End Sub
Private Sub Creat_Poker_interface()
Dim i As Integer
For i = 0 To pic.GetUpperBound(0)
Cards(i) = New PictureBox
With Cards(i)
.Visible = True
.Width = 130
.Height = 200
.Left = 20 + (i * 160)
.Top = 20
.BorderStyle = BorderStyle.Fixed3D
.SizeMode = PictureBoxSizeMode.StretchImage
End With
Me.Controls.Add(pic(i))
AddHandler Cards(i).Click, AddressOf CardsSelection
Next
End Sub
Private Sub cmdNewGame_Click(sender As Object, e As EventArgs) Handles cmdNewGame.Click
Dim i As Integer
Dim hasard As New Random
For i = 0 To main.GetUpperBound(0)
Hand(i) = hasard.Next(52)
Next
For i = 0 To main.GetUpperBound(0)
Cards(i).Image = imaCards.Images(Hand(i))
Next
End Sub
End Class

Related

How can I make a timer slower and slower until it stops?

Can someone tell me what's wrong or what I need to add to make it run the way that I want? I need to make it slower, then slower again, then make it stop.
Heres what I have tried:
Dim A As Integer
If Timer.Interval = 1 Then
A = Timer.Interval + 1000
If Timer.Interval = 1000 Then
Timer.Enabled = False
End If
End If
I want to stop the timer after a specific time but I have button 1 and 2 its random and stop, if I push the button random it will select any on given list then if I push stop the selection of random is going stop delay like spinning wheel, the missing on my program is the delay selection after a seconds its like selecting random then it will slowly selecting then it will stop.
Here's the btnRandom.Click event handler:
Private Sub btnRandom_Click(sender As Object, e As EventArgs) Handles btnRandom.Click
If lstList1.Items.Count <= 1 Then
MessageBox.Show("Name must more than 1 to run the randomizer.", "Error Loading", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
txtName.Enabled = False
btnRandom.Enabled = False
btnAdd.Enabled = False
btnRemove.Enabled = False
btnStop.Enabled = True
btnAdd.ForeColor = Color.FromArgb(64, 64, 64)
btnRemove.ForeColor = Color.FromArgb(64, 64, 64)
btnRandom.ForeColor = Color.FromArgb(64, 64, 64)
If btnRandom.Enabled = False Then
Timer2.Start()
lstList1.ClearSelected()
End If
End If
End Sub
And the Timer.Tick handler:
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If btnRandom.Enabled = True Then
Timer2.Enabled = False
End If
If btnRandom.Enabled = False Then
txtNameLabel.Text = lstList1.Items(Rnd() * (lstList1.Items.Count - 1))
lstList1.SelectedItem = txtNameLabel.Text
txtNameLabel.Text = lstList1.SelectedItem
End If
End Sub
If you want something to decelerate then you need to multiply the speed by a constant less than one, which in this case is equivalent to increasing the display time interval by a constant greater than one.
So, you can start something at one speed, and then add a deceleration by changing the amount the interval is changed by.
Something like this:
Public Class Form1
Dim sw As Stopwatch
Dim tim As Timer
Dim intervalMultiplier As Double = 1.0
Dim itemsToDisplay As List(Of String)
Dim itemIndex As Integer = 0
Private Sub tim_Tick(sender As Object, e As EventArgs)
' When intervalMultiplier > 1.0 the time between ticks will increase.
tim.Interval = CInt(tim.Interval * intervalMultiplier)
Label1.Text = itemsToDisplay(itemIndex)
If tim.Interval >= 1000 Then
tim.Stop()
End If
itemIndex = (itemIndex + 1) Mod itemsToDisplay.Count
End Sub
Private Sub StartTimer()
If tim Is Nothing Then
tim = New Timer()
AddHandler tim.Tick, AddressOf tim_Tick
End If
If sw Is Nothing Then
sw = Stopwatch.StartNew()
Else
sw.Restart()
End If
intervalMultiplier = 1.0
tim.Interval = 100
tim.Start()
End Sub
Private Sub bnStop_Click(sender As Object, e As EventArgs) Handles bnStop.Click
intervalMultiplier = 1.25
End Sub
Private Sub btnRandom_Click(sender As Object, e As EventArgs) Handles btnRandom.Click
' Your code to disable buttons etc. goes here.
StartTimer()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set up some data for this example.
itemsToDisplay = New List(Of String) From {"Capacitor", "Diode", "Inductor", "Resistor", "Transistor"}
End Sub
End Class

Finding the name of labels based on text inside its name

I’m not really sure this is possible but it would be helpful: I have quite a large grid of labels and would like to change the visibility of them based on words inside their names, for example finding a list of labels which all contain “Twentytwo” in their name and setting their visibility to false
Here is a solution based on Control.GetNextControl:
Const textToSearch = "Twentytwo" 'Text to search
Dim c As Control = Me.GetNextControl(Me, True)
Do Until c Is Nothing
'Check if c is label and its name contains "textToSearch"
If TypeOf c Is Label AndAlso c.Name.Contains(textToSearch) Then
c.Visible = False 'Hide this label
End If
c = Me.GetNextControl(c, True)
Loop
If you want to search "Twentytwo" in the label text and not in its name, replace c.Name.Containswith c.Text.Contains.
Let's say that your labels are all in one container, perhaps a Panel.
Further, let's assume you that you want only the specified labels to have .Visible = False, so the visibility is simply whether or not the label's name contains the given string.
You can select for controls of a particular type, so that there's no need to check the type of the control before using it, like this:
Sub SetLabelVisibility(container As Control, namePart As String)
container.SuspendLayout()
For Each lbl In container.Controls.OfType(Of Label)
lbl.Visible = lbl.Name.Contains(namePart)
Next
container.ResumeLayout()
End Sub
And you would call it with
SetLabelVisibility(Panel1, "Twentytwo")
Suspending the layout of the container while the updates to its child controls are being made is so that the display of it is done in one go.
Demonstration using an appropriately-sized Panel on a Form:
Public Class Form1
Dim tim As New Timer With {.Interval = 1000}
Sub CreateLabels(target As Panel)
For j = 0 To 6
For i = 0 To 4
Dim lbl = New Label With {
.Name = $"Label{j}_{i}",
.BackColor = Color.BlanchedAlmond,
.Text = i & j,
.Location = New Point(j * 40, i * 20),
.Size = New Size(38, 18),
.TextAlign = ContentAlignment.MiddleCenter
}
target.Controls.Add(lbl)
Next
Next
End Sub
Sub SetLabelVisibility(container As Control, namePart As String)
container.SuspendLayout()
For Each lbl In container.Controls.OfType(Of Label)
lbl.Visible = lbl.Name.Contains(namePart)
Next
container.ResumeLayout()
End Sub
Sub ChangeVisibleLabels(sender As Object, e As EventArgs)
Static n As Integer = 0
SetLabelVisibility(Panel1, n.ToString())
n = (n + 1) Mod 7
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateLabels(Panel1)
AddHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Start()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If tim IsNot Nothing Then
RemoveHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Dispose()
End If
End Sub
End Class
Public Class Form1
Dim tim As New Timer With {.Interval = 1000}
Sub CreateLabels(target As Panel)
For j = 0 To 6
For i = 0 To 4
Dim lbl = New Label With {
.Name = $"Label{j}_{i}",
.BackColor = Color.BlanchedAlmond,
.Text = i & j,
.Location = New Point(j * 40, i * 20),
.Size = New Size(38, 18),
.TextAlign = ContentAlignment.MiddleCenter
}
target.Controls.Add(lbl)
Next
Next
End Sub
Sub SetLabelVisibility(container As Control, namePart As String)
container.SuspendLayout()
For Each lbl In container.Controls.OfType(Of Label)
lbl.Visible = lbl.Name.Contains(namePart)
Next
container.ResumeLayout()
End Sub
Sub ChangeVisibleLabels(sender As Object, e As EventArgs)
Static n As Integer = 0
SetLabelVisibility(Panel1, n.ToString())
n = (n + 1) Mod 7
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateLabels(Panel1)
AddHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Start()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If tim IsNot Nothing Then
RemoveHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Dispose()
End If
End Sub
End Class
Public Class Form1
Dim tim As New Timer With {.Interval = 1000}
Sub CreateLabels(target As Panel)
For j = 0 To 6
For i = 0 To 4
Dim lbl = New Label With {
.Name = $"Label{j}_{i}",
.BackColor = Color.BlanchedAlmond,
.Text = i & j,
.Location = New Point(j * 40, i * 20),
.Size = New Size(38, 18),
.TextAlign = ContentAlignment.MiddleCenter
}
target.Controls.Add(lbl)
Next
Next
End Sub
Sub SetLabelVisibility(container As Control, namePart As String)
container.SuspendLayout()
For Each lbl In container.Controls.OfType(Of Label)
lbl.Visible = lbl.Name.Contains(namePart)
Next
container.ResumeLayout()
End Sub
Sub ChangeVisibleLabels(sender As Object, e As EventArgs)
Static n As Integer = 0
SetLabelVisibility(Panel1, n.ToString())
n = (n + 1) Mod 7
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateLabels(Panel1)
AddHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Start()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If tim IsNot Nothing Then
RemoveHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Dispose()
End If
End Sub
End Class

Creating handle in a Windows Form with a declared object as an array

Im trying to make a Connect 4 game just to practice some windows forms which im new to. What my code does is creates a grid of 7 x 6 regularly spaces blank PictureBox's. But since im creating them in the script and not using the form1 design windows i dont know how i would add Handles to them, especially since the PictureBox's are in an array. Any ideas?
Public Class Form1
Dim Grid(6, 5) As PictureBox
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Visible = False
Me.FormBorderStyle = FormBorderStyle.FixedSingle
For i As Integer = 0 To 6
For j As Integer = 0 To 5
Grid(i, j) = New PictureBox
Grid(i, j).BackColor = Color.LightGray
Grid(i, j).Size = New Size(90, 90)
Grid(i, j).Location = New Point((i * 100) + 10, (j * 100) + 10)
Grid(i, j).Visible = True
Controls.Add(Grid(i, j))
Next
Next
End Sub
Private Sub Grid_MouseHover(sender As Object, e As EventArgs) Handles Grid(x, y).MouseHover 'Doesnt work
'Run depending on which picturebox in array
End Sub
End Class
I can get an error which is "Handles clause requires a WithEvents variable defined in the containing type or one of its base types."
One possible way would be to set the .Tag property using the coordinates -
add something like into your For..Next loop
Grid(i, j).Tag = i.ToString & j.ToString
and use
AddHandler Grid(i, j).MouseHover, AddressOf Grid_MouseHover
and add this after the one above.
Then, change the first line of your MouseHover Sub to
Private Sub Grid_MouseHover(sender As Object, e As EventArgs)
with no handler on the end.
Finally, change the type of the sender to a PictureBox
Private Sub Grid_MouseHover(sender As Object, e As EventArgs)
Dim Pbox As PictureBox = CType(sender, PictureBox)
Dim i As Integer = Integer.Parse(Pbox.Tag.ToString(0))
Dim j As Integer = Integer.Parse(Pbox.Tag.ToString(1))
End Sub
To access the Picturebox and its properties, just use PBox and if you need the coordinates, use i and j

PictureBox initial image

Is there a way that you can set all of the picboxes to a initial image when they are loaded? Such as: when you start a round, all the images are set to a image of a card facing down, then when the round starts it shows an image.
EDIT: How do I clear what is in the PicCard.Image?
Public Class Form1
Private Cards As New List(Of PictureBox)
Private randomnumber As Integer
Private HowManyCards As Integer
Private Sub SetupCards(numberofcards As Integer)
ClearGame()
For i As Integer = 0 To numberofcards
Dim PicCard As PictureBox = New PictureBox()
PicCard.Width = 100
PicCard.Height = 200
PicCard.Top = 50
PicCard.Left = 50 + PicCard.Width * i
Me.Controls.Add(PicCard)
PicCard.Image = imglistBackOfCard.Images(0)
Next i
End Sub
Private Sub ClearGame()
If Cards.Count > 0 Then
For i As Integer = 0 To Cards.Count - 1
Me.Controls.Remove(Cards(i))
Next
End If
' Clear the cards if they were already setup from a previous game.
Cards.Clear()
End Sub
Private Sub RandomCard()
Randomize()
randomnumber = Int(Rnd() * imglist1.Images.Count - 1) + 1
End Sub
Public Sub ShowCards(numberofcards As Integer)
For i As Integer = 0 To numberofcards
Dim PicCard As PictureBox = New PictureBox()
RandomCard()
PicCard.Width = 100
PicCard.Height = 200
PicCard.Top = 50
PicCard.Left = 50 + PicCard.Width * i
Me.Controls.Add(PicCard)
PicCard.Image = imglist1.Images(randomnumber)
PicCard.Tag = randomnumber
AddHandler PicCard.Click, AddressOf Me.cardflip_click
Cards.Add(PicCard)
Next i
End Sub
Private Sub StartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
ShowCards(HowManyCards - 1)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
HowManyCards = InputBox("How Many Cards?", "Please Enter")
SetupCards(Int(howmanycards - 1))
End Sub
End Class

Collision detection in vb.net

I am trying to recreate Copter in visual basic, so far I have the player, roof and helicopter but I can't seem to get the game to end when the player touches the floor or roof. Sorry for posting so much, this is my first time on here and I didn't know what to paste. Any help is appreciated :D
Public Class Form1
Dim pb_field As PictureBox
Private Sub create_field()
pb_field = New PictureBox
With pb_field
.Top = 20
.Left = 20
.Width = 500
.Height = 300
.BackColor = Color.Black
End With
Me.Controls.Add(pb_field)
pb_field.BringToFront()
End Sub
Dim pb_player As PictureBox
Private Sub create_player()
pb_player = New PictureBox
With pb_player
.Width = 20
.Height = 20
.BackColor = Color.Red
.Top = pb_field.Top + pb_field.Bottom / 2
.Left = pb_field.Left + 20
End With
Me.Controls.Add(pb_player)
pb_player.BringToFront()
End Sub
#Region "Roof Stuff"
Dim roof(10000) As PictureBox
Dim num_of_roof As Integer = -1
Dim r As New Random
Private Sub create_roof()
num_of_roof += 1
roof(num_of_roof) = New PictureBox
With roof(num_of_roof)
.Top = pb_field.Top
.Left = pb_field.Right
.Height = r.Next(20, 40)
.Width = 20
.BackColor = Color.RoyalBlue
End With
Me.Controls.Add(roof(num_of_roof))
roof(num_of_roof).BringToFront()
End Sub
#End Region
#Region "floor Stuff"
Dim floor(10000) As PictureBox
Dim num_of_floor As Integer = -1
Private Sub create_floor()
num_of_floor += 1
floor(num_of_floor) = New PictureBox
With floor(num_of_floor)
.Left = pb_field.Right
.Height = r.Next(20, 40)
.Width = 20
.Top = pb_field.Bottom - floor(num_of_floor).Height
.BackColor = Color.YellowGreen
End With
Me.Controls.Add(floor(num_of_floor))
floor(num_of_floor).BringToFront()
End Sub
#End Region
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
Me.Text = e.KeyChar
If e.KeyChar = "w" Then
pb_player.Top -= 10
End If
**Dim collision As Boolean
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For
Else : collision = False
End If
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If**
Next
End Sub
Private Sub form1_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
create_field()
create_roof()
create_player()
tm_background.Start()
tm_gravity.Start()
End Sub
Private Sub tm_background_Tick(sender As Object, e As EventArgs) Handles tm_background.Tick
For i = 0 To num_of_roof
roof(i).Left -= 20
If roof(i).Left < pb_field.Left Then
Me.Controls.Remove(roof(i))
End If
Next
create_roof()
For i = 0 To num_of_floor
floor(i).Left -= 20
If floor(i).Left < pb_field.Left Then
Me.Controls.Remove(floor(i))
End If
Next
create_floor()
End Sub
Private Sub tm_gravity_Tick(sender As Object, e As EventArgs) Handles tm_gravity.Tick
pb_player.Top += 5
End Sub
This is the code I was attempting to use after looking online at possible solutions
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) HandlesMe.KeyPress
Me.Text = e.KeyChar
If e.KeyChar = "w" Then
pb_player.Top -= 10
End If
Dim collision As Boolean
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For
Else : collision = False
End If
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If
Next
End Sub
Your problem is where you exit the for loop before displaying the message:
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For ' Note that exiting skips your check after the End If below
Else : collision = False
End If
' Whenever this is true you have already exited your 'for' loop
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If
Next
Instead you need something like this where you evaluate the condition after the loop:
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For
Else : collision = False
End If
Next
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If
First you need to set tag's in the floor and ceiling regions
With floor(num_of_floor)
.Tag = "boundaries"
Then you can refer to each picturebox in your controls
For Each box As PictureBox In Me.Controls
If box.Tag <> "boundaries" Then Continue For
If pb_player.Bounds.IntersectsWith(box.Bounds) Then
collision = True
Exit For
Else : collision = False
End If
Next
However, you are still going to have a problem that when it hits the floor it will not pass as a collision, because all this code is going on in the key press,
If a user lets the coptor fall, it will only lose the next time they click on the keyboard