Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program GameDeskGenerator;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- type
- TFieldCellState = (stImpossible = -1, stFree, stShortShip, stSmallShip, stMediumShip, stLongShip);
- TField = array [0..7, 0..7] of TFieldCellState;
- TFieldForGeneration = array [-1..10, -1..10] of TFieldCellState;
- TShip = (tShortShip = 1, tSmallShip, tMiddleShip, tLongShip);
- TShipsArray = array [0..9] of TShip;
- TReturnCellStateFunction = function (ShipField: TFieldForGeneration; I, J: ShortInt): TFieldCellState;
- TPlaceShipProcedure = procedure (var ShipsField: TFieldForGeneration; Ship: TShip; X, Y: ShortInt);
- var
- Field: TField;
- TempField: TFieldForGeneration;
- ShipsArray: TShipsArray;
- Ship: TShip;
- CommonShipsCount: Byte;
- procedure CreateNewField(var Field: TFieldForGeneration);
- var
- I, J: ShortInt;
- begin
- for J := Low(Field) to High(Field) do
- Field[-1, J] := stImpossible;
- for I := Low(Field)+1 to High(Field)-1 do
- begin
- for J := Low(Field)+1 to High(Field)-1 do
- Field[I, J] := stFree;
- Field[I, Low(Field)] := stImpossible;
- Field[I, High(Field)] := stImpossible;
- end;
- for J := Low(Field) to High(Field) do
- Field[High(Field), J] := stImpossible;
- end;
- procedure OutputField(Field: TFieldForGeneration);
- var
- I, J: ShortInt;
- begin
- for I := Low(Field) to High(Field) do
- begin
- for J := Low(Field) to High(Field) do
- Write(StrToFloat(IntToStr(Ord(Field[J, I]))):4:0);
- Writeln;
- end;
- end;
- procedure InitializeShips(var ShipsArray: TShipsArray);
- var
- CurrShip: TShip;
- I, J: Byte;
- begin
- J := Low(ShipsArray);
- for CurrShip := Low(TShip) to High(TShip) do
- for I := 5 - Ord(CurrShip) DownTo 1 do
- begin
- ShipsArray[J] := CurrShip;
- Inc(J);
- end;
- CommonShipsCount := 10;
- end;
- procedure OutputShips(ShipsArray: TShipsArray);
- var
- I: Byte;
- begin
- for I := Low(ShipsArray) to CommonShipsCount-1 do
- Write(Ord(ShipsArray[I]), ' ');
- end;
- function PullShip(var ShipsArray: TShipsArray): TShip;
- var
- Ship: TShip;
- begin
- Ship := ShipsArray[CommonShipsCount-1];
- Dec(CommonShipsCount);
- PullShip := Ship;
- end;
- function GetRandomDirection(): Boolean;
- var
- IsHorizontal: Boolean;
- begin
- if Random(2) = 0 then
- IsHorizontal := True
- else
- IsHorizontal := False;
- GetRandomDirection := isHorizontal;
- end;
- function ConvertShipToFieldState(Ship: TShip): TFieldCellState;
- const
- TempArr: array [TShip] of TFieldCellState = (stShortShip, stSmallShip, stMediumShip, stLongShip);
- begin
- ConvertShipToFieldState := TempArr[Ship];
- end;
- function ReturnRowElemState(ShipField: TFieldForGeneration; I, J: ShortInt): TFieldCellState;
- begin
- ReturnRowElemState := ShipField[J, I];
- end;
- function ReturnColElemState(ShipField: TFieldForGeneration; I, J: ShortInt): TFieldCellState;
- begin
- ReturnColElemState := ShipField[I, J];
- end;
- procedure PlaceShipHorizontal (var ShipsField: TFieldForGeneration; Ship: TShip; Y, X: ShortInt);
- var
- I: ShortInt;
- begin
- for I := -1 to Ord(Ship) do
- begin
- ShipsField[X+I, Y-1] := stImpossible;
- ShipsField[X+I, Y] := ConvertShipToFieldState(Ship);
- ShipsField[X+I, Y+1] := stImpossible;
- end;
- ShipsField[X-1, Y] := stImpossible;
- ShipsField[X+Ord(Ship), Y] := stImpossible;
- end;
- procedure PlaceShipVertical (var ShipsField: TFieldForGeneration; Ship: TShip; X, Y: ShortInt);
- var
- I: ShortInt;
- begin
- for I := -1 to Ord(Ship) do
- begin
- ShipsField[X-1, Y+I] := stImpossible;
- ShipsField[X, Y+I] := ConvertShipToFieldState(Ship);
- ShipsField[X+1, Y+I] := stImpossible;
- end;
- ShipsField[X, Y-1] := stImpossible;
- ShipsField[X, Y+Ord(Ship)] := stImpossible;
- end;
- function IsPlacedShipInField(var ShipsField: TFieldForGeneration; Ship: TShip; Coord: ShortInt; IsHorizontal: Boolean): Boolean;
- var
- I, SecondCoord, Counter: ShortInt;
- HasFreePlace: Boolean;
- ReturnCellState: TReturnCellStateFunction;
- PlaceShip: TPlaceShipProcedure;
- begin
- Counter := 0;
- HasFreePlace := False;
- I := Low(ShipsField)+1;
- if IsHorizontal then
- begin
- ReturnCellState := ReturnColElemState;
- PlaceShip := PlaceShipHorizontal;
- end
- else
- begin
- ReturnCellState := ReturnRowElemState;
- PlaceShip := PlaceShipVertical;
- end;
- while ((I < High(ShipsField)) and not HasFreePlace) do
- begin
- if Ord(ReturnCellState(ShipsField, I, Coord)) = 0 then
- Inc(Counter)
- else
- Counter := 0;
- if Counter = Ord(Ship) then
- begin
- HasFreePlace := True;
- SecondCoord := I - Ord(Ship)+1;
- PlaceShip(ShipsField, Ship, Coord, SecondCoord );
- end;
- Inc(I);
- end;
- IsPlacedShipInField := HasFreePlace;
- end;
- procedure PutShipToField(Ship: TShip; var ShipsField: TFieldForGeneration);
- var
- Coord, I: ShortInt;
- IsHorizontal: Boolean;
- begin
- repeat
- IsHorizontal := GetRandomDirection;
- Coord := Random(11);
- until IsPlacedShipInField(ShipsField, Ship, Coord, IsHorizontal);
- end;
- procedure FillGameField(var Field: TFieldForGeneration; var ShipsArray: TShipsArray);
- var
- Ship: TShip;
- I: ShortInt;
- begin
- for I := 1 to 10 do
- begin
- Ship := PullShip(ShipsArray);
- PutShipToField(Ship, TempField);
- end;
- end;
- begin
- Randomize;
- CreateNewField(TempField);
- OutputField(TempField);
- InitializeShips(ShipsArray);
- OutputShips(ShipsArray);
- FillGameField(TempField, ShipsArray);
- Writeln;
- Writeln;
- OutputField(TempField);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement