9 Replies - 15450 Views - Last Post: 29 December 2010 - 11:34 AM Rate Topic: -----

#1 dominatingpcs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 01-April 07

Bingo Game Help

Posted 01 April 2007 - 05:25 PM

Hello Everyone,

I am working on a Bingo game and I am stuck. I got the program to generate random number but they are not unique. I dont know how to fix this. I have researched online and nothing this is my last resource. I am using Visual Basics 2005 Express Edition.

-Hopefully someone can help me thanks.

Here is the code and an image is attached

Public Class Form2
	Dim objRandom As New Random
	Dim blnUsed(73) As Boolean
	


	Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		Dim blnDone As Boolean
		Dim intLower As Integer
		Dim intHigher As Integer

		'B
		Dim intNumber As Integer
		Dim intNumber1 As Integer
		Dim intNumber2 As Integer
		Dim intNumber3 As Integer
		Dim intNumber4 As Integer

		'I
		Dim intNumber5 As Integer
		Dim intNumber6 As Integer
		Dim intNumber7 As Integer
		Dim intNumber8 As Integer
		Dim intNumber9 As Integer

		'N
		Dim intNumber10 As Integer
		Dim intNumber11 As Integer
		Dim intNumber12 As Integer
		Dim intNumber13 As Integer

		'G
		Dim intNumber14 As Integer
		Dim intNumber15 As Integer
		Dim intNumber16 As Integer
		Dim intNumber17 As Integer
		Dim intNumber18 As Integer

		'O
		Dim intNumber19 As Integer
		Dim intNumber20 As Integer
		Dim intNumber21 As Integer
		Dim intNumber22 As Integer
		Dim intNumber23 As Integer


	 ‘Creats random number in 1st label

		Do
			intNumber = objRandom.Next(1, 16)
			If blnUsed(intNumber) = False Then
				blnUsed(intNumber) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		B1Label.Text = intNumber


		Do
			intNumber1 = objRandom.Next(1, 16)
			If blnUsed(intNumber1) = False Then
				blnUsed(intNumber1) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		B2Label.Text = intNumber1

		Do
			intNumber2 = objRandom.Next(1, 16)
			If blnUsed(intNumber2) = False Then
				blnUsed(intNumber2) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		B3Label.Text = intNumber2

		Do
			intNumber3 = objRandom.Next(1, 16)
			If blnUsed(intNumber3) = False Then
				blnUsed(intNumber3) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		B4Label.Text = intNumber3

		Do
			intNumber4 = objRandom.Next(1, 16)
			If blnUsed(intNumber4) = False Then
				blnUsed(intNumber4) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		B5Label.Text = intNumber4

		Do
			intNumber5 = objRandom.Next(16, 31)
			If blnUsed(intNumber5) = False Then
				blnUsed(intNumber5) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		I1Label.Text = intNumber5

		Do
			intNumber6 = objRandom.Next(16, 31)
			If blnUsed(intNumber6) = False Then
				blnUsed(intNumber6) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		I2Label.Text = intNumber6

		Do
			intNumber7 = objRandom.Next(16, 31)
			If blnUsed(intNumber7) = False Then
				blnUsed(intNumber7) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		I3Label.Text = intNumber7

		Do
			intNumber8 = objRandom.Next(16, 31)
			If blnUsed(intNumber8) = False Then
				blnUsed(intNumber8) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		I4Label.Text = intNumber8

		Do
			intNumber9 = objRandom.Next(16, 31)
			If blnUsed(intNumber9) = False Then
				blnUsed(intNumber9) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		I5Label.Text = intNumber9


		Do
			intNumber10 = objRandom.Next(31, 46)
			If blnUsed(intNumber10) = False Then
				blnUsed(intNumber10) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		N1Label.Text = intNumber10

		Do
			intNumber11 = objRandom.Next(31, 46)
			If blnUsed(intNumber11) = False Then
				blnUsed(intNumber11) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		N2Label.Text = intNumber11

		Do
			intNumber12 = objRandom.Next(31, 46)
			If blnUsed(intNumber12) = False Then
				blnUsed(intNumber12) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		N4Label.Text = intNumber12

		Do
			intNumber13 = objRandom.Next(31, 46)
			If blnUsed(intNumber13) = False Then
				blnUsed(intNumber13) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		N5Label.Text = intNumber13

		Do
			intNumber14 = objRandom.Next(46, 61)
			If blnUsed(intNumber14) = False Then
				blnUsed(intNumber14) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		G1Label.Text = intNumber14

		Do
			intNumber15 = objRandom.Next(46, 61)
			If blnUsed(intNumber15) = False Then
				blnUsed(intNumber15) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		G2Label.Text = intNumber15

		Do
			intNumber16 = objRandom.Next(46, 61)
			If blnUsed(intNumber16) = False Then
				blnUsed(intNumber16) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		G3Label.Text = intNumber16

		Do
			intNumber17 = objRandom.Next(46, 61)
			If blnUsed(intNumber17) = False Then
				blnUsed(intNumber17) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		G4Label.Text = intNumber17

		Do
			intNumber18 = objRandom.Next(46, 61)
			If blnUsed(intNumber18) = False Then
				blnUsed(intNumber18) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		G5Label.Text = intNumber18

		Do
			intNumber19 = objRandom.Next(46, 61)
			If blnUsed(intNumber19) = False Then
				blnUsed(intNumber19) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		O1Label.Text = intNumber19

		Do
			intNumber20 = objRandom.Next(46, 61)
			If blnUsed(intNumber20) = False Then
				blnUsed(intNumber20) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		O2Label.Text = intNumber20

		Do
			intNumber21 = objRandom.Next(46, 61)
			If blnUsed(intNumber21) = False Then
				blnUsed(intNumber21) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		O3Label.Text = intNumber21

		Do
			intNumber22 = objRandom.Next(46, 61)
			If blnUsed(intNumber22) = False Then
				blnUsed(intNumber22) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		O4Label.Text = intNumber22


		Do
			intNumber23 = objRandom.Next(46, 61)
			If blnUsed(intNumber23) = False Then
				blnUsed(intNumber23) = True
				blnDone = True
			End If
		Loop Until blnDone = True
		O5Label.Text = intNumber23




	End Sub
