Join 137,267 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,497 people online right now. Registration is fast and FREE... Join Now!
On a boardgame with 16 empty square(4x4 and columns) the program shall randomly place out 15 numbers from 1 to 15. 1 square will be empty. The user shall then be able to move 1 number asing to the empty square, only to to one of the numbers that are closest - in a way so that the squaren will in the end be sorted 1 to 15 with empty box furthest down to right.
I did 16 buttons with random numbers( meaning 15 buttons have numbers and one is empty with text) and then i did lots of If- functions, and a if function that gives a msgbox if you lay out 1-15 in a correct order.
Like this
CODE
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Randomize()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Button2.Text = "" Then Button2.Text = Button1.Text Button1.Text = "" End If If Button5.Text = "" Then Button5.Text = Button1.Text Button1.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If Button1.Text = "" Then Button1.Text = Button2.Text Button2.Text = "" End If If Button3.Text = "" Then Button3.Text = Button2.Text Button2.Text = "" End If If Button6.Text = "" Then Button6.Text = Button2.Text Button2.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click If Button2.Text = "" Then Button2.Text = Button3.Text Button3.Text = "" End If If Button4.Text = "" Then Button4.Text = Button3.Text Button3.Text = "" End If If Button7.Text = "" Then Button7.Text = Button3.Text Button3.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click If Button3.Text = "" Then Button3.Text = Button4.Text Button4.Text = "" End If If Button8.Text = "" Then Button8.Text = Button4.Text Button4.Text = "" End I If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End iff End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click If Button1.Text = "" Then Button1.Text = Button5.Text Button5.Text = "" End If If Button6.Text = "" Then Button6.Text = Button5.Text Button5.Text = "" End If If Button9.Text = "" Then Button9.Text = Button5.Text Button5.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click If Button2.Text = "" Then Button2.Text = Button6.Text Button6.Text = "" End If If Button10.Text = "" Then Button10.Text = Button6.Text Button6.Text = "" End If If Button7.Text = "" Then Button7.Text = Button6.Text Button6.Text = "" End If If Button5.Text = "" Then Button5.Text = Button6.Text Button6.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click If Button3.Text = "" Then Button3.Text = Button7.Text Button7.Text = "" End If If Button6.Text = "" Then Button6.Text = Button7.Text Button7.Text = "" End If If Button8.Text = "" Then Button8.Text = Button7.Text Button7.Text = "" End If If Button11.Text = "" Then Button11.Text = Button7.Text Button7.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click If Button4.Text = "" Then Button4.Text = Button8.Text Button8.Text = "" End If If Button7.Text = "" Then Button7.Text = Button8.Text Button8.Text = "" End If If Button12.Text = "" Then Button12.Text = Button8.Text Button8.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click If Button5.Text = "" Then Button5.Text = Button9.Text Button9.Text = "" End If If Button10.Text = "" Then Button10.Text = Button9.Text Button9.Text = "" End If If Button13.Text = "" Then Button13.Text = Button9.Text Button9.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click If Button9.Text = "" Then Button9.Text = Button10.Text Button10.Text = "" End If If Button6.Text = "" Then Button6.Text = Button10.Text Button10.Text = "" End If If Button11.Text = "" Then Button11.Text = Button10.Text Button10.Text = "" End If If Button14.Text = "" Then Button14.Text = Button10.Text Button10.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click If Button10.Text = "" Then Button10.Text = Button11.Text Button11.Text = "" End If If Button12.Text = "" Then Button12.Text = Button11.Text Button11.Text = "" End If If Button15.Text = "" Then Button15.Text = Button11.Text Button11.Text = "" End If If Button7.Text = "" Then Button7.Text = Button11.Text Button11.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click If Button11.Text = "" Then Button11.Text = Button12.Text Button12.Text = "" End If If Button16.Text = "" Then Button16.Text = Button12.Text Button12.Text = "" End If If Button8.Text = "" Then Button8.Text = Button12.Text Button12.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click If Button14.Text = "" Then Button14.Text = Button13.Text Button13.Text = "" End If If Button9.Text = "" Then Button9.Text = Button13.Text Button13.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click If Button13.Text = "" Then Button13.Text = Button14.Text Button14.Text = "" End If If Button15.Text = "" Then Button15.Text = Button14.Text Button14.Text = "" End If If Button10.Text = "" Then Button10.Text = Button14.Text Button14.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click If Button16.Text = "" Then Button16.Text = Button15.Text Button15.Text = "" End If If Button11.Text = "" Then Button11.Text = Button15.Text Button15.Text = "" End If If Button14.Text = "" Then Button14.Text = Button15.Text Button15.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click If Button15.Text = "" Then Button15.Text = Button16.Text Button16.Text = "" End If If Button12.Text = "" Then Button12.Text = Button16.Text Button16.Text = "" End If If button1.text=1 and button2.text=2 and button3.text=3 and so on till button16.text="" Then msgbox("You have won gratz") End if End Sub
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click Me.Dispose()
End Sub End Class
But now I am stuck, I have no idea how I supposed to randomize the numbers to the buttons and how to randomize the button with the no text at all, and therfore I ask you guys and i appreciate all the help I can get. The lenght of the code doesnt matter, the only thing important is that it works and that it has beginners code. Please help, just get me back on track .
You could have made things so much easiar by using a control array for your buttons... You could then do something like.
CODE
Dim i As Integer Randomize For i = 1 To 15 button(Rnd(15)+1).caption = CStr(Rnd(15)+1)) Next i
Actually i do not understand how would that help a newb. (S)he would have to make a event handler to capture the click event, and that is far to complicated for him to understand. (Opinion is based on the fact (s)he did not solve it).
So, 1 variant of the code would look something like this:
CODE
Public Class Form1
Private Sub Form1_Load(...) Handles MyBase.Load dim väärtused$,i%,a(15)$ Randomize() väärtused=",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16" For i = 0 To 15 do kala=väärtused sala=int(Rnd(15)+1)) loop while väärtused<>replace(kala,sala,"*")
väärtused=replase(väärtused,"," & sala,"") a(i)=sala Next i
a(int(Rnd(15)+1))="Tühi" For i = 0 To 15 button1(i).caption = a(i) Next i End Sub private sub button1(0)_Click(...) call jagaja(1) end sub private sub button1(1)_Click(...) call jagaja(2) end sub '*** private sub button1(14)_Click(...) call jagaja(15) end sub private sub button1(15)_Click(...) call jagaja(16) end sub
sub jagaja(nr) on error resume next dim saab_liikuda as boolean dim j%.i% saab_liikuda=true if button1(nr-1)="Tühi" then saab_liikuda=true:button1(nr-1)=nr:button1(nr)="" if button1(nr+1)="Tühi" then saab_liikuda=true:button1(nr+1)=nr:button1(nr)="" if button1(nr-4)="Tühi" then saab_liikuda=true:button1(nr-4)=nr:button1(nr)="" if button1(nr+4)="Tühi" then saab_liikuda=true:button1(nr+4)=nr:button1(nr)=""
if saab_liikuda=true then for i=0 to 15 if val(button1(i).caption)=i then j=j+1 next i if j=15 then msgbox("Võitsid :P") else msgbox("Viga") end if end sub End Class
This post has been edited by m2s87: 18 Dec, 2006 - 11:12 AM
this does make a lot of sense, and i understand it alla but i cant get it to work and thank you for helping me.
If Button1(nr - 1) = "Tühi" Then saab_liikuda = True : Button1(nr - 1) = nr : Button1(nr) = ""
i get an error on button1 = Error 4 Class 'System.Windows.Forms.Button' cannot be indexed because it has no default property
I't was a quiky, without a debug. Now this is debuged in vb 6 (classic) and working. If i'm not mistaken vb .net does not have a default value set to make object arrays of button. To make 1 you need to make a reference to it, or use the simple x16 times diversion to the divader protsedure like shown before. The code that is working in vb 6:
CODE
Private Sub Command1_Click(Index As Integer) Dim saab_liikuda As Boolean Dim j%, i% saab_liikuda = False
If (Index + 4) Mod 4 > 0 Then If Command1(Index - 1).Caption = "Tühi" Then saab_liikuda = True Command1(Index - 1).Caption = Command1(Index).Caption Command1(Index).Caption = "Tühi" End If End If If (Index + 1) Mod 4 > 0 And saab_liikuda = False Then If Command1(Index + 1).Caption = "Tühi" Then saab_liikuda = True Command1(Index + 1).Caption = Command1(Index).Caption Command1(Index).Caption = "Tühi" End If End If If Index > 3 And saab_liikuda = False Then If Command1(Index - 4).Caption = "Tühi" Then saab_liikuda = True Command1(Index - 4).Caption = Command1(Index).Caption Command1(Index).Caption = "Tühi" End If End If If Index < 12 And saab_liikuda = False Then If Command1(Index + 4).Caption = "Tühi" Then saab_liikuda = True Command1(Index + 4).Caption = Command1(Index).Caption Command1(Index).Caption = "Tühi" End If End If
If saab_liikuda = True Then For i = 0 To 15 If Val(Command1(i).Caption) = i Then j = j + 1 Next i If j = 15 Then MsgBox ("Võitsid :P") Else MsgBox ("Viga") End If End Sub Private Sub Form_Load() Dim väärtused$, i%, a(15) Randomize väärtused = ",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16" b = Split(väärtused, ",")
For i = 0 To 15 Do sala = Int(Rnd() * 16 + 1) DoEvents Loop While b(sala) < 1
b(sala) = 0 a(i) = sala Next i
a(Int(Rnd() * 16 + 1)) = "Tühi" For i = 0 To 15 Command1(i).Caption = a(i) Next i End Sub