Real Software Forums
http://forums.realsoftware.com/

Slider Puzzle
http://forums.realsoftware.com/viewtopic.php?f=4&t=38079
Page 1 of 1

Author:  Dantez [ Sat Mar 12, 2011 10:01 am ]
Post subject:  Slider Puzzle

Hi,

I have a couple questions. I'm trying to make a slider puzzle, you know the one with the pictures, but i cant seem to figure out how to make the canvases shuffle around. My questions are.

- Should i use canvases, or is there a way to make pushbuttons accept graphics?
- If I should have to use canvases then how do i make them into a(n) Control Array?
-- Everytime i do and i put the code "g.drawgraphics mypic,0,0,80,80,0,0,80,80" it puts it in everysingle canvas in the array.

Finally...
- How do i shuffle them?
-- I've tried everything from making the array to selecting a random number and moving it.

Can anyone please help me. :|

Author:  DaveS [ Sat Mar 12, 2011 11:53 am ]
Post subject:  Re: Slider Puzzle

first off... do not use an array of canvas.... use ONE canvas

use a simple 4 x 4 array to keep track of where the "pieces" are....

and in the Paint Event of your single canvas..... redraw the current state of the puzzle.

Use the MouseDown/Up/Move/Drag events to determine what element of your array to "move"
and on the MouseUP .... fire off the Paint event again

Author:  DaveS [ Sat Mar 12, 2011 12:36 pm ]
Post subject:  Re: Slider Puzzle

ok... I was intrigued.......
so here it is ... not too bad for 20 min work :)

create a window with two controls
CANVAS1 (400 x 400)
PUSHBUTTON1 - Label it "New Puzzle"

Global Properties
Dim PUZZLE(3,3) as INTEGER
dim RND_NUM as RANDOM
dim SQUARE_SIZE as integer


Global CONSTANT
puzzle_size=3 ' this should match the dimensions of PUZZLE()


Global Method - NAME = NEW_PUZZLE
dim i as integer
dim x as integer
dim y as integer
dim cnt as integer
cnt=((puzzle_size+1)*(puzzle_size+1))-1
for x=0 to puzzle_size
for y=0 to puzzle_size
puzzle(x,y)=0 ' empty square
next y
next x
x=rnd_num.InRange(0,puzzle_size)
y=rnd_num.InRange(0,puzzle_size)
for i=0 to cnt
while puzzle(x,y)>0
x=rnd_num.InRange(0,puzzle_size)
y=rnd_num.InRange(0,puzzle_size)
wend
puzzle(x,y)=i
next i
square_size=(canvas1.width\(puzzle_size+1))
Canvas1.refresh


WINDOW1.OPEN EVENT
rnd_num=new random
New_Puzzle


PUSHBUTTON1.ACTION EVENT
New_Puzzle



CANVA1.PAINT EVENT
dim x as integer
dim y as integer
dim ypos as integer
dim xpos as integer

dim s as string
g.ForeColor=&c00ff00
g.fillrect 0,0,g.width,g.height
g.textsize=72

for x=0 to puzzle_size
xpos=(x*square_size)
for y=0 to puzzle_size
ypos=(y*square_size)
if puzzle(x,y)>0 then ' draw the piece
g.ForeColor=&cffffff
g.fillrect xpos,ypos,square_size,square_size
g.ForeColor=&c0000ff
g.drawrect xpos,ypos,square_size,square_size
s=str(puzzle(x,y))
g.drawstring s,xpos+((square_size-g.StringWidth(s))\2),ypos+((square_size-g.textheight)\2)+g.TextAscent
end if
next y
next x
g.ForeColor=&c000000
g.drawrect 0,0,g.width,g.height


CANVAS1.MOUSEDOWN EVENT
dim good_move as boolean
x=x\square_size
y=y\Square_Size
good_move=false
if puzzle(x,y)>0 then ' can't move the empty square
' check if move up
if y>0 and not good_move then
if puzzle(x,y-1)=0 then
puzzle(x,y-1)=puzzle(x,y)
puzzle(x,y)=0
good_move=true
end if
end if
' check if move down
if y<puzzle_size and not good_move then
if puzzle(x,y+1)=0 then
puzzle(x,y+1)=puzzle(x,y)
puzzle(x,y)=0
good_move=true
end if
end if
' check if move left
if x>0 and not good_move then
if puzzle(x-1,y)=0 then
puzzle(x-1,y)=puzzle(x,y)
puzzle(x,y)=0
good_move=true
end if
end if

' check if move right
if x<puzzle_size and not good_move then
if puzzle(x+1,y)=0 then
puzzle(x+1,y)=puzzle(x,y)
puzzle(x,y)=0
good_move=true
end if
end if
end if
if not good_move then
beep
return true
end if
canvas1.refresh

Author:  DaveS [ Sat Mar 12, 2011 1:05 pm ]
Post subject:  Re: Slider Puzzle

and if you want to use a PICTURE....

add a graphic image to the IDE the exact same size as CANVAS1 and name it PUZZLE_PIC

change PAINT EVENT to this
dim x as integer
dim y as integer
dim ypos as integer
dim xpos as integer
dim s as string
dim xpic as integer
dim ypic as integer
const number_puzzle=false
g.ForeColor=&c00ff00
g.fillrect 0,0,g.width,g.height
g.textsize=72

for x=0 to puzzle_size
xpos=(x*square_size)
for y=0 to puzzle_size
ypos=(y*square_size)
if puzzle(x,y)>0 then ' draw the piece
if number_puzzle then
g.ForeColor=&cffffff
g.fillrect xpos,ypos,square_size,square_size
g.ForeColor=&c0000ff
g.drawrect xpos,ypos,square_size,square_size
s=str(puzzle(x,y))
g.drawstring s,xpos+((square_size-g.StringWidth(s))\2),ypos+((square_size-g.textheight)\2)+g.TextAscent
else
ypic=(puzzle(x,y)-1)\(puzzle_size+1)
xpic=(puzzle(x,y)-1)-(ypic*(puzzle_size+1))
xpic=xpic*Square_Size
ypic=ypic*Square_Size
g.drawpicture puzzle_pic,xpos,ypos,Square_Size,Square_Size,xpic,ypic,Square_Size,Square_Size
end if
end if
next y
next x
g.ForeColor=&c000000
g.drawrect 0,0,g.width,g.height

Author:  Dantez [ Sat Mar 12, 2011 2:54 pm ]
Post subject:  Re: Slider Puzzle

When you say "Global" Do you mean a module or do you mean put it in the window?

Author:  Agent [ Mon Jul 04, 2011 5:59 pm ]
Post subject:  Re: Slider Puzzle

Dantez wrote:
When you say "Global" Do you mean a module or do you mean put it in the window?


It doesn't matter, or rather, from what I can read from his code, you can either put it in the window or in a module.

Author:  ccfman2004 [ Tue Aug 16, 2011 5:12 am ]
Post subject:  Re: Slider Puzzle

Wow that code works really good.

Author:  eudosia [ Wed Nov 16, 2011 4:12 am ]
Post subject:  Re: Slider Puzzle

Thanks to Dave Simeons I realized the Puzzle game.
If you want to test the Dave's code you can directly download the project From
here:
http://www.eudosia.com/downloads/download.php?download=15
Image

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/