End Class

Attached image(s)

  • Attached Image

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Bingo Game Help

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Bingo Game Help

Posted 01 April 2007 - 06:29 PM

You are correct that they are not unique. They are not supposed to be unique, just random.

What you will need to do is when a random number is selected, you will need to compare it to the previous numbers already selected. If it matches an existing number, then discard it and generate a new number. If it doesn't match any previous number then assign it to the bingo card.

To avoid redundancy in your code I would suggest putting the code to check all the previous numbers inside a Function/Procedure. Then just call the Function/Procedure every time you select a new number.
Was This Post Helpful? 0
  • +
  • -

#3 dominatingpcs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 01-April 07

Re: Bingo Game Help

Posted 02 April 2007 - 09:36 AM

Hello,

I tried this and it still wont work can you lead me in the right direction. It seems like everyway I go I get stuck.


Do
intNumber = objRandom.Next(1, 16)
If intNumber = intNumber2 or intNumber1 then
intNumber = objRandom.Next(1, 16)
End If
Loop Until intNumber1 <> intNumber1 or intNumber2
B1Label.Text = intNumber



Do
intNumber1 = objRandom.Next(1, 16)
If intNumber1 = intNumber or intNumber2 then
intNumber1 = objRandom.Next(1, 16)
End If
Loop Until intNumber1 <> intNumber or intNumber2
B2Label.Text = intNumber1

Do
intNumber2 = objRandom.Next(1, 16)
If intNumber2 = intNumber or intNumber1 then
intNumber1 = objRandom.Next(1, 16)
End If
Loop Until intNumber2 <> intNumber or intNumber1
B3Label.Text = intNumber2


Thanks for helping me
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Bingo Game Help

Posted 02 April 2007 - 11:39 AM

It is going to get very complicated very quickly with so many variables.

Have you considered using arrays to store the values, rather than using so many individual variables?

It will make this a whole lot easier.
Was This Post Helpful? 0
  • +
  • -

#5 dominatingpcs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 01-April 07

Re: Bingo Game Help

Post icon  Posted 02 April 2007 - 12:06 PM

Thanks Jayman9 for all your help I got it to work. But a lot of code that I needed to write

