Results 1 to 5 of 5

Thread: [VB.Net] I think I may be looping to much.

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,589

    [VB.Net] I think I may be looping to much.

    I'm using visual studios 2010 and I'm working on a Sudoku project. First let me show y'all my code:

    Code:
    Option Strict On
    Option Explicit On
    Public Class Sudoku
        Private intBoard(8, 8) As Integer
        Private rectBoard(8, 8) As Rectangle
    
        Private Sub Panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
            Call drawBoard(e.Graphics)
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Call addColumns()
        End Sub
    
        Private Sub addColumns()
            '81 cells for a 9x9 board
            Dim higt As Integer = CInt(Panel1.Size.Height / 9)
            Dim wid As Integer = CInt(Panel1.Size.Width / 9)
            Dim cellSize As New Size(wid, higt)
    
            'Column 1
            For i As Integer = 0 To 8 Step 1
                rectBoard(0, i) = New Rectangle(New Point(wid - wid, i * higt), cellSize)
            Next
            'Column 2
            For i As Integer = 0 To 8 Step 1
                rectBoard(1, i) = New Rectangle(New Point(wid * 2 - wid, i * higt), cellSize)
            Next
            'Column 3
            For i As Integer = 0 To 8 Step 1
                rectBoard(2, i) = New Rectangle(New Point(wid * 3 - wid, i * higt), cellSize)
            Next
            'Column 4
            For i As Integer = 0 To 8 Step 1
                rectBoard(3, i) = New Rectangle(New Point(wid * 4 - wid, i * higt), cellSize)
            Next
            'Column 5
            For i As Integer = 0 To 8 Step 1
                rectBoard(4, i) = New Rectangle(New Point(wid * 5 - wid, i * higt), cellSize)
            Next
            'Column 6
            For i As Integer = 0 To 8 Step 1
                rectBoard(5, i) = New Rectangle(New Point(wid * 6 - wid, i * higt), cellSize)
            Next
            'Column 7
            For i As Integer = 0 To 8 Step 1
                rectBoard(6, i) = New Rectangle(New Point(wid * 7 - wid, i * higt), cellSize)
            Next
            'Column 8
            For i As Integer = 0 To 8 Step 1
                rectBoard(7, i) = New Rectangle(New Point(wid * 8 - wid, i * higt), cellSize)
            Next
            'Column 9
            For i As Integer = 0 To 8 Step 1
                rectBoard(8, i) = New Rectangle(New Point(wid * 9 - wid, i * higt), cellSize)
            Next
        End Sub
    
        Private Sub drawBoard(g As Graphics)
            Dim bigPen As New Pen(Brushes.Black, 4)
    
            'Column 1
            For i As Integer = 0 To 8 Step 1
                g.DrawRectangle(Pens.Black, rectBoard(0, i))
            Next
    
            'Column 2
            For i As Integer = 0 To 8 Step 1
                g.DrawRectangle(Pens.Black, rectBoard(1, i))
            Next
    
            'Column 3
            For i As Integer = 0 To 8 Step 1
                g.DrawRectangle(Pens.Black, rectBoard(2, i))
            Next
    
            'Column 4
            For i As Integer = 0 To 8 Step 1
                g.DrawRectangle(Pens.Black, rectBoard(3, i))
            Next
    
            'Column 5
            For i As Integer = 0 To 8 Step 1
                g.DrawRectangle(Pens.Black, rectBoard(4, i))
            Next
    
            'Column 6
            For i As Integer = 0 To 8 Step 1
                g.DrawRectangle(Pens.Black, rectBoard(5, i))
            Next
    
            'Column 7
            For i As Integer = 0 To 8 Step 1
                g.DrawRectangle(Pens.Black, rectBoard(6, i))
            Next
    
            'Column 8
            For i As Integer = 0 To 8 Step 1
                g.DrawRectangle(Pens.Black, rectBoard(7, i))
            Next
    
            'Column 9
            For i As Integer = 0 To 8 Step 1
                g.DrawRectangle(Pens.Black, rectBoard(8, i))
            Next
    
            'Draws the bolded lines to make a 3X3 board outta the 9X9 board
            g.DrawLine(bigPen, rectBoard(3, 0).Location, New Point(rectBoard(3, 8).X, rectBoard(3, 8).Y + rectBoard(3, 8).Height))
            g.DrawLine(bigPen, rectBoard(6, 0).Location, New Point(rectBoard(6, 8).X, rectBoard(6, 8).Y + rectBoard(6, 8).Height))
            g.DrawLine(bigPen, rectBoard(0, 3).Location, New Point(rectBoard(8, 3).X + rectBoard(8, 3).Width, rectBoard(8, 3).Y))
            g.DrawLine(bigPen, rectBoard(0, 6).Location, New Point(rectBoard(8, 6).X + rectBoard(8, 6).Width, rectBoard(8, 6).Y))
        End Sub
    
    End Class
    As you can tell, I haven't done much yet. I just made the board and drew it. I think that I'm looping way to much. My concern is that, if somebody wants to play this on their computer, I think that it would slow down tremendously. Do y'all have any suggestions?
    Last edited by dday9; Jul 30th, 2012 at 10:38 PM. Reason: I edited the code a bit

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,580

    Re: [VB.Net] I think I may be looping to much.

    Indeed too much loops, nested loop will make the code much shorter

    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private intBoard(8, 8) As Integer
        Private rectBoard(8, 8) As Rectangle
    
        Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
            Call drawBoard(e.Graphics)
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Call addColumns()
        End Sub
    
        Private Sub addColumns()
            '81 cells for a 9x9 board
            Dim intHeight As Integer = 50
            Dim intWidth As Integer = 50
    
            For x As Integer = 0 To 8
                For y As Integer = 0 To 8
                    rectBoard(x, y) = New Rectangle(New Point(x * intHeight, y * intWidth), New Size(intWidth, intHeight))
                Next
            Next
        End Sub
    
        Private Sub drawBoard(ByVal g As Graphics)
    
            For x As Integer = 0 To 8
                For y As Integer = 0 To 8
                    g.DrawRectangle(Pens.Black, rectBoard(x, y))
                Next
            Next
    
            'Draws the bolded lines to make a 3X3 board outta the 9X9 board
            Dim bigPen As New Pen(Brushes.Black, 4)
            g.DrawLine(bigPen, rectBoard(3, 0).Location, New Point(rectBoard(3, 8).X, rectBoard(3, 8).Y + rectBoard(3, 8).Height))
            g.DrawLine(bigPen, rectBoard(6, 0).Location, New Point(rectBoard(6, 8).X, rectBoard(6, 8).Y + rectBoard(6, 8).Height))
            g.DrawLine(bigPen, rectBoard(0, 3).Location, New Point(rectBoard(8, 3).X + rectBoard(8, 3).Width, rectBoard(8, 3).Y))
            g.DrawLine(bigPen, rectBoard(0, 6).Location, New Point(rectBoard(8, 6).X + rectBoard(8, 6).Width, rectBoard(8, 6).Y))
        End Sub
    
    End Class
    You don't have to concern about speed in this part of your program as you just draw 8x8 or even more.
    Last edited by 4x2y; Jul 30th, 2012 at 11:04 PM.



  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,589

    Re: [VB.Net] I think I may be looping to much.

    Ah, thank you. I was having trouble wrapping my head around how to do this for some reason:
    Code:
    For x As Integer = 0 To 8
                For y As Integer = 0 To 8
                    g.DrawRectangle(Pens.Black, rectBoard(x, y))
                Next
            Next
    Thank you :]

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,580

    Re: [VB.Net] I think I may be looping to much.

    There was an error in the sub addColumns, you have to replace

    Code:
    rectBoard(x, y) = New Rectangle(New Point(x * intHeight, y * intWidth), New Size(intWidth, intHeight))
    with
    Code:
    rectBoard(x, y) = New Rectangle(New Point(x * intWidth, y * intHeight), New Size(intWidth, intHeight))
    as x must multiply by intWidth, you can notice the error effect if you set different value for width and height



  5. #5

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,589

    Re: [VB.Net] I think I may be looping to much.

    Yeah, I did notice that when I tried:
    Code:
    Dim intWidth As Integer = CInt(Panel1.Width / 9)
    but no biggie ;P
    Last edited by dday9; Jul 31st, 2012 at 08:59 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width