473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

SuDoKu challenge

I am totally new to Visual Basic and started out with a copy of version 4 that I was given. I am hooked. I enjoy writing code and am looking forward to creating my own spins on different code projects just like the rest of you.

I have put together a small program for the totally beat-to-death term SuDoKu and am having trouble with generating the boards (arrays) to display.

I have did this thus far:

[Dim MyNum(9,9) As Integer]

[ for x = 1 to 9 ]

[ for y = 1 ro 9 ]


[newZ:]
[Randomize]
[z = Int( ( 9 * RND) + 1)]

[if y = 1 then goto getnewnum]
[ for w = ( y - 1) to 1 Step -1]
[ if MyNum(x , y) = MyNum(x , w) then goto newZ]
[ next w]

[if x = 1 then goto getnewnum]
[ for v = (x - 1) to 1 Step -1]
[ if MyNum(x , y) = MyNum(v , y) then goto newZ]
[ next v]

[getnewnum]
[next y, x]


I have done the "homework" suggested by the web page authors. There are many posts that I see that are written for you more experienced programmers, but none for me. Does anyone have a clue how to assist me or even want to try?
I would appreciate some assistance.
Oct 4 '07 #1
10 2952
kadghar
1,295 Expert 1GB
This might be of help:

Random Filled Array
Oct 4 '07 #2
I have alreaady viewed that post and it seemed way to confusing. I did not wish to convert any of the numbers in the arrays to just "1's" and "2's".

Additionally, trying the code before I made my original post left me with duplicates in my 3 x 3 arrays and in my vertical arrays (columns).

No one seems to go past the first row. Do only SuDoKu book makers have the ability to do this? I would guess that VB4 is powerful enough to handle this task,
but fully willing to admit that I could be wrong because I am new.

Confused!!!!!!!!!!!
Oct 4 '07 #3
kadghar
1,295 Expert 1GB
I have alreaady viewed that post and it seemed way to confusing. I did not wish to convert any of the numbers in the arrays to just "1's" and "2's".

Additionally, trying the code before I made my original post left me with duplicates in my 3 x 3 arrays and in my vertical arrays (columns).

No one seems to go past the first row. Do only SuDoKu book makers have the ability to do this? I would guess that VB4 is powerful enough to handle this task,
but fully willing to admit that I could be wrong because I am new.

Confused!!!!!!!!!!!
Check out the las option, in this case, for the sudoku, only consider the 3x3 squares, start generating the 1st, 4th and 7th row:

123456789
234567891
345678912

then the 2nd, 5th and 8th, and finaly the 3rd, 6th and 9th, will be something like:

123456789
456789123
879123456
234567891
567891234
891235467
345678912
678912345
912345678

Then just randomly change columns and rows, but be sure that you'll only change column
1 with 2 or 3,
4 with 5 or 6,
7 with 8 or 9 and the same for rows..
you can also change sets of 3 columns and sets of 3 rows
do this a random number of times and you'll have a nice sudoku board
Oct 4 '07 #4
kadghar
1,295 Expert 1GB
oh, and i forgot to tell you
if you want it even more randomly, remember that 123456789 is not the only original sequence, you can create an alternative one at random, lets say:

159362874

and follow the same procedure, so you'll get:

159 362 874
362 874 159
874 159 362

593 628 741
628 741 593
741 593 628

936 287 415
287 415 936
415 936 287

And then swap columns and rows.
Oct 4 '07 #5
Please forgive me for taking so long to reply.

I also do not want to sound like an idiot, lazy or selfish, but exactly how do you do the things that you have described in code?

Please remember two things:

1) I am running an older version of VB (version 4, heck I got it for free!)
2) I am new to coding

I do realize that all of your time is valuable to you, so can you sorta "block" out the steps for me? I have already tried several approaches on my machine but they all take too long to come back with a properly filled in board quickly (yes my machine is fast enough).

Thank you fourm for your kindness, understanding and help.

eibwenbv
Oct 9 '07 #6
kadghar
1,295 Expert 1GB
...
1) I am running an older version of VB (version 4, heck I got it for free!)
2) I am new to coding
VB 4?!! yeah!! i loved that version, it was the first one i had.

And i apologize if im being confusing.

What im saying is that if you try to randomly generate an array with 1 to 9 x 1 to 9 numbers, it will be harder to make it a sudoku board. So what you should do is to generate a "basic board" and then swap some rows and columns, but swap them in a way that it'll keep the "sudoku's properties".

a basic sudoku board will be something like this

Expand|Select|Wrap|Line Numbers
  1.  F G            HI
  2. 123    456    789
  3. 456    789    123
  4. 789    123    456
  5.  
  6. 234    567    891
  7. 567    891    234       A
  8. 891    234    567       B
  9.  
  10. 345    678    912
  11. 678    912    345       D
  12. 912    345    678       E
And you can swap any column or any row in the same "block of three"
i.e. you can swap the whole row A with the B or D with E, but you cannot swap A with E.
Same, you can swap columns F and G but not F with H

And as you can see, you can replace every 1 with 3 and every 3 with 1 in the matrix... or every 9 with a 6, every 6 with a 4 and every 4 with a 9... it doesnt matter which number you have as long as they follow an order, so it will be nice that istead of creating this "original sudoku board" based in 123456789 sequence, use it as the "index map"

So, generate a 9x1 array with numbers 1 to 9 and use the "index map" e.g.

Generate a random array that could look like this

