[Solved] Solving the Sudoku Puzzle using OO BASIC Calc macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

[Solved] Solving the Sudoku Puzzle using OO BASIC Calc macro

Post by OldStd »

30/10/2018. By OldStd
  • I changed the topic title so that I can put the word [Solved], as I have found a complete solution to the Sudoku puzzle using only the OO BASIC CALC macros.

    I couldn't place a word [Solved] to the topic because [Learning to program in OpenOffice Basic] is ongoing and never ending process, Sudoku, or otherwise.
I am quite new to learning how to program in OpenOffice Basic.
I always feel that having a pet project would make learning easier as the project forces us to dig for items that help to solve some problems at hand.
  • I. Recently, a childhood classmate of mine told me he is playing Sudoku games everyday.
    • I.1. I am now wondering whether I could use the OpenOffice to make some sort of [Sudoku solver] that can at least solve some easy to medium Sudoku games?
      • I.1.1. Any suggestions from anyone as to what application to use: writer, calc, base, or impress?
        I.1.2.What do I need to learn to start the project going?
I first posted this in another forum and was advised to open a new topic on Sudoku.
Since then I had gone through some websites and their attached codes. However, as the codes were written in C, and the explanation done in non-English, I was unable to comprehend what is being done.
One idea did catch my attention: that of back-tracking when the application gets stuck.
I have yet to figure out how to do this back-tracking in codes.
Any further examples in back-tracking explained in English would be much appreciated.
From Ziz46's suggested site, I have downloaded Sudoku 0.5.5 extension and installed the extension, only to realise that I don't even what an extension is, and how to use the extension manager.

Zizi46 suggested a couple of sites to start me off.
https://extensions.openoffice.org/en/project/sudoku
v ... =5&t=89193
My search in the OO Basic forum yielded the following sites:
viewtopic.php?f=9&t=89193&p=420482&hilit=sudoku#p420482
viewtopic.php?f=9&t=89193&p=420432&hilit=sudoku#p420432
viewtopic.php?f=9&t=89193&p=420291&hilit=sudoku#p420291
viewtopic.php?f=9&t=89193&p=420285&hilit=sudoku#p420285
viewtopic.php?f=9&t=89193&p=420253&hilit=sudoku#p420253
viewtopic.php?f=9&t=40025&p=184040&hilit=sudoku#p184040
https://extensions.openoffice.org/project/sudoku1
viewtopic.php?f=7&t=31971&p=146120&hilit=sudoku#p146120
viewtopic.php?f=7&t=31971&p=146116&hilit=sudoku#p146116
viewtopic.php?f=7&t=31971&p=146104&hilit=sudoku#p146104
Last edited by OldStd on Tue Oct 30, 2018 4:34 pm, edited 2 times in total.
Openoffice 4.15
Windows 10
John_Ha
Volunteer
Posts: 9583
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Learning to program in OpenOffice Basic through Sudoku

Post by John_Ha »

I doubt it will be easy to program a Sudoku solver in OpenOffice Basic though no doubt it will be possible and you will learn much.

OO Basic is used to write office applications like a word processor. Similarly, a hammer is used to hammer nails and a knife is used to spread butter. If you want, you can spread butter with a hammer and bash nails in with a knife but ...

You ideally need a code which allows recursion to do the backtracking - it is then all taken care of in the program without you doing very much. I don't know if OOBasic supports recursion.

See http://www.sudokuwiki.org/ - at one time Andrew Stuart published a PDF on how his solver worked and how he graded his puzzles.

See Macros and UNO API, OpenOffice.org BASIC Programming Guide and Andrew Pitonyak's OpenOffice Macro Information. I think [Tutorial] Introduction into object inspection with MRI will be useful too.
Last edited by John_Ha on Thu Oct 11, 2018 5:29 pm, edited 1 time in total.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Learning to program in OpenOffice Basic through Sudoku

Post by RoryOF »

This is like learning a language and at the same time writing the Great XXXX Novel in that language. I suggest you concentrate on learning one programming language, outside of OO - something such as Python - a good book on this at
A Byte of Python