Do
intNumber = objRandom.Next(1, 16)
If intNumber = intNumber1 Or intNumber = intNumber2 Or intNumber = intNumber3 Or intNumber = intNumber4 Then
intNumber = objRandom.Next(1, 16)
End If

Loop Until intNumber <> intNumber1 Or intNumber <> intNumber2 Or intNumber <> intNumber3 Or intNumber <> intNumber4
B1Label.Text = intNumber


Do
intNumber1 = objRandom.Next(1, 16)

If intNumber1 = intNumber Or intNumber1 = intNumber2 Or intNumber1 = intNumber3 Or intNumber1 = intNumber4 Then
intNumber1 = objRandom.Next(1, 16)
End If

Loop Until intNumber1 <> intNumber Or intNumber1 <> intNumber2 Or intNumber1 <> intNumber3 Or intNumber1 <> intNumber4
B2Label.Text = intNumber1


Do
intNumber2 = objRandom.Next(1, 16)

If intNumber2 = intNumber Or intNumber2 = intNumber1 Or intNumber2 = intNumber3 Or intNumber2 = intNumber4 Then
intNumber2 = objRandom.Next(1, 16)
End If

Loop Until intNumber2 <> intNumber Or intNumber2 <> intNumber1 Or intNumber2 <> intNumber3 Or intNumber2 <> intNumber4
B3Label.Text = intNumber2


Do
intNumber3 = objRandom.Next(1, 16)

If intNumber3 = intNumber Or intNumber3 = intNumber1 Or intNumber3 = intNumber2 Or intNumber3 = intNumber4 Then
intNumber3 = objRandom.Next(1, 16)
End If

Loop Until intNumber3 <> intNumber Or intNumber3 <> intNumber1 Or intNumber3 <> intNumber2 Or intNumber3 <> intNumber4
B4Label.Text = intNumber3
Was This Post Helpful? 0
  • +
  • -

#6 dawmail333  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 174
  • Joined: 02-July 07

Re: Bingo Game Help

Posted 28 January 2008 - 04:25 AM

View Postdominatingpcs, on 2 Apr, 2007 - 12:06 PM, said:

Thanks Jayman9 for all your help I got it to work. But a lot of code that I needed to write
 Do
			intNumber = objRandom.Next(1, 16)
			If intNumber = intNumber1 Or intNumber = intNumber2 Or intNumber = intNumber3 Or intNumber = intNumber4 Then
				intNumber = objRandom.Next(1, 16)
			End If

		Loop Until intNumber <> intNumber1 Or intNumber <> intNumber2 Or intNumber <> intNumber3 Or intNumber <> intNumber4
		B1Label.Text = intNumber


		Do
			intNumber1 = objRandom.Next(1, 16)

			If intNumber1 = intNumber Or intNumber1 = intNumber2 Or intNumber1 = intNumber3 Or intNumber1 = intNumber4 Then
				intNumber1 = objRandom.Next(1, 16)
			End If

		Loop Until intNumber1 <> intNumber Or intNumber1 <> intNumber2 Or intNumber1 <> intNumber3 Or intNumber1 <> intNumber4
		B2Label.Text = intNumber1


		Do
			intNumber2 = objRandom.Next(1, 16)

			If intNumber2 = intNumber Or intNumber2 = intNumber1 Or intNumber2 = intNumber3 Or intNumber2 = intNumber4 Then
				intNumber2 = objRandom.Next(1, 16)
			End If

		Loop Until intNumber2 <> intNumber Or intNumber2 <> intNumber1 Or intNumber2 <> intNumber3 Or intNumber2 <> intNumber4
		B3Label.Text = intNumber2


		Do
			intNumber3 = objRandom.Next(1, 16)

			If intNumber3 = intNumber Or intNumber3 = intNumber1 Or intNumber3 = intNumber2 Or intNumber3 = intNumber4 Then
				intNumber3 = objRandom.Next(1, 16)
			End If

		Loop Until intNumber3 <> intNumber Or intNumber3 <> intNumber1 Or intNumber3 <> intNumber2 Or intNumber3 <> intNumber4
		B4Label.Text = intNumber3



PLEASE use [code] tags.

I would have used a custom class, if you want to know how I would do that, just PM me or post.