Arr1 = {4,7,5,6,8,2,1,9,3}

And the "basic board" will be (supose the indexes go from 1 to 9 instead of 0 to 8) i.e.

Expand|Select|Wrap|Line Numbers
  1. arr1(1)arr1(2)arr1(3)      arr1(4)arr1(5)arr1(6)     arr1(7)arr1(8)arr1(9)
  2. arr1(4)arr1(5)arr1(6)      arr1(7)arr1(8)arr1(9)     arr1(1)arr1(2)arr1(3)
  3. arr1(7)arr1(8)arr1(9)      arr1(1)arr1(2)arr1(3)     arr1(4)arr1(5)arr1(6)
  4. (..)
And so on, so it'll look like:

Expand|Select|Wrap|Line Numbers
  1. 745    682    193
  2. 682    193    745
  3. 193    745    682
  4.  
  5. 456    821    937
  6. 821    937    456
  7. 937    456    821
  8.  
  9. 568    219    374
  10. 219    374    568
  11. 374    568    219
And then make the swaps on this array.

That should be the main idea.

with a coulpe of arrays and some FOR or DO you should be able to do it, give it a try and if you have any further question just post it here.

HTH
Oct 9 '07 #7
Tig201
103 100+
where is that pesky subscribing button.
BTW Nifty Trick kadghar.
Oct 9 '07 #8
Wow, I am so sorry I asked. I am so still confused
I thought my arrays (in the code above) allowed me to put these numbers
in any order I wanted, in any group of three I wanted, row, column or box
I wanted. Am I wrong?


When these sudoku book publishers create puzzles, how do they do it?
I appreciate all you guys, if no more posts appear here, have the admin
remove my access.


thanks for your time
Oct 10 '07 #9
kadghar
1,295 Expert 1GB
Wow, I am so sorry I asked. I am so still confused
I thought my arrays (in the code above) allowed me to put these numbers
in any order I wanted, in any group of three I wanted, row, column or box
I wanted. Am I wrong?
I tried to run your code but it didnt asigned the Z to the array, so i assumed (sorry if i mistaked) that what you wanted to do is something like this:

Expand|Select|Wrap|Line Numbers
  1. Dim MyNum(9, 9) As Integer
  2.  For x = 1 To 9
  3.  For y = 1 To 9
  4. newZ:
  5. Randomize
  6. z = Int((9 * Rnd) + 1)
  7. If y = 1 Then GoTo getnewnum
  8.  For w = (y - 1) To 1 Step -1
  9.  If z = MyNum(x, w) Then GoTo newZ
  10.  Next w
  11. If x = 1 Then GoTo getnewnum
  12.  For v = (x - 1) To 1 Step -1
  13.  If z = MyNum(v, y) Then GoTo newZ
  14.  MyNum(x, y) = z
  15.  Next v
  16. getnewnum:
  17. Next y, x
Even then, the array will not be finished in most of the cases, this is because the probability of having a "free space" is decreasing.

lets say you have the 5x5 board and you are asigning the number 5 and your array is something like:

1 3 4 5 2
4 2 5 1 3
3 1 2 4 5
2 4 1 3 X

The only free space for that number is X but you cannot put there a 5 or any other number, then your code will enter into an infinite loop. That's why i recomended you to generate a table with one of the permutations of the problem and make columns and rows swaping. of course this method has many many limitations but will give you a solution after all.
Oct 10 '07 #10
Like I said, I am so sorry, I give up.

I just don't get it.

I know why it don't work

ADMIN: please remove my access from the forum

Thanks guys!

later
Oct 17 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: sub1ime_uk | last post by:
Thought I'd offer a method for solving all possible 9x9 sudoku puzzles in one go. It'll takes a bit of time to run however (and 9x9 seems to be about as big as is reasonably possible before...
5
by: Stewart Gordon | last post by:
I have a few Sudoku puzzles on my site. http://www.stewartsplace.org.uk/mindbenders/ But adding extra columns and rows to separate the 3x3 blocks seems a rather kludgy approach, and the result...
11
by: ago | last post by:
Inspired by some recent readings on LinuxJournal and an ASPN recipe, I decided to revamp my old python hack... The new code is a combination of (2) reduction methods and brute force and it is quite...
12
by: kalinga1234 | last post by:
hy guys i am having a problem with my sudoku program which i coded using c++.; currently in my program if a duplicate number exist in either row/column/block i would make the particualr square...
0
by: JosAH | last post by:
Greetings, a couple of years ago a large part of the world went totally mad. Not because of global climate changes, not because of terrible wars that were started in the Middle East, nor because...
21
by: ningxin | last post by:
Hi, i am currently taking a module in c++ in the university, and was given an assignment. because i have no prior background on the subject, everything is kind of new to me. i have tried for quite...
3
by: deanchhsw | last post by:
Hello, I'm trying to build a program that solves sudokus and prints out the result on the screen. Here's the code for the class SudokuBoard. this will later be called in a class Sudoku. I'm a newbie,...
1
by: deanchhsw | last post by:
Part A (http://bytes.com/topic/java/insights/645821-sudoku) B (http://bytes.com/topic/java/insights/739704-sudoku-b) C (http://bytes.com/topic/java/insights/739703-sudoku-c) this question refers...
3
by: DannyB13 | last post by:
Hi, and thanks for possible help in advance. Here's my dilemma. I've been making a sudoku generator, and I'm now stuck on one part. I must be able to take a 'solution' and verify that it is correct,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.