Having learned Fortran 2 and progressed to BASIC, with excursions through C, Pascal, Modula, Java, I advise against learning a procedural language first, hence my suggestion of Python.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Dear John_Ha
Thank you for your info and advice.
You have given me so many URL's I need to go through them first, before I can make head and tails out of them.
I had downloaded and installed MRI but haven't found out how to use the MRI yet.
I like your hammer and butter knife metaphor.
I hope I would be able to write a program in OO Basic to solve the Sudoku, someday!
Best regards, and wish me luck!
Last edited by OldStd on Fri Oct 12, 2018 5:10 pm, edited 1 time in total.
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Dear RoryOF.
Thank you for your advice on procedural language vs other language. I started learning programming by recording macros in Microsoft Excel and then buying a book in VBA for Excel. A while back, I had taken some Introductory course in Python from a free online course.
I have recently downloaded some eBooks from the web on OO Basic programming and haven't really gone through too much of the reading yet.
I understand your metaphor on learning a new language and trying to write a novel in it.
You remind me of the story of a famous NBA coach who started his training session with the champion national basketball team players:
“Gentlemen: this is a basketball, ….”
Over here, it is 03:03 am. It is well past my bedtime as I was watching a YouTube series on “New Shanghai Beach” …
I'll read up the materials and see what I can make of them in the new few days.

Best regards
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Hi. Zizi46, John_Ha, RoryOF, and everyone.

It occurs to me that most of the posts in Sudoku in the OO forum are found in the [Calc Forum], and so I think it might be easier to program with Calc. I saw something that look like matrices in some of the codes I saw. As Matrix looks awfully difficult in OO, I will settle to using the Arrays.

Here are my codes for
1. Copying the manual entries of a sample Sudoku game I saw in one author into an array.
2. Duplicating the same Sudoku game from the array back to another worksheet.
3. How the cells in the game table template are named: Top to bottom, left to right. 0 based. 0 to 80 for the 81 cells.

Code: Select all

REM  *****  BASIC  *****
option explicit

Dim Doc As Object
Dim Sheet As Object
Dim Cell As Object  
Dim   MyArray(149) as string

function CopyFromCellRangeToArray
dim i, j, k as integer
Doc = ThisComponent
Sheet = Doc.Sheets(0)
	for i = 0 to 8
		for j = 0 to 8
			if i =0 and j = 0 then
				k = 0
			else 
				k = k + 1 
			end if		
			Cell = Sheet.getCellByPosition(j,i)
			MyArray(k)= Cell.string
		next j
	next i
	CopyFromCellRangeToArray = MyArray()
	Msgbox "Cell(78) = " & MyArray(78)
end function

Sub DuplicatedFromArrayToCellRange
dim i,j,k as integer
MyArray = CopyFromCellRangeToArray
Sheet = Doc.Sheets(1)
	for i = 0 to 8
		for j = 0 to 8
			if i =0 and j = 0 then
				k = 0
			else 
				k = k + 1 
			end if		
			Cell = Sheet.getCellByPosition(j,i)
			Cell.setstring(MyArray(k))
		next j
	next i
end sub

sub NamingCells
dim i,j,k as integer
Doc = ThisComponent
Sheet = Doc.Sheets(2)
	for i = 0 to 8
		for j = 0 to 8
			if i =0 and j = 0 then
				k = 0
			else 
				k = k + 1 
			end if		
			Cell = Sheet.getCellByPosition(j,i)
			Cell.setstring(CStr(k))
		next j
	next i
end sub

Attachments
Sudoku_001.ods
First glimpse
(13.76 KiB) Downloaded 235 times
Last edited by OldStd on Fri Oct 12, 2018 5:12 pm, edited 1 time in total.
Openoffice 4.15
Windows 10
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Learning to program in OpenOffice Basic through Sudoku

Post by RoryOF »

It might be best to distance yourself from the low level code and concentrate instead on the algorithm you will use to solve the Suduko problem. Once you have the algorithm writing the code is relatively trivial in any language.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Hi. RoryOF.
Thanks for the advice.
As I am so new to the programming in OO Basic, that even to write these few lines of codes, I have to look up the syntax at the forum and elsewhere in the Google search.
I haven't even done much reading in the basic guide yet.
Just a few days ago, all I knew was to record a macro to learn the syntax and terms used in OO.
Hopefully, I will come to the stage where I can concentrate on the algorithm, and that, too, is a big word for me for the time being. :D
Last edited by OldStd on Fri Oct 12, 2018 5:14 pm, edited 1 time in total.
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Hi. RoryOF.
Responding to your suggestion on concentrating on the [algorithms], I have used [Brute-force] approach before but I have just come across the term [Depth First Search].
At first glance, I can't quite understand the algorithm, especially, how to record the list of options available, how to mark the option as visited but failed, and how to go back to use the option not visited.
If you have some code snippets on this algorithm, it would be much appreciated.
Best regards.
Openoffice 4.15
Windows 10
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Learning to program in OpenOffice Basic through Sudoku

Post by RoryOF »

A very quick search found this
Mathematics and Sudokus
which seems to lead to presentation and discussion of various algorithms for solution of Sudoku puzzles.

I'm currently involved in research for and writings set in the 1812-30 and 1946 periods (before Sudoku was invented?) so am not giving any thought to programming at present.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Hi. RoryOF.
Thank you for your link and info.
It is rather interesting. I may want to try some of the algorithms mentioned there.
Just wondering how you could come across this kind of document in your search and I couldn't?
Do you have any special way of searching or perhaps using some kind of search tool I have never heard of?
Maybe it is one of your research tools?
Openoffice 4.15
Windows 10
John_Ha
Volunteer
Posts: 9583
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Learning to program in OpenOffice Basic through Sudoku

Post by John_Ha »

See Sudoku Solver using Recursive Backtracking which gives a Java program using recursion which is about 60 or 70 lines long.

The recursion occurs because the solveSudoku() method calls itself - that is why backtracking is so easy and why the code is so short.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Hi. John_Ha.
Thanks for the link.
I'll go through the codes and see if I can adapt them into the OO Basic program.
Best regards.
Openoffice 4.15
Windows 10
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Learning to program in OpenOffice Basic through Sudoku

Post by RoryOF »

OldStd wrote:Hi. RoryOF.
Thank you for your link and info.
It is rather interesting. I may want to try some of the algorithms mentioned there.
Just wondering how you could come across this kind of document in your search and I couldn't?
Do you have any special way of searching or perhaps using some kind of search tool I have never heard of?
Maybe it is one of your research tools?
I use Google, as available to all. The trick is just the selection of keywords - one needs to pick the key terms, but not to overspecify.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
John_Ha
Volunteer
Posts: 9583
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Learning to program in OpenOffice Basic through Sudoku

Post by John_Ha »

RoryOF wrote:I use Google, as available to all. The trick is just the selection of keywords - one needs to pick the key terms, but not to overspecify.
... and is a skill we all need these days!
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Hi. RoryOF and John_Ha.
Thank you for your responses.
You two have proven to be a better Google searcher than I am.

Hi. John_Ha.
I took a look at the Java codes. I can't quite understand Java OOP language on class, etc.
I guess it is the recursive call to the function that does the tricks, while I don't understand how it goes back to the original cell and do a re-assignment of value.
Looking at the codes again, I think I probably need to use the matrix instead of the array to do the job.
Like it or not, I have to go back to learn matrix in OO Basic.
I can't find the topic on matrix in the few ebooks I have download. However, searching on Google, I could find some materials on matrix.
I'll see what will come out of the new searches in the basic knowledge on how to use the matrix to program.
Thanks and regards.
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Hi. Zizi46, John_Ha, RoryOF, and everyone.

I still find the recursive, or depth-first, approach a bit difficult to understand and therefore I will stick to the lower level methods of using pencil and paper as suggested in the book by Crook.
https://www.ams.org/notices/200904/tx090400460p.pdf

Trying towards this direction, I have so far done the following steps:
  • 1 Copied the sample Sudoku game data into an array.
    2 Formulated functions to extract from the array
    • 2.1 a row (9 in total)
      2.2 a column (9 in total)
      2.3 a block of 3 x 3 cells (9 in total)
    3 Filled up all the blank cells with no data with [123456789]
The next steps will be
  • 1 To eliminate from the filled cells with the 9 digits those digits that occur in the same row, same column, or same block of the selected cell.
    2 Repeat the process until no more can be eliminated.
    3 Next, look for cells in the row, or column, or block that contain some common digits and eliminate those common digits from all other cells outside the cells in the respective rows, columns,block.
    4 After these, I'll find out what other steps to learn and apply.
I took a long time to learn the arrays as I can't find much materials on the topic apart from one or examples in the basic section of the OO guide.

I feel that I am making some progress.

