VB.Net War Game - vb.net

I have been working on a "WAR" card game in class. I seem to have most of it setup correctly, however, I am having some issues dealing 2 new cards to the image boxes when clicking the deal button.
The exact things I need the draw button to accomplish is
When the draw button is pressed, the first two cards from the shuffled deck
should show up, one on the left and one on the right side. The middle card will be the
“Left win”, “Right Win”, or “Tie” image and is correctly indicate who won (with the higher
card). Furthermore, the winner’s score should increase by 1 point.
I will include a screenshot of my form to give you an idea of what I am working with and where I should go with the deal button. My guess is when I click the deal button a second time it is resetting the values back to 0 and 26 respectively.
enter image description here
Public Class Form1
'WAR
Dim cardarray(52) As Image
Dim valuearray(52) As Integer
Dim cardval1, cardval2 As Integer
Dim card1, card2 As Integer
Dim P1, P2 As Integer
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pct1.Image = My.Resources.back
pct2.Image = My.Resources.back
pct3.Image = My.Resources.back
'Needed to generate a random number/card
Randomize()
'create 52 card element array
CardArray(0) = My.Resources.twoc
CardArray(1) = My.Resources.twod
CardArray(2) = My.Resources.twoh
CardArray(3) = My.Resources.twos
CardArray(4) = My.Resources.threec
CardArray(5) = My.Resources.threed
CardArray(6) = My.Resources.threeh
CardArray(7) = My.Resources.threes
CardArray(8) = My.Resources.fourc
CardArray(9) = My.Resources.fourd
CardArray(10) = My.Resources.fourh
CardArray(11) = My.Resources.fours
CardArray(12) = My.Resources.fivec
CardArray(13) = My.Resources.fived
CardArray(14) = My.Resources.fiveh
CardArray(15) = My.Resources.fives
CardArray(16) = My.Resources.sixc
CardArray(17) = My.Resources.sixd
CardArray(18) = My.Resources.sixh
CardArray(19) = My.Resources.sixs
CardArray(20) = My.Resources.sevenc
CardArray(21) = My.Resources.sevend
CardArray(22) = My.Resources.sevenh
CardArray(23) = My.Resources.sevens
CardArray(24) = My.Resources.eightc
CardArray(25) = My.Resources.eightd
CardArray(26) = My.Resources.eighth
CardArray(27) = My.Resources.eights
CardArray(28) = My.Resources.ninec
CardArray(29) = My.Resources.nined
CardArray(30) = My.Resources.nineh
CardArray(31) = My.Resources.nines
CardArray(32) = My.Resources.tenc
CardArray(33) = My.Resources.tend
CardArray(34) = My.Resources.tenh
CardArray(35) = My.Resources.tens
CardArray(36) = My.Resources.jackc
CardArray(37) = My.Resources.jackd
CardArray(38) = My.Resources.jackh
CardArray(39) = My.Resources.jacks
CardArray(40) = My.Resources.queenc
CardArray(41) = My.Resources.queend
CardArray(42) = My.Resources.queenh
CardArray(43) = My.Resources.queens
CardArray(44) = My.Resources.kingc
CardArray(45) = My.Resources.kingd
CardArray(46) = My.Resources.kingh
CardArray(47) = My.Resources.kings
CardArray(48) = My.Resources.acec
CardArray(49) = My.Resources.aced
CardArray(50) = My.Resources.aceh
CardArray(51) = My.Resources.aces
'52 integer value array
valueArray(0) = 1
valueArray(1) = 1
valueArray(2) = 1
valueArray(3) = 1
valueArray(4) = 2
valueArray(5) = 2
valueArray(6) = 2
valueArray(7) = 2
valueArray(8) = 3
valueArray(9) = 3
valueArray(10) = 3
valueArray(11) = 3
valueArray(12) = 4
valueArray(13) = 4
valueArray(14) = 4
valueArray(15) = 4
valueArray(16) = 5
valueArray(17) = 5
valueArray(18) = 5
valueArray(19) = 5
valueArray(20) = 6
valueArray(21) = 6
valueArray(22) = 6
valueArray(23) = 6
valueArray(24) = 7
valueArray(25) = 7
valueArray(26) = 7
valueArray(27) = 7
valueArray(28) = 8
valueArray(29) = 8
valueArray(30) = 8
valueArray(31) = 8
valueArray(32) = 9
valueArray(33) = 9
valueArray(34) = 9
valueArray(35) = 9
valueArray(36) = 10
valueArray(37) = 10
valueArray(38) = 10
valueArray(39) = 10
valueArray(40) = 11
valueArray(41) = 11
valueArray(42) = 11
valueArray(43) = 11
valueArray(44) = 12
valueArray(45) = 12
valueArray(46) = 12
valueArray(47) = 12
valueArray(48) = 13
valueArray(49) = 13
valueArray(50) = 13
valueArray(51) = 13
End Sub
Public Sub shuffel()
Dim switch As Integer
Dim tempcard As Image
Dim number_of_cards, tempval As Integer
number_of_cards = 52
'Go through the deck one card at a time:
For i = 0 To number_of_cards - 1
'Get a random card number from the deck
switch = Int(Rnd() * number_of_cards)
'Switch the current card’s value with the random card’s value
tempval = valuearray(i)
valuearray(i) = valuearray(switch)
valuearray(switch) = tempval
'Switch the current card’s image with the random card’s image
tempcard = cardarray(i)
cardarray(i) = cardarray(switch)
cardarray(switch) = tempcard
Next
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
'shuffle deck and set scores to 0
Call shuffel()
txtP1.Text = 0
txtP2.Text = 0
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles txtP2.TextChanged
End Sub
Private Sub btnShuffle_Click(sender As Object, e As EventArgs) Handles btnShuffle.Click
'shuffle deck and change card backs
Call shuffel()
pct1.Image = My.Resources.back
pct2.Image = My.Resources.back
pct3.Image = My.Resources.back
End Sub
Private Sub btnDraw_Click(sender As Object, e As EventArgs) Handles btnDraw.Click
'set interger values for starting points
cardval1 = 0
cardval2 = 26
'load those starting points into image boxes
pct1.Image = cardarray(cardval1)
pct3.Image = cardarray(cardval2)
'increase card values by 1
cardval1 = (cardval1 + 1)
cardval2 = (cardval2 + 1)
End Sub
End Class

