Sign in Join
Wiki copytemporary

15-puzzle/fi: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## 15-peli
22
33![](/wiki-images/2/20/Puzzle15.png)
44
5515-peli on liukupalapeli joka koostuu kehyksestä jossa on numeroituja neliömäisiä laattoja satunnaisessa järjestyksessä missä yksi laatta puuttuu. Pelissä tavoitteena on sijoittaa laatat järjestykseen liu'uttamalla siirrettävä pala tyhjään tilaan.
66
77## Luodaan ohjelmakoodi
88
99- Luo uusi tyhjä graafinen sovellus lomakkeella [[TForm/fi|Form1]]
1010- Muuta lomakkeen Form1 ominaisuus caption arvoon *15Puzzle*'.
1111- Lisää uusi käännösyksikkö [[15-puzzle/fi|unitGameBoard.pas]]
1212- Luodaan OnCreate tapahtumakäsittelijä lomakkeelle, käyttämällä komponenttimuokkaimen tapahtumat-välilehteä, valitsemalla OnCreate tapahtuma, klikkaamalla sen \[...\] painiketta tai kaksoisklikkaamalla painiketta lomakkeella.
1313- Lisää siihen seuraava koodi:
1414
1515``` pascal
1616procedure TForm1.FormCreate(Sender: TObject);
1717var
1818 i : Integer;
1919 aButton: TButton;
2020 point:TPoint;
2121begin
2222 Randomize;
2323 Puzzle15:= TGameBoard.Create;
2424 i := 1;
2525 while i < 16 do
2626 begin //luodaan 15 painiketta
2727 aButton:=TButton.Create(Self); //luodaan Button, Owner on Form1, kun painike myöhemmin julkaistaan
2828 aButton.Parent:=Self; //määrittää, missä se on näytettävä
2929 aButton.Caption:=IntToStr(i); //Painikkeessa näkyvä teksti
3030 aButton.Width:=aButton.Height; //Painike on neliö
3131 point := Puzzle15.ButtonPlace(aButton.Caption);
3232 aButton.Left:= point.x* aButton.Width; //painikkeen vasemman reunan etäisyys
3333 aButton.Top := point.y* aButton.Height;
3434
3535 aButton.OnClick:=@aButtonClick; //painikkeen tapahtumakäsittelijä
3636 inc(i);
3737 end;
3838 Self.Height:=aButton.Height*6; //lomakkeen korkeuden määritys
3939 Self.Width:=aButton.Width*6; //lomakkeen leveyden määritys
4040end;
4141```
4242
4343- Luodaan komponenttimuokkaimella tapahtumakäsittelijä tapahtumalle FormDestroy, klikkaamalla sen painiketta \[...\].
4444- Täydennetään tapahtuman koodi seuraavaksi:
4545
4646``` pascal
4747procedure TForm1.FormDestroy(Sender: TObject);
4848begin
4949 FreeAndNil(Puzzle15);
5050end;
5151```
5252
5353- Seuraavaksi luodaan tapahtumankäsittelijä painikkeiden klikkaukselle.
5454- Lähdekoodieditorissa siirrytään luokan TForm1 esittelyn private osaan.
5555- Lisää [[Procedure/fi|aliohjelma]] `procedure aButtonClick (Sender: TObject);` ja paina näppäimiä `Ctrl`+`Shift`+`c` (Koodin täydennys aktivoituu ja luo aliohjelman rungon `procedure TForm1.aButtonClick (Sender: TObject) ;`.
5656- Täydennetään se tälläiseksi:
5757
5858``` pascal
5959procedure TForm1.aButtonClick(Sender: TObject); //Painikkeen tapahtumankäsittelijä
6060var
6161 i:integer;
6262 point : TPoint;
6363begin
6464 if (Sender is TButton) and //Tarkistetaan että tapahtuman tultiin näppäimen painalluksella
6565 TryStrToInt(TButton(Sender).Caption, i) // ja onko sen teksti kokonaisluku
6666 then
6767 begin
6868 if Puzzle15.CanMove(TButton(Sender).Caption) then
6969 begin
7070 point := Puzzle15.Change(TButton(Sender).Caption);
7171 TButton(Sender).Left:= point.x* TButton(Sender).Width;
7272 TButton(Sender).Top := point.y* TButton(Sender).Height;
7373 end;
7474 end;
7575end;
7676```
7777
7878## Lähdekoodi
7979
8080### unit1.pas
8181
8282``` pascal
8383unit Unit1;
8484
8585{$mode objfpc}{$H+}
8686
8787interface
8888
8989uses
9090 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9191 unitGameBoard;
9292
9393type
9494
9595 { TForm1 }
9696
9797 TForm1 = class(TForm)
9898 procedure FormCreate(Sender: TObject);
9999 procedure FormDestroy(Sender: TObject);
100100 private
101101 { private declarations }
102102 Puzzle15:TGameBoard;
103103 procedure aButtonClick(Sender: TObject);
104104
105105 public
106106 { public declarations }
107107 end;
108108
109109var
110110 Form1: TForm1;
111111
112112implementation
113113
114114{$R *.lfm}
115115
116116{ TForm1 }
117117
118118procedure TForm1.FormCreate(Sender: TObject);
119119var
120120 i : Integer;
121121 aButton: TButton;
122122 point:TPoint;
123123begin
124124 Randomize;
125125 Puzzle15:= TGameBoard.Create;
126126 i := 1;
127127 while i < 16 do
128128 begin //luodaan 15 painiketta
129129 aButton:=TButton.Create(Self); //luodaan Button, Owner on Form1, kun painike myöhemmin julkaistaan
130130 aButton.Parent:=Self; //määrittää, missä se on näytettävä
131131 aButton.Caption:=IntToStr(i); //Painikkeessa näkyvä teksti
132132 aButton.Width:=aButton.Height; //Painike on neliö
133133 point := Puzzle15.ButtonPlace(aButton.Caption);
134134 aButton.Left:= point.x* aButton.Width; //painikkeen vasemman reunan etäisyys
135135 aButton.Top := point.y* aButton.Height;
136136
137137 aButton.OnClick:=@aButtonClick; //painikkeen tapahtumakäsittelijä
138138 inc(i);
139139 end;
140140 Self.Height:=aButton.Height*6; //lomakkeen korkeuden määritys
141141 Self.Width:=aButton.Width*6; //lomakkeen leveyden määritys
142142end;
143143
144144procedure TForm1.FormDestroy(Sender: TObject);
145145begin
146146 FreeAndNil(Puzzle15);
147147end;
148148
149149procedure TForm1.aButtonClick(Sender: TObject); //Painikkeen tapahtumankäsittelijä
150150var
151151 i:integer;
152152 point : TPoint;
153153begin
154154 if (Sender is TButton) and //Tarkistetaan että tapahtuman tultiin näppäimen painalluksella
155155 TryStrToInt(TButton(Sender).Caption, i) // ja onko sen teksti kokonaisluku
156156 then
157157 begin
158158 if Puzzle15.CanMove(TButton(Sender).Caption) then
159159 begin
160160 point := Puzzle15.Change(TButton(Sender).Caption);
161161 TButton(Sender).Left:= point.x* TButton(Sender).Width;
162162 TButton(Sender).Top := point.y* TButton(Sender).Height;
163163 end;
164164 end;
165165end;
166166
167167end.
168168```
169169
170170### Unit1.lfm
171171
172-``` pascal
172+``` lfm
173173object Form1: TForm1
174174 Left = 362
175175 Height = 184
176176 Top = 162
177177 Width = 432
178178 Caption = '15Puzzle'
179179 OnCreate = FormCreate
180180 OnDestroy = FormDestroy
181181 LCLVersion = '1.6.0.4'
182182end
183183```
184184
185185### unitGameBoard.pas
186186
187187``` pascal
188188unit unitGameBoard;
189189
190190{$mode objfpc}{$H+}
191191
192192interface
193193
194194uses
195195 Classes, SysUtils;
196196
197197const
198198 GB_MAX_BUTTONS = 15;
199199 GB_MAX_X = 4;
200200 GB_MAX_Y = 4;
201201 GB_EMPTY_PLACE ='-';
202202type
203203
204204 { TGameBoard }
205205
206206 TGameBoard = class
207207 private
208208 GameArray:array[1..GB_MAX_X, 1..GB_MAX_Y] of string;
209209 function ValidPoint(a_point:TPoint):boolean;
210210 public
211211 constructor Create;
212212 function ButtonPlace(a_name:string):TPoint;
213213 function CanMove(a_name:string):Boolean;
214214 function Change(a_name: string): TPoint;
215215 procedure RandomArray;
216216 destructor Destroy; override;
217217 end;
218218implementation
219219
220220{ TGameBoard }
221221
222222function TGameBoard.ValidPoint(a_point: TPoint): boolean;
223223begin
224224 result := true;
225225 if a_point.y > GB_MAX_Y then result := false;
226226 if a_point.x > GB_MAX_X then result := false;
227227 if a_point.y < 1 then result := false;
228228 if a_point.x < 1 then result := false;
229229end;
230230
231231constructor TGameBoard.Create;
232232var
233233 i, x, y : integer;
234234begin
235235 i := 1;
236236 for y := 1 to GB_MAX_Y do
237237 for x := 1 to GB_MAX_X do
238238 begin
239239 if i < GB_MAX_BUTTONS+1
240240 then GameArray[x,y] := IntToStr(i)
241241 else GameArray[x,y] := GB_EMPTY_PLACE;
242242 inc(i);
243243 end;
244244 RandomArray;
245245end;
246246
247247function TGameBoard.ButtonPlace(a_name: string): TPoint;
248248var
249249 x,y:integer;
250250begin
251251 result.x := 0;
252252 result.y := 0;
253253 for y := 1 to GB_MAX_Y do
254254 for x := 1 to GB_MAX_X do
255255 if GameArray[x,y] = a_name then
256256 begin
257257 result.x := x;
258258 result.y := y;
259259 end;
260260end;
261261
262262function TGameBoard.CanMove(a_name: string): Boolean;
263263var
264264 empty, point:TPoint;
265265begin
266266 result := false;
267267 empty := ButtonPlace( GB_EMPTY_PLACE );
268268 point := ButtonPlace(a_name);
269269 if (abs(empty.x-point.x)=1) and (empty.y=point.y) then result := true;
270270 if (abs(empty.y-point.y)=1) and (empty.x=point.x) then result := true;
271271end;
272272
273273function TGameBoard.Change(a_name: string): TPoint;
274274var
275275 empty, point:TPoint;
276276begin
277277 empty := ButtonPlace( GB_EMPTY_PLACE );
278278 result := empty;
279279 point := ButtonPlace(a_name);
280280 GameArray[point.x, point.y] := GB_EMPTY_PLACE;
281281 GameArray[empty.x, empty.y] := a_name;
282282end;
283283
284284procedure TGameBoard.RandomArray;
285285var
286286 j,i:integer;
287287 name:string;
288288 point,point2:TPoint;
289289begin
290290 for i := 0 to 1000 do
291291 begin
292292 j := random(4);
293293 point := ButtonPlace( GB_EMPTY_PLACE );
294294 point2 := point;
295295 case j of
296296 0: point2.x := point.x-1;
297297 1: point2.x := point.x+1;
298298 2: point2.y := point.y-1;
299299 3: point2.y := point.y+1;
300300 end;
301301 if ValidPoint( point2 ) then
302302 begin
303303 name := GameArray[point2.x, point2.y];
304304 GameArray[point.x, point.y] := name;
305305 GameArray[point2.x, point2.y] := GB_EMPTY_PLACE;
306306 end;
307307 end;
308308end;
309309
310310destructor TGameBoard.Destroy;
311311begin
312312 inherited Destroy;
313313end;
314314
315315end.
316316```
317317
318318## Katso myös
319319
320320- [[TButton/fi|TButton]]
321321
322322---
323323*From the Free Pascal wiki, page "15-puzzle/fi", as it stood on 2022-08-06 after a change by Kai Burghardt, 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).*

Back to history