At least, it shouldn't require as much code.
Was This Post Helpful? 0
  • +
  • -

#7 daspiel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-June 08

Re: Bingo Game Help

Posted 12 June 2008 - 05:32 PM

Did you finally get this to work?
Was This Post Helpful? 0
  • +
  • -

#8 Guest_B Grant*


Reputation:

Re: Bingo Game Help

Posted 28 December 2010 - 11:12 AM

The issue is blnDone is never set back to False after the first time it is set to True.
Was This Post Helpful? 0

#9 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 605
  • Joined: 01-October 08

Re: Bingo Game Help

Posted 28 December 2010 - 03:09 PM

A partial idea using TableLayoutPanel

Public Class Form1

    Private Sub Form1_Shown(ByVal sender As Object, _
                            ByVal e As System.EventArgs) Handles Me.Shown

        'TableLayoutPanel1 size = 312, 238
        'TableLayoutPanel1 Rows
        '6 rows - 16%
        'TableLayoutPanel1 Column
        '5 columns - 20%
        '  B I N G O
        '  0 1 2 3 4
        '0
        '1
        '2
        '3
        '4
        '5
        Dim s As String = "BINGO"

        For col As Integer = 0 To 4 'set header row to B I N G O
            Dim lbl As New Label
            lbl.Text = s.Substring(col, 1)
            lbl.TextAlign = ContentAlignment.MiddleCenter
            TableLayoutPanel1.Controls.Add(lbl, col, 0)
        Next

        For col As Integer = 0 To 4 'set up buttons for the board
            'rw 0 has B I N G O
            For rw As Integer = 1 To 5
                Dim btn As New Button
                btn.Name = s.Substring(col, 1) & rw.ToString
                btn.Text = btn.Name
                AddHandler btn.Click, AddressOf btn_Click
                TableLayoutPanel1.Controls.Add(btn, col, rw) 'add to TableLayoutPanel
                board.Add(btn.Name, btn) 'add to Dictionary
            Next
        Next

        NewBoard()
    End Sub

    Dim board As New Dictionary(Of String, Button)

    Private Sub btn_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs)

        'handles all clicks
        Dim btn As Button = CType(sender, Button)
        btn.Enabled = False
        Debug.WriteLine(btn.Name)
        'add code for bingo check here
    End Sub

    ' 1 to 15 in the B column
    '16 to 30 in the I column
    '31 to 45 in the N column
    '46 to 60 in the G column
    '61 to 75 in the O column

    Dim PRNG As New Random
    Dim b As IEnumerable(Of Integer) = Enumerable.Range(1, 15)
    Dim i As IEnumerable(Of Integer) = Enumerable.Range(16, 15)
    Dim n As IEnumerable(Of Integer) = Enumerable.Range(31, 15)
    Dim g As IEnumerable(Of Integer) = Enumerable.Range(46, 15)
    Dim o As IEnumerable(Of Integer) = Enumerable.Range(61, 15)

    Private Sub NewBoard()
        'shuffle
        Dim ba As List(Of Integer) = b.OrderBy(Function(zb) PRNG.Next).Take(5).ToList
        Dim ia As List(Of Integer) = i.OrderBy(Function(zi) PRNG.Next).Take(5).ToList
        Dim na As List(Of Integer) = n.OrderBy(Function(zn) PRNG.Next).Take(5).ToList
        Dim ga As List(Of Integer) = g.OrderBy(Function(zg) PRNG.Next).Take(5).ToList
        Dim oa As List(Of Integer) = o.OrderBy(Function(zo) PRNG.Next).Take(5).ToList

        Dim idx As Integer
        Dim foo As List(Of Integer)
        For Each btn As Button In board.Values
            btn.Enabled = True
            If btn.Name <> "N3" Then
                Select Case btn.Name.Substring(0, 1)
                    Case "B"
                        foo = ba
                    Case ("I")
                        foo = ia
                    Case "N"
                        foo = na
                    Case "G"
                        foo = ga
                    Case "O"
                        foo = oa
                End Select
                idx = PRNG.Next(foo.Count)
                btn.Text = foo(idx).ToString
                foo.RemoveAt(idx)
            Else
                btn.Text = "FREE"
                btn.Enabled = False
            End If
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles Button1.Click
        NewBoard()
    End Sub
