Lorsque j'ajoute un debug à la fin de cette procédure, j'ai un nombre de clusters (possibilités) énorme certaines fois, c'est étrange, non ?
Code : Tout sélectionner
;{ infos
; match 3
; date : 4/11/2016
; blendman
; purebasic 5.50 beta 1
; 4, 5 /11/2016 : conversion du code trouvé sur le net : https://github.com/rembound/Match-3-Game-HTML5/blob/master/match3-example.js#L85
;}
;{ on initilialise les encode/decorder
If UseJPEGImageDecoder() =0 Or UseJPEGImageEncoder()=0 Or UsePNGImageDecoder()=0 Or UsePNGImageEncoder()=0
MessageRequester("Error", "unable To open the encoder/decorder")
End
EndIf
InitSprite()
;}
Enumeration ; window
#Win
EndEnumeration
;{ Levels
Structure Sselectedtile
selected.b
column.a
row.a
EndStructure
Structure sTile
Typ.i
shift.i
EndStructure
Structure SLevel
x.i
y.i
Columns.b
rows.b
tilewidth.i ; ; Visual width of a tile
tileheight.i ; ; Visual height of a tile
Array tiles.sTile(100,100) ; ; The two-dimensional tile Array
selectedtile.Sselectedtile ; { selected: false, column: 0, row: 0 endif
EndStructure
Global Level.sLevel
With level
\x = 250
\y = 113
\Columns = 8
\Rows = 8
\tilewidth= 40 ;; Visual width of a tile
\tileheight= 40
\selectedtile\selected=-1 ; selected: false, column: 0, row: 0
\selectedtile\column = 0
\selectedtile\row = 0
EndWith
;}
;{ Colors
Global Maxcol = 5
Global Dim col(5)
Col(0) = RGB(255, 128, 128)
Col(1) = RGB(128, 255, 128)
Col(2) = RGB(128, 128, 255)
Col(3) = RGB(255, 255, 128)
Col(4) = RGB(255, 128, 255)
Col(5) = RGB(128, 255, 255)
;}
;{ Clusters And moves that were found
Structure move
column1.i
row1.i
column2.i
row2.i
EndStructure
; Array of moves
Global NewList moves.move()
; Game states
Structure GameState
init.i
ready.i
resolve.i
EndStructure
Global gamestates.gamestate
gamestates\init = 0
gamestates\ready = 1
gamestates\resolve = 2
Global gamestate = gamestates\init
; Current move
Global NewList currentmove.move()
Structure sClusters
Length.i
column.i
row.i
horizontal.i
EndStructure
Global NewList clusters.sClusters() ; ; { column, row, length, horizontal endif
; Swap two tiles IN the level
Procedure Swap_(x1, y1, x2, y2)
typeswap = level\tiles(x1,y1)\Typ
level\tiles(x1,y1)\Typ = level\tiles(x2,y2)\Typ
level\tiles(x2,y2)\Typ = typeswap
EndProcedure
; Find clusters IN the level
Procedure findClusters0()
; Reset clusters
ClearList(clusters())
; Find horizontal clusters
Debug "horizontal ----"
For j=0 To level\rows
; Start With a single tile, cluster of 1
matchlength = 1;
For i=0 To level\columns
checkcluster = 0; false;
If (i = level\columns-1)
; Last tile
checkcluster = 1;
Else
; Check the Type of the Next tile
If (Level\Tiles(i,j)\Typ = level\tiles(i+1,j)\Typ And level\tiles(i,j)\Typ <> -1)
; Same Type As the previous tile, increase matchlength
matchlength + 1;
If matchlength>=2
;Debug ""+Level\Tiles(i,j)\Typ+"\"+Level\Tiles(i+1,j)\Typ
Debug ""+i+"\"+j
Debug ""+Str(i+1)+"\"+Str(j)
Debug matchlength
EndIf
Else
; Different Type
checkcluster = 1;
EndIf
EndIf
; Check If there was a cluster
If (checkcluster)
If (matchlength >= 3)
; Found a horizontal cluster
; clusters.push({ column: i+1-matchlength, row:j,length: matchlength, horizontal: true );
AddElement(clusters())
clusters()\column = i+1-matchlength
clusters()\row = j
clusters()\Length = matchlength
clusters()\horizontal = 1
EndIf
EndIf
matchlength = 1;
Next
Next
; Find vertical clusters
For i=0 To level\columns
; Start With a single tile, cluster of 1
matchlength = 1;
For j=0 To level\rows
checkcluster = 0
If (j = level\rows-1)
; Last tile
checkcluster = 1
Else
; Check the Type of the Next tile
If (level\tiles(i,j)\Typ = level\tiles(i,j+1)\Typ And level\tiles(i,j)\Typ <> -1)
; Same Type As the previous tile, increase matchlength
matchlength + 1;
Else
; Different Type
checkcluster = 1;
EndIf
EndIf
; Check If there was a cluster
If (checkcluster)
If (matchlength >= 3)
; Found a vertical cluster
;clusters.push({ column: i, row:j+1-matchlength,length: matchlength, horizontal: false );
AddElement(clusters())
clusters()\column = i
clusters()\row = j+1-matchlength
clusters()\Length = matchlength
clusters()\horizontal = 0
EndIf
EndIf
matchlength = 1
Next
Next
; Debug ListSize(clusters())
EndProcedure
;Find clusters in the level
Procedure findClusters()
Protected checkcluster
; Reset clusters
ResetList(clusters())
; Find horizontal clusters
For j=0 To level\rows-1
; Start With a single tile, cluster of 1
matchlength = 1;
For i=0 To level\columns-1
checkcluster = #False
If (i = level\columns-1)
; Last tile
checkcluster = #True
Else
; Check the type of the Next tile
If level\tiles(i,j)\typ = level\tiles(i+1,j)\typ And level\tiles(i,j)\typ <> -1
; Same type As the previous tile, increase matchlength
matchlength + 1
If matchlength>=3
;Debug ""+Level\Tiles(i,j)\Typ+"\"+Level\Tiles(i+1,j)\Typ
; Debug ""+i+"\"+j
; Debug ""+Str(i+1)+"\"+Str(j)
; Debug matchlength
EndIf
Else
; Different type
checkcluster = #True
EndIf
EndIf
; Check If there was a cluster
If checkcluster
If matchlength >= 3
; Found a horizontal cluster
AddElement(clusters())
clusters()\column = i+1-matchlength
clusters()\row = j
clusters()\length = matchlength
clusters()\horizontal = #True
EndIf
matchlength = 1;
EndIf
Next
Next
; Find vertical clusters
For i=0 To level\columns-1
; Start With a single tile, cluster of 1
matchlength = 1
For j=0 To level\rows-1
checkcluster = #False
If j = level\rows-1
; Last tile
checkcluster = #True
Else
; Check the type of the Next tile
If level\tiles(i,j)\typ = level\tiles(i,j+1)\typ And level\tiles(i,j)\typ <> -1
; Same type As the previous tile, increase matchlength
matchlength + 1
Else
; Different type
checkcluster = #True
EndIf
EndIf
; Check If there was a cluster
If checkcluster
If matchlength >= 3
; Found a vertical cluster
AddElement(clusters())
clusters()\column = i
clusters()\row = j+1-matchlength
clusters()\length = matchlength
clusters()\horizontal = #False
EndIf
matchlength = 1
EndIf
Next
Next
Debug ListSize(clusters())
EndProcedure
; Find available moves
Procedure findMoves()
; Reset moves
ClearList(moves())
; Check horizontal swaps
For j=0 To level\rows-1
For i=0 To level\columns-1
; Swap, find clusters And Swap back
Swap_(i, j, i+1, j);
findClusters();
Swap_(i, j, i+1, j);
; Check If the Swap made a cluster
If (ListSize(clusters()) > 0)
; Found a move
;moves.push({column1: i, row1: j, column2: i+1, row2: j );
AddElement(moves())
Moves()\column1 = i
Moves()\row1 = j
Moves()\column2 = i+1
Moves()\row2 = j
EndIf
Next
Next
; Check vertical swaps
For i=0 To level\columns-1
For j=0 To level\rows - 1
; Swap, find clusters And Swap back
Swap_(i, j, i, j+1);
findClusters();
Swap_(i, j, i, j+1);
; Check If the Swap made a cluster
If (ListSize(clusters()) > 0)
; Found a move
;moves.push({column1: i, row1: j, column2: i, row2: j+1);
AddElement(moves())
Moves()\column1 = i
Moves()\row1 = j
Moves()\column2 = i
Moves()\row2 = j+1
EndIf
Next
Next
; Reset clusters
ClearList(clusters())
; temporaire
; AddElement(Moves())
Debug "Moves : "+ListSize(moves())
EndProcedure
;}
;{ Game prop
; Timing And frames per second
Global lastframe = 0;
Global fpstime = 0 ;
Global framecount = 0;
Global fps = 0 ;
; Mouse dragging
Global drag = #False;
; Score
Global score = 0;
; Animation variables
Global animationstate = 0;
Global animationtime = 0 ;
Global animationtimetotal.f = 0.3;
; Show available moves
Global showmoves = #False;
; The AI bot
Global aibot = #False;
; Game Over
Global gameover = #False;
;}
;{ Gui buttons & Score
Structure sButton
x.i
y.i
w.i
h.i
t.s
EndStructure
Global Dim buttons.sButton(2)
Procedure SetButton(i,u$)
buttons(i)\x = Val(StringField(u$,1,","))
buttons(i)\y = Val(StringField(u$,2,","))
buttons(i)\w = Val(StringField(u$,3,","))
buttons(i)\h = Val(StringField(u$,4,","))
buttons(i)\t = StringField(u$,5,",")
CreateSprite(10+i,buttons(i)\w,buttons(i)\h,#PB_Sprite_AlphaBlending)
If StartDrawing(SpriteOutput(10+i))
Box (0,0,buttons(i)\w,buttons(i)\h,RGB(50,50,50))
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(10,10,buttons(i)\t)
StopDrawing()
EndIf
EndProcedure
Dim u$(2)
u$(0)="30,240,150,50,New Game"
u$(1)="30,300,150,50,Show Moves"
u$(2)="30,360,150,50, Enable AI Bot"
; Score
Global ScoreW, ScoreH
ScoreW = 250
ScoreH = 30
Procedure UpdateScore()
; si on doit updater le score
If StartDrawing(SpriteOutput(20))
Box (0,0,ScoreW,ScoreH,RGB(50,50,50))
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(5,5,"Score : "+Str(score))
StopDrawing()
EndIf
EndProcedure
Procedure InitScore()
; pour initialiser le score
CreateSprite(20,ScoreW,ScoreH,#PB_Sprite_AlphaBlending)
UpdateScore()
EndProcedure
;}
;{ Initialize the game
Procedure init()
;
; ; Initialize the two-dimensional tile Array
;For i=0 To level\Columns
; For j=0 To level\rows
;level\tiles(i,j)
;Next
;Next
For i=0 To level\columns
; ; leveltiles[i] (0);
For j=0 To level\rows
; ; Define a tile Type And a shift parameter For animation
level\tiles(i,j)\Typ = 0
level\tiles(i,j)\shift = 0
Next
Next
EndProcedure
Procedure GetRandomTile()
ProcedureReturn Random(Maxcol)
EndProcedure
; Create a random level
Procedure CreateLevel()
done = 0
; Keep generating levels Until it is correct
While done = 0
; Create a level With random tiles
For i=0 To level\columns
For j=0 To level\rows
level\tiles(i,j)\Typ = GetRandomTile()
Next
Next
; Resolve the clusters
; resolveClusters();
; Check If there are valid moves
FindMoves();
; Done when there is a valid move
If ListSize(moves())>0
done = 1
EndIf
Wend
EndProcedure
;}
;{ render
Procedure DrawButtons()
For i=0 To 2
DisplayTransparentSprite(10+i, buttons(i)\x, buttons(i)\y)
Next
EndProcedure
Procedure DrawScore()
DisplayTransparentSprite(20,5,5)
EndProcedure
Procedure Render()
ClearScreen(RGB(100,100,100))
; the tiles
u = 68
e = 100 ; sprite de départ des tiles
w = 200
For i=0 To level\columns
For j=0 To level\rows
DisplayTransparentSprite(e,w+i*u,50+j*u,255,Col(level\tiles(i,j)\Typ))
e=e+1
Next
Next
; draw score
DrawScore()
; draw buttons
DrawButtons()
FlipBuffers()
EndProcedure
;}
Init()
CreateLevel()
;{ openwindow
screenwidth = 1024
screenheight = 768
If OpenWindow(0, 0, 0, screenwidth, screenheight, "Hit Shiby", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) : EndIf
If OpenWindowedScreen(WindowID(0),0,0,screenwidth, screenheight)=0
MessageRequester("Error", "Can't Open Screen!", 0)
End
EndIf
; on crée les sprites
; on crée les buttons
For i=0 To 2
SetButton(i,u$(i))
Next
; score
InitScore()
; les tiles
e=100
For i=0 To level\columns
For j=0 To level\rows
CreateSprite(e,64,64,#PB_Sprite_AlphaBlending)
e=e+1
Next
Next
;}
Repeat
Repeat
EventID = WindowEvent()
Select EventID
Case #PB_Event_CloseWindow
End
EndSelect
Until event = 0
Render()
Until Quit = 1