I would think that you should move the part in btnDraw_Click where you are setting the starting points to btnShuffle_CLick and this would get what you are after?

Related

Add or Update data in Checkbox + Radiobutton Visual basic

Okay, this is my problem; I have code like this:
Private Sub process_Click(sender As Object, e As EventArgs) Handles hitung.Click
If radiobuttonplus.Checked = True Then
Me.h1.Text = Val(txt1.Text) + Val(txt5.Text)
Me.h2.Text = Val(txt2.Text) + Val(txt6.Text)
Me.h3.Text = Val(txt3.Text) + Val(txt7.Text)
Me.h4.Text = Val(txt4.Text) + Val(txt8.Text)
result = (h1.Text * h4.Text) - (h2.Text * h3.Text)
resultdeterminan.Text = result
ElseIf checkboxadjoint.Checked = True Then
Me.a1.Text = 1 * (Val(Me.h4.Text))
Me.a2.Text = -1 * (Val(Me.h3.Text))
Me.a3.Text = -1 * (Val(Me.h2.Text))
Me.a4.Text = 1 * (Val(Me.h1.Text))
result = (a1.Text * a4.Text) - (a2.Text * a3.Text)
resultdeterminan.Text = result
resultadjoint.Text = result
End if
End Sub
As you can see, radiobuttonplus has data underneath it and checkbuttonadjoint also has data below it.
I use checkbuttonadjoint to update data from radiobuttonplus.
Initially, there were no problems regarding radiobuttonplus, because I tried to successfully display all the data in radiobuttonplus; however, the problem arose when I clicked radiobutton + ticked checkbuttonadjoint. The data in checkbuttonadjoint is not displayed and does not update the data from radiobuttonplus.
Because you have ElseIf, when the first If is executed the second block will never be executed. If you want both to execute then change ElseIf to simply If. You'll need to add an End If before that to close off the first block.
So when you're all done, it'd look like this:
Private Sub process_Click(sender As Object, e As EventArgs) Handles hitung.Click
If radiobuttonplus.Checked = True Then
Me.h1.Text = Val(txt1.Text) + Val(txt5.Text)
Me.h2.Text = Val(txt2.Text) + Val(txt6.Text)
Me.h3.Text = Val(txt3.Text) + Val(txt7.Text)
Me.h4.Text = Val(txt4.Text) + Val(txt8.Text)
result = (h1.Text * h4.Text) - (h2.Text * h3.Text)
resultdeterminan.Text = result
End If
If checkboxadjoint.Checked = True Then
Me.a1.Text = 1 * (Val(Me.h4.Text))
Me.a2.Text = -1 * (Val(Me.h3.Text))
Me.a3.Text = -1 * (Val(Me.h2.Text))
Me.a4.Text = 1 * (Val(Me.h1.Text))
result = (a1.Text * a4.Text) - (a2.Text * a3.Text)
resultdeterminan.Text = result
resultadjoint.Text = result
End If
End Sub

