Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,406 members, 7,819,439 topics. Date: Monday, 06 May 2024 at 04:20 PM

Help With Sudoku Puzzle Generator - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help With Sudoku Puzzle Generator (1536 Views)

Sudoku And Linear Algebra / Sorting List Of Numbers And Strings: Simple Puzzle / Sudoku Puzzle (2) (3) (4)

(1) (Reply) (Go Down)

Help With Sudoku Puzzle Generator by kheme(m): 4:01pm On Feb 21, 2009
hi. i'm trying to write a sudoku puzzle generator with vb.net but my algorithm seems correct, but runs out of numbers at some stages while generating the puzzle. can anyone look into ma code and see why?

Public Class home
   Public rp = 0, arr(8, cool As Integer, rsv(8, cool As List(Of Integer), ax(2, 2)

   Public Function sot(ByRef ar As Array)
       Dim o, p, q, r
       For o = 0 To 8
           If (ar(o) = Nothing) Then
               For p = o To 1 Step -1
                   q = ar(p)
                   r = ar(p - 1)
                   ar(p) = r
                   ar(p - 1) = q
               Next
           End If
       Next
       Return Nothing
   End Function

   Public Function rnd(ByVal x As Integer, ByVal y As Integer)
       Dim r1 = Nothing, rd As New Random
       For a As Integer = 0 To 8
           If (arr(x, a) <> 0 And rsv(x, y).IndexOf(arr(x, a)) <> -1) Then
               rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(x, a)))
           End If

           If (arr(a, y) <> 0 And rsv(x, y).IndexOf(arr(a, y)) <> -1) Then
               rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(a, y)))
           End If
       Next

       If (x >= 0 And x <= 2 And y >= 0 And y <= 2) Then
           For a1 As Integer = 0 To 2
               For b1 As Integer = 0 To 2
                   If (arr(a1, b1) <> Nothing And rsv(x, y).IndexOf(arr(a1, b1)) <> -1) Then
                       rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(a1, b1)))
                   End If
               Next
           Next
       End If
       If (x >= 0 And x <= 2 And y >= 3 And y <= 5) Then
           For a1 As Integer = 0 To 2
               For b1 As Integer = 3 To 5
                   If (arr(a1, b1) <> Nothing And rsv(x, y).IndexOf(arr(a1, b1)) <> -1) Then
                       rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(a1, b1)))
                   End If
               Next
           Next
       End If
       If (x >= 0 And x <= 2 And y >= 6 And y <= cool Then
           For a1 As Integer = 0 To 2
               For b1 As Integer = 6 To 8
                   If (arr(a1, b1) <> Nothing And rsv(x, y).IndexOf(arr(a1, b1)) <> -1) Then
                       rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(a1, b1)))
                   End If
               Next
           Next
       End If

       If (x >= 3 And x <= 5 And y >= 0 And y <= 2) Then
           For a1 As Integer = 3 To 5
               For b1 As Integer = 0 To 2
                   If (arr(a1, b1) <> Nothing And rsv(x, y).IndexOf(arr(a1, b1)) <> -1) Then
                       rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(a1, b1)))
                   End If
               Next
           Next
       End If
       If (x >= 3 And x <= 5 And y >= 3 And y <= 5) Then
           For a1 As Integer = 3 To 5
               For b1 As Integer = 3 To 5
                   If (arr(a1, b1) <> Nothing And rsv(x, y).IndexOf(arr(a1, b1)) <> -1) Then
                       rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(a1, b1)))
                   End If
               Next
           Next
       End If
       If (x >= 3 And x <= 5 And y >= 6 And y <= cool Then
           For a1 As Integer = 3 To 5
               For b1 As Integer = 6 To 8
                   If (arr(a1, b1) <> Nothing And rsv(x, y).IndexOf(arr(a1, b1)) <> -1) Then
                       rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(a1, b1)))
                   End If
               Next
           Next
       End If

       If (x >= 6 And x <= 8 And y >= 0 And y <= 2) Then
           For a1 As Integer = 6 To 8
               For b1 As Integer = 0 To 2
                   If (arr(a1, b1) <> Nothing And rsv(x, y).IndexOf(arr(a1, b1)) <> -1) Then
                       rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(a1, b1)))
                   End If
               Next
           Next
       End If
       If (x >= 6 And x <= 8 And y >= 3 And y <= 5) Then
           For a1 As Integer = 6 To 8
               For b1 As Integer = 3 To 5
                   If (arr(a1, b1) <> Nothing And rsv(x, y).IndexOf(arr(a1, b1)) <> -1) Then
                       rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(a1, b1)))
                   End If
               Next
           Next
       End If
       If (x >= 6 And x <= 8 And y >= 6 And y <= cool Then
           For a1 As Integer = 6 To 8
               For b1 As Integer = 6 To 8
                   If (arr(a1, b1) <> Nothing And rsv(x, y).IndexOf(arr(a1, b1)) <> -1) Then
                       rsv(x, y).RemoveAt(rsv(x, y).IndexOf(arr(a1, b1)))
                   End If
               Next
           Next
       End If

       Try
           arr(x, y) = rsv(x, y)(rd.Next(0, rsv(x, y).Count))
       Catch ex As Exception

       End Try
       Return Nothing
   End Function

   Private Sub home_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim a = 0, b = 0, c = 0, n
       For a = 0 To 8
           For b = 0 To 8
               n = "s" & a & b
               rsv(a, b) = New List(Of Integer)
               For c = 1 To 9
                   rsv(a, b).Add(c)
               Next
               rnd(a, b)
               Panel2.Controls.Item(n).text = arr(a, b)
           Next
       Next

   End Sub

   Private Sub RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshToolStripMenuItem.Click
       rp = 0
       Array.Clear(arr, 0, 81)
       home_Load(Nothing, Nothing)
   End Sub
End Class


i've attached screen shots, the "0" in the puzzle are the places where the algorithm runs out of numbers to put cos the next number to put cannot be placed there due to the rules of sudoku. i need help guys!

Re: Help With Sudoku Puzzle Generator by Nobody: 7:36am On Feb 23, 2009
I wrote one of these in Java over a year ago.
You have to use a backtracking algorithm.
Set up the rules. Make the algorithm fill in the slots with random numbers line by line. When it gets stuck it should go back a random number of steps(nothing large else you get "array out of bounds").

I will try to find my code, or write it again if I can find the time.
Re: Help With Sudoku Puzzle Generator by Nobody: 9:26am On Feb 23, 2009
Had to write it again.
Java code.


import java.util.Random;


public class Main {


public static void main(String[] args) {

int[][] grid= new int[9][9];
//change random seed to get a differnet grid
Random rand= new Random(99);
int count=0;
int failCount=0;
while(count<81)
{
boolean fail=false;
//generate number
int temp=rand.nextInt(9)+1;
//check horizontal
for(int i=0;i<count%9;i++)
if(temp==grid[i][count/9])
{
fail=true;
failCount++;
break;
}
//check vertical
for(int i=0;i<count/9;i++)
if(temp==grid[count%9][i])
{
fail=true;
failCount++;
break;
}
//check 3x3
for(int i=(count%9)/3*3;i<(count%9)/3*3+3;i++)
for(int j=(count/9)/3*3;j<(count/9)/3*3+3;j++)
if(temp==grid[i][j])
{
fail=true;
failCount++;
break;
}
//if stuck then backtrack
if(failCount>40)
{
int x=rand.nextInt(50)+9;
for(int i=0;i<x && count>0;i++)
{
grid[count%9][count/9]=0;
count--;
}
failCount=0;

}

// all clear then add new number to the grid
if(fail==false)
{
grid[count%9][count/9]=temp;
count++;
failCount=0;
}
}

for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
System.out.print(grid[i][j]);
}
System.out.println();
}
}

}

Re: Help With Sudoku Puzzle Generator by kheme(m): 7:14pm On Feb 23, 2009
ok, i'd see if i can convert the java codes into vb. thanks for the input!
Re: Help With Sudoku Puzzle Generator by kheme(m): 8:14pm On Apr 05, 2009
tried ya code, still didnt work man!

(1) (Reply)

How To Create Api In Asp.net Mvc 4 For Beginners / Dhtmlconsole - A Google Chrome Plugin For Sending Real-time From Server / Help Him Create Interdependent Fields Microsoft Access

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 44
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.