# 15's game

• ### Question

• Someone could give me a hint how to build a simple 15's game ?
Thanks

Sunday, January 11, 2009 12:29 PM

• Here you are.
 '  Demo program for Small Basic ver 0.2 '  Writen by András FEHÉR 2009. 01. 13. '  Széchenyi István University, Department of Telecommunications '  Győr, HUNGARY 'size of graphics window GRsize=300 'number of Rows ans Coulums N=3 GraphicsWindow.Width=GRsize GraphicsWindow.Height=GRsize GraphicsWindow.Title="Game" GraphicsWindow.Show() 'size of grid dx= GRsize/N dy= dx 'size of  stones xs= dx - 15 ys= xs ' Grid draw For x=1 To N-1 GraphicsWindow.AddLine(x*dx,0,x*dx,GRsize) EndFor For y=1 To N-1 GraphicsWindow.AddLine(0,y*dy,GRsize,y*dy) EndFor 'set New values  of  Stone Array (A1) 1 .... N*N For i=1 to N For j=1 To N Array.SetValue("A1",(i-1)*N+j,(i-1)*N+j) EndFor Endfor ' one stone is emty: value of arryay = 0 Array.SetValue("A1",N*N,0) 'set initial Previous values  of  Stone Array (A0) 0.....0 For i=1 to N For j=1 To N Array.SetValue("A0",(i-1)*N+j,0) EndFor Endfor DebugArrays() 'draws Stones, if Previous different as New DrawPositionChanged() DebugArrays() GraphicsWindow.MouseDown = clik Sub boxdraw 'draws to (x,y)  nb number x0=x*dx-dx/2 y0=y*dy-dy/2 If nb>0 Then GraphicsWindow.BrushColor = "Yellow" Else GraphicsWindow.BrushColor = "White" EndIf GraphicsWindow.FillRectangle(x0-xs/2,y0-ys/2,xs,ys) If nb>0 Then GraphicsWindow.FontName = "Courier New" GraphicsWindow.FontSize = dx/1.5 GraphicsWindow.BrushColor = "Black" If nb>9 Then GraphicsWindow.DrawText(x0-xs/2,y0-ys/3,nb) Else GraphicsWindow.DrawText(x0-xs/4,y0-ys/3,nb) EndIf EndIf EndSub Sub clik 'Mouse handling, calculing the box numbers mx=Math.Floor(GraphicsWindow.MouseX/dx)+1 my=Math.Floor(GraphicsWindow.MouseY/dy)+1 hop() DrawPositionChanged() EndSub Sub hop 'Moving the stones (numbers) in the memory If mx<1 then mx=1 endif If mx>N then mx=N endif If my<1 then my=1 endif If my>N then my=N endif If(Array.GetValue("A0",(my-1)*N+mx)<1) then 'emty field don't need change stone position '   endsub BUG???: can't use here endsub statement Goto endsubid endif 'search empty stone position For i=1 to N For j=1 To N if (Array.GetValue("A0",(i-1)*N+j) <1) Then ' found empty cell position ey=i ex=j Goto Label1 EndIf EndFor Endfor Label1:     'BUG???  :Editor doesn't like Labels for ident 'Distance of the clicked and the empty stone DebugPositions() diff=Math.SquareRoot(Math.Power(ex-mx,2)+Math.Power(ey-my,2)) DebugDistance() If math.Abs(diff-1)<0.001 then 'distance of two position almost 1 (only x or y difference) then change the two stones 'copy past to future values DebugArrays() For i=1 to N*N Array.SetValue("A1",i,Array.GetValue("A0",i)) EndFor DebugArrays() Array.SetValue("A1",(ey-1)*N+ex,Array.GetValue("A0",(my-1)*N+mx)) Array.SetValue("A1",(my-1)*N+mx,Array.GetValue("A0",(ey-1)*N+ex)) DebugArrays() endif endsubid: endsub Sub DrawPositionChanged 'Redraw the Stones in the screen if necesary and Save previous values For i=1 to N For j=1 To N if (Array.GetValue("A0",(i-1)*N+j) <> Array.GetValue("A1",(i-1)*N+j)) Then 'cell is changed y=i x=j nb= Array.GetValue("A1",(i-1)*N+j) 'draw stone boxdraw() ' Save the previous value Array.SetValue("A0",(i-1)*N+j,Array.GetValue("A1",(i-1)*N+j)) EndIf EndFor Endfor EndSub Sub DebugPositions TextWindow.WriteLine("ex="+ex) TextWindow.WriteLine("ey="+ey) TextWindow.WriteLine("mx="+mx) TextWindow.WriteLine("my="+my) EndSub Sub DebugDistance TextWindow.WriteLine("di="+diff) Endsub Sub DebugArrays TextWindow.Write("A0=") For q=1 To N*N TextWindow.Write(" "+Array.GetValue("A0",q)+" ") If Math.Remainder(q,N)=0 Then TextWindow.Write("|") EndIf EndFor TextWindow.WriteLine("")  'BUG???: can't use without operand for new line TextWindow.Write("A1=") For q=1 To N*N TextWindow.Write(" "+Array.GetValue("A1",q)+" ") If Math.Remainder(q,N)=0 Then TextWindow.Write("|") EndIf EndFor TextWindow.WriteLine("") EndSub