Exception Thrown - Receive data from Arduino to Visual Basic

I'm trying to send serial data from Arduino and read it on Visual Basic. When I execute the code sometimes works and sometimes doesn't: throwing exception, System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string. Can you help me?
I'm new to Visual Basic, thanks.
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim TWSL, TWAL, THL, AoAL, WAL, PeL, RoilL, RyL, RydL As Integer
Dim TWS, TWA, TH, AoA, WA, Pe, Roil, Ry, Ryd, TWSResult, TWAResult, THResult, AoAResult, WAResult, PeResult, RoilResult, RyResult, RydResult As String
Dim StrSerialIn, StrSerialInRam As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToParent()
SerialPort1.PortName = "COM4"
SerialPort1.BaudRate = 9600
SerialPort1.Open()
Timer1.Start()
SerialPort1.Write(TrackBarAWA.Value & Chr(10))
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
StrSerialIn = SerialPort1.ReadExisting
Dim TB As New TextBox
TB.Multiline = True
TB.Text = StrSerialIn
If TB.Lines.Count > 0 Then
If TB.Lines(0) = "Failed to read" Then
Timer1.Stop()
SerialPort1.Close()
Return
End If
StrSerialInRam = TB.Lines(0).Substring(0, 2)
If StrSerialInRam = "A" Then
TWS = TB.Lines(0)
TWSL = TWS.Length
Else
TWS = TWS
End If
StrSerialInRam = TB.Lines(1).Substring(0, 3)
If StrSerialInRam = "B" Then
TWA = TB.Lines(1)
TWAL = TWA.Length
Else
TWA = TWA
End If
StrSerialInRam = TB.Lines(2).Substring(0, 3)
If StrSerialInRam = "C" Then
TH = TB.Lines(2)
THL = TH.Length
Else
TH = TH
End If
StrSerialInRam = TB.Lines(3).Substring(0, 2)
If StrSerialInRam = "D" Then
AoA = TB.Lines(3)
AoAL = AoA.Length
Else
AoA = AoA
End If
StrSerialInRam = TB.Lines(4).Substring(0, 1)
If StrSerialInRam = "E" Then
WA = TB.Lines(4)
WAL = WA.Length
Else
WA = WA
End If
StrSerialInRam = TB.Lines(5).Substring(0, 3)
If StrSerialInRam = "F" Then
Pe = TB.Lines(5)
PeL = Pe.Length
Else
Pe = Pe
End If
StrSerialInRam = TB.Lines(6).Substring(0, 3)
If StrSerialInRam = "G" Then
Roil = TB.Lines(6)
RoilL = Roil.Length
Else
Roil = Roil
End If
StrSerialInRam = TB.Lines(7).Substring(0, 3)
If StrSerialInRam = "H" Then
Ry = TB.Lines(7)
RyL = Ry.Length
Else
Ry = Ry
End If
StrSerialInRam = TB.Lines(8).Substring(0, 3)
If StrSerialInRam = "I" Then
Ryd = TB.Lines(8)
RydL = Ryd.Length
Else
Ryd = Ryd
End If
TWSResult = Mid(TWS, 2, TWSL)
TWAResult = Mid(TWA, 2, TWAL)
THResult = Mid(TH, 2, THL)
AoAResult = Mid(AoA, 2, AoAL)
WAResult = Mid(WA, 2, WAL)
PeResult = Mid(Pe, 2, PeL)
RoilResult = Mid(Roil, 2, RoilL)
RyResult = Mid(Ry, 2, RyL)
RydResult = Mid(Ryd, 2, RydL)
TWSvalue.Text = TWSResult
TWAvalue.Text = TWAResult
THvalue.Text = THResult
AoAvalue.Text = AoAResult
WAvalue.Text = WAResult
PeValue.Text = PeResult
RoilValue.Text = RoilResult
RyValue.Text = RyResult
RydValue.Text = RydResult
From what I gather, you are trying to get characters from specific places in specific lines on a text box. Based on the error message you included in your question, I assume the error occurs on a line of code containing the "String. Substring" method. If the string where you are getting the substring from is too short to cover the range you have specified in the substring method, you will get this error. For instance, if you are getting a substring that's 3 characters long from line 2 starting at character 0 and it has less than 3 characters, you will get this error.
See the documentation on the String.Substring method here

VB "Label is not a type and can not be used as an expression"

