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..8, -1..8] 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; X, Y: 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; Y, X: 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, 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;
- PlaceShip(ShipsField, Ship, I - Ord(Ship)+1, Coord);
- end;
- Inc(I);
- end;
- IsPlacedShipInField := HasFreePlace;
- end;
- {function FindRandomCellForShipInRow(ShipsField: TFieldForGeneration; Ship: TShip; Y: ShortInt): ShortInt;
- var
- I, Counter, X: ShortInt;
- IsPlaceFounded: Boolean;
- begin
- Counter := 0;
- IsPlaceFounded := False;
- I := Low(ShipsField)+1;
- while ((I < High(ShipsField)) and not IsPlaceFounded) do
- begin
- if Ord(ShipsField[I, Y]) = 0 then
- Inc(Counter)
- else
- Counter := 0;
- if Counter = Ord(Ship) then
- begin
- X := I - Ord(Ship)+1;
- IsPlaceFounded := True;
- end;
- Inc(I);
- end;
- FindRandomCellForShipInRow := X;
- end;
- function FindRandomCellForShipInCol(ShipsField: TFieldForGeneration; Ship: TShip; X: ShortInt): ShortInt;
- var
- I, Counter, Y: ShortInt;
- IsPlaceFounded: Boolean;
- begin
- Counter := 0;
- IsPlaceFounded := False;
- I := Low(ShipsField)+1;
- while ((I < High(ShipsField)) and not IsPlaceFounded) do
- begin
- if Ord(ShipsField[X, I]) = 0 then
- Inc(Counter)
- else
- Counter := 0;
- if Counter = Ord(Ship) then
- begin
- Y := I - Ord(Ship)+1;
- IsPlaceFounded := True;
- end;
- Inc(I);
- end;
- FindRandomCellForShipInCol := Y;
- end;}
- procedure PutShipToField(Ship: TShip; var ShipsField: TFieldForGeneration);
- var
- X, Y, I: ShortInt;
- IsHorizontal: Boolean;
- begin
- IsHorizontal := GetRandomDirection;
- //IsHorizontal := false;///////////////////////////////////////////////
- if IsHorizontal then
- begin
- repeat
- Y := Random(9);
- until IsPlacedShipInField(ShipsField, Ship, Y, IsHorizontal);
- {X := FindRandomCellForShipInRow(ShipsField, Ship, Y);
- 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
- else
- begin
- repeat
- X := Random(9);
- until IsPlacedShipInField(ShipsField, Ship, X, IsHorizontal);
- {Y := FindRandomCellForShipInCol(ShipsField, Ship, X);
- 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;
- end;
- procedure FillDesk(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);
- FillDesk(TempField, ShipsArray);
- Writeln;
- Writeln;
- OutputField(TempField);
- //OutputShips(ShipsArray);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement