Wow, there are a lot of little things wrong here. I REWOTE YOUR CODE AND BROKE IT UP.
So don't get upset. There is a method called Hungarian Format which is
used by a lot of programmers but not all. But if you get into the habbit when you are just
starting it is easier. Basicly you precede your varible name with the type of object it is in
a three letter code.
Button ------------------ btnMyButton or cmdMyButton (Command Button VB6 and Prior)
TextBox ---------------- txtMyTextBox
Label ------------------- lblMyLabel
ComboBox ------------ cmbMyCombo
ListBox ----------------- lstMyListBox
DataGrid --------------- dgdMyDataGrid
PictureBox ------------- picMyPictureBox
and so on
well here is your hacked code, I tried to document most of the changes
CODE
Public Class Form1
'Declare Variables that need to maintain a changing value at the beginning of the form,
' this gives the variable scope thoughout the form
Dim Number, Count As Integer ' use Integer not Byte
Dim Matches, NumberLeft As Integer
Dim Player1, Player2 As String
Dim thisPlayer As String
Public Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'initialise variables
Count = 0
Matches = 23
' Create UI ' I added the second Instruction label so the number of matches left is
' always visible
InstructionLabel2.Text = "There are " & Matches & " matches."
Player1 = InputBox("Enter your name Player 1.", "Name of Player 1.", "Type your name here")
Player2 = InputBox("Enter your name Player 2.", "Name of Player 2.", "Type your name here")
Player1Button.Text = Player1
Player2Button.Text = Player2
Player2Button.Enabled = False
RulesLabel.Text = ("1. Each game starts with 23 matches.")
RulesLabel.Text = RulesLabel.Text + vbCrLf + ("2. Each player will take turn and must remove")
RulesLabel.Text = RulesLabel.Text + vbCrLf + (" 1, 2, or 3 matches; otherwise 1 match is removed.")
RulesLabel.Text = RulesLabel.Text + vbCrLf + ("3. The player that removes the last match or")
RulesLabel.Text = RulesLabel.Text + vbCrLf + (" matches looses the game.")
InstructionLabel.Text = "Player " & Player1 & " please enter the number of matches to be removed."
'You need to work out your graphics better I left this part alone for you to do
MatchesLabel.Text = "ooooo ooooo ooooo ooooo ooo" + vbCrLf + "||||| ||||| ||||| ||||| |||"
End Sub
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Close()
End Sub
Private Sub Player1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Player1Button.Click
' When two processes are exactly the same, create a sub for just that portion
' this allows you to call the same routine from multiple actions
thisPlayer = Player2 ' for displaing name for next player in RemoveMatch()
RemoveMatch() ' I created a routine for removing matches
Player1Button.Enabled = False
Player2Button.Enabled = True
NumberTextBox.Text = ""
NumberTextBox.Focus()
End Sub
Private Sub Player2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Player2Button.Click
' When two processes are exactly the same, create a sub for just that portion
' this allows you to call the same routine from multiple actions
thisPlayer = Player1 ' for displaing name for next player in RemoveMatch()
RemoveMatch()
Player1Button.Enabled = True
Player2Button.Enabled = False
NumberTextBox.Text = ""
NumberTextBox.Focus()
End Sub
Private Sub RemoveMatch()
' you were declaring your variables here which reset them to 0, I moved them to
' the Class Form1 General Declarations
'try to make your comments about what is happening, not just a copy of the code
' ie instead of
'If Count = 0 Then Matches = 23
'Matches = Matches - Number
' use something more like
' if game is over reset variables
If Matches = 0 Then Matches = 23
'Remove a number of matches
Number = Val(NumberTextBox.Text)
If (Number > 3) Or (Number < 1) Then 'A Comparitor is required for each side of the Boolean Operation
Number = 1
End If
Matches = Matches - Number
InstructionLabel2.Text = "There are " & Matches & " matches."
InstructionLabel.Text = "Player " & thisPlayer & " please enter the number of matches to be removed."
' clear matches graphic
MatchesLabel.Text = ""
'Create MatchHeads
For NumberLeft = 1 To Matches
MatchesLabel.Text = MatchesLabel.Text + "o"
Next
End Sub
End Class
I tried to comment all my changes but I may have missed one or two.
This post has been edited by KeyWiz: 16 Nov, 2006 - 09:22 PM