• Proposed as answer by Wednesday, January 14, 2009 9:26 AM
• Marked as answer by Thursday, January 22, 2009 4:58 PM
Wednesday, January 14, 2009 9:25 AM

### All replies

• It would be helpful to have more detail.
...or a link to game rules, or something like that.

The image doesn't tell me anything.
Tuesday, January 13, 2009 4:36 PM
•  Path_drc

Look at  http://www.holotronix.com/samlloyd15.php.  I assume this is the game he is trying to build.
Tuesday, January 13, 2009 8:54 PM
• Landreoli;

By the way, I believe programming a game is one of the best ways to learn programming.
Games usually have many facets that typically go beyond the straight rows and columns of business or administrative programming, as well as (depending on the game) being simpler so you can learn basics - not to mention fun.

OK, without giving away too much, hopefully, I would use the following parts of Small Basic to build it.

a) need to think of background color and title
b) size needs to make room for the squares as well as any information you want to display like number of movements performed

2) I would use a combination of the GraphicsWindow.FillRectangle (x, y, width, height) and GraphicsWindow.DrawText(x, y, text) for the individual squares.
a) don't forget to change the color so the text is different than the square's color.

3) I would use an array to keep track of the position of the numbers.  (This might be the most difficult concept to understand.)

4) Use Math.GetRandomNumber to help randomize the position of each number.
a) instead of coding a Math.GetRandomNumber 15 times, I would use a For/EndFor loop

5) Need to setup test to see if player wins and how you will inform and/or reward player's success.

Hope that starts you off in the correct direction.
As you create your program, you may come up with extra ideas (like ask for a person's name?). That is good.
If you get stuck anywhere, post a question about something specific and we will help you the best we can.

THNX!

drc
Tuesday, January 13, 2009 10:59 PM
• Here you are.
 '  Demo program for Small Basic ver 0.2 '  Writen by András FEHÉR 2009. 01. 13. '  Széchenyi István University, Department of Telecommunications '  Győr, HUNGARY 'size of graphics window GRsize=300 'number of Rows ans Coulums N=3 GraphicsWindow.Width=GRsize GraphicsWindow.Height=GRsize GraphicsWindow.Title="Game" GraphicsWindow.Show() 'size of grid dx= GRsize/N dy= dx 'size of  stones xs= dx - 15 ys= xs ' Grid draw For x=1 To N-1 GraphicsWindow.AddLine(x*dx,0,x*dx,GRsize) EndFor For y=1 To N-1 GraphicsWindow.AddLine(0,y*dy,GRsize,y*dy) EndFor 'set New values  of  Stone Array (A1) 1 .... N*N For i=1 to N For j=1 To N Array.SetValue("A1",(i-1)*N+j,(i-1)*N+j) EndFor Endfor ' one stone is emty: value of arryay = 0 Array.SetValue("A1",N*N,0) 'set initial Previous values  of  Stone Array (A0) 0.....0 For i=1 to N For j=1 To N Array.SetValue("A0",(i-1)*N+j,0) EndFor Endfor DebugArrays() 'draws Stones, if Previous different as New DrawPositionChanged() DebugArrays() GraphicsWindow.MouseDown = clik Sub boxdraw 'draws to (x,y)  nb number x0=x*dx-dx/2 y0=y*dy-dy/2 If nb>0 Then GraphicsWindow.BrushColor = "Yellow" Else GraphicsWindow.BrushColor = "White" EndIf GraphicsWindow.FillRectangle(x0-xs/2,y0-ys/2,xs,ys) If nb>0 Then GraphicsWindow.FontName = "Courier New" GraphicsWindow.FontSize = dx/1.5 GraphicsWindow.BrushColor = "Black" If nb>9 Then GraphicsWindow.DrawText(x0-xs/2,y0-ys/3,nb) Else GraphicsWindow.DrawText(x0-xs/4,y0-ys/3,nb) EndIf EndIf EndSub Sub clik 'Mouse handling, calculing the box numbers mx=Math.Floor(GraphicsWindow.MouseX/dx)+1 my=Math.Floor(GraphicsWindow.MouseY/dy)+1 hop() DrawPositionChanged() EndSub Sub hop 'Moving the stones (numbers) in the memory If mx<1 then mx=1 endif If mx>N then mx=N endif If my<1 then my=1 endif If my>N then my=N endif If(Array.GetValue("A0",(my-1)*N+mx)<1) then 'emty field don't need change stone position '   endsub BUG???: can't use here endsub statement Goto endsubid endif 'search empty stone position For i=1 to N For j=1 To N if (Array.GetValue("A0",(i-1)*N+j) <1) Then ' found empty cell position ey=i ex=j Goto Label1 EndIf EndFor Endfor Label1:     'BUG???  :Editor doesn't like Labels for ident 'Distance of the clicked and the empty stone DebugPositions() diff=Math.SquareRoot(Math.Power(ex-mx,2)+Math.Power(ey-my,2)) DebugDistance() If math.Abs(diff-1)<0.001 then 'distance of two position almost 1 (only x or y difference) then change the two stones 'copy past to future values DebugArrays() For i=1 to N*N Array.SetValue("A1",i,Array.GetValue("A0",i)) EndFor DebugArrays() Array.SetValue("A1",(ey-1)*N+ex,Array.GetValue("A0",(my-1)*N+mx)) Array.SetValue("A1",(my-1)*N+mx,Array.GetValue("A0",(ey-1)*N+ex)) DebugArrays() endif endsubid: endsub Sub DrawPositionChanged 'Redraw the Stones in the screen if necesary and Save previous values For i=1 to N For j=1 To N if (Array.GetValue("A0",(i-1)*N+j) <> Array.GetValue("A1",(i-1)*N+j)) Then 'cell is changed y=i x=j nb= Array.GetValue("A1",(i-1)*N+j) 'draw stone boxdraw() ' Save the previous value Array.SetValue("A0",(i-1)*N+j,Array.GetValue("A1",(i-1)*N+j)) EndIf EndFor Endfor EndSub Sub DebugPositions TextWindow.WriteLine("ex="+ex) TextWindow.WriteLine("ey="+ey) TextWindow.WriteLine("mx="+mx) TextWindow.WriteLine("my="+my) EndSub Sub DebugDistance TextWindow.WriteLine("di="+diff) Endsub Sub DebugArrays TextWindow.Write("A0=") For q=1 To N*N TextWindow.Write(" "+Array.GetValue("A0",q)+" ") If Math.Remainder(q,N)=0 Then TextWindow.Write("|") EndIf EndFor TextWindow.WriteLine("")  'BUG???: can't use without operand for new line TextWindow.Write("A1=") For q=1 To N*N TextWindow.Write(" "+Array.GetValue("A1",q)+" ") If Math.Remainder(q,N)=0 Then TextWindow.Write("|") EndIf EndFor TextWindow.WriteLine("") EndSub
• Proposed as answer by Wednesday, January 14, 2009 9:26 AM
• Marked as answer by Thursday, January 22, 2009 4:58 PM
Wednesday, January 14, 2009 9:25 AM
•   Andras, you're not suppose to write the program for him.

Objective of Small Basic is to learn programming and I am pretty sure Landreoli wanted to figure it out for himself.

...of course, I guess Landreoli could look over your code to understand how your program works.
Wednesday, January 14, 2009 2:36 PM
• Well, Andras, you didn't give the full 15, you have a text window showing the array and variables, and you don't check for a winning condition so I guess you could call your post a very, very, very big hint.  lol

Leaves room for Landreoli to improve upon and learn that way.

Well written.  How long did it take you Andras?

Wednesday, January 14, 2009 3:05 PM
• You can change the size to 15 by N=4
Wednesday, January 14, 2009 4:32 PM
• Andras;

I think I wrote my post poorly.  That you did not understand what I was trying to say.

I was trying to say that you did not give him the entire objective, and that is good.
You gave him most of  what he wanted, but he can still correct and improve what you have created.
By improving your program, Landreoli will be able to learn.

That is what I was trying to say.

Also, you did not answer my question.  How long did it take you to create the program?
It seems you created the program very fast, and I am impressed.  :-)
Wednesday, January 14, 2009 5:05 PM
•  My 1st program in Small Basic. From the 2nd start of reading of Introducing Small Basic.pdf  was ready in 5 hours.
• Proposed as answer by Wednesday, January 14, 2009 5:17 PM
Wednesday, January 14, 2009 5:17 PM
• Thank you very much! You helped me a lot!
I've been absent for a few days, but now i try to learn small basic writing (finishing) the game, or at least i'll try to do it :)
Thursday, January 22, 2009 1:05 PM
•  2 lines changed in the program, but in the SMALLBASIC  the bugs? is the same.
 '  Demo program for Small Basic ver 0.3.1 '  Writen by András FEHÉR 2009. 02. 12. '  Széchenyi István University, Department of Telecommunications '  Győr, HUNGARY 'size of graphics window GRsize=300 'number of Rows ans Coulums N=3 GraphicsWindow.Width=GRsize GraphicsWindow.Height=GRsize GraphicsWindow.Title="Game" GraphicsWindow.Show() 'size of grid dx= GRsize/N dy= dx 'size of  stones xs= dx - 15 ys= xs ' Grid draw For x=1 To N-1 GraphicsWindow.DrawLine(x*dx,0,x*dx,GRsize) EndFor For y=1 To N-1 GraphicsWindow.DrawLine(0,y*dy,GRsize,y*dy) EndFor 'set New values  of  Stone Array (A1) 1 .... N*N For i=1 to N For j=1 To N Array.SetValue("A1",(i-1)*N+j,(i-1)*N+j) EndFor Endfor ' one stone is emty: value of arryay = 0 Array.SetValue("A1",N*N,0) 'set initial Previous values  of  Stone Array (A0) 0.....0 For i=1 to N For j=1 To N Array.SetValue("A0",(i-1)*N+j,0) EndFor Endfor DebugArrays() 'draws Stones, if Previous different as New DrawPositionChanged() DebugArrays() GraphicsWindow.MouseDown = clik Sub boxdraw 'draws to (x,y)  nb number x0=x*dx-dx/2 y0=y*dy-dy/2 If nb>0 Then GraphicsWindow.BrushColor = "Yellow" Else GraphicsWindow.BrushColor = "White" EndIf GraphicsWindow.FillRectangle(x0-xs/2,y0-ys/2,xs,ys) If nb>0 Then GraphicsWindow.FontName = "Courier New" GraphicsWindow.FontSize = dx/1.5 GraphicsWindow.BrushColor = "Black" If nb>9 Then GraphicsWindow.DrawText(x0-xs/2,y0-ys/3,nb) Else GraphicsWindow.DrawText(x0-xs/4,y0-ys/3,nb) EndIf EndIf EndSub Sub clik 'Mouse handling, calculing the box numbers mx=Math.Floor(GraphicsWindow.MouseX/dx)+1 my=Math.Floor(GraphicsWindow.MouseY/dy)+1 hop() DrawPositionChanged() EndSub Sub hop 'Moving the stones (numbers) in the memory If mx<1 then mx=1 endif If mx>N then mx=N endif If my<1 then my=1 endif If my>N then my=N endif If(Array.GetValue("A0",(my-1)*N+mx)<1) then 'emty field don't need change stone position '   endsub BUG???:  still can't use here endsub statement   (v0.3.1) Goto endsubid endif 'search empty stone position For i=1 to N For j=1 To N if (Array.GetValue("A0",(i-1)*N+j) <1) Then ' found empty cell position ey=i ex=j Goto Label1 EndIf EndFor Endfor Label1: 'BUG???  :Editor doesn't like Labels for ident 'Distance of the clicked and the empty stone DebugPositions() diff=Math.SquareRoot(Math.Power(ex-mx,2)+Math.Power(ey-my,2)) DebugDistance() If math.Abs(diff-1)<0.001 then 'distance of two position almost 1 (only x or y difference) then change the two stones 'copy past to future values DebugArrays() For i=1 to N*N Array.SetValue("A1",i,Array.GetValue("A0",i)) EndFor DebugArrays() Array.SetValue("A1",(ey-1)*N+ex,Array.GetValue("A0",(my-1)*N+mx)) Array.SetValue("A1",(my-1)*N+mx,Array.GetValue("A0",(ey-1)*N+ex)) DebugArrays() endif endsubid: endsub Sub DrawPositionChanged 'Redraw the Stones in the screen if necesary and Save previous values For i=1 to N For j=1 To N if (Array.GetValue("A0",(i-1)*N+j) <> Array.GetValue("A1",(i-1)*N+j)) Then 'cell is changed y=i x=j nb= Array.GetValue("A1",(i-1)*N+j) 'draw stone boxdraw() ' Save the previous value Array.SetValue("A0",(i-1)*N+j,Array.GetValue("A1",(i-1)*N+j)) EndIf EndFor Endfor EndSub Sub DebugPositions TextWindow.WriteLine("ex="+ex) TextWindow.WriteLine("ey="+ey) TextWindow.WriteLine("mx="+mx) TextWindow.WriteLine("my="+my) EndSub Sub DebugDistance TextWindow.WriteLine("di="+diff) Endsub Sub DebugArrays TextWindow.Write("A0=") For q=1 To N*N TextWindow.Write(" "+Array.GetValue("A0",q)+" ") If Math.Remainder(q,N)=0 Then TextWindow.Write("|") EndIf EndFor TextWindow.WriteLine("")  'BUG???: still can't use without operand for new line   (v0.3.1) TextWindow.Write("A1=") For q=1 To N*N TextWindow.Write(" "+Array.GetValue("A1",q)+" ") If Math.Remainder(q,N)=0 Then TextWindow.Write("|") EndIf EndFor TextWindow.WriteLine("") EndSub
• Proposed as answer by Thursday, February 12, 2009 8:36 AM
Thursday, February 12, 2009 8:35 AM
• CAN YOU STOP THINKING ADDLINE IS IN THE GRAPHICSWINDOW , ITS IN SHAPES
Saturday, October 15, 2011 7:11 PM
• Augs, you are right about that. But there is a GraphicsWindow.DrawLine() method.
~~AirWaves!!~~
Saturday, October 15, 2011 9:20 PM
• CAN YOU STOP THINKING ADDLINE IS IN THE GRAPHICSWINDOW , ITS IN SHAPES

Augs,
Early versions of Small Basic (ver prior to 0.3) did have operations like AddLine in the GraphicsWindow object.

You may also notice Array.SetValue & Array.GetValue.  These have also been replaced(since ver 0.5) as Small Basic has evolved.
• Edited by Monday, October 17, 2011 9:25 AM typo
Sunday, October 16, 2011 10:41 PM