PDA

View Full Version : puzzle game in spe



largo_winch
09-08-2011, 13:00
hello. I am finished nearly completely this little puzzle button game. only these lines of programming into thinbasic aren't succesfull. I don't understand meaning of "CBCTL - 1". I think better to use an equal. perhaps petr or somebody else can check this little game example. 1)
If CBMSG = %WM_COMMAND And CBCTLMSG = %BN_CLICKED Then If CBCTL-1 >= %BUTTONSTARTS Then Control Get Text CBHNDL, CBCTL-1 To ButnLabel 'Control Get Text CBHNDL, %NUMBEROFBTN-1 To ButnLabel If ButnLabel = "0" Then MoveButton CBHNDL,CBCTL - 1,CBCTL 'MoveButton CBHNDL,%NUMBEROFBTN- 1,%NUMBEROFBTN End If End If 2) other problemzone: the random Numbers don't working correct, there are too many "1" numbers (values).
3) the task: to get all numbers in correct order of increasing (1,2,3,4,5,6...) side by side :)
Info: click buttons besides empty place to move the button-numbers. example I add as tbasic file.


4) new EDIT:
a) new version of my "puzzle-gui-game (15 bricks)" I send as attachement (tbasic file).
b) the number of bricks you've clicked you can see in saved file.
c) "end game" factor with 90 clicks!

bye, largo

Petr Schreiber
09-08-2011, 13:28
Hi,

thanks for nice game, it is also one of those frequently used to measure efficiency of AI algorithms :)

1)
This "problem" is related to how you designed the control ID generation. I would suggest to hold the state of the board in globally accessible NxN matrix (2D array) and perform queries there instead of looking at the controls directly.
Once you perform the move, you can update the board in one go, but it is good idea to keep the main game code UI independent.

2)
RND is not documented to never repeat the number. If you need to randomize set of number 1..n, the better choice is to take advantage of STAT module and ARRAY SHUFFLE:


USES "STAT"

...
Dim MyNotRepeatingSequence(NumberButtons) As Long

' -- This will fill the array with values: 1, 2, ..., NumberOfButtons
Stat_FillArray(MyNotRepeatingSequence,%STAT_FILL_NATURAL)

' -- This will randomize their order, 10 times
Array Shuffle MyNotRepeatingSequence() For 10




Petr

largo_winch
09-08-2011, 19:45
hi petr. thanks for the tip with "stat" module. these lines are responsible to get the numbers in correct order of increasement. In my example I wanted to have a random variety for 36 numbers over the board.

this new example shows only 35 numbers all in correct order with using "stat" module. however the number "1" appears for two times what I didn't wanted. I think there must be an easy solution for all random numbers (1-36 numbers). I did it just for fun with "stat" fill and array sort. there must be missing other thing I am still looking for.



Sub RandomTheNumbers(ByVal hDlg As DWord) '******************************************************************************
Local i As Long
Local NumBtns As Long
Local Btn As Long, nTimes As Long
Dim BtnId(%NUMBEROFBTN*%NUMBEROFBTN-1) As Long
Dim Board(%NUMBEROFBTN*%NUMBEROFBTN-1) As Long
Dim BoardTmp(%NUMBEROFBTN*%NUMBEROFBTN-1) As Long

NumBtns = %NUMBEROFBTN*%NUMBEROFBTN-1

For i = 1 To %NUMBEROFBTN*%NUMBEROFBTN-1
BtnId(i) = i Next '-------------------------------------------------------> ? nearly perfect ? ---->

For i = 1 To %NUMBEROFBTN*%NUMBEROFBTN-1
Btn = Rnd(1,NumBtns) '
Board(i)=BtnId(Btn)
' -- This will fill the array with values: 1, 2, ..., NumberOfButtons '
'Array Shuffle myData() For 36 '
Array Shuffle BtnId(Btn) For 36 'nTimes+%NUMBEROFBTN
'Stat_FillArray(myData,%STAT_FILL_NATURAL)
Stat_FillArray(Board,%STAT_FILL_NATURAL)

