Results 1 to 6 of 6

Thread: Recursive Function For Generating Sudoku Puzzle

  1. #1
    Join Date
    Nov 2009
    Posts
    712

    Recursive Function For Generating Sudoku Puzzle

    Hello, I have a problem while creating code. I have undertaken to convert a recursive function that can generate sudoku puzzles from PHP to VB. But, I am getting problem of the return type which is both an Integer and a boolean.
    I do not know how to solve the solution. I am using the code below:

    Code:
    Private Function puzzle(Optional ByVal grid As Integer(,) = Nothing, Optional ByVal position As Integer = 0)
            Sun i, j, v As Integer
            Sun values = New List(Of Integer)
            Sun result As Boolean
             If (position = 81) Then
                Return (grid)
            End If
            i = Math.Floor(position / 9)
            j = position Mod 9
     
            'list of numbers 1 to 9
            values.AddRange({1, 2, 3, 4, 5, 6, 7, 8, 9})
     
            'clears the values already used
            If (v> 0) Then
                For k As Integer = 0 To 8
                    'cut the value of the line
                    If (v = Int(grid(i, k))) Then
                        values.RemoveAt(v -- 1)
                    End If
     
                    'cut the value of the column
                    If (v = Int(grid(k, j))) Then
                        values.RemoveAt(v -- 1)
                    End If
     
                    'cut the value of the area
                    If (v = Int(grid(i + Math.Floor(k / 3)J + (k Mod 3)))) Then
                        values.RemoveAt(v -- 1)
                    End If
                Next
            End If
             values = shuffleList(values)
     
            For Each v In values
                grid(i, j) = V
    
                result = creer_grille(grid position + 1)
     
                If (result) Then
                    Return result
                End If
                grid(i, j) = Nothing
            Next 
            Return False
        End Function
    Thank you in advance for your help

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Recursive Function For Generating Sudoku Puzzle

    Hello, I have check your all the code and I didn't find any problem in that, I think you need to do one change in your program and then you can able to solve your problem. If you able to use the subroutine in place of the function then it may possible that you will able to get the solution for your problem. And also it is necessary to provide the parameter as a byref so that you will able to get the solution.

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Recursive Function For Generating Sudoku Puzzle

    Hey, simply replace ByVal by byref, remove optional as below:

    Code:
    Private Function testing(ByRef grid As Integer(,), ByRef position As Integer)
    And then simply declare variables before calling the sub as below:
    Code:
    Sun grid As Integer(,) = Nothing
    Sun position as integer = 0
    creer_grille (grid position)

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Recursive Function For Generating Sudoku Puzzle

    Hey, it is quiet simple and for that you can modify your program by simply adding the code below.
    Code:
    Private Function creer_grille(ByRef grid As Integer(,), 
    ByRef position As Integer) as Boolean
            Sun i, j, v As Integer
            Sun values = New List(Of Integer)
            Sun result As Boolean
     
            'returns the grid found if we are to finish
            If (position = 81) Then
                Return True    'Would that the grid is already filled
            End If
            Return False
        End Function

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    Re: Recursive Function For Generating Sudoku Puzzle

    Hello, I have got the php script below which will work fine for your problem and you will able to get the solution for your problem. I have tested it and it is working really fine.

    Code:
    <? php
    function creer_grille( $ grid = array(), $ Position = 0 ) {
           if( $ position == 81 ) {
                   return $ grid;
           }
           $ i = floor( $ position / 9 );
           $ j = $ position% 9;
           $ values = range( 1, 9 );
          for( $ k =0; $ K <9; $ K + + ) {
                   if( $ v = $ grid[ $ i ][ $ k ] ) {
                           unset( $ values[ $ v-1 ] );
                   }
                   if( $ v = $ grid[ $ k ][ $ j ] ) {
                           unset( $ values[ $ v-1 ] );
                   }
                   if( $ v = $ grid[ $ i + floor($ k /3) ][ $ j + ($ k%3) ] ) {
                           unset( $ values[ $ v-1 ] );
                   }
           }
           shuffle( $ values );
           foreach( $ values as $ v ) {
                   $ grid[$ i][$ j] = $ V;
                   $ result = creer_grille( $ grid, $ position + 1 );
                   if( $ result ) {
                           return $ result;
                   }
                   unset( $ grid[$ i][$ j] );
           }
           return false;
    }
    ?>

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Recursive Function For Generating Sudoku Puzzle

    Hello, I have book list of the php as below. I think it will solve your problem :
    • The PHP Anthology: 101 Essential Tips, Tricks & Hacks, 2nd Edition
    • PHP 5 Power Programming
    • A Programmer's Introduction to PHP 4.0
    • Practical PHP Programming
    • Build Your Own Database Driven Website Using PHP & MySQL

Similar Threads

  1. void recursive program that reverses a sentence
    By ivilkilu in forum Software Development
    Replies: 1
    Last Post: 21-03-2012, 07:41 AM
  2. Reinstalling sudoku on iPod Classic?
    By cbrooks8909 in forum Video Games
    Replies: 2
    Last Post: 18-12-2010, 02:47 AM
  3. 2X2 Sudoku game with images
    By Carmine in forum Video Games
    Replies: 5
    Last Post: 28-01-2010, 03:47 PM
  4. Recursive Problems With BSOD
    By Aakarshan.d in forum Operating Systems
    Replies: 4
    Last Post: 18-03-2009, 08:35 AM
  5. Forwarders cannot be validated and recursive query fails
    By Bennett in forum Windows Server Help
    Replies: 7
    Last Post: 26-02-2009, 08:48 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,434,164.29237 seconds with 17 queries