Codes are attached in the Calc workbook for those who are free to comment.

Thanks and regards.
Attachments
Sudoku_002.ods
For your reading pleasure and comments
(14.12 KiB) Downloaded 235 times
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Hi. Everyone.

I have succeeded in getting the Sudoku games from Table 1 (the given game) to Table 2 by applying only removing the digits repeated.

However, this method now gets stuck after only 2 rounds of the operations. Number of [clues], single digits in a cell, has increased from 24 clues to 26 clues. The digits [6] and [5] colored red in the image are the results of this method.

Stage 2 method involved in removing a subset of digits from another cell in the same row, or column, or block of 3x3 cells.

For example, in the block in the center, [79] can be removed from [2379] resulting in the cell now having digits of [23] only. Similarly, [39] can be removed from [239], resulting in increasing a clue [2]. As the 2 cells are in the same block, [3] can be removed from 23, resulting in another [clue], [3].

I have yet to figure out how to do this task in codes.

In the meantime, I am looking up material on how to use the XRAY extension.

Regards.
Attachments
Don't know how to insert this in the message
Don't know how to insert this in the message
Openoffice 4.15
Windows 10
User avatar
robleyd
Moderator
Posts: 5055
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Learning to program in OpenOffice Basic through Sudoku

Post by robleyd »

Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.1.2; SlackBuild for 24.2.1 by Eric Hameleers
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Learning to program in OpenOffice Basic through Sudoku

Post by RoryOF »

MRI documentation at
https://github.com/hanya/MRI/wiki

This chapter may be helpful in actually applying MRI
https://fivedots.coe.psu.ac.th/~ad/jlop ... mining.pdf
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Can't be that easy.
Can't be that easy.
Hi. robleyd.
Hi. RoryOF.

Thank you for your links.

I'll follow up on these in order to learn how to use MRI.
For example, in the block in the center, [79] can be removed from [2379] resulting in the cell now having digits of [23] only. Similarly, [39] can be removed from [239], resulting in increasing a clue [2]. As the 2 cells are in the same block, [3] can be removed from 23, resulting in another [clue], [3].
Unfortunately, I had not understood the method introduced by Crook. As I tried the idea, in a fraction of seconds, the entire sheet is filled with single digits in every cell, with some blocks of 3x3 correct, but in some other blocks, rows, or columns, digits are repeated, and hence violating the rules of Sudoku.
Openoffice 4.15
Windows 10
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Learning to program in OpenOffice Basic through Sudoku

Post by Zizi64 »

In the meantime, I am looking up material on how to use the XRAY extension.

Code: Select all

Sub SomeSub
dim...

rem Load Xray:
	If (Not GlobalScope.BasicLibraries.isLibraryLoaded("XrayTool")) Then
		GlobalScope.BasicLibraries.LoadLibrary("XrayTool")
	End If


...here you get the object variable oMyObject somehow...

rem Examine oMyObject with the xraytool:
	xray oMyObject



ens sub
viewtopic.php?f=20&t=54217
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Thank you Tibor.

I didn't realize we have to load the library first.
I'll read up the materials and try it out later.

Thanks and regards
Openoffice 4.15
Windows 10
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Learning to program in OpenOffice Basic through Sudoku

Post by Zizi64 »

I didn't realize we have to load the library first.
You can load it manually too: just click on the library icon in the IDE.
(When you can edit it, that means: it has loaded, and it can work)
loadxray.png
loadxray.png (11.32 KiB) Viewed 8451 times
When the library icon is yellow, then the xray is in the memory.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Hi. Tibor.

Thanks for the info.

I copied your line of codes into a test macro carrying codes snippet copied from the web and run it.
I checked the icon of Xray, and it is now yellow.

I need to play around with it a bit more to see how it actually works.

As for the Sudoku application, I am not so concerned about getting it fully functional but more so to learn some OO Basic codes along the way.

It appears that I am mainly making use of the [arrays] in this project.

The arrays in OO seem to be quite different from what I saw in some other languages.

For example, I don't even find anything that describe the length of the array, such as Arr.length.

In the present stage of development, I have removed the unwanted digits from cells until it doesn't work any more. The next thing to do, as I imagine it, would be to choose one cell containing 2 digits and insert one as a new clue, save the other in an array for future use if the process gets stuck down the line, and save the current configuration of data in another array that can be retrieved.

This means, I need to access the last element in an array, or append an item at the bottom of an array.

Right now I don't know how to access the last element, as I don't the actual number, other than using the Ubound of an empty array? Even that I don't find any example on how to apply it. Where do I find any reference for this basic knowledge of syntax of arrays?

Another approach is not to save anything, but apply a random choice between the 2 or 3 digits, and if it doesn't work, start all over again, and because of the random choice, it eventually will hit on a combination that works. I have to learn to use the random function, yet.

Attached is [work-in-progress] of the project. Perhaps, I'll use it to test out how to use the Xray later.

Best regards
Attachments
Sudoku_003.ods
Program gets stuck after 6 rounds and 9 added clues
(14.97 KiB) Downloaded 211 times
Openoffice 4.15
Windows 10
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Learning to program in OpenOffice Basic through Sudoku

Post by Zizi64 »

For example, I don't even find anything that describe the length of the array, such as Arr.length.
...
Where do I find any reference for this basic knowledge of syntax of arrays?
https://wiki.openoffice.org/wiki/Docume ... ide/Arrays

Code: Select all

MsgBox(LBound(MyArray)) 
MsgBox(UBound(MyArray))
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Learning to program in OpenOffice Basic through Sudoku

Post by Zizi64 »

Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

Thanks for the links.

Yes. This would be the syntax of arrays if I want to save a copy of the [work-in-progress] Sudoku table in an array for future retrieval.
Sub Create_Array_of_Arrays(i,k)
Dim aArray(i) as variant
for j = 0 to i
aArray(j) = dimArray(k)
next j
xray aArray
End Sub
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by OldStd »

My algorithm to solve the Sudoku puzzle
My algorithm to solve the Sudoku puzzle
The attachment My Algorithm.png is no longer available
Testing out Step 3. Part 3

Hi. Ziz64 and everyone.

With reference to the diagram on my algorithm to solve the Sudoku game, I am now working on Step 3 (part 3)
Saving the other digit, together with the cell number and the current array of strings of digits
Thanks to Zizi64, for his links, I have found out how to do these, by performing the following tests with codes

I think I am on track. [Eating a whale, one bite at a time]

By the way, I am looking for the equivalent of [collections], [collection] but can't find them. Can anyone show me where to find [collections] in OO BASIC?

Code: Select all

sub test2
dim oArr() as object   ''' An array to hold the saved cell number, cell value, and current array
dim nArr1(1) as integer   ''' a variable to save the current cell number
dim sArr2(1) as string   ''' a variable to save the remaining part of the cell value
dim sArr3(80) as string   ''' to mimic the current array being processed
dim oArrk() as object   ''' a worker array to hold the 3 items above.
dim i as integer

rem Current cell number
nArr1(0) = 28
rem Current cell value, a string of digits
sArr2 (0) = "357"
rem Not exactly the current array under processing
rem The following array is just demo to mimic an array to be saved
rem In actual program, this is the current array being processed
for i = 0 to 80
sArr3(i) = CStr(i)
next i

rem Inserting items into the arrays designed
AddItemToArray(oArrk,nArr1)
AddItemToArray(oArrk,sArr2)
AddItemToArray(oArrk,sArr3)
AddItemToArray(oArr, oArrk())

rem How to access the data from the array holding the saved data

rem To retrieve the saved cell number
msgbox oArr(0)(0)(0)
rem To retrieve the saved remaining cell value (for future use)
msgbox oArr(0)(1)(0)
Rem To retrieve the saved array
msgbox oArr(0)(2)(1)

end sub


Sub AddItemToArray(xArray, vNextElement)
Dim iUB%,iLB%
iLB = lBound(xArray())
iUB = uBound(xArray())
If iLB > iUB then
iUB = iLB
redim xArray(iLB To iUB)
else
iUB = iUB +1
redim preserve xArray(iLB To iUB)
endif
xArray(iUB) = vNextElement
End Sub

Sub removeItemFromArray(xArray())
Dim iUB%,iLB%
iLB = lBound(xArray())
iUB = uBound(xArray())
iUB = iUB - 1
redim preserve xArray(iLB To iUB)

End Sub
Openoffice 4.15
Windows 10
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Learning to program in OpenOffice Basic through Sudoku

Post by JeJe »

Collection Object example:

viewtopic.php?f=20&t=61325
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply