Цель работы: Изучить среду Delphi.
Задание:
Игра «Пятнашки»
Задается поле 4х4 клеток, заполненных в случайном порядке квадратиками (фишками) с цифрами от 1 до 15. Нужно путем перемещений фишек собрать их в возрастающем порядке. Пустая фишка (пустое место) должно в результате находиться в правом нижнем углу игрового поля.
Текст программы:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
Модуль «Main.pas»: unit Main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus,About; const S_GAME=0;const S_WIN=1; const S_LOSE=2; const S_NONE=3; const MAX=4; type TMainfrm = class(TForm) MainMenu: TMainMenu; N1: TMenuItem; NewGamemi: TMenuItem; Exitmi: TMenuItem; N2: TMenuItem; About: TMenuItem; procedure AboutClick(Sender: TObject); procedure ExitmiClick(Sender: TObject); procedure NewGamemiClick(Sender: TObject); procedure FormPaint(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormResize(Sender: TObject); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } public { Public declarations } end; var Mainfrm: TMainfrm; Situation: integer; Scene:array [1..MAX,1..MAX] of integer; Sector_SizeX:integer; Sector_SizeY:integer; FontSize:integer; implementation {$R *.dfm} procedure MyMouseDown(X, Y: Integer); var n,m,sx,sy:integer; begin n:=Trunc(x / Sector_SizeX)+1; m:=Trunc(y/Sector_SizeY)+1; sx:=n; sy:=m; if (n>1) then begin if (Scene[n-1,m]=0) then sx:=n-1; end; if (n<MAX) then begin if (scene[n+1,m]=0) then sx:=n+1; end; if (m>1) then begin if (Scene[n,m-1]=0) then sy:=m-1; end; if (m<MAX) then begin if (scene[n,m+1]=0) then sy:=m+1; end; if ((sx=n) and (sy=m)) then else begin scene[sx,sy]:=scene[n,m]; scene[n,m]:=0; end; Mainfrm.Paint; end; procedure TMainfrm.AboutClick(Sender: TObject); begin AboutBox.ShowModal end; procedure TMainfrm.ExitmiClick(Sender: TObject); begin Mainfrm.Close end; procedure TMainfrm.NewGamemiClick(Sender: TObject); var n:integer; begin For n:=1 to 1000 do MyMouseDown(Random(Mainfrm.ClientWidth-1),Random(Mainfrm.ClientHeight-1)); end; procedure TMainfrm.FormPaint(Sender: TObject); var n,m:integer; var str1:string; begin Mainfrm.Canvas.Pen.Color:=RGB(0,255,0); Mainfrm.Canvas.Font.Color:=RGB(0,255,0); Mainfrm.Canvas.Brush.Color:=RGB(0,0,0); for n:=1 to MAX do begin for m:=1 to MAX do begin //if (Scene[n,m]<>0) then Mainfrm.Canvas.Brush.Color:=RGB(0,255,0) //else Mainfrm.Canvas.Brush.Color:=RGB(255,255,255); Mainfrm.canvas.Rectangle((n-1)*Sector_SizeX,(m-1)*Sector_SizeY,(n-1)*Sector_SizeX+Sector_SizeX,(m-1)*Sector_SizeY+Sector_SizeY); str1:=IntToStr(Scene[n,m]); if (Scene[n,m]=0) then str1:=' '; Mainfrm.Canvas.TextOut((n-1)*Sector_SizeX+Round(Sector_SizeX/2)-Round(Mainfrm.Canvas.TextWidth(str1)/2 ),(m-1)*Sector_SizeY+Round(Sector_SizeY/2)-Round(Mainfrm.Canvas.TextHeight(str1)/2) ,str1); end; end; end; procedure TMainfrm.FormCreate(Sender: TObject); var n,x,y:integer; st:TShiftState; begin Sector_sizeX:=Trunc((Mainfrm.ClientWidth)/ MAX) ; Sector_SizeY:=Trunc((Mainfrm.ClientHeight)/(MAX)); randomize; Situation:=S_GAME; Mainfrm.Paint ; x:=-1; y:=-1; for n:=1 to 15 do begin x:=(x+1) mod (MAX); if (x=0) then y:=(y+1) mod (MAX); scene[x+1,y+1]:=n; end; Mainfrm.NewGamemiClick(Mainfrm) ; end; procedure TMainfrm.FormResize(Sender: TObject); var n,m:integer; begin Sector_sizeX:=Trunc((Mainfrm.ClientWidth)/ MAX) ; Sector_SizeY:=Trunc((Mainfrm.ClientHeight)/(MAX)); if (Sector_SizeX<Sector_SizeY) then FontSize:=Round(Sector_SizeX/2) else FontSize:=Round(Sector_SizeY/2); Mainfrm.Canvas.Font.Size :=Round(FontSize); Mainfrm.Paint; end; procedure TMainfrm.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var n,m,k:integer; yes:boolean; begin if (Situation=S_GAME) then MyMouseDown(x,y); k:=0; yes:=TRUE; n:=1; m:=1; while (n<=MAX) do begin m:=1; while (m<=MAX) do begin k:=k+1; if (n=MAX) and (m=MAX) then break; if (scene[m,n]<>k) then yes:=FALSE; m:=m+1; end; n:=n+1; end; if (yes=TRUE ) then begin MessageBox(Mainfrm.ClientHandle,'ПОБЕДА!!! Первый раз вижу такого сообразительного человека!','ПОБЕДА',MB_OK); Situation:=S_WIN; end; end; end. Модуль «About.pas»: unit About; interface uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls, Buttons, ExtCtrls; type TAboutBox = class(TForm) Panel1: TPanel; ProgramIcon: TImage; ProductName: TLabel; Version: TLabel; Copyright: TLabel; Comments: TLabel; OKButton: TButton; private { Private declarations } public { Public declarations } end; var AboutBox: TAboutBox; implementation {$R *.dfm} end. |
Окно главной формы при игре:
Вывод:
Была изучена среда Deplhi.