Bingo Game with class (Code redundancy and randomNumberGenerator.next)

Homework assignment to make a bingo game using a class.

Page 1 of 1

1 Replies - 2584 Views - Last Post: 25 November 2008 - 10:04 AM Rate Topic: -----

#1 jklaroe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-February 05

Bingo Game with class (Code redundancy and randomNumberGenerator.next)

Post icon  Posted 23 November 2008 - 03:50 PM

hello there,

My homework assignment was to make a bingo game using a class. I've done just that i'm basically trying to work out a few kinks in duplicate numbers being called and code redundancy.

Here's what i have so far:

Main Form.vb
Option Strict On
Option Explicit On
Option Infer Off

Public Class MainForm

	Private game As BingoClass
	Private bNums(5), iNums(5), nNums(5), gNums(5), oNums(5) As Integer
	Private clicks As Integer = 0

	Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click

		Me.Close()

	End Sub


	Private Sub newGameButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles newGameButton.Click

		' reset list box
		numbersListBox.Items.Clear()
		getNumberButton.Enabled = True

		' reset clicks
		clicks = 0

		' variables
		game = New BingoClass

		' :3 (Generate some arrays for comparison)
		For i As Integer = 0 To 5

			bNums(i) = game.b(i)
			iNums(i) = game.i(i)
			nNums(i) = game.n(i)
			gNums(i) = game.g(i)
			oNums(i) = game.o(i)

		Next

		' O_o
		b1.Text = game.b(0).ToString
		b2.Text = game.b(1).ToString
		b3.Text = game.b(2).ToString
		b4.Text = game.b(3).ToString
		b5.Text = game.b(4).ToString

		i1.Text = game.i(0).ToString
		i2.Text = game.i(1).ToString
		i3.Text = game.i(2).ToString
		i4.Text = game.i(3).ToString
		i5.Text = game.i(4).ToString

		n1.Text = game.n(0).ToString
		n2.Text = game.n(1).ToString
		'n3.Text = game.n(2).ToString
		n4.Text = game.n(3).ToString
		n5.Text = game.n(4).ToString

		g1.Text = game.g(0).ToString
		g2.Text = game.g(1).ToString
		g3.Text = game.g(2).ToString
		g4.Text = game.g(3).ToString
		g5.Text = game.g(4).ToString

		o1.Text = game.o(0).ToString
		o2.Text = game.o(1).ToString
		o3.Text = game.o(2).ToString
		o4.Text = game.o(3).ToString
		o5.Text = game.o(4).ToString

		' ='(
		b1.BackColor = Me.BackColor
		b2.BackColor = Me.BackColor
		b3.BackColor = Me.BackColor
		b4.BackColor = Me.BackColor
		b5.BackColor = Me.BackColor

		i1.BackColor = Me.BackColor
		i2.BackColor = Me.BackColor
		i3.BackColor = Me.BackColor
		i4.BackColor = Me.BackColor
		i5.BackColor = Me.BackColor

		n1.BackColor = Me.BackColor
		n2.BackColor = Me.BackColor
		'n3.BackColor = Me.BackColor
		n4.BackColor = Me.BackColor
		n5.BackColor = Me.BackColor

		g1.BackColor = Me.BackColor
		g2.BackColor = Me.BackColor
		g3.BackColor = Me.BackColor
		g4.BackColor = Me.BackColor
		g5.BackColor = Me.BackColor

		o1.BackColor = Me.BackColor
		o2.BackColor = Me.BackColor
		o3.BackColor = Me.BackColor
		o4.BackColor = Me.BackColor
		o5.BackColor = Me.BackColor

	End Sub

	Private Sub getNumberButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles getNumberButton.Click

		If clicks = 75 Then

			MessageBox.Show("You have reached the end of this bingo game", "The End", MessageBoxButtons.OK, MessageBoxIcon.Information)

		Else

			Dim number As String
			Dim numberI As Integer
			Dim counter As Integer = 0
			number = game.getNumber()

			numbersListBox.Items.Add(number)

			' some logic to highlight the squares...
			' I was going to implement something to the effect where i had a function handle all the squares and if you clicked it and it was a number in the list
			' box then it would highlight... like putting a marker or chip on the bingo card... but well this is what i give you.
			' I also couldn't quite figured out (if there is) a way to perhaps store labels in an array so that I could use the array to switch the color instead of
			' using 5 FOR EACH IN statements.
			number = number.Substring(1)
			Integer.TryParse(number, numberI)

			For Each num As Integer In bNums

				If num = numberI Then

					Select Case counter

						Case 0
							b1.BackColor = Color.Azure
						Case 1
							b2.BackColor = Color.Azure
						Case 2
							b3.BackColor = Color.Azure
						Case 3
							b4.BackColor = Color.Azure
						Case 4
							b5.BackColor = Color.Azure

					End Select

				End If

				counter = counter + 1

			Next num

			counter = 0

			For Each num As Integer In iNums

				If num = numberI Then

					Select Case counter

						Case 0
							i1.BackColor = Color.Azure
						Case 1
							i2.BackColor = Color.Azure
						Case 2
							i3.BackColor = Color.Azure
						Case 3
							i4.BackColor = Color.Azure
						Case 4
							i5.BackColor = Color.Azure

					End Select

				End If

				counter = counter + 1

			Next num

			counter = 0

			For Each num As Integer In nNums

				If num = numberI Then

					Select Case counter

						Case 0
							n1.BackColor = Color.Azure
						Case 1
							n2.BackColor = Color.Azure
							'Case 2
							'n3.BackColor = Color.Azure
						Case 3
							n4.BackColor = Color.Azure
						Case 4
							n5.BackColor = Color.Azure

					End Select

				End If

				counter = counter + 1

			Next num

			counter = 0

			For Each num As Integer In gNums

				If num = numberI Then

					Select Case counter

						Case 0
							g1.BackColor = Color.Azure
						Case 1
							g2.BackColor = Color.Azure
						Case 2
							g3.BackColor = Color.Azure
						Case 3
							g4.BackColor = Color.Azure
						Case 4
							g5.BackColor = Color.Azure

					End Select

				End If

				counter = counter + 1

			Next num

			counter = 0

			For Each num As Integer In oNums

				If num = numberI Then

					Select Case counter

						Case 0
							o1.BackColor = Color.Azure
						Case 1
							o2.BackColor = Color.Azure
						Case 2
							o3.BackColor = Color.Azure
						Case 3
							o4.BackColor = Color.Azure
						Case 4
							o5.BackColor = Color.Azure

					End Select

				End If

				counter = counter + 1

			Next num

			clicks = clicks + 1

		End If

		' you win?! :3 <terribad block of code... er wall o' text, but it gets the job done, with all 12 ways of winning BINGO
		If (b1.BackColor = Color.Azure AndAlso b2.BackColor = Color.Azure AndAlso b3.BackColor = Color.Azure AndAlso b4.BackColor = Color.Azure AndAlso b5.BackColor = Color.Azure) OrElse _
		   (i1.BackColor = Color.Azure AndAlso i2.BackColor = Color.Azure AndAlso i3.BackColor = Color.Azure AndAlso i4.BackColor = Color.Azure AndAlso i5.BackColor = Color.Azure) OrElse _
		   (n1.BackColor = Color.Azure AndAlso n2.BackColor = Color.Azure AndAlso n3.BackColor = Color.Red AndAlso n4.BackColor = Color.Azure AndAlso n5.BackColor = Color.Azure) OrElse _
		   (g1.BackColor = Color.Azure AndAlso g2.BackColor = Color.Azure AndAlso g3.BackColor = Color.Azure AndAlso g4.BackColor = Color.Azure AndAlso g5.BackColor = Color.Azure) OrElse _
		   (o1.BackColor = Color.Azure AndAlso o2.BackColor = Color.Azure AndAlso o3.BackColor = Color.Azure AndAlso o4.BackColor = Color.Azure AndAlso o5.BackColor = Color.Azure) OrElse _
		   (b1.BackColor = Color.Azure AndAlso i1.BackColor = Color.Azure AndAlso n1.BackColor = Color.Azure AndAlso g1.BackColor = Color.Azure AndAlso o1.BackColor = Color.Azure) OrElse _
		   (b2.BackColor = Color.Azure AndAlso i2.BackColor = Color.Azure AndAlso n2.BackColor = Color.Azure AndAlso g2.BackColor = Color.Azure AndAlso o2.BackColor = Color.Azure) OrElse _
		   (b3.BackColor = Color.Azure AndAlso i3.BackColor = Color.Azure AndAlso n3.BackColor = Color.Red AndAlso g3.BackColor = Color.Azure AndAlso o3.BackColor = Color.Azure) OrElse _
		   (b4.BackColor = Color.Azure AndAlso i4.BackColor = Color.Azure AndAlso n4.BackColor = Color.Azure AndAlso g4.BackColor = Color.Azure AndAlso o4.BackColor = Color.Azure) OrElse _
		   (b5.BackColor = Color.Azure AndAlso i5.BackColor = Color.Azure AndAlso n5.BackColor = Color.Azure AndAlso g5.BackColor = Color.Azure AndAlso o5.BackColor = Color.Azure) OrElse _
		   (b1.BackColor = Color.Azure AndAlso i2.BackColor = Color.Azure AndAlso n3.BackColor = Color.Red AndAlso g4.BackColor = Color.Azure AndAlso o5.BackColor = Color.Azure) OrElse _
		   (b5.BackColor = Color.Azure AndAlso i4.BackColor = Color.Azure AndAlso n3.BackColor = Color.Azure AndAlso g2.BackColor = Color.Azure AndAlso o1.BackColor = Color.Azure) Then

			MessageBox.Show("You've won BINGO", "Winner!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			getNumberButton.Enabled = False

		End If

	End Sub

End Class



and BingoClass.vb
Option Strict On
Option Explicit On
Option Infer Off

Public Class BingoClass

	'variables...
	Private _b(5), _i(5), _n(5), _g(5), _o(5), _numbers(74), _num, _subscript, _searchSubscript, _randomNum, _index, _pickedIndexes(74) As Integer
	Private _id As Integer
	Private _numS As String
	Private _isFound As Boolean
	Public randomNumberGenerator As New Random

	Public ReadOnly Property b(ByVal index As Integer) As Integer
		Get
			Return _b(index)
		End Get
	End Property

	Public ReadOnly Property i(ByVal index As Integer) As Integer
		Get
			Return _i(index)
		End Get
	End Property

	Public ReadOnly Property n(ByVal index As Integer) As Integer
		Get
			Return _n(index)
		End Get
	End Property

	Public ReadOnly Property g(ByVal index As Integer) As Integer
		Get
			Return _g(index)
		End Get
	End Property

	Public ReadOnly Property o(ByVal index As Integer) As Integer
		Get
			Return _o(index)
		End Get
	End Property

	Public Sub New()

		' awesomely redundant code here, where I can't figure out how to combine 5 or so do until loops into one,
		' my thought was that if I did one big do until and random 1, 76 -- i could then use a case statement to determine
		' which array to store it in...

		' do until code stolen from chapter 9 (?) exercise that i did awhile ago (I remember writing code to check for duplicate random numbers in an array
		' this was the lottery exercise

		_id = 0

		_subscript = 1

		' declare the first variable in each column...
		_b(0) = randomNumberGenerator.Next(1, 16)
		_i(0) = randomNumberGenerator.Next(16, 31)
		_n(0) = randomNumberGenerator.Next(31, 46)
		_g(0) = randomNumberGenerator.Next(46, 61)
		_o(0) = randomNumberGenerator.Next(61, 76)
		_numbers(0) = randomNumberGenerator.Next(1, 76)

		' B is numbers  1 - 15
		' I is numbers 16 - 30
		' N is numbers 31 - 45
		' G is numbers 46 - 60
		' O is numbers 61 - 75

		Do Until _subscript = _b.Length

			_randomNum = randomNumberGenerator.Next(1, 16)
			_searchSubscript = 0
			_isFound = False

			Do Until _searchSubscript = _subscript OrElse _isFound = True

				If _b(_searchSubscript) = _randomNum Then

					_isFound = True

				Else

					_searchSubscript = _searchSubscript + 1

				End If

			Loop

			If _isFound = False Then

				_b(_subscript) = _randomNum
				_subscript = _subscript + 1

			End If

		Loop

		_subscript = 1

		Do Until _subscript = _i.Length

			_randomNum = randomNumberGenerator.Next(16, 31)
			_searchSubscript = 0
			_isFound = False

			Do Until _searchSubscript = _subscript OrElse _isFound = True

				If _i(_searchSubscript) = _randomNum Then

					_isFound = True

				Else

					_searchSubscript = _searchSubscript + 1

				End If

			Loop

			If _isFound = False Then

				_i(_subscript) = _randomNum
				_subscript = _subscript + 1

			End If

		Loop

		_subscript = 1

		Do Until _subscript = _n.Length

			_randomNum = randomNumberGenerator.Next(31, 46)
			_searchSubscript = 0
			_isFound = False

			Do Until _searchSubscript = _subscript OrElse _isFound = True

				If _n(_searchSubscript) = _randomNum Then

					_isFound = True

				Else

					_searchSubscript = _searchSubscript + 1

				End If

			Loop

			If _isFound = False Then

				_n(_subscript) = _randomNum
				_subscript = _subscript + 1

			End If

		Loop

		_subscript = 1

		Do Until _subscript = _g.Length

			_randomNum = randomNumberGenerator.Next(46, 61)
			_searchSubscript = 0
			_isFound = False

			Do Until _searchSubscript = _subscript OrElse _isFound = True

				If _g(_searchSubscript) = _randomNum Then

					_isFound = True

				Else

					_searchSubscript = _searchSubscript + 1

				End If

			Loop

			If _isFound = False Then

				_g(_subscript) = _randomNum
				_subscript = _subscript + 1

			End If

		Loop

		_subscript = 1


		Do Until _subscript = _o.Length

			_randomNum = randomNumberGenerator.Next(61, 76)
			_searchSubscript = 0
			_isFound = False

			Do Until _searchSubscript = _subscript OrElse _isFound = True

				If _o(_searchSubscript) = _randomNum Then

					_isFound = True

				Else

					_searchSubscript = _searchSubscript + 1

				End If

			Loop

			If _isFound = False Then

				_o(_subscript) = _randomNum
				_subscript = _subscript + 1

			End If

		Loop

		_subscript = 1

		'Do Until _subscript = _numbers.Length

		'	_randomNum = randomNumberGenerator.Next(1, 76)
		'	_searchSubscript = 0
		'	_isFound = False

		'	Do Until _searchSubscript = _subscript OrElse _isFound = True

		'		If _numbers(_searchSubscript) = _randomNum Then

		'			_isFound = True

		'		Else

		'			_searchSubscript = _searchSubscript + 1

		'		End If

		'	Loop

		'	If _isFound = False Then

		'		_numbers(_subscript) = _randomNum
		'		_subscript = _subscript + 1

		'	End If

		'Loop

		For x As Integer = 1 To 75

			_numbers(x - 1) = x

		Next

	End Sub

	Public Function getNumber() As String

		' this is my sad attempt at making a random guess at the index then storing it in an array so it doesn't get used again, however it doesn't seem to do that
		' 
		_index = randomNumberGenerator.Next(0, 75)
		_pickedIndexes(_id) = _index
		_num = _numbers(_index)

		If _id >= 1 Then

			_index = randomNumberGenerator.Next(0, 75)
			_searchSubscript = 0
			_isFound = False
			_subscript = 1

			Do Until _searchSubscript = _subscript OrElse _isFound = True

				If _pickedIndexes(_searchSubscript) = _index Then

					_isFound = True

				Else

					_searchSubscript = _searchSubscript + 1

				End If

			Loop

			If _isFound = False Then

				_num = _numbers(_index)
				_pickedIndexes(_id) = _index
				_subscript = _subscript + 1

			End If
		End If

		'_num = randomNumberGenerator.Next(1, 76)
		'_num = _numbers(_num)

		Select Case _num

			Case 1 To 15
				_numS = "B" & _num
			Case 16 To 30
				_numS = "I" & _num
			Case 31 To 45
				_numS = "N" & _num
			Case 46 To 60
				_numS = "G" & _num
			Case 61 To 75
				_numS = "O" & _num
			Case Else
				_numS = _num.ToString

		End Select

		_id = _id + 1

		Return _numS

	End Function

End Class



My duplicate number call problem stems form my terrible code in the class...
	Public Function getNumber() As String

		' this is my sad attempt at making a random guess at the index then storing it in an array so it doesn't get used again, however it doesn't seem to do that
		' 
		_index = randomNumberGenerator.Next(0, 75)
		_pickedIndexes(_id) = _index
		_num = _numbers(_index)

		If _id >= 1 Then

			_index = randomNumberGenerator.Next(0, 75)
			_searchSubscript = 0
			_isFound = False
			_subscript = 1

			Do Until _searchSubscript = _subscript OrElse _isFound = True

				If _pickedIndexes(_searchSubscript) = _index Then

					_isFound = True

				Else

					_searchSubscript = _searchSubscript + 1

				End If

			Loop

			If _isFound = False Then

				_num = _numbers(_index)
				_pickedIndexes(_id) = _index
				_subscript = _subscript + 1

			End If
		End If

		'_num = randomNumberGenerator.Next(1, 76)
		'_num = _numbers(_num)

		Select Case _num

			Case 1 To 15
				_numS = "B" & _num
			Case 16 To 30
				_numS = "I" & _num
			Case 31 To 45
				_numS = "N" & _num
			Case 46 To 60
				_numS = "G" & _num
			Case 61 To 75
				_numS = "O" & _num
			Case Else
				_numS = _num.ToString

		End Select

		_id = _id + 1

		Return _numS

	End Function



And the last one is reducing the amount of redundant code. I use the same thing for 5 x 5 labels to reset the color and to check if there are 5 in a row. I also use the same do until loops 5 times for each column. If there were some way of calling the labels through an array I could use a loop in that regard. As far as the 5 do until loops in the class I could use one and maybe use a select case to determine which column the number should go into, similar to the select case i have in the getNumber function.

Any help on this would be greatly appreciated, I could probably turn it in now for full credit, but know how to take care of this big wall of code in the future would be helpful.

Regards,
Josh L.

Is This A Good Question/Topic? 0
  • +

Replies To: Bingo Game with class (Code redundancy and randomNumberGenerator.next)

#2 jklaroe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-February 05

Re: Bingo Game with class (Code redundancy and randomNumberGenerator.next)

Posted 25 November 2008 - 10:04 AM

I think I've gotten it condensed down as far as the do..until loops. My first idea of using select case didnt' work out because it would only loop through 5 times, not filling the array. This way I generate all the numbers b,i,n,g and o then compare and set them in the array each iteration through the loop.

Public Class MainForm

	Private Sub generateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles generateButton.Click

		' Initialize some variables
		Dim b(5), i(5), n(5), g(5), o(5), searchSubscript, subscript As Integer
		Dim randB, randI, randN, randG, randO As Integer
		Dim isFound As Boolean
		Dim randomNumberGenerator As New Random

		' Set the first element in each array to a random number
		b(0) = randomNumberGenerator.Next(1, 16)
		i(0) = randomNumberGenerator.Next(16, 31)
		n(0) = randomNumberGenerator.Next(31, 46)
		g(0) = randomNumberGenerator.Next(46, 61)
		o(0) = randomNumberGenerator.Next(61, 76)

		' Set the subscript to 1
		subscript = 1

		' Main do..until loop
		' Normally set to subscript = $array.length, however we are trying to set up everything in one set of do..until nested loops
		Do Until subscript = 5

			' Reset some variables each run through the loop
			' We'll generate a number between 1 and 75
			' Then we will use a case select statement to select which array to insert it into
			' randomNumber = randomNumberGenerator.Next(1, 76)
			isFound = False
			searchSubscript = 0

			' Generate numbs for each column, B,I,N,G,O
			randB = randomNumberGenerator.Next(1, 16)
			randI = randomNumberGenerator.Next(16, 31)
			randN = randomNumberGenerator.Next(31, 46)
			randG = randomNumberGenerator.Next(46, 61)
			randO = randomNumberGenerator.Next(61, 76)

			' Nested do..until loop
			' Either loop through the whole array or till we find a duplicate
			Do Until subscript = searchSubscript OrElse isFound = True

				If randB = b(searchSubscript) Then

					isFound = True

				ElseIf randI = i(searchSubscript) Then

					isFound = True

				ElseIf randN = n(searchSubscript) Then

					isFound = True

				ElseIf randG = g(searchSubscript) Then

					isFound = True

				ElseIf randO = o(searchSubscript) Then

					isFound = True

				Else

					searchSubscript = searchSubscript + 1

				End If

			Loop ' nested do..until loop

			' If we didn't find a duplicate then we'll put it into the array
			If isFound = False Then

				b(subscript) = randB
				i(subscript) = randI
				n(subscript) = randN
				g(subscript) = randG
				o(subscript) = randO

				subscript = subscript + 1

			End If

		Loop ' main do..until loop

	End Sub

End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1