the code used is below but I didn't include the media as it uses the Media that came with the asset packs
// Project: Mahjong
// Created: 2017-01-07
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Mahjong" )
SetWindowSize( 800, 600, 0 )
// set displarow properties
SetVirtualResolution( 800,600 )
SetOrientationAllowed( 0, 0, 1, 1 )
//SetSyncRate( 30, 0 ) // 30fps instead of 60 to save batterrow
UseNewDefaultFonts( 1 ) // since version 2.0.20 we can use nicer default fonts
Type tileType
col as integer
row as integer
img as integer
id as integer
layer as integer
visible as integer
//selected as integer
endtype
#constant maxTiles=54 //9 cols * 6 rows
#constant maxMoves=150
global tilesLeft=maxTiles
global movesLeft=maxMoves
global moves=0
global previousId=0
global previousImg=0
gameName=CreateText("Mahjong")
SetTextSize(gameName,50)
SetTextColor(gameName,255,255,255,255)
SetTextPosition(gameName,50,500)
tilesLeftTxt=CreateText("Tiles Left=")
SetTextSize(tilesLeftTxt,20)
SetTextColor(tilesLeftTxt,255,255,255,255)
SetTextPosition(tilesLeftTxt,405,500)
counter=CreateText(str(tilesLeft))
SetTextSize(counter,20)
SetTextColor(counter,255,255,255,255)
SetTextPosition(counter,555,500)
movesTxt=CreateText("Moves used=")
SetTextSize(movesTxt,20)
SetTextColor(movesTxt,255,255,255,255)
SetTextPosition(movesTxt,405,520)
movesCount=CreateText(str(moves))
SetTextSize(movesCount,20)
SetTextColor(movesCount,255,255,255,255)
SetTextPosition(movesCount,555,520)
movesLeftTxt=CreateText("Moves Left=")
SetTextSize(movesLeftTxt,20)
SetTextColor(movesLeftTxt,255,255,255,255)
SetTextPosition(movesLeftTxt,405,540)
movesLeftText=CreateText(str(movesLeft))
SetTextSize(movesLeftText,20)
SetTextColor(movesLeftText,255,255,255,255)
SetTextPosition(movesLeftText,555,540)
//64*80
dim tiles[maxTiles+1] as tileType
image=LoadImage("tiles1.png")
for tile = 1 to 9 //create 9 tile images 64by80
CopyImage( tile,image, (tile-1)*64, 0,64,80 )
next tile
sync()
DeleteImage(image)
titleImg=LoadImage("title.png")
title=createSprite(titleImg)
SetSpriteOffset(title,0,0)
SetSpritePositionByOffset(title,590,15)
myLogoImg=LoadImage("logo.png")
myLogo=createSprite(myLogoImg)
SetSpriteOffset(myLogo,0,0)
SetSpritePositionByOffset(myLogo,590,420)
agkLogoImg=LoadImage("agkLogo.png")
agkLogo=createSprite(agkLogoImg)
SetSpriteOffset(agkLogo,0,0)
SetSpritePositionByOffset(agkLogo,590,535)
//tiles[0].selected=0
setupArray()
shuffleArray()
//create an exit button and a restart button later il implement a shuffle button
restartBtn=1:exitBtn=2
AddVirtualButton(restartBtn,245,530,60) //make a button
SetVirtualButtonText(restartBtn, "Restart" ) //set number to image
SetVirtualButtonColor( restartBtn, 200,200,200 ) //random colour
AddVirtualButton(exitBtn,305,530,60) //make a button
SetVirtualButtonText( exitBtn, "Quit" ) //set number to image
SetVirtualButtonColor( exitBtn,200,200,200) //random colour
do
if tilesLeft<=0
youWon()
previousId=0:previousImg=0
tilesLeft=maxTiles:movesLeft=maxMoves:moves=0
setupArray()
shuffleArray()
endif
if movesLeft<=0
youLose()
previousId=0:previousImg=0
tilesLeft=maxTiles:movesLeft=maxMoves:moves=0
setupArray()
shuffleArray()
endif
if GetVirtualButtonPressed(restartBtn)=1
previousId=0:previousImg=0
tilesLeft=maxTiles:movesLeft=maxMoves:moves=0
setupArray()
shuffleArray()
endif
if GetVirtualButtonPressed(exitBtn)=1
exit
endif
if getpointerpressed()=1
hit = GetSpriteHitGroup (1, GetPointerX ( ), GetPointerY ( ) )
if hit > 0
tileSelect(hit)
endif
endif
SetTextString(counter,str(tilesLeft))
SetTextString(movesCount,str(moves))
SetTextString(movesLeftText,str(movesLeft))
Sync()
loop
function tileSelect(hit as integer)
inc moves:dec movesLeft
if tiles[hit-1].row<>tiles[hit].row //not on same col
if previousId=0 //nothing selected
SetSpriteColorAlpha( hit, 100 )
previousId=tiles[hit].id
previousImg=tiles[hit].img
elseif tiles[hit].img=previousImg and tiles[hit].id <> previousId
removeTiles(tiles[hit].id,previousId)
previousId=0:previousImg=0
else
SetSpriteColorAlpha(previousId,255) //:SetSpriteColorAlpha(hit,255)
SetSpriteColorAlpha( hit, 100 )
previousId=tiles[hit].id:previousImg=tiles[hit].img
endif
endif
if tiles[hit+1].row<>tiles[hit].row //not on same row
if previousId=0 //nothing selected
SetSpriteColorAlpha( hit, 100 )
previousId=tiles[hit].id
previousImg=tiles[hit].img
elseif tiles[hit].img=previousImg and tiles[hit].id <> previousId
removeTiles(tiles[hit].id,previousId)
previousId=0:previousImg=0
else
SetSpriteColorAlpha(previousId,255) //:SetSpriteColorAlpha(hit,255)
SetSpriteColorAlpha( hit, 100 )
previousId=tiles[hit].id:previousImg=tiles[hit].img
endif
endif
if tiles[hit].row=tiles[hit-1].row and tiles[hit-1].visible=0 //and tiles[hit].layer=tiles[hit-1].layer//the one to left previouslrow clicked on larower
if previousId=0 //nothing selected
SetSpriteColorAlpha( hit, 100 )
previousId=tiles[hit].id
previousImg=tiles[hit].img
elseif tiles[hit].img=previousImg and tiles[hit].id <> previousId
removeTiles(tiles[hit].id,previousId)
previousId=0:previousImg=0
else //reselect a different tile
SetSpriteColorAlpha(previousId,255) //:SetSpriteColorAlpha(hit,255)
SetSpriteColorAlpha( hit, 100 )
previousId=tiles[hit].id:previousImg=tiles[hit].img
endif
endif
if tiles[hit].row=tiles[hit+1].row and tiles[hit+1].visible=0 //and tiles[hit].layer=tiles[hit-1].layer//the one to right previosulrow clicked on larower
if previousId=0 //nothing selected
SetSpriteColorAlpha( hit, 100 )
previousId=tiles[hit].id
previousImg=tiles[hit].img
elseif tiles[hit].img=previousImg and tiles[hit].id <> previousId
removeTiles(tiles[hit].id,previousId)
previousId=0:previousImg=0
else //reselect a different tile
SetSpriteColorAlpha(previousId,255) //:SetSpriteColorAlpha(hit,255)
SetSpriteColorAlpha( hit, 100 )
previousId=tiles[hit].id:previousImg=tiles[hit].img
endif
endif
endfunction
function removeTiles(num as integer, num2 as integer)
SetSpriteVisible(tiles[num].id,0)
SetSpriteVisible(tiles[num2].id,0)
tiles[num].visible=0:tiles[num2].visible=0
//tiles[num].selected=0:tiles[num2].selected=0
dec tilesLeft,2
endfunction
function setupArray ()
num=0:tiles[0].visible=0
tiles[0].col=0:tiles[0].row=0
for row = 1 to 6
for col = 1 to 9
inc num //:inc img:if img>9 then img=1 //maximum of 9 tile types
tiles[num].col=col:tiles[num].row=row
//tiles[num].selected=0:
tiles[num].id=num
tiles[num].visible=1
tiles[num].img=col
if GetSpriteExists(tiles[num].id)=0 then CreateSprite(tiles[num].id,tiles[num].img)
SetSpriteVisible(tiles[num].id,1)
SetSpriteColorAlpha(tiles[num].id,255)
SetSpriteOffset(tiles[num].id,0,0)
SetSpriteSize(tiles[num].id,64,80)
SetSpritePositionByOffset(tiles[num].id,((col-1)*64)+10,((row-1)*80)+15) //+45 +15
SetSpriteGroup(tiles[num].id,1)
sync()
next col
next row
endfunction
function shuffleArray ()
Dim myArray[maxTiles] as Integer
For n = 1 to maxTiles
myArray[n] = tiles[n].img
Next n
` Shuffle the tiles.
For n = 1 to 200
element1 = Random(1, maxTiles)
element2 = Random(1, maxTiles)
tempNumber = myArray[element1]
myArray[element1] = myArray[element2]
myArray[element2] = tempNumber
Next n
For n = 1 to maxTiles
tiles[n].img=myArray[n]
SetSpriteImage(n,tiles[n].img)
//SetSpriteVisible(tiles[n].id,1)
//sync()
Next n
endfunction
function youWon ()
win$="You Solved it."
win=CreateText ( win$ )
// set its size
SetTextSize ( win, 60 )
SetTextDepth( win, 0 )
SetTextPosition(win,75,125)
play$="Tap Screen to play again"
play=CreateText(play$)
SetTextSize ( play, 60 )
SetTextDepth( play, 0 )
SetTextPosition(play,75,200)
sleep(250)
// main loop
while GetPointerPressed ( ) <> 1
// apply a random color to the text entity
red=Random (1,255):green=random (1,255):blue=random (1,255)
SetTextColor ( win, red, green, blue , 255 )
// update the screen
sync ( )
endwhile
DeleteText(win)
DeleteText(play)
endfunction
function youLose ()
lose$="You ran out of moves"
lose=CreateText ( lose$ )
// set its size
SetTextSize ( lose, 60 )
SetTextDepth( lose, 0 )
SetTextPosition(lose,75,125)
play$="Tap Screen to play again"
play=CreateText(play$)
SetTextSize ( play, 60 )
SetTextDepth( play, 0 )
SetTextPosition(play,75,200)
sleep(250)
// main loop
while GetPointerPressed ( ) <> 1
// apply a random color to the text entity
red=Random (1,255):green=random (1,255):blue=random (1,255)
SetTextColor ( lose, red, green, blue , 255 )
// update the screen
sync ( )
endwhile
DeleteText(lose)
DeleteText(play)
endfunction
fubar