'Array Shuffle myData() For 36 '
Array Shuffle BtnId(Btn) For 36 'nTimes+%NUMBEROFBTN

'Stat_FillArray(myData,%STAT_FILL_NATURAL)
Decr NumBtns Next '-------------------------------------------------------> ? nearly perfect Ends ? ---->

'Stat_FillArray(myData,%STAT_FILL_NATURAL) 'Decr myData(%NUMBEROFBTN)
'MAT BoardTmp() = Board()
'MAT Board() = myData

For i = 1 To %NUMBEROFBTN*%NUMBEROFBTN-2'
Control Set Text hDlg,%BUTTONSTARTS+i,Str$(Board(i))
Control Show State hDlg,%BUTTONSTARTS+i,%SW_SHOW
Next
Control Set Text hDlg,%BUTTONSTARTS+%NUMBEROFBTN*%NUMBEROFBTN-1,"0"
Control Show State hDlg,%BUTTONSTARTS+%NUMBEROFBTN*%NUMBEROFBTN-1,%SW_HIDE'
PlayNum = 0
Dialog Set Text hDlg, "puzzle-button-move-click"
End Sub

"stat_random" command doesn't fit for my purpose, has checked it. If you have another solution for random factor for all 36 numbers in unregularly order (for example: 3, 18, 24, 35, 5, 15, 21, 6...)am happy to see or an advice

Info: click buttons besides empty place to move the button-numbers.

bye, largo

Petr Schreiber
09-08-2011, 20:10
Hi Frank,

it is very simple, the STAT_ArrayFill and ARRAY SHUFFLE make the code shorter, see for yourself here, I added comments so you can see what is going on:


Sub RandomTheNumbers(ByVal hDlg As DWord)

Local i, NumberOfVisibleButtons As Long

' -- This is our board, initally filled with zeros
NumberOfVisibleButtons = %NUMBEROFBTN*%NUMBEROFBTN-1
Dim Board(NumberOfVisibleButtons) As Long

' -- This fills it in linear fashion, like:
' -- Board(1) = 1
' -- Board(2) = 2
' -- ...
' -- Board(NumberOfVisibleButtons) = NumberOfVisibleButtons
Stat_FillArray(Board,%STAT_FILL_NATURAL)

' -- But we don't want to have it linear!
' -- So we randomize the order of elements in array
Array Shuffle Board()

' -- This updates the play pieces
For i = 1 To NumberOfVisibleButtons
Control Set Text hDlg,%BUTTONSTARTS+(i-1),Format$(Board(i))
Control Show State hDlg,%BUTTONSTARTS+(i-1),%SW_SHOW
Next

' -- This sets the 0 piece
Control Set Text hDlg,%BUTTONSTARTS+NumberOfVisibleButtons,"0"
Control Show State hDlg,%BUTTONSTARTS+NumberOfVisibleButtons,%SW_HIDE'

' --
PlayNum = 0
Dialog Set Text hDlg, "puzzle-button-move-click"

End Sub



Petr

largo_winch
10-08-2011, 06:57
hello petr, many thanks for new "randomthenumbers" part! I can follow your explanations with "stat" module. Very good work, mr. detective ;) A saying that applies says: "Near enough is not good enough." But you are very close: Frank is my uncle. He directed me some month ago to thinbasic forum. While I stayed at him for holidays in spring time and saw a lot of his work and this active forum I entered to thinbasic programming language. bye, largo

Petr Schreiber
10-08-2011, 08:42
Hehe,

now I see that. My apologies, it has to be something subconscious :)
Say hello to your uncle!


Petr

Petr Schreiber
12-08-2011, 14:06
Thanks for the update, how long do you click before you get the correct order, on average? :)


Petr

largo_winch
15-08-2011, 10:45
hello. you need for the 15 puzzle around 80-90 clicks to be perfect. I managed this game with 92 clicks last week but didn't save it (file save command I did at later moment). I add new version with "game end" modus at 90 clicks. try it! :) see first post! bye, largo