Sign in Join
Wiki copytemporary

15-puzzle/fi: what changed

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

OldNewLine
11## 15-peli
22
3-<figure>
4-<img src="puzzle15.png" title="puzzle15.png" />
5-<figcaption>puzzle15.png</figcaption>
6-</figure>
3+![](/wiki-images/2/20/Puzzle15.png)
74
8515-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.
96
107## Luodaan ohjelmakoodi
118
129- Luo uusi tyhjä graafinen sovellus lomakkeella [[TForm/fi|Form1]]
1310- Muuta lomakkeen Form1 ominaisuus caption arvoon *15Puzzle*'.
1411- Lisää uusi käännösyksikkö [[15-puzzle/fi|unitGameBoard.pas]]
1512- Luodaan OnCreate tapahtumakäsittelijä lomakkeelle, käyttämällä komponenttimuokkaimen tapahtumat-välilehteä, valitsemalla OnCreate tapahtuma, klikkaamalla sen \[...\] painiketta tai kaksoisklikkaamalla painiketta lomakkeella.
1613- Lisää siihen seuraava koodi:
1714
1815``` pascal
1916procedure TForm1.FormCreate(Sender: TObject);
2017var
2118 i : Integer;
2219 aButton: TButton;
2320 point:TPoint;
2421begin
2522 Randomize;
2623 Puzzle15:= TGameBoard.Create;
2724 i := 1;
2825 while i < 16 do
2926 begin //luodaan 15 painiketta
3027 aButton:=TButton.Create(Self); //luodaan Button, Owner on Form1, kun painike myöhemmin julkaistaan
3128 aButton.Parent:=Self; //määrittää, missä se on näytettävä
3229 aButton.Caption:=IntToStr(i); //Painikkeessa näkyvä teksti
3330 aButton.Width:=aButton.Height; //Painike on neliö
3431 point := Puzzle15.ButtonPlace(aButton.Caption);
3532 aButton.Left:= point.x* aButton.Width; //painikkeen vasemman reunan etäisyys
3633 aButton.Top := point.y* aButton.Height;
3734
3835 aButton.OnClick:=@aButtonClick; //painikkeen tapahtumakäsittelijä
3936 inc(i);
4037 end;
4138 Self.Height:=aButton.Height*6; //lomakkeen korkeuden määritys
4239 Self.Width:=aButton.Width*6; //lomakkeen leveyden määritys
4340end;
4441```
4542
4643- Luodaan komponenttimuokkaimella tapahtumakäsittelijä tapahtumalle FormDestroy, klikkaamalla sen painiketta \[...\].
4744- Täydennetään tapahtuman koodi seuraavaksi:
4845
4946``` pascal
5047procedure TForm1.FormDestroy(Sender: TObject);
5148begin
5249 FreeAndNil(Puzzle15);
5350end;
5451```
5552
5653- Seuraavaksi luodaan tapahtumankäsittelijä painikkeiden klikkaukselle.
5754- Lähdekoodieditorissa siirrytään luokan TForm1 esittelyn private osaan.
58-- Lisää [[Procedure/fi|aliohjelma]] `procedure aButtonClick (Sender: TObject);` ja paina näppäimiä <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">c</span> (Koodin täydennys aktivoituu ja luo aliohjelman rungon `procedure TForm1.aButtonClick (Sender: TObject) ;`.
55+- 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) ;`.
5956- Täydennetään se tälläiseksi:
6057
6158``` pascal
6259procedure TForm1.aButtonClick(Sender: TObject); //Painikkeen tapahtumankäsittelijä
6360var
6461 i:integer;
6562 point : TPoint;
6663begin
6764 if (Sender is TButton) and //Tarkistetaan että tapahtuman tultiin näppäimen painalluksella
6865 TryStrToInt(TButton(Sender).Caption, i) // ja onko sen teksti kokonaisluku
6966 then
7067 begin
7168 if Puzzle15.CanMove(TButton(Sender).Caption) then
7269 begin
7370 point := Puzzle15.Change(TButton(Sender).Caption);
7471 TButton(Sender).Left:= point.x* TButton(Sender).Width;
7572 TButton(Sender).Top := point.y* TButton(Sender).Height;
7673 end;
7774 end;
7875end;
7976```
8077
8178## Lähdekoodi
8279
8380### unit1.pas
8481
8582``` pascal
8683unit Unit1;
8784
8885{$mode objfpc}{$H+}
8986
9087interface
9188
9289uses
9390 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9491 unitGameBoard;
9592
9693type
9794
9895 { TForm1 }
9996
10097 TForm1 = class(TForm)
10198 procedure FormCreate(Sender: TObject);
10299 procedure FormDestroy(Sender: TObject);
103100 private
104101 { private declarations }
105102 Puzzle15:TGameBoard;
106103 procedure aButtonClick(Sender: TObject);
107104
108105 public
109106 { public declarations }
110107 end;
111108
112109var
113110 Form1: TForm1;
114111
115112implementation
116113
117114{$R *.lfm}
118115
119116{ TForm1 }
120117
121118procedure TForm1.FormCreate(Sender: TObject);
122119var
123120 i : Integer;
124121 aButton: TButton;
125122 point:TPoint;
126123begin
127124 Randomize;
128125 Puzzle15:= TGameBoard.Create;
129126 i := 1;
130127 while i < 16 do
131128 begin //luodaan 15 painiketta
132129 aButton:=TButton.Create(Self); //luodaan Button, Owner on Form1, kun painike myöhemmin julkaistaan
133130 aButton.Parent:=Self; //määrittää, missä se on näytettävä
134131 aButton.Caption:=IntToStr(i); //Painikkeessa näkyvä teksti
135132 aButton.Width:=aButton.Height; //Painike on neliö
136133 point := Puzzle15.ButtonPlace(aButton.Caption);
137134 aButton.Left:= point.x* aButton.Width; //painikkeen vasemman reunan etäisyys
138135 aButton.Top := point.y* aButton.Height;
139136
140137 aButton.OnClick:=@aButtonClick; //painikkeen tapahtumakäsittelijä
141138 inc(i);
142139 end;
143140 Self.Height:=aButton.Height*6; //lomakkeen korkeuden määritys
144141 Self.Width:=aButton.Width*6; //lomakkeen leveyden määritys
145142end;
146143
147144procedure TForm1.FormDestroy(Sender: TObject);
148145begin
149146 FreeAndNil(Puzzle15);
150147end;
151148
152149procedure TForm1.aButtonClick(Sender: TObject); //Painikkeen tapahtumankäsittelijä
153150var
154151 i:integer;
155152 point : TPoint;
156153begin
157154 if (Sender is TButton) and //Tarkistetaan että tapahtuman tultiin näppäimen painalluksella
158155 TryStrToInt(TButton(Sender).Caption, i) // ja onko sen teksti kokonaisluku
159156 then
160157 begin
161158 if Puzzle15.CanMove(TButton(Sender).Caption) then
162159 begin
163160 point := Puzzle15.Change(TButton(Sender).Caption);
164161 TButton(Sender).Left:= point.x* TButton(Sender).Width;
165162 TButton(Sender).Top := point.y* TButton(Sender).Height;
166163 end;
167164 end;
168165end;
169166
170167end.
171168```
172169
173170### Unit1.lfm
174171
175172``` pascal
176173object Form1: TForm1
177174 Left = 362
178175 Height = 184
179176 Top = 162
180177 Width = 432
181178 Caption = '15Puzzle'
182179 OnCreate = FormCreate
183180 OnDestroy = FormDestroy
184181 LCLVersion = '1.6.0.4'
185182end
186183```
187184
188185### unitGameBoard.pas
189186
190187``` pascal
191188unit unitGameBoard;
192189
193190{$mode objfpc}{$H+}
194191
195192interface
196193
197194uses
198195 Classes, SysUtils;
199196
200197const
201198 GB_MAX_BUTTONS = 15;
202199 GB_MAX_X = 4;
203200 GB_MAX_Y = 4;
204201 GB_EMPTY_PLACE ='-';
205202type
206203
207204 { TGameBoard }
208205
209206 TGameBoard = class
210207 private
211208 GameArray:array[1..GB_MAX_X, 1..GB_MAX_Y] of string;
212209 function ValidPoint(a_point:TPoint):boolean;
213210 public
214211 constructor Create;
215212 function ButtonPlace(a_name:string):TPoint;
216213 function CanMove(a_name:string):Boolean;
217214 function Change(a_name: string): TPoint;
218215 procedure RandomArray;
219216 destructor Destroy; override;
220217 end;
221218implementation
222219
223220{ TGameBoard }
224221
225222function TGameBoard.ValidPoint(a_point: TPoint): boolean;
226223begin
227224 result := true;
228225 if a_point.y > GB_MAX_Y then result := false;
229226 if a_point.x > GB_MAX_X then result := false;
230227 if a_point.y < 1 then result := false;
231228 if a_point.x < 1 then result := false;
232229end;
233230
234231constructor TGameBoard.Create;
235232var
236233 i, x, y : integer;
237234begin
238235 i := 1;
239236 for y := 1 to GB_MAX_Y do
240237 for x := 1 to GB_MAX_X do
241238 begin
242239 if i < GB_MAX_BUTTONS+1
243240 then GameArray[x,y] := IntToStr(i)
244241 else GameArray[x,y] := GB_EMPTY_PLACE;
245242 inc(i);
246243 end;
247244 RandomArray;
248245end;
249246
250247function TGameBoard.ButtonPlace(a_name: string): TPoint;
251248var
252249 x,y:integer;
253250begin
254251 result.x := 0;
255252 result.y := 0;
256253 for y := 1 to GB_MAX_Y do
257254 for x := 1 to GB_MAX_X do
258255 if GameArray[x,y] = a_name then
259256 begin
260257 result.x := x;
261258 result.y := y;
262259 end;
263260end;
264261
265262function TGameBoard.CanMove(a_name: string): Boolean;
266263var
267264 empty, point:TPoint;
268265begin
269266 result := false;
270267 empty := ButtonPlace( GB_EMPTY_PLACE );
271268 point := ButtonPlace(a_name);
272269 if (abs(empty.x-point.x)=1) and (empty.y=point.y) then result := true;
273270 if (abs(empty.y-point.y)=1) and (empty.x=point.x) then result := true;
274271end;
275272
276273function TGameBoard.Change(a_name: string): TPoint;
277274var
278275 empty, point:TPoint;
279276begin
280277 empty := ButtonPlace( GB_EMPTY_PLACE );
281278 result := empty;
282279 point := ButtonPlace(a_name);
283280 GameArray[point.x, point.y] := GB_EMPTY_PLACE;
284281 GameArray[empty.x, empty.y] := a_name;
285282end;
286283
287284procedure TGameBoard.RandomArray;
288285var
289286 j,i:integer;
290287 name:string;
291288 point,point2:TPoint;
292289begin
293290 for i := 0 to 1000 do
294291 begin
295292 j := random(4);
296293 point := ButtonPlace( GB_EMPTY_PLACE );
297294 point2 := point;
298295 case j of
299296 0: point2.x := point.x-1;
300297 1: point2.x := point.x+1;
301298 2: point2.y := point.y-1;
302299 3: point2.y := point.y+1;
303300 end;
304301 if ValidPoint( point2 ) then
305302 begin
306303 name := GameArray[point2.x, point2.y];
307304 GameArray[point.x, point.y] := name;
308305 GameArray[point2.x, point2.y] := GB_EMPTY_PLACE;
309306 end;
310307 end;
311308end;
312309
313310destructor TGameBoard.Destroy;
314311begin
315312 inherited Destroy;
316313end;
317314
318315end.
319316```
320317
321318## Katso myös
322319
323320- [[TButton/fi|TButton]]
324321
325322---
326323*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