First, I have searched using SO and have not found answer.
Program cannot compile due to this build error in which I attempt to use dynamic label.
Here is the offending part of code (how fix this error message)?
Label(num).text = userchoice(num) Then
Error message:
Label is not a type and cannot be used as an expresssion
All of the code:
Public Class MainForm
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim foodtype As Decimal
Dim userchoice
For num As Integer = 1 To 7 Step 1
If num <= 4 Then
foodtype = "Main course"
ElseIf num = 5 Then
foodtype = "Bread"
ElseIf num = 6 Then
foodtype = "Drink"
ElseIf num = 7 Then
foodtype = "Dessert"
End If
userchoice(num).to = InputBox(foodtype & num)
Dim lc = userchoice(num).tolower
Dim calories
If lc = "stuffing" Then
calories = 165
ElseIf lc = "turkey" Then
calories = 180
ElseIf lc = "mashed potatoes" Then
calories = 220
ElseIf lc = "carrots" Then
calories = 25
ElseIf lc = "french bread" Then
calories = 224
ElseIf lc = "everclear" OrElse lc = "water" Then
calories = 1
ElseIf lc = "pudding" Then
calories = "170"
End If
Label(num).text = userchoice(num) Then
Next
End Sub
End Class

Why is my Select Case not working

Okay my question goes as follows; I think i coded everything right execpt for the part where i do my select case, I want the first 3 Flavours to only cost 55 cents but when I do my code it always makes the scoops 65 cents no matter what icecream type i select and i dont know how to make it change, i thought i had it right but it isnt working
Public Class frmJoeyIceCreamParlour
Const MIN_SCOOPS = 1
Const MAX_SCOOPS = 9
Const BASIC_FLAVOUR = 0.55
Const PREMIUM_FLAVOUR = 0.65
Const TOPPING = 0.6
Const DEEZ_NUTS = 0.5
Const WHIPPED_CREAM = 0.65
Public scoopEntry As Single
Public scoopType As Double
Public runningTotal As Double
Private Sub frmJoeyIceCreamParlour_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstFlavours.Items.Add("Vanilla")
lstFlavours.Items.Add("Chocolate")
lstFlavours.Items.Add("Strawberry")
lstFlavours.Items.Add("Mango")
lstFlavours.Items.Add("Bananna")
lstFlavours.Items.Add("Grape")
lstFlavours.Items.Add("Mint Chocolate Chip")
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If txtScoops.Text = Nothing Then
MessageBox.Show("Please enter a value in the scoops category.")
txtScoops.Focus()
ElseIf Not IsNumeric(txtScoops.Text) Then
MessageBox.Show("Entry must be numeric! Please try again.")
txtScoops.Focus()
ElseIf txtScoops.Text < MIN_SCOOPS Or txtScoops.Text > MAX_SCOOPS Then
MessageBox.Show("Please enter a number between 1 and 9 scoops.")
txtScoops.Focus()
ElseIf lstFlavours.SelectedItem = Nothing Then
MessageBox.Show("Please select a flavour.")
ElseIf rdoNoTopping.Checked = False And rdoOneTopping.Checked = False And rdoTwoTopping.Checked = False And rdoThreeTopping.Checked = False Then
MessageBox.Show("Please select the amount of toppings you would like.")
Else
Dim number As Integer = 7
Select Case number
Case 1 To 3
scoopType = BASIC_FLAVOUR
Case 4 To 7
scoopType = PREMIUM_FLAVOUR
runningTotal = scoopType * Double.Parse(txtScoops.Text)
End Select
If rdoOneTopping.Checked = True Then
runningTotal = runningTotal + TOPPING
ElseIf rdoTwoTopping.Checked = True Then
runningTotal = runningTotal + (TOPPING * 2)
ElseIf rdoThreeTopping.Checked = True Then
runningTotal = runningTotal + (TOPPING * 3)
End If
If chkWhippedCream.Checked = True Then
runningTotal = runningTotal + WHIPPED_CREAM
End If
If chkNuts.Checked = True Then
runningTotal = runningTotal + DEEZ_NUTS
End If
lblOutputTotal.Text = (FormatCurrency(runningTotal))
End If
End Sub
End Class
You have it hard coded to use 7 via the line just above your Select case number statement: Dim number As Integer = 7. You should instead be looking up the selected index by doing something like Dim number As Integer = lstFlavours.SelectedIndex
Dim number As Integer = lstFlavours.SelectedIndex
Select Case number
Case 1 To 3
scoopType = BASIC_FLAVOUR
Case 4 To 7
scoopType = PREMIUM_FLAVOUR
End Select
runningTotal = scoopType * Double.Parse(txtScoops.Text)

