Hello
I finished this program and it seems to work perfectly, except that the second tile form the bottom left will not go down when you click on it. If what I'm saying doesn't make sense, boot the game up and try it out. You should be able to see the problem.
Rem Project: ImageShuffle Program
Rem Created: Monday, February 13, 2012
Rem Designer: Andrew D. South
Rem ***** Main Source File *****
`Tile position data for calculating valid moves
DATA 2,5,0,0
DATA 1,3,6,0
DATA 2,4,7,0
DATA 3,8,0,0
DATA 1,6,9,0
DATA 2,5,7,10
DATA 3,6,8,11
DATA 4,7,12,0
DATA 5,10,10,0
DATA 6,9,11,14
DATA 7,10,12,15
DATA 8,11,16,0
DATA 9,14,0,0
DATA 13,10,15,0
DATA 14,16,11,0
DATA 12,15,0,0
`Declare some variables
DIM Tiles(17) as integer
DIM MovingMatrix(64) as integer
DIM Selected(2) as integer
DIM MoveIt(4) as integer
TempX as integer = 0
TempY as integer = 0
done as integer = 0
Width as integer = 0
Height as integer = 0
`initialize the display
SYNC ON
`load the game graphics
LOAD BITMAP "Media\mainback.bmp", 17
LOAD BITMAP "Media\tiles.bmp", 18
`Create the tile bitmaps
FOR x = 1 TO 16
CREATE BITMAP X, 100, 100
NEXT x
`Copy the tile bitmaps
FOR y = 0 TO 3
FOR x = 0 TO 3
pos = (y * 4 + x) + 1
TX = x * 100
TY = y * 100
COPY BITMAP 18,TX,TY,TX+99,TY+99,pos,0,0,99,99
NEXT x
NEXT y
`Set the moving matrix
FOR x = 1 TO 64
READ a
MovingMatrix(x) = a
NEXT x
`initialize the game
SET CURRENT BITMAP 0
ShuffleTiles()
DisplayBoard()
Selected(1) = 0
Selected(2) = 0
`Main game loop
REPEAT
SYNC
IF MOUSECLICK()=1
`Wait for mouse button to be released
WHILE MOUSECLICK()=1
ENDWHILE
`Figure out which tile was clicked
bitmap = BitmapNumber(MOUSEX(),MOUSEY())
b2=CheckValid(bitmap)
`Swap the clicked tile and the blank tile
IF b2 <> 0
SwapTiles(bitmap, b2)
DisplayBoard()
done = CheckForWin()
ENDIF
ENDIF
UNTIL done = 1
`GAME OVER
PRINT "Congratulations, You Win! "
SYNC
`Delete the tile bitmaps from memory
FOR x = 1 TO 16
DELETE BITMAP x
NEXT x
`End Game
WAIT KEY
END
`This function will randomize the tiles
FUNCTION ShuffleTiles
FOR count = 1 TO 16
Tiles(count) = count
IF count = 16 THEN Tiles(count) = -1
NEXT count
RandomMoves = RND(50) + 100
FOR count = 1 TO RandomMoves
MoveTheSpace()
Next count
ENDFUNCTION
`This function displays the tiles on the screen
FUNCTION DisplayBoard
COPY BITMAP 17, 0
FOR y = 0 TO 3
FOR x = 0 TO 3
pos = (y * 4 + x) + 1
IF Tiles(pos) >= 0
TX = x * 100
TY = y * 100
W = TX + 99 + 120
H = TY + 99 + 56
COPY BITMAP Tiles(pos),0,0,99,99,0,TX+120,TY+56,W,H
ENDIF
NEXT X
NEXT Y
SYNC
ENDFUNCTION
`This function checks the coordinates for a valid tile
FUNCTION CheckValid(rx)
IF rx = -1 THEN EXITFUNCTION 0
IF Tiles(rx) = -1 THEN EXITFUNCTION 0
FOR X = 1 TO 16
ptr = ((x - 1) * 4) + 1
IF rx = x
a1 = MovingMatrix(ptr)
a2 = MovingMatrix(ptr + 1)
a3 = MovingMatrix(ptr + 2)
a4 = MovingMatrix(ptr + 3)
IF a1 <> 0 and Tiles(a1) = -1 THEN EXITFUNCTION a1
IF a2 <> 0 and Tiles(a2) = -1 THEN EXITFUNCTION a2
IF a3 <> 0 and Tiles(a3) = -1 THEN EXITFUNCTION a3
IF a4 <> 0 and Tiles(a4) = -1 THEN EXITFUNCTION a4
ENDIF
NEXT X
ENDFUNCTION 0
`This function returns the value of the bitmap located at x,y
FUNCTION BitmapNumber(x, y)
dx = x - 120
dy = y - 56
IF dx < 0 OR dy < 0 THEN EXITFUNCTION -1
dx = dx / 100
dy = dy / 100
pos = ((dy * 4) + dx) + 1
IF pos > 16 THEN pos = -1
IF pos < 0 THEN pos = -1
ENDFUNCTION pos
`This function swaps two tiles in the Tiles array
FUNCTION SwapTiles(c1, c2)
temp = Tiles(c1)
Tiles(c1) = Tiles(c2)
Tiles(c2) = temp
ENDFUNCTION
`This function oves the empty tile
FUNCTION MoveTheSpace
spt = 0
FOR x = 1 TO 16
IF Tiles(x) = -1 THEN spt = x
NEXT x
IF spt = 0 THEN EXITFUNCTION
FOR x = 1 TO 16
ptr = ((x-1)*4)+1
IF spt = x
MoveIt(1) = MovingMatrix(ptr)
MoveIt(2) = MovingMatrix(ptr + 1)
MoveIt(3) = MovingMatrix(ptr + 2)
MoveIt(4) = MovingMatrix(ptr + 3)
movenum = RND(3)+1
While MoveIt(movenum) = 0
movenum = RND(3)+1
ENDWHILE
c1 = spt
c2 = MoveIt(movenum)
SwapTiles(c1,c2)
ENDIF
NEXT x
ENDFUNCTION
`This function scans the tiles for a win
FUNCTION CheckForWin
FOR x = 1 TO 15
IF Tiles(x) <> x THEN EXITFUNCTION 0
NEXT x
Tiles(16) = 16
DisplayBoard()
SLEEP 1000
EXITFUNCTION 1
ENDFUNCTION 0
When we all lend our power together, there is nothing we
can't do!
Please Lend me your STRENGTH!