End Class


This post has been edited by dbasnett: 28 December 2010 - 03:17 PM

Was This Post Helpful? 0
  • +
  • -

#10 Shadar  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: Bingo Game Help

Posted 29 December 2010 - 11:34 AM

Public Class frmCard
    Private Sub frmCard_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Width = 407
        Me.Height = 473
        CreateHeader()
        CreateSquares()
    End Sub

    Private Sub CreateHeader()
        Dim intLabelLeft As Integer = 0
        Dim strBingo As String = "BINGO"
        For col = 1 To 5
            Dim lblNew As New Label
            Me.Controls.Add(lblNew)
            lblNew.TextAlign = ContentAlignment.MiddleCenter
            lblNew.Font = New Font(lblNew.Font.Name, 14, lblNew.Font.Style, lblNew.Font.Unit)
            lblNew.Height = 40
            lblNew.Width = 80
            lblNew.BorderStyle = BorderStyle.FixedSingle
            lblNew.Left = intLabelLeft
            lblNew.Text = Strings.Mid(strBingo, col, 1)
            lblNew.Name = "1" & col.ToString
            If col < 6 Then
                intLabelLeft = lblNew.Left + lblNew.Width
            Else
                intLabelLeft = 0
            End If
        Next
    End Sub

    Private Sub CreateSquares()
        Dim intLabelLeft As Integer = 0
        Dim intTop As Integer = 40
        For row = 1 To 5
            For col = 1 To 5
                Dim intNumToAdd As Integer
                Dim lblNew As New Label
                Me.Controls.Add(lblNew)
                lblNew.TextAlign = ContentAlignment.MiddleCenter
                lblNew.Top = intTop
                lblNew.Height = 80
                lblNew.Width = 80
                lblNew.BorderStyle = BorderStyle.FixedSingle
                lblNew.Left = intLabelLeft
                lblNew.Name = row.ToString & col.ToString
                intLabelLeft = lblNew.Left + lblNew.Width
                intNumToAdd = GetNumbers(row, col)
                lblNew.Text = intNumToAdd
                If row = 3 And col = 3 Then lblNew.Text = "FREE"
            Next
            intTop += 80
            intLabelLeft = 0
        Next
    End Sub

    Private Function Exists(ByVal col As Integer, ByVal inNumToCheck As Integer) As Boolean

        For Each lbl As Label In Me.Controls
            If Strings.Mid(lbl.Name, 2, 1) = col Then
                If lbl.Text = inNumToCheck.ToString Then
                    Return True
                    Exit For
                End If
            End If
        Next
        Return False
    End Function

    Private Function GetNumbers(ByVal inRow As Integer, ByVal inCol As Integer) As Integer
        Dim retValue As Integer
        Randomize()

        Select Case inCol
            Case 1 'B
                retValue = CInt(Int((15 * Rnd()) + 1))
                Do Until Exists(inCol, retValue) = False
                    retValue = CInt(Int((15 * Rnd()) + 1))
                Loop
            Case 2 'I

                Do While retValue < 16
                    retValue = CInt(Int((30 * Rnd()) + 1))
                    Do Until Exists(inCol, retValue) = False
                        retValue = CInt(Int((30 * Rnd()) + 1))
                    Loop
                Loop
            Case 3 'N
                Do While retValue < 31
                    retValue = CInt(Int((45 * Rnd()) + 1))
                    Do Until Exists(inCol, retValue) = False
                        retValue = CInt(Int((45 * Rnd()) + 1))
                    Loop
                Loop
            Case 4 'G
                Do While retValue < 46
                    retValue = CInt(Int((60 * Rnd()) + 1))
                    Do Until Exists(inCol, retValue) = False
                        retValue = CInt(Int((60 * Rnd()) + 1))
                    Loop
                Loop
            Case 5 'O
                Do While retValue < 61
                    retValue = CInt(Int((75 * Rnd()) + 1))
                    Do Until Exists(inCol, retValue) = False
                        retValue = CInt(Int((75 * Rnd()) + 1))
                    Loop
                Loop
                   
        End Select
        Return retValue
    End Function
End Class



This post has been edited by Shadar: 29 December 2010 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1