Need help adding number to dim's in my loop sequence? vb.net

Here's basically what I have:
Public checkprogresstime_p1 As String = ""
Public checkprogresstime_p2 As String = ""
'P1 Progress bar updater
checkprogresstime_p1 = (time_total.Text - time_p1_hour.Value)
If checkprogresstime_p1 >= 60 Then
checkprogresstime_p1 = 60
time_p1_progress.ForeColor = Color.LimeGreen
ElseIf checkprogresstime_p1 <= 0 Then
checkprogresstime_p1 = 1
End If
If time_p1_progress.Value < 60 Then
time_p1_progress.ForeColor = Color.Red
End If
time_p1_progress.Value = checkprogresstime_p1
Here's basically what I need:
Dim cnt As Integer = 1
Do
'P1 Progress bar updater
checkprogresstime_p(cnt) = (time_total.Text - time_p(cnt)_hour.Value)
If checkprogresstime_p(cnt) >= 60 Then
checkprogresstime_p(cnt) = 60
time_p(cnt)_progress.ForeColor = Color.LimeGreen
ElseIf checkprogresstime_p(cnt) <= 0 Then
checkprogresstime_p(cnt) = 1
End If
If time_p(cnt)_progress.Value < 60 Then
time_p(cnt)_progress.ForeColor = Color.Red
End If
time_p(cnt)_progress.Value = checkprogresstime_p(cnt)
Loop While cnt <= 25
I have no idea how to do it... I need it to loop and add +1, 25 times. I basically have it written out 25 times at the moment...
This is the For/Loop with your current request. The cnt variable will increment itself in this type of Loop.
For cnt As Integer = 1 To 25
'P1 Progress bar updater
checkprogresstime_p(cnt) = (time_total.Text - time_p(cnt)_hour.Value)
If checkprogresstime_p(cnt) >= 60 Then
checkprogresstime_p(cnt) = 60
time_p(cnt)_progress.ForeColor = Color.LimeGreen
ElseIf checkprogresstime_p(cnt) <= 0 Then
checkprogresstime_p(cnt) = 1
End If
If time_p(cnt)_progress.Value < 60 Then
time_p(cnt)_progress.ForeColor = Color.Red
End If
time_p(cnt)_progress.Value = checkprogresstime_p(cnt)
Next
I believe what you're wanting to do has more to do with having 25 progress bars on your form where each one is named time_p#_progress where # is the number of the progress bar. That being said, there are two ways to acheive updating your progress bars without having to copy and paste your code 25 times...
1. Use Me.Controls to get a reference to the progress bar
For j = 1 To 25
Dim pbar As ProgressBar = Me.Controls("time_p" & j & "_progress")
Dim ph As NumericUpDown = Me.Controls("time_p" & j & "_hour")
Dim checkprogresstime As Long = (time_total.Text - ph.Value)
If checkprogresstime >= 60 Then
checkprogresstime = 60
pbar.ForeColor = Color.LimeGreen
ElseIf checkprogresstime <= 0 Then
checkprogresstime = 1
End If
If time_p1_progress.Value < 60 Then
pbar.Value = checkprogresstime
End If
pbar.Value = checkprogresstime
Application.DoEvents()
Next
Note: You didn't tell us what type of control time_p1_hour was. I assumed it was a NumericUpDown down control. So, if it's not, you need to replace it the type of control that time_p1_hour is.
2. Dynamically create your controls as a control array
Initizliaze your progress bars in the Form1_Load method (MyBase.Load)
Private pbars(24) As ProgressBar
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i = LBound(pbars) To UBound(pbars)
pbars(i) = New ProgressBar()
pbars(i).Parent = Me
pbars(i).Top = i * pbars(i).Height
pbars(i).Left = 0
pbars(i).Visible = True
Next
End Sub
Put your code inside of a loop like so
For cnt = 0 To 24
checkprogresstime_p(cnt) = (time_total.Text - time_hour(cnt).Value)
If checkprogresstime_p(cnt) >= 60 Then
checkprogresstime_p(cnt) = 60
time_p_progress(cnt).ForeColor = Color.LimeGreen
ElseIf checkprogresstime_p(cnt) <= 0 Then
checkprogresstime_p(cnt) = 1
End If
If time_p_progress(cnt).Value < 60 Then
time_p_progress(cnt).ForeColor = Color.Red
End If
time_p_progress(cnt).Value = checkprogresstime_p(cnt)
Next