Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- ' BOWLING GAME
- ' a giant pile of messy code :)
- Dim Frames(14, 4) As Integer ' sloppy code example #1 - frame 10 is handled with up to 3 frames,
- ' and 13 and 14 are only there so that when it looks ahead two frames on 12 it doesn't crash.
- Dim CurFrame As Integer = 1
- Dim CurBall As Integer = 1
- Dim Strikes As Integer = 0 ' only used for the STRIKE! DOUBLE! TURKEY! effects
- Dim GameRunning As Boolean = True
- Dim generator As New Random
- Dim TenBLeft As Integer ' sloppy code example #2
- Dim TenCLeft As Integer ' stores frame 10's 2nd and 3rd slots for a sloppy fix for when you get a strike on the first ball
- Function GetTotalScore() ' get the total score from adding everything in RealScores()
- Dim i As Integer
- Dim score As Integer = 0
- For i = 1 To 10
- Dim s As Integer = RealScores(i)
- score += CInt(s)
- Next
- Return score
- End Function
- Sub ResetGame() ' initializes everything
- lblPerfect.Visible = False
- CurFrame = 1
- CurBall = 1
- Strikes = 0
- GameRunning = True
- Dim i, j As Integer
- For i = 1 To 13 ' reset the frames
- For j = 1 To 4
- Frames(i, j) = 0
- Next
- Next
- For i = 1 To 10 ' reset all the text boxes
- GetScorebox(i, 1).Clear()
- GetScorebox(i, 2).Clear()
- GetScorebox(i, 4).Clear()
- GetScorebox(i, 1).BackColor = Color.White
- GetScorebox(i, 2).BackColor = Color.White
- RealScores(i) = 0
- Next
- GetScorebox(11, 1).Clear()
- btnRoll.Enabled = True
- GetScorebox(1, 1).BackColor = Color.Gray()
- txtTotal.Text = 0
- txtF10B.Left = TenBLeft
- txtF10C.Left = TenCLeft
- lstLog.Items.Clear()
- End Sub
- Function SafeCInt(ByVal s As String) ' just like CInt() but won't crash if you pass it ""
- If s = "" Then
- Return 0
- End If
- Return CInt(s)
- End Function
- Dim RealScores(12) As Integer
- Sub BackwardsFix(ByVal Start As Integer) ' steps backwards and fixes scores
- Dim i As Integer
- For i = Start To 1 Step -1
- Dim Total As Integer = Frames(i, 1) + Frames(i, 2)
- Dim Score As Integer = Total
- If Total = 10 Then
- If Frames(i, 1) <> 10 Then ' spare
- Score = 10 + Frames(i + 1, 1)
- Else
- Score = 10 + Frames(i + 1, 1) + Frames(i + 1, 2)
- If Frames(i + 2, 1) = 10 Then ' special case for 3 strikes in a row
- Score += 10
- End If
- End If
- End If
- GetScorebox(i, 4).Text = Score
- RealScores(i) = Score
- Next
- End Sub
- Sub RunningTotal()
- ' apparently we have to have the score under a frame be the total AT that frame, not FOR the frame,
- ' and basically everything relies on the latter, so this is a sloppy fix that runs after the real stuff
- Dim Score2 As Integer = 0
- Dim i As Integer
- For i = 1 To 10
- If Frames(i, 1) = 0 And Frames(i, 2) = 0 Then
- Exit Sub
- End If
- Score2 += SafeCInt(GetScorebox(i, 4).Text)
- GetScorebox(i, 4).Text = Score2
- Next
- End Sub
- Sub RollBall(ByVal Roll As Integer)
- If (Frames(CurFrame, 1) + Frames(CurFrame, 2) + Roll) > 10 Then
- lstLog.Items.Clear()
- lstLog.Items.Add("Not enough pins to roll " & Roll)
- Dim pinsleft As Integer = 10 - Frames(CurFrame, 1) - Frames(CurFrame, 2)
- lstLog.Items.Add("(There are " & pinsleft & " left)")
- txtGuess.Text = pinsleft
- Exit Sub
- End If
- GetScorebox(CurFrame, CurBall).BackColor = Color.White
- Frames(CurFrame, CurBall) = Roll
- lstLog.Items.Clear()
- If CurFrame < 11 Then
- lstLog.Items.Add("== Frame #" & CurFrame & " - " & CurBall & " ==")
- Else
- lstLog.Items.Add("== Frame #10 - extended - ==")
- End If
- lstLog.Items.Add("Knocked over " & Roll & " pins.")
- Dim Left As Integer = 10 - (Frames(CurFrame, 1) + Frames(CurFrame, 2) + Frames(CurFrame, 3))
- If Roll = 0 Then
- MakeBigNotice(My.Resources.gutter)
- lstLog.Items.Add("GUTTER BALL")
- End If
- lstLog.Items.Add("Pins left: " & Left)
- GetScorebox(CurFrame, CurBall).Text = Frames(CurFrame, CurBall)
- GetScorebox(CurFrame, 4).Text = Frames(CurFrame, 1) + Frames(CurFrame, 2) + Frames(CurFrame, 3)
- If CurFrame <> 11 Then
- RealScores(CurFrame) = GetScorebox(CurFrame, 4).Text
- End If
- If CurFrame <> 1 Then
- BackwardsFix(CurFrame - 1)
- End If
- ' check for strikes and spares ( only for the effects; BackwardsFix() does the actual handling )
- If Left = 0 Then
- If CurBall = 1 Then
- GetScorebox(CurFrame, CurBall).Text = "X"
- Strikes += 1
- MakeBigNotice(My.Resources.SPARE_)
- Dim NumSpelledOut() As String = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen"}
- Select Case Strikes ' http://en.wikipedia.org/wiki/Strike_(bowling)#Consecutive_strikes
- Case 1
- lstLog.Items.Add("Strike!!!")
- MakeBigNotice(My.Resources.STRIKE_2)
- Case 2
- lstLog.Items.Add("Double!!!")
- MakeBigNotice(My.Resources.DOUBLE_)
- Case 3
- lstLog.Items.Add("Turkey!!!")
- MakeBigNotice(My.Resources.TURKEY_)
- Case 5
- lstLog.Items.Add("Yahtzee!!!")
- MakeBigNotice(My.Resources.STRIKE_2_again)
- Case 6
- lstLog.Items.Add("Six Pack!!!")
- MakeBigNotice(My.Resources.STRIKE_2_again)
- Case 9
- lstLog.Items.Add("Golden Turkeys!!!")
- MakeBigNotice(My.Resources.STRIKE_2_again)
- Case 12
- lstLog.Items.Add("Thanksgiving Turkey!")
- MakeBigNotice(My.Resources.STRIKE_2_again)
- Case Else
- lstLog.Items.Add(NumSpelledOut(Strikes) + "-bagger!!!")
- MakeBigNotice(My.Resources.STRIKE_2_again)
- End Select
- Else
- GetScorebox(CurFrame, CurBall).Text = "/"
- lstLog.Items.Add("Spare!!!")
- MakeBigNotice(My.Resources.SPARE_)
- End If
- CurBall = 0
- CurFrame += 1
- Else
- Strikes = 0
- End If
- Dim total As Integer = GetTotalScore()
- txtTotal.Text = total
- ' move onto the next ball
- CurBall += 1
- If CurBall = 3 Then
- CurBall = 1
- CurFrame += 1
- End If
- If CurFrame = 11 Then
- If (Frames(CurFrame - 1, 1) + Frames(CurFrame - 1, 2)) <> 10 Or CurBall = 2 Then
- GameRunning = False
- End If
- End If
- If CurFrame = 12 And CurBall = 2 Or CurFrame = 13 Then
- GameRunning = False
- End If
- txtTotal.Text = total
- If CurFrame = 11 And Frames(CurFrame - 1, 1) = 10 Then
- txtF10B.Left = TenCLeft
- txtF10C.Left = TenBLeft
- End If
- If GameRunning Then
- GetScorebox(CurFrame, CurBall).BackColor = Color.Gray
- Else
- lstLog.Items.Add("Game over; Score: " & total)
- If total = 300 Then
- lstLog.Items.Add("PERFECT GAME!")
- lblPerfect.Visible = True
- End If
- btnRoll.Enabled = False
- End If
- Me.Text = "BOWLING GAME!! - Score: " & total
- RunningTotal()
- End Sub
- Private Sub btnRoll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRoll.Click
- If chkRandom.Checked Then
- Dim rolled As Integer
- rolled = generator.Next(0, 2 * (10 - Frames(CurFrame, 1) - Frames(CurFrame, 2))) / 2
- RollBall(rolled)
- Else
- If txtGuess.Text <> "" Then
- Try
- RollBall(CInt(txtGuess.Text))
- Catch ex As Exception
- MsgBox("Please enter a number in the text box." & Chr(13) & Chr(10) & "What you entered does not seem to be a number.", MsgBoxStyle.Information, "Bowling Game!!!!")
- End Try
- End If
- txtGuess.Focus()
- End If
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- TenBLeft = txtF10B.Left
- TenCLeft = txtF10C.Left
- ResetGame()
- End Sub
- Dim Wavy As Double = 0
- Sub MakeBigNotice(ByVal pic As Image)
- picNotice.Image = pic
- picNotice.Left = -picNotice.Width
- Wavy = 0
- BigNoticeTimer.Start()
- End Sub
- Private Sub BigNoticeTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles BigNoticeTimer.Tick
- picNotice.Left += 10
- picNotice.Top = (Me.Height / 2) - (picNotice.Height / 2) + Math.Sin(Wavy) * 50 + 100 ' standard wavy sinewave thing
- picNotice.Visible = True
- Wavy += 0.1
- If picNotice.Left > Me.Width Then
- BigNoticeTimer.Stop()
- picNotice.Visible = False
- End If
- End Sub
- Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
- ResetGame()
- End Sub
- Function GetScorebox(ByVal i As Integer, ByVal ball As Integer) As TextBox
- ' too lazy to try and do a control array :(
- Select Case i
- Case 1
- Select Case ball
- Case 1
- Return txtF1A
- Case 2
- Return txtF1B
- Case 4
- Return txtF1T
- End Select
- Case 2
- Select Case ball
- Case 1
- Return txtF2A
- Case 2
- Return txtF2B
- Case 4
- Return txtF2T
- End Select
- Case 3
- Select Case ball
- Case 1
- Return txtF3A
- Case 2
- Return txtF3B
- Case 4
- Return txtF3T
- End Select
- Case 4
- Select Case ball
- Case 1
- Return txtF4A
- Case 2
- Return txtF4B
- Case 4
- Return txtF4T
- End Select
- Case 5
- Select Case ball
- Case 1
- Return txtF5A
- Case 2
- Return txtF5B
- Case 4
- Return txtF5T
- End Select
- Case 6
- Select Case ball
- Case 1
- Return txtF6A
- Case 2
- Return txtF6B
- Case 4
- Return txtF6T
- End Select
- Case 7
- Select Case ball
- Case 1
- Return txtF7A
- Case 2
- Return txtF7B
- Case 4
- Return txtF7T
- End Select
- Case 8
- Select Case ball
- Case 1
- Return txtF8A
- Case 2
- Return txtF8B
- Case 4
- Return txtF8T
- End Select
- Case 9
- Select Case ball
- Case 1
- Return txtF9A
- Case 2
- Return txtF9B
- Case 4
- Return txtF9T
- End Select
- Case 10
- Select Case ball
- Case 1
- Return txtF10A
- Case 2
- Return txtF10B
- Case 4
- Return txtF10T
- End Select
- Case 11
- Select Case ball
- Case 1
- Return txtF10C
- Case 4
- Return txtF10T
- End Select
- Case 12
- Select Case ball
- Case 1
- Return txtF10B
- Case 4
- Return txtF10T
- End Select
- End Select
- Return txtF1A
- End Function
- Private Sub chkRandom_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkRandom.CheckedChanged
- If chkRandom.Checked Then
- txtGuess.ReadOnly = True
- lstInstructions.Items.Clear()
- lstInstructions.Items.Add("Just click " + Chr(34) + "Roll Ball" + Chr(34))
- lstInstructions.Items.Add("and the program will")
- lstInstructions.Items.Add("pick each score for you.")
- Else
- txtGuess.ReadOnly = False
- lstInstructions.Items.Clear()
- lstInstructions.Items.Add("Enter a number from")
- lstInstructions.Items.Add("0 to 10 in the textbox")
- lstInstructions.Items.Add("to the right then click")
- lstInstructions.Items.Add(Chr(34) + "Roll Ball" + Chr(34) + " to enter")
- lstInstructions.Items.Add("each score.")
- End If
- End Sub
- End Class
Add Comment
Please, Sign In to add comment