15-puzzle/fr: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ## Taquin | |
| 2 | 2 | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 6 | - | ||
| 3 | + |  | |
| 7 | 4 | ||
| 8 | 5 | Le taquin est un casse-tête avec des pièces glissantes qui consiste en un cadre de tuiles carrées numérotées dans un ordre aléatoire avec une tuile manquante. Le but du jeu consiste à replacer les tuiles dans l'ordre à l'aide de glissements qui utilise la place vide. | |
| 9 | 6 | ||
| 10 | 7 | ## Créer le code | |
| 11 | 8 | ||
| 12 | 9 | - Créer une nouvelle application IHM avec la fiche Form1 | |
| 13 | 10 | - Changer le titre de Form1 en *15Puzzle*'. | |
| 14 | 11 | - Ajouter la nouvelle unité unitGameBoard.pas | |
| 15 | 12 | - Créer le gestionnaire d'événement OnCreate pour la fiche, en cliquant sur votre fiche, utiliser l'inspecteur d'objets, l'onglet Evénement, sélectionner OnCreate et cliquer sur le bouton \[...\] ou double-cliquer directement sur la fiche. | |
| 16 | 13 | - Ajouter le code suivant : | |
| 17 | 14 | ||
| 18 | 15 | ``` pascal | |
| 19 | 16 | procedure TForm1.FormCreate(Sender: TObject); | |
| 20 | 17 | var | |
| 21 | 18 | i : Integer; | |
| 22 | 19 | aButton: TButton; | |
| 23 | 20 | point:TPoint; | |
| 24 | 21 | begin | |
| 25 | 22 | Randomize; | |
| 26 | 23 | Puzzle15:= TGameBoard.Create; | |
| 27 | 24 | i := 1; | |
| 28 | 25 | while i < 16 do | |
| 29 | 26 | begin //create 15 Buttons | |
| 30 | 27 | aButton:=TButton.Create(Self); //create Button, Owner is Form1, where the button is released later | |
| 31 | 28 | aButton.Parent:=Self; //determine where it is to be displayed | |
| 32 | 29 | aButton.Caption:=IntToStr(i); //Captions of the buttons | |
| 33 | 30 | aButton.Width:=aButton.Height; //Width should correspond to the height of the buttons | |
| 34 | 31 | point := Puzzle15.ButtonPlace(aButton.Caption); | |
| 35 | 32 | aButton.Left:= point.x* aButton.Width; //Distance from left | |
| 36 | 33 | aButton.Top := point.y* aButton.Height; | |
| 37 | 34 | ||
| 38 | 35 | aButton.OnClick:=@aButtonClick; //the event handler for the button -> will be created yet | |
| 39 | 36 | inc(i); | |
| 40 | 37 | end; | |
| 41 | 38 | Self.Height:=aButton.Height*6; //Height of the form should correspond to the height of the buttons | |
| 42 | 39 | Self.Width:=aButton.Width*6; //Width of the form to match the width of all buttons | |
| 43 | 40 | end; | |
| 44 | 41 | ``` | |
| 45 | 42 | ||
| 46 | 43 | - Créer dans l'inspecteur d'objet, le gestionnaire pour l'événement FormDestroy, en cliquant sur le bouton \[...\]. | |
| 47 | 44 | - Ajouter le code suivant dans le gestionnaire : | |
| 48 | 45 | ||
| 49 | 46 | ``` pascal | |
| 50 | 47 | procedure TForm1.FormDestroy(Sender: TObject); | |
| 51 | 48 | begin | |
| 52 | 49 | FreeAndNil(Puzzle15); | |
| 53 | 50 | end; | |
| 54 | 51 | ``` | |
| 55 | 52 | ||
| 56 | 53 | - Maintenant vous devez créer le gestionnaire d'événement pour les clics de bouton. | |
| 57 | 54 | - Dans l'éditeur de source, en entrant dans la section private de votre classe TForm1. | |
| 58 | - | ||
| 55 | + | - Ajouter la procédure aButtonClick(Sender: TObject); and ensuite pressez la touche `Ctrl`+`Shift`+`c` (la complétion de code devient active et crée la procédure TForm1.aButtonClick(Sender: TObject);. | |
| 59 | 56 | - Coller le code suivant : | |
| 60 | 57 | ||
| 61 | 58 | ``` pascal | |
| 62 | 59 | procedure TForm1.aButtonClick(Sender: TObject); //the event handler for the button | |
| 63 | 60 | var | |
| 64 | 61 | i:integer; | |
| 65 | 62 | point : TPoint; | |
| 66 | 63 | begin | |
| 67 | 64 | if (Sender is TButton) and //called the event handler of a button out? | |
| 68 | 65 | TryStrToInt(TButton(Sender).Caption, i) //then try to convert the label in a integer | |
| 69 | 66 | then | |
| 70 | 67 | begin | |
| 71 | 68 | if Puzzle15.CanMove(TButton(Sender).Caption) then | |
| 72 | 69 | begin | |
| 73 | 70 | point := Puzzle15.Change(TButton(Sender).Caption); | |
| 74 | 71 | TButton(Sender).Left:= point.x* TButton(Sender).Width; //Distance from left | |
| 75 | 72 | TButton(Sender).Top := point.y* TButton(Sender).Height; | |
| 76 | 73 | end; | |
| 77 | 74 | end; | |
| 78 | 75 | end; | |
| 79 | 76 | ``` | |
| 80 | 77 | ||
| 81 | 78 | ## Code source | |
| 82 | 79 | ||
| 83 | 80 | ### unit1.pas | |
| 84 | 81 | ||
| 85 | 82 | ``` pascal | |
| 86 | 83 | unit Unit1; | |
| 87 | 84 | ||
| 88 | 85 | {$mode objfpc}{$H+} | |
| 89 | 86 | ||
| 90 | 87 | interface | |
| 91 | 88 | ||
| 92 | 89 | uses | |
| 93 | 90 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, | |
| 94 | 91 | unitGameBoard; | |
| 95 | 92 | ||
| 96 | 93 | type | |
| 97 | 94 | ||
| 98 | 95 | { TForm1 } | |
| 99 | 96 | ||
| 100 | 97 | TForm1 = class(TForm) | |
| 101 | 98 | procedure FormCreate(Sender: TObject); | |
| 102 | 99 | procedure FormDestroy(Sender: TObject); | |
| 103 | 100 | private | |
| 104 | 101 | { private declarations } | |
| 105 | 102 | Puzzle15:TGameBoard; | |
| 106 | 103 | procedure aButtonClick(Sender: TObject); | |
| 107 | 104 | ||
| 108 | 105 | public | |
| 109 | 106 | { public declarations } | |
| 110 | 107 | end; | |
| 111 | 108 | ||
| 112 | 109 | var | |
| 113 | 110 | Form1: TForm1; | |
| 114 | 111 | ||
| 115 | 112 | implementation | |
| 116 | 113 | ||
| 117 | 114 | {$R *.lfm} | |
| 118 | 115 | ||
| 119 | 116 | { TForm1 } | |
| 120 | 117 | ||
| 121 | 118 | procedure TForm1.FormCreate(Sender: TObject); | |
| 122 | 119 | var | |
| 123 | 120 | i : Integer; | |
| 124 | 121 | aButton: TButton; | |
| 125 | 122 | point:TPoint; | |
| 126 | 123 | begin | |
| 127 | 124 | Randomize; | |
| 128 | 125 | Puzzle15:= TGameBoard.Create; | |
| 129 | 126 | i := 1; | |
| 130 | 127 | while i < 16 do | |
| 131 | 128 | begin //create 15 Buttons | |
| 132 | 129 | aButton:=TButton.Create(Self); //create Button, Owner is Form1, where the button is released later | |
| 133 | 130 | aButton.Parent:=Self; //determine where it is to be displayed | |
| 134 | 131 | aButton.Caption:=IntToStr(i); //Captions of the buttons | |
| 135 | 132 | aButton.Width:=aButton.Height; //Width should correspond to the height of the buttons | |
| 136 | 133 | point := Puzzle15.ButtonPlace(aButton.Caption); | |
| 137 | 134 | aButton.Left:= point.x* aButton.Width; //Distance from left | |
| 138 | 135 | aButton.Top := point.y* aButton.Height; | |
| 139 | 136 | aButton.OnClick:=@aButtonClick; //the event handler for the button -> will be created yet | |
| 140 | 137 | inc(i); | |
| 141 | 138 | end; | |
| 142 | 139 | Self.Height:=aButton.Height*6; //Height of the form should correspond to the height of the buttons | |
| 143 | 140 | Self.Width:=aButton.Width*6; //Width of the form to match the width of all buttons | |
| 144 | 141 | end; | |
| 145 | 142 | ||
| 146 | 143 | procedure TForm1.FormDestroy(Sender: TObject); | |
| 147 | 144 | begin | |
| 148 | 145 | FreeAndNil(Puzzle15); | |
| 149 | 146 | end; | |
| 150 | 147 | ||
| 151 | 148 | procedure TForm1.aButtonClick(Sender: TObject); //the event handler for the button | |
| 152 | 149 | var | |
| 153 | 150 | i:integer; | |
| 154 | 151 | point : TPoint; | |
| 155 | 152 | begin | |
| 156 | 153 | if (Sender is TButton) and //called the event handler of a button out? | |
| 157 | 154 | TryStrToInt(TButton(Sender).Caption, i) //then try to convert the label in a integer | |
| 158 | 155 | then | |
| 159 | 156 | begin | |
| 160 | 157 | if Puzzle15.CanMove(TButton(Sender).Caption) then | |
| 161 | 158 | begin | |
| 162 | 159 | point := Puzzle15.Change(TButton(Sender).Caption); | |
| 163 | 160 | TButton(Sender).Left:= point.x* TButton(Sender).Width; //Distance from left | |
| 164 | 161 | TButton(Sender).Top := point.y* TButton(Sender).Height; | |
| 165 | 162 | end; | |
| 166 | 163 | end; | |
| 167 | 164 | end; | |
| 168 | 165 | ||
| 169 | 166 | end. | |
| 170 | 167 | ``` | |
| 171 | 168 | ||
| 172 | 169 | ### Unit1.lfm | |
| 173 | 170 | ||
| 174 | 171 | ``` pascal | |
| 175 | 172 | object Form1: TForm1 | |
| 176 | 173 | Left = 362 | |
| 177 | 174 | Height = 184 | |
| 178 | 175 | Top = 162 | |
| 179 | 176 | Width = 432 | |
| 180 | 177 | Caption = '15Puzzle' | |
| 181 | 178 | OnCreate = FormCreate | |
| 182 | 179 | OnDestroy = FormDestroy | |
| 183 | 180 | LCLVersion = '1.6.0.4' | |
| 184 | 181 | end | |
| 185 | 182 | ``` | |
| 186 | 183 | ||
| 187 | 184 | ### unitGameBoard.pas | |
| 188 | 185 | ||
| 189 | 186 | ``` pascal | |
| 190 | 187 | unit unitGameBoard; | |
| 191 | 188 | ||
| 192 | 189 | {$mode objfpc}{$H+} | |
| 193 | 190 | ||
| 194 | 191 | interface | |
| 195 | 192 | ||
| 196 | 193 | uses | |
| 197 | 194 | Classes, SysUtils; | |
| 198 | 195 | ||
| 199 | 196 | const | |
| 200 | 197 | GB_MAX_BUTTONS = 15; | |
| 201 | 198 | GB_MAX_X = 4; | |
| 202 | 199 | GB_MAX_Y = 4; | |
| 203 | 200 | GB_EMPTY_PLACE ='-'; | |
| 204 | 201 | type | |
| 205 | 202 | ||
| 206 | 203 | { TGameBoard } | |
| 207 | 204 | ||
| 208 | 205 | TGameBoard = class | |
| 209 | 206 | private | |
| 210 | 207 | GameArray:array[1..GB_MAX_X, 1..GB_MAX_Y] of string; | |
| 211 | 208 | function ValidPoint(a_point:TPoint):boolean; | |
| 212 | 209 | public | |
| 213 | 210 | constructor Create; | |
| 214 | 211 | function ButtonPlace(a_name:string):TPoint; | |
| 215 | 212 | function CanMove(a_name:string):Boolean; | |
| 216 | 213 | function Change(a_name: string): TPoint; | |
| 217 | 214 | procedure RandomArray; | |
| 218 | 215 | destructor Destroy; override; | |
| 219 | 216 | end; | |
| 220 | 217 | implementation | |
| 221 | 218 | ||
| 222 | 219 | { TGameBoard } | |
| 223 | 220 | ||
| 224 | 221 | function TGameBoard.ValidPoint(a_point: TPoint): boolean; | |
| 225 | 222 | begin | |
| 226 | 223 | result := true; | |
| 227 | 224 | if a_point.y > GB_MAX_Y then result := false; | |
| 228 | 225 | if a_point.x > GB_MAX_X then result := false; | |
| 229 | 226 | if a_point.y < 1 then result := false; | |
| 230 | 227 | if a_point.x < 1 then result := false; | |
| 231 | 228 | end; | |
| 232 | 229 | ||
| 233 | 230 | constructor TGameBoard.Create; | |
| 234 | 231 | var | |
| 235 | 232 | i, x, y : integer; | |
| 236 | 233 | begin | |
| 237 | 234 | i := 1; | |
| 238 | 235 | for y := 1 to GB_MAX_Y do | |
| 239 | 236 | for x := 1 to GB_MAX_X do | |
| 240 | 237 | begin | |
| 241 | 238 | if i < GB_MAX_BUTTONS+1 | |
| 242 | 239 | then GameArray[x,y] := IntToStr(i) | |
| 243 | 240 | else GameArray[x,y] := GB_EMPTY_PLACE; | |
| 244 | 241 | inc(i); | |
| 245 | 242 | end; | |
| 246 | 243 | RandomArray; | |
| 247 | 244 | end; | |
| 248 | 245 | ||
| 249 | 246 | function TGameBoard.ButtonPlace(a_name: string): TPoint; | |
| 250 | 247 | var | |
| 251 | 248 | x,y:integer; | |
| 252 | 249 | begin | |
| 253 | 250 | result.x := 0; | |
| 254 | 251 | result.y := 0; | |
| 255 | 252 | for y := 1 to GB_MAX_Y do | |
| 256 | 253 | for x := 1 to GB_MAX_X do | |
| 257 | 254 | if GameArray[x,y] = a_name then | |
| 258 | 255 | begin | |
| 259 | 256 | result.x := x; | |
| 260 | 257 | result.y := y; | |
| 261 | 258 | end; | |
| 262 | 259 | end; | |
| 263 | 260 | ||
| 264 | 261 | function TGameBoard.CanMove(a_name: string): Boolean; | |
| 265 | 262 | var | |
| 266 | 263 | empty, point:TPoint; | |
| 267 | 264 | begin | |
| 268 | 265 | result := false; | |
| 269 | 266 | empty := ButtonPlace( GB_EMPTY_PLACE ); | |
| 270 | 267 | point := ButtonPlace(a_name); | |
| 271 | 268 | if (abs(empty.x-point.x)=1) and (empty.y=point.y) then result := true; | |
| 272 | 269 | if (abs(empty.y-point.y)=1) and (empty.x=point.x) then result := true; | |
| 273 | 270 | end; | |
| 274 | 271 | ||
| 275 | 272 | function TGameBoard.Change(a_name: string): TPoint; | |
| 276 | 273 | var | |
| 277 | 274 | empty, point:TPoint; | |
| 278 | 275 | begin | |
| 279 | 276 | empty := ButtonPlace( GB_EMPTY_PLACE ); | |
| 280 | 277 | result := empty; | |
| 281 | 278 | point := ButtonPlace(a_name); | |
| 282 | 279 | GameArray[point.x, point.y] := '-'; | |
| 283 | 280 | GameArray[empty.x, empty.y] := a_name; | |
| 284 | 281 | end; | |
| 285 | 282 | ||
| 286 | 283 | procedure TGameBoard.RandomArray; | |
| 287 | 284 | var | |
| 288 | 285 | j,i:integer; | |
| 289 | 286 | name:string; | |
| 290 | 287 | point,point2:TPoint; | |
| 291 | 288 | begin | |
| 292 | 289 | for i := 0 to 1000 do | |
| 293 | 290 | begin | |
| 294 | 291 | j := random(4); | |
| 295 | 292 | point := ButtonPlace( GB_EMPTY_PLACE ); | |
| 296 | 293 | point2 := point; | |
| 297 | 294 | case j of | |
| 298 | 295 | 0: point2.x := point.x-1; | |
| 299 | 296 | 1: point2.x := point.x+1; | |
| 300 | 297 | 2: point2.y := point.y-1; | |
| 301 | 298 | 3: point2.y := point.y+1; | |
| 302 | 299 | end; | |
| 303 | 300 | if ValidPoint( point2 ) then | |
| 304 | 301 | begin | |
| 305 | 302 | name := GameArray[point2.x, point2.y]; | |
| 306 | 303 | GameArray[point.x, point.y] := name; | |
| 307 | 304 | GameArray[point2.x, point2.y] := GB_EMPTY_PLACE; | |
| 308 | 305 | end; | |
| 309 | 306 | end; | |
| 310 | 307 | end; | |
| 311 | 308 | ||
| 312 | 309 | destructor TGameBoard.Destroy; | |
| 313 | 310 | begin | |
| 314 | 311 | inherited Destroy; | |
| 315 | 312 | end; | |
| 316 | 313 | ||
| 317 | 314 | end. | |
| 318 | 315 | ``` | |
| 319 | 316 | ||
| 320 | 317 | ## Voir aussi | |
| 321 | 318 | ||
| 322 | 319 | - [[TButton/fr|TButton]] | |
| 323 | 320 | - [Jeu de Taquin](https://fr.wikipedia.org/wiki/Taquin) | |
| 324 | - | ||
| 325 | - | ||
| 326 | 321 | ||
| 327 | 322 | --- | |
| 328 | 323 | *From the Free Pascal wiki, page "15-puzzle/fr", as it stood on 2020-02-01 after a change by GAN, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
