Advertisement
THOMAS_SHELBY_18

Untitled

Apr 7th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. unit ConstructorUnit;
  2.  
  3. interface
  4.  
  5. uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.ExtCtrls;
  8.  
  9. type
  10. TConstructorForm = class(TForm)
  11. MainMenu: TMainMenu;
  12. FileMenuItem: TMenuItem;
  13. ManualMenuItem: TMenuItem;
  14. AboutDeveloperMenuItem: TMenuItem;
  15. LoadTemplateMenuItem: TMenuItem;
  16. SaveTemplateMenuItem: TMenuItem;
  17. BackLabel: TLabel;
  18. StartLabel: TLabel;
  19. Ship1: TImage;
  20. UserFieldImage: TImage;
  21. procedure LabelMouseEnter(Sender: TObject);
  22. procedure LabelMouseLeave(Sender: TObject);
  23. procedure BackLabelClick(Sender: TObject);
  24. procedure FormShow(Sender: TObject);
  25. procedure UserFieldImageDragOver(Sender, Source: TObject; X, Y: Integer;
  26. State: TDragState; var Accept: Boolean);
  27. procedure UserFieldImageDragDrop(Sender, Source: TObject; X, Y: Integer);
  28. procedure UserFieldImageMouseDown(Sender: TObject; Button: TMouseButton;
  29. Shift: TShiftState; X, Y: Integer);
  30. procedure UserFieldImageMouseMove(Sender: TObject; Shift: TShiftState; X,
  31. Y: Integer);
  32. procedure UserFieldImageMouseUp(Sender: TObject; Button: TMouseButton;
  33. Shift: TShiftState; X, Y: Integer);
  34. private
  35. { Private declarations }
  36. public
  37. { Public declarations }
  38. end;
  39.  
  40. var
  41. ConstructorForm: TConstructorForm;
  42.  
  43. implementation
  44.  
  45. uses
  46. GridUnit, FieldGeneratorUnit;
  47.  
  48. const
  49. CELL_WIDTH = 30;
  50. var
  51. Field, TempField, NewField: TField;
  52. IsDrag, IsMovingShipHorizontal: Boolean;
  53. MovingShipType: TShip;
  54. ImpossibleCellsMatrix, TempMatrix: TImpossibleCellsMatrix;
  55. ShipCol, ShipRow: Byte;
  56. CanPlaceShip: Boolean;
  57.  
  58. {$R *.dfm}
  59.  
  60. procedure TConstructorForm.BackLabelClick(Sender: TObject);
  61. begin
  62. ConstructorForm.Close;
  63. end;
  64.  
  65. procedure TConstructorForm.FormShow(Sender: TObject);
  66. begin
  67. Field := CreateField();
  68. ImpossibleCellsMatrix := CreateImpossibleCellsMatrix();
  69. DrawField(UserFieldImage, Field);
  70. DrawShip(Ship1, 4, 1);
  71. end;
  72.  
  73. procedure TConstructorForm.LabelMouseEnter(Sender: TObject);
  74. begin
  75. with Sender as TLabel do
  76. begin
  77. Font.Color := clBlack;
  78. end;
  79. end;
  80.  
  81. procedure TConstructorForm.LabelMouseLeave(Sender: TObject);
  82. begin
  83. with Sender as TLabel do
  84. begin
  85. Font.Color := clGrayText;
  86. end;
  87. end;
  88.  
  89.  
  90.  
  91.  
  92.  
  93. function IsShipFromFormHorizontal(MovingShip: TImage): Boolean;
  94. var
  95. IsHorizontal: Boolean;
  96. begin
  97. if MovingShip.Width Div CELL_WIDTH = 1 then
  98. IsHorizontal := False
  99. else
  100. IsHorizontal := True;
  101.  
  102. IsShipFromFormHorizontal := IsHorizontal;
  103. end;
  104.  
  105. function CanPlaceShipHere(Field: TField; Ship: TShip; Col, Row: ShortInt; IsHorizontal: Boolean): Boolean;
  106. var
  107. I: ShortInt;
  108. CanPlace: Boolean;
  109. ReturnCellStateFunction: TReturnCellStateFunction;
  110. begin
  111. I := 0;
  112. CanPlace := True;
  113. if IsMovingShipHorizontal then
  114. ReturnCellStateFunction := ReturnRowElemState
  115. else
  116. ReturnCellStateFunction := ReturnColElemState;
  117.  
  118. while (I < Ord(Ship)) and CanPlace do
  119. begin
  120. CanPlace := ReturnCellStateFunction(Field, Row, Col+I) = stFree;
  121. Inc(I);
  122. end;
  123.  
  124. CanPlaceShipHere := CanPlace;
  125. end;
  126.  
  127. function ConvertImageToShipType(MovingShip: TImage; IsHorizontal: Boolean): TShip;
  128. const
  129. TempArr: array [1..4] of TShip = (tShortShip, tSmallShip, tMiddleShip, tLongShip);
  130. var
  131. DeckCount: Byte;
  132. begin
  133. if IsHorizontal then
  134. DeckCount := MovingShip.Width div CELL_WIDTH
  135. else
  136. DeckCount := MovingShip.Height div CELL_WIDTH;
  137.  
  138. ConvertImageToShipType := TempArr[DeckCount];
  139. end;
  140.  
  141. procedure TConstructorForm.UserFieldImageDragOver(Sender, Source: TObject; X,
  142. Y: Integer; State: TDragState; var Accept: Boolean);
  143. var
  144. Col, Row: ShortInt;
  145. DeckCount: Byte;
  146. MovingShip: TImage;
  147. begin
  148. Col := X div CELL_WIDTH;
  149. Row := Y div CELL_WIDTH;
  150.  
  151. MovingShip := Source as TImage;
  152. IsMovingShipHorizontal := IsShipFromFormHorizontal(MovingShip);
  153. MovingShipType := ConvertImageToShipType(MovingShip, IsMovingShipHorizontal);
  154. Accept := CanPlaceShipHere(Field, MovingShipType, Col, Row, IsMovingShipHorizontal);
  155.  
  156. if Accept then
  157. begin
  158. TempField := Field;
  159. if IsMovingShipHorizontal then
  160. PlaceShipHorizontal (TempField, MovingShipType, Row, Col)
  161. else
  162. PlaceShipVertical (TempField, MovingShipType, Row, Col);
  163.  
  164. DrawField(UserFieldImage, TempField);
  165. end;
  166.  
  167. if State = dsDragLeave then
  168. DrawField(UserFieldImage, Field);
  169. end;
  170.  
  171. procedure TConstructorForm.UserFieldImageDragDrop(Sender, Source: TObject; X,
  172. Y: Integer);
  173. var
  174. Col, Row: Byte;
  175. begin
  176. Col := X div CELL_WIDTH;
  177. Row := Y div CELL_WIDTH;
  178.  
  179. Field := TempField;
  180. if IsMovingShipHorizontal then
  181. FillImpossibleCellsHorizontal(ImpossibleCellsMatrix, MovingShipType, Row, Col)
  182. else
  183. FillImpossibleCellsVertical(ImpossibleCellsMatrix, MovingShipType, Row, Col); ///////////////////////////COLROW
  184.  
  185. DrawField(UserFieldImage, Field);
  186. end;
  187.  
  188. ////////////Поправляем вставленный корабль
  189. function IsShipInFieldHorizontal(Field: TField; Ship: TShip; Col, Row: ShortInt): Boolean;
  190. var
  191. IsHorizontal: Boolean;
  192. begin
  193. if Ship = tShortShip then
  194. IsHorizontal := True
  195. else
  196. begin
  197. if (Field[Col-1, Row] <> stImpossible) or (Field[Col+1, Row] <> stImpossible) then
  198. IsHorizontal := True
  199. else
  200. IsHorizontal := False;
  201. end;
  202. IsShipInFieldHorizontal := IsHorizontal;
  203. end;
  204.  
  205. procedure TConstructorForm.UserFieldImageMouseDown(Sender: TObject;
  206. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  207. var
  208. Col, Row, FirstSideCol, FirstSideRow, SecondSideCol, SecondSideRow: ShortInt;
  209. IsShip: Boolean;
  210. begin
  211. Col := X div CELL_WIDTH;
  212. Row := Y div CELL_WIDTH;
  213.  
  214. IsShip := Ord(Field[Col, Row]) > 0;
  215.  
  216. if IsShip then
  217. begin
  218. IsDrag := True;
  219.  
  220. TempField := Field;
  221. TempMatrix := ImpossibleCellsMatrix;
  222.  
  223. MovingShipType := ConvertFieldStateToShip(TempField[Col, Row]);
  224. IsMovingShipHorizontal := IsShipInFieldHorizontal(TempField, MovingShipType, Col, Row);
  225.  
  226. FindSideOfShip(TempField, MovingShipType, Col, Row, FirstSideCol, FirstSideRow, IsMovingShipHorizontal, -1);
  227. FindSideOfShip(TempField, MovingShipType, Col, Row, SecondSideCol, SecondSideRow, IsMovingShipHorizontal, 1);
  228.  
  229. DeleteShip(TempField, TempMatrix, FirstSideCol, FirstSideRow, SecondSideCol, SecondSideRow, IsMovingShipHorizontal);
  230. end;
  231. end;
  232.  
  233. procedure TConstructorForm.UserFieldImageMouseMove(Sender: TObject;
  234. Shift: TShiftState; X, Y: Integer);
  235. begin
  236. if IsDrag then
  237. begin
  238. ShipCol := X div CELL_WIDTH;
  239. ShipRow := Y div CELL_WIDTH;
  240. NewField := TempField;
  241.  
  242. CanPlaceShip := CanPlaceShipHere(NewField, MovingShipType, ShipCol, ShipRow, IsMovingShipHorizontal);
  243.  
  244. if CanPlaceShip then
  245. begin
  246. if IsMovingShipHorizontal then
  247. PlaceShipHorizontal (NewField, MovingShipType, ShipRow, ShipCol)
  248. else
  249. PlaceShipVertical (NewField, MovingShipType, ShipRow, ShipCol);
  250.  
  251. DrawField(UserFieldImage, NewField);
  252. end
  253. else
  254. begin
  255. DrawField(UserFieldImage, TempField);
  256. end;
  257. end;
  258. end;
  259.  
  260. procedure TConstructorForm.UserFieldImageMouseUp(Sender: TObject;
  261. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  262. begin
  263. if IsDrag and CanPlaceShip then
  264. begin
  265. Field := NewField;
  266. if IsMovingShipHorizontal then
  267. FillImpossibleCellsHorizontal(TempMatrix, MovingShipType, ShipRow, ShipCol)
  268. else
  269. FillImpossibleCellsVertical(TempMatrix, MovingShipType, ShipRow, ShipCol);
  270.  
  271. ImpossibleCellsMatrix := TempMatrix;
  272. end;
  273. IsDrag := False;
  274. DrawField(UserFieldImage, Field);
  275. end;
  276. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement