jmc88 Newbie
Joined: 01 Mar 2006 Posts: 1
|
Posted: Mar 1st, 2006 11:49 PM Post subject: Craps Program |
|
|
I am trying to make a craps-like game. The interface has 5 labels to display the turn, status, point value, 2 dice values, and the dice total. In the game, you click the roll button. The program displays 2 random dice. If the total equals 2, 3, or 12 then you lose. If the total is 7 or 11 you win. If something else is rolled then you continue rolling two dice at a time until the toal is equal to the total (Point value) or 7. If the total equal the point value you win. If the total is 7 you lose. Anyways, I can get two dice to roll separately, and randomly, but on subsequent rolls I cannot get the program to call a different set of code. I know this code is not pretty, but if you know how to fix the game, or clean up the code then I would appreciate the help. Thanks. Also, I have only been programming for about 6 months so please explain any technical language.
Private Sub Roll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiRollButton.Click
Dim add1, number1, number2, total As Integer
Static counter As Integer
Dim randomGenerator As New Random
Dim winner, loser, keeprolling As Boolean
number1 = randomGenerator.Next(1, 6)
number2 = randomGenerator.Next(1, 6)
'code for turn counter
counter = counter + 1
Me.uiCounterLabel.Text = Convert.ToString(counter)
If counter > 1 Then
Call continuousrolls(total, number2, number2, counter)
If total = 7 Then
MessageBox.Show("You rolled a 7. You lose.", "Chance Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
'code for first roll
Call die1(number1)
'call sub for tie or second die
If number1 = number2 Then
Call tie(add1)
Else
Call die2(number2)
End If
'display all labels except status and code for total variable
total = number1 + number2
Me.uiTotalLabel.Text = Convert.ToString(total)
Me.uiPointValueLabel.Text = "Point value " & total
Me.uiTurnLabel.Text = "First roll"
Me.uiDie1Label.Text = Convert.ToString(number1)
Me.uiDie2Label.Text = Convert.ToString(number2)
'code for status label
If counter < 2 Then
Call status(total, counter)
End If
End Sub
Private Sub winScenario(ByVal total As Integer, ByVal counter As Integer)
MessageBox.Show("You rolled a " & total & ". You win.", "Chance Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call restart(total, counter)
End Sub
Private Sub loseScenario(ByVal total As Integer, ByVal counter As Integer)
MessageBox.Show("You rolled a " & total & ". You lose.", "Chance Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
counter = 0
Call restart(total, counter)
End Sub
Private Sub continuousrolls(ByVal total As Integer, ByVal number1 As Integer, ByVal number2 As Integer, ByVal counter As Integer)
Call clearselect(total)
End Sub
Private Sub status(ByVal total As Integer, ByVal counter As Integer)
If total = 7 Then
Call winScenario(total, counter)
ElseIf total = 11 Then
Call winScenario(total, counter)
ElseIf total = 2 Then
Call loseScenario(total, counter)
ElseIf total = 3 Then
Call loseScenario(total, counter)
ElseIf total = 12 Then
Call loseScenario(total, counter)
Else
Me.uiStatusLabel.Text = "Keep rolling."
End If
End Sub
Private Sub statusContinue(ByVal total As Integer, ByVal counter As Integer)
If total = 7 Then
MessageBox.Show("You rolled a 7. You lose", "Chance Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call loseScenario(total, counter)
Else
Me.uiStatusLabel.Text = "Keep rolling."
End If
End Sub
Private Sub secondrollstatus(ByVal total As Integer)
Me.uiStatusLabel.Text = "Keep rolling."
Me.uiTurnLabel.Text = "Next roll."
End Sub
Private Sub uiHelpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiHelpButton.Click
MessageBox.Show("Click the roll button. If the dice show a total of 7 or 11 then you win." & ControlChars.NewLine & _
"If the total is 2, 3, or 12 then you lose. Otherwise, the total becomes " & ControlChars.NewLine & _
"the point, or the goal of subsequent tosses. Keep rolling until the total " & ControlChars.NewLine & _
"equals the point or 7. If the roll equals 7 then you lose. If the total " & ControlChars.NewLine & _
"equals the point then you win.", "Chance Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub uiExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiExitButton.Click
Me.Close()
End Sub
Private Sub die1(ByVal number1 As Integer)
'code for first die to display
If number1 = 1 Then
Me.uiDie1PictureBox.Visible = True
ElseIf number1 = 2 Then
Me.uiDie2PictureBox.Visible = True
ElseIf number1 = 3 Then
Me.uiDie3PictureBox.Visible = True
ElseIf number1 = 4 Then
Me.uiDie4PictureBox.Visible = True
ElseIf number1 = 5 Then
Me.uiDie5PictureBox.Visible = True
Else
Me.uiDie6PictureBox.Visible = True
End If
End Sub
Private Sub die2(ByVal number2 As Integer)
'code for second die to display
If number2 = 1 Then
Me.uiDie1PictureBox.Visible = True
ElseIf number2 = 2 Then
Me.uiDie2PictureBox.Visible = True
ElseIf number2 = 3 Then
Me.uiDie3PictureBox.Visible = True
ElseIf number2 = 4 Then
Me.uiDie4PictureBox.Visible = True
ElseIf number2 = 5 Then
Me.uiDie5PictureBox.Visible = True
Else
Me.uiDie6PictureBox.Visible = True
End If
End Sub
Private Sub tie(ByVal add1 As Integer)
Dim tienum As Integer
Dim newrandom As New Random
tienum = newrandom.Next(1, 6)
If tienum = 1 Then
Me.uitie1PictureBox.Visible = True
ElseIf tienum = 2 Then
Me.uitie2PictureBox.Visible = True
ElseIf tienum = 3 Then
Me.uitie3PictureBox.Visible = True
ElseIf tienum = 4 Then
Me.uitie4PictureBox.Visible = True
ElseIf tienum = 5 Then
Me.uitie5PictureBox.Visible = True
Else
Me.uitie6PictureBox.Visible = True
End If
End Sub
Private Sub restart(ByVal total As Integer, ByVal counter As Integer)
Me.uiDie1PictureBox.Visible = False
Me.uiDie2PictureBox.Visible = False
Me.uiDie3PictureBox.Visible = False
Me.uiDie4PictureBox.Visible = False
Me.uiDie5PictureBox.Visible = False
Me.uiDie6PictureBox.Visible = False
Me.uitie1PictureBox.Visible = False
Me.uitie2PictureBox.Visible = False
Me.uitie3PictureBox.Visible = False
Me.uitie4PictureBox.Visible = False
Me.uitie5PictureBox.Visible = False
Me.uitie6PictureBox.Visible = False
Me.uiStatusLabel.Text = ""
Me.uiDie1Label.Text = ""
Me.uiDie2Label.Text = ""
Me.uiTotalLabel.Text = ""
Me.uiPointValueLabel.Text = ""
Me.uiTurnLabel.Text = ""
counter = 0
End Sub
Private Sub clearselect(ByVal total As Integer)
Me.uiDie1PictureBox.Visible = False
Me.uiDie2PictureBox.Visible = False
Me.uiDie3PictureBox.Visible = False
Me.uiDie4PictureBox.Visible = False
Me.uiDie5PictureBox.Visible = False
Me.uiDie6PictureBox.Visible = False
Me.uitie1PictureBox.Visible = False
Me.uitie2PictureBox.Visible = False
Me.uitie3PictureBox.Visible = False
Me.uitie4PictureBox.Visible = False
Me.uitie5PictureBox.Visible = False
Me.uitie6PictureBox.Visible = False
Me.uiStatusLabel.Text = "Keep rolling"
Me.uiDie1Label.Text = ""
Me.uiDie2Label.Text = ""
Me.uiTotalLabel.Text = ""
Me.uiPointValueLabel.Text = "Point value " & total
Me.uiTurnLabel.Text = "Next roll"
End Sub
End Class |
|