Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit ConstructorUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.ExtCtrls;
- type
- TConstructorForm = class(TForm)
- MainMenu: TMainMenu;
- FileMenuItem: TMenuItem;
- ManualMenuItem: TMenuItem;
- AboutDeveloperMenuItem: TMenuItem;
- LoadTemplateMenuItem: TMenuItem;
- SaveTemplateMenuItem: TMenuItem;
- BackLabel: TLabel;
- StartLabel: TLabel;
- Ship1: TImage;
- UserFieldImage: TImage;
- procedure LabelMouseEnter(Sender: TObject);
- procedure LabelMouseLeave(Sender: TObject);
- procedure BackLabelClick(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure UserFieldImageDragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- procedure UserFieldImageDragDrop(Sender, Source: TObject; X, Y: Integer);
- procedure UserFieldImageMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure UserFieldImageMouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- procedure UserFieldImageMouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- ConstructorForm: TConstructorForm;
- implementation
- uses
- GridUnit, FieldGeneratorUnit;
- const
- CELL_WIDTH = 30;
- var
- Field, TempField, NewField: TField;
- IsDrag, IsMovingShipHorizontal: Boolean;
- MovingShipType: TShip;
- ImpossibleCellsMatrix, TempMatrix: TImpossibleCellsMatrix;
- ShipCol, ShipRow: Byte;
- CanPlaceShip: Boolean;
- {$R *.dfm}
- procedure TConstructorForm.BackLabelClick(Sender: TObject);
- begin
- ConstructorForm.Close;
- end;
- procedure TConstructorForm.FormShow(Sender: TObject);
- begin
- Field := CreateField();
- ImpossibleCellsMatrix := CreateImpossibleCellsMatrix();
- DrawField(UserFieldImage, Field);
- DrawShip(Ship1, 4, 1);
- end;
- procedure TConstructorForm.LabelMouseEnter(Sender: TObject);
- begin
- with Sender as TLabel do
- begin
- Font.Color := clBlack;
- end;
- end;
- procedure TConstructorForm.LabelMouseLeave(Sender: TObject);
- begin
- with Sender as TLabel do
- begin
- Font.Color := clGrayText;
- end;
- end;
- function IsShipFromFormHorizontal(MovingShip: TImage): Boolean;
- var
- IsHorizontal: Boolean;
- begin
- if MovingShip.Width Div CELL_WIDTH = 1 then
- IsHorizontal := False
- else
- IsHorizontal := True;
- IsShipFromFormHorizontal := IsHorizontal;
- end;
- function CanPlaceShipHere(Field: TField; Ship: TShip; Col, Row: ShortInt; IsHorizontal: Boolean): Boolean;
- var
- I: ShortInt;
- CanPlace: Boolean;
- ReturnCellStateFunction: TReturnCellStateFunction;
- begin
- I := 0;
- CanPlace := True;
- if IsMovingShipHorizontal then
- ReturnCellStateFunction := ReturnRowElemState
- else
- ReturnCellStateFunction := ReturnColElemState;
- while (I < Ord(Ship)) and CanPlace do
- begin
- CanPlace := ReturnCellStateFunction(Field, Row, Col+I) = stFree;
- Inc(I);
- end;
- CanPlaceShipHere := CanPlace;
- end;
- function ConvertImageToShipType(MovingShip: TImage; IsHorizontal: Boolean): TShip;
- const
- TempArr: array [1..4] of TShip = (tShortShip, tSmallShip, tMiddleShip, tLongShip);
- var
- DeckCount: Byte;
- begin
- if IsHorizontal then
- DeckCount := MovingShip.Width div CELL_WIDTH
- else
- DeckCount := MovingShip.Height div CELL_WIDTH;
- ConvertImageToShipType := TempArr[DeckCount];
- end;
- procedure TConstructorForm.UserFieldImageDragOver(Sender, Source: TObject; X,
- Y: Integer; State: TDragState; var Accept: Boolean);
- var
- Col, Row: ShortInt;
- DeckCount: Byte;
- MovingShip: TImage;
- begin
- Col := X div CELL_WIDTH;
- Row := Y div CELL_WIDTH;
- MovingShip := Source as TImage;
- IsMovingShipHorizontal := IsShipFromFormHorizontal(MovingShip);
- MovingShipType := ConvertImageToShipType(MovingShip, IsMovingShipHorizontal);
- Accept := CanPlaceShipHere(Field, MovingShipType, Col, Row, IsMovingShipHorizontal);
- if Accept then
- begin
- TempField := Field;
- if IsMovingShipHorizontal then
- PlaceShipHorizontal (TempField, MovingShipType, Row, Col)
- else
- PlaceShipVertical (TempField, MovingShipType, Row, Col);
- DrawField(UserFieldImage, TempField);
- end;
- if State = dsDragLeave then
- DrawField(UserFieldImage, Field);
- end;
- procedure TConstructorForm.UserFieldImageDragDrop(Sender, Source: TObject; X,
- Y: Integer);
- var
- Col, Row: Byte;
- begin
- Col := X div CELL_WIDTH;
- Row := Y div CELL_WIDTH;
- Field := TempField;
- if IsMovingShipHorizontal then
- FillImpossibleCellsHorizontal(ImpossibleCellsMatrix, MovingShipType, Row, Col)
- else
- FillImpossibleCellsVertical(ImpossibleCellsMatrix, MovingShipType, Row, Col); ///////////////////////////COLROW
- DrawField(UserFieldImage, Field);
- end;
- ////////////Поправляем вставленный корабль
- function IsShipInFieldHorizontal(Field: TField; Ship: TShip; Col, Row: ShortInt): Boolean;
- var
- IsHorizontal: Boolean;
- begin
- if Ship = tShortShip then
- IsHorizontal := True
- else
- begin
- if (Field[Col-1, Row] <> stImpossible) or (Field[Col+1, Row] <> stImpossible) then
- IsHorizontal := True
- else
- IsHorizontal := False;
- end;
- IsShipInFieldHorizontal := IsHorizontal;
- end;
- procedure TConstructorForm.UserFieldImageMouseDown(Sender: TObject;
- Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
- var
- Col, Row, FirstSideCol, FirstSideRow, SecondSideCol, SecondSideRow: ShortInt;
- IsShip: Boolean;
- begin
- Col := X div CELL_WIDTH;
- Row := Y div CELL_WIDTH;
- IsShip := Ord(Field[Col, Row]) > 0;
- if IsShip then
- begin
- IsDrag := True;
- TempField := Field;
- TempMatrix := ImpossibleCellsMatrix;
- MovingShipType := ConvertFieldStateToShip(TempField[Col, Row]);
- IsMovingShipHorizontal := IsShipInFieldHorizontal(TempField, MovingShipType, Col, Row);
- FindSideOfShip(TempField, MovingShipType, Col, Row, FirstSideCol, FirstSideRow, IsMovingShipHorizontal, -1);
- FindSideOfShip(TempField, MovingShipType, Col, Row, SecondSideCol, SecondSideRow, IsMovingShipHorizontal, 1);
- DeleteShip(TempField, TempMatrix, FirstSideCol, FirstSideRow, SecondSideCol, SecondSideRow, IsMovingShipHorizontal);
- end;
- end;
- procedure TConstructorForm.UserFieldImageMouseMove(Sender: TObject;
- Shift: TShiftState; X, Y: Integer);
- begin
- if IsDrag then
- begin
- ShipCol := X div CELL_WIDTH;
- ShipRow := Y div CELL_WIDTH;
- NewField := TempField;
- CanPlaceShip := CanPlaceShipHere(NewField, MovingShipType, ShipCol, ShipRow, IsMovingShipHorizontal);
- if CanPlaceShip then
- begin
- if IsMovingShipHorizontal then
- PlaceShipHorizontal (NewField, MovingShipType, ShipRow, ShipCol)
- else
- PlaceShipVertical (NewField, MovingShipType, ShipRow, ShipCol);
- DrawField(UserFieldImage, NewField);
- end
- else
- begin
- DrawField(UserFieldImage, TempField);
- end;
- end;
- end;
- procedure TConstructorForm.UserFieldImageMouseUp(Sender: TObject;
- Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
- begin
- if IsDrag and CanPlaceShip then
- begin
- Field := NewField;
- if IsMovingShipHorizontal then
- FillImpossibleCellsHorizontal(TempMatrix, MovingShipType, ShipRow, ShipCol)
- else
- FillImpossibleCellsVertical(TempMatrix, MovingShipType, ShipRow, ShipCol);
- ImpossibleCellsMatrix := TempMatrix;
- end;
- IsDrag := False;
- DrawField(UserFieldImage, Field);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement