Advertisement
THOMAS_SHELBY_18

added shortShip

Mar 31st, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | Source Code | 0 0
  1. program Intellect;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8. System.SysUtils;
  9.  
  10. type
  11. TFieldCellState = (stImpossible = -1, stFree, stShortShip, stSmallShip, stMediumShip, stLongShip);
  12. TField = array [-1..10, -1..10] of TFieldCellState;
  13.  
  14. TShip = (tShortShip = 1, tSmallShip, tMiddleShip, tLongShip);
  15. TShipsArray = array [0..9] of TShip;
  16.  
  17. TReturnCellStateFunction = function (ShipField: TField; I, J: ShortInt): TFieldCellState;
  18. TPlaceShipProcedure = procedure (var ShipsField: TField; Ship: TShip; X, Y: ShortInt);
  19.  
  20. TCellsArray = array [0..99] of Byte;
  21. var
  22. TempField, ShootField: TField;
  23. ShipsArray: TShipsArray;
  24. CommonShipsCount, I: Byte;
  25. FreeCellsCount: ShortInt;
  26. CellsIndex, FreeCells: TCellsArray;
  27.  
  28.  
  29. procedure CreateNewField(var Field: TField);
  30. var
  31. I, J: ShortInt;
  32. begin
  33. for J := Low(Field) to High(Field) do
  34. Field[-1, J] := stImpossible;
  35.  
  36. for I := Low(Field)+1 to High(Field)-1 do
  37. begin
  38. for J := Low(Field)+1 to High(Field)-1 do
  39. Field[I, J] := stFree;
  40. Field[I, Low(Field)] := stImpossible;
  41. Field[I, High(Field)] := stImpossible;
  42. end;
  43.  
  44. for J := Low(Field) to High(Field) do
  45. Field[High(Field), J] := stImpossible;
  46. end;
  47.  
  48. procedure OutputField(Field: TField);
  49. var
  50. I, J: ShortInt;
  51. begin
  52. for I := Low(Field) to High(Field) do
  53. begin
  54. for J := Low(Field) to High(Field) do
  55. Write(StrToFloat(IntToStr(Ord(Field[J, I]))):4:0);
  56. Writeln;
  57. end;
  58. end;
  59.  
  60. procedure InitializeShips(var ShipsArray: TShipsArray);
  61. var
  62. CurrShip: TShip;
  63. I, J: Byte;
  64. begin
  65. J := Low(ShipsArray);
  66. for CurrShip := Low(TShip) to High(TShip) do
  67. for I := 5 - Ord(CurrShip) DownTo 1 do
  68. begin
  69. ShipsArray[J] := CurrShip;
  70. Inc(J);
  71. end;
  72.  
  73. CommonShipsCount := 10;
  74. end;
  75.  
  76. procedure OutputShips(ShipsArray: TShipsArray);
  77. var
  78. I: Byte;
  79. begin
  80. for I := Low(ShipsArray) to CommonShipsCount-1 do
  81. Write(Ord(ShipsArray[I]), ' ');
  82. end;
  83.  
  84. function PullShip(var ShipsArray: TShipsArray): TShip;
  85. var
  86. Ship: TShip;
  87. begin
  88. Ship := ShipsArray[CommonShipsCount-1];
  89. Dec(CommonShipsCount);
  90.  
  91. PullShip := Ship;
  92. end;
  93.  
  94. function GetRandomDirection(): Boolean;
  95. var
  96. IsHorizontal: Boolean;
  97. begin
  98. if Random(2) = 0 then
  99. IsHorizontal := True
  100. else
  101. IsHorizontal := False;
  102.  
  103. GetRandomDirection := isHorizontal;
  104. end;
  105.  
  106. function ConvertShipToFieldState(Ship: TShip): TFieldCellState;
  107. const
  108. TempArr: array [TShip] of TFieldCellState = (stShortShip, stSmallShip, stMediumShip, stLongShip);
  109. begin
  110. ConvertShipToFieldState := TempArr[Ship];
  111. end;
  112.  
  113. function ReturnRowElemState(ShipField: TField; I, J: ShortInt): TFieldCellState;
  114. begin
  115. ReturnRowElemState := ShipField[J, I];
  116. end;
  117.  
  118. function ReturnColElemState(ShipField: TField; I, J: ShortInt): TFieldCellState;
  119. begin
  120. ReturnColElemState := ShipField[I, J];
  121. end;
  122.  
  123. procedure PlaceShipHorizontal (var ShipsField: TField; Ship: TShip; Y, X: ShortInt);
  124. var
  125. I: ShortInt;
  126. begin
  127. for I := -1 to Ord(Ship) do
  128. begin
  129. ShipsField[X+I, Y-1] := stImpossible;
  130. ShipsField[X+I, Y] := ConvertShipToFieldState(Ship);
  131. ShipsField[X+I, Y+1] := stImpossible;
  132. end;
  133. ShipsField[X-1, Y] := stImpossible;
  134. ShipsField[X+Ord(Ship), Y] := stImpossible;
  135. end;
  136.  
  137. procedure PlaceShipVertical (var ShipsField: TField; Ship: TShip; X, Y: ShortInt);
  138. var
  139. I: ShortInt;
  140. begin
  141. for I := -1 to Ord(Ship) do
  142. begin
  143. ShipsField[X-1, Y+I] := stImpossible;
  144. ShipsField[X, Y+I] := ConvertShipToFieldState(Ship);
  145. ShipsField[X+1, Y+I] := stImpossible;
  146. end;
  147. ShipsField[X, Y-1] := stImpossible;
  148. ShipsField[X, Y+Ord(Ship)] := stImpossible;
  149. end;
  150.  
  151. function IsShipPlacedInField(var ShipsField: TField; Ship: TShip; Coord: ShortInt; IsHorizontal: Boolean): Boolean;
  152. var
  153. I, SecondCoord, Counter: ShortInt;
  154. HasFreePlace: Boolean;
  155. ReturnCellState: TReturnCellStateFunction;
  156. PlaceShip: TPlaceShipProcedure;
  157. begin
  158. Counter := 0;
  159. HasFreePlace := False;
  160. I := Low(ShipsField)+1;
  161.  
  162. if IsHorizontal then
  163. begin
  164. ReturnCellState := ReturnColElemState;
  165. PlaceShip := PlaceShipHorizontal;
  166. end
  167. else
  168. begin
  169. ReturnCellState := ReturnRowElemState;
  170. PlaceShip := PlaceShipVertical;
  171. end;
  172.  
  173. while ((I < High(ShipsField)) and not HasFreePlace) do
  174. begin
  175. if Ord(ReturnCellState(ShipsField, I, Coord)) = 0 then
  176. Inc(Counter)
  177. else
  178. Counter := 0;
  179.  
  180. if Counter = Ord(Ship) then
  181. begin
  182. HasFreePlace := True;
  183. SecondCoord := I - Ord(Ship)+1;
  184. PlaceShip(ShipsField, Ship, Coord, SecondCoord );
  185. end;
  186. Inc(I);
  187. end;
  188.  
  189. IsShipPlacedInField := HasFreePlace;
  190. end;
  191.  
  192. procedure PutShipToField(Ship: TShip; var ShipsField: TField);
  193. var
  194. Coord: ShortInt;
  195. IsHorizontal: Boolean;
  196. begin
  197. repeat
  198. IsHorizontal := GetRandomDirection;
  199. Coord := Random(10);
  200. until IsShipPlacedInField(ShipsField, Ship, Coord, IsHorizontal);
  201. end;
  202.  
  203. procedure FillGameField(var Field: TField; var ShipsArray: TShipsArray);
  204. var
  205. Ship: TShip;
  206. I: ShortInt;
  207. begin
  208. for I := 1 to 10 do
  209. begin
  210. Ship := PullShip(ShipsArray);
  211. PutShipToField(Ship, TempField);
  212. end;
  213. end;
  214.  
  215. procedure CellsArrayInitialize(var FreeCells: TCellsArray);
  216. var
  217. I, J, Temp: Byte;
  218. begin
  219. for I := 0 to 9 do
  220. for J := 0 to 9 do
  221. begin
  222. Temp := I + 9 * I + J;
  223. FreeCells[Temp] := Temp;
  224. end;
  225. FreeCellsCount := 100;
  226. end;
  227.  
  228. procedure EditFreeCells(UserField: TField; var FreeCells: TCellsArray; var CellsIndex: TCellsArray; Col, Row: ShortInt; var FreeCellsCount: ShortInt);
  229. var
  230. IsCellInField: Boolean;
  231. LastFreeCellsValue, Coord: Byte;
  232. begin
  233. if UserField[Col, Row] <> stImpossible then
  234. begin
  235. Coord := 10 * Col + Row;
  236. LastFreeCellsValue := FreeCells[FreeCellsCount-1];
  237. FreeCells[CellsIndex[Coord]] := LastFreeCellsValue;
  238. CellsIndex[LastFreeCellsValue] := CellsIndex[Coord];
  239. Dec(FreeCellsCount);
  240. end;
  241. end;
  242.  
  243. procedure EditFieldAroundShootPlace(UserField: TField; var ShootField: TField; var FreeCells: TCellsArray; var CellsIndex: TCellsArray; Col, Row: ShortInt; var FreeCellsCount: ShortInt);
  244. var
  245. I, J: ShortInt;
  246. begin
  247. I := -1;
  248. while I < 2 do
  249. begin
  250. J := -1;
  251. while J < 2 do
  252. begin
  253. ShootField[Col+I, Row+J] := stImpossible;
  254. EditFreeCells(UserField, FreeCells, CellsIndex, Col+I, Row+J, FreeCellsCount);
  255. Inc(J, 2);
  256. end;
  257. Inc(I, 2);
  258. end;
  259. end;
  260.  
  261. procedure EditFieldForDestroyedShip(UserField: TField; var ShootField: TField; Ship: TShip; var FreeCells: TCellsArray; var CellsIndex: TCellsArray; Col, Row: ShortInt; var FreeCellsCount: ShortInt);
  262. var
  263. I: ShortInt;
  264. begin
  265. if Ship = tShortShip then
  266. begin
  267. I := -1;
  268. while I < 2 do
  269. begin
  270. ShootField[Col+I, Row] := stImpossible;
  271. ShootField[Col, Row+I] := stImpossible;
  272. EditFreeCells(UserField, FreeCells, CellsIndex, Col+I, Row, FreeCellsCount);
  273. EditFreeCells(UserField, FreeCells, CellsIndex, Col, Row+I, FreeCellsCount);
  274. Inc(I, 2);
  275. end;
  276. end;
  277. end;
  278.  
  279. procedure MakeShoot(var ShootField: TField; UserField: TField; var FreeCells: TCellsArray; var CellsIndex: TCellsArray; var FreeCellsCount: ShortInt);
  280. var
  281. Coord, I, Col, Row: ShortInt;
  282. State: TFieldCellState;
  283. Ship: TShip;
  284. begin
  285. I := Random(FreeCellsCount);
  286. Coord := FreeCells[I];
  287. FreeCells[I] := FreeCells[FreeCellsCount-1];
  288. CellsIndex[FreeCellsCount-1] := I;
  289. Dec(FreeCellsCount);
  290.  
  291.  
  292.  
  293. Col := Coord div 10;
  294. Row := Coord mod 10;
  295.  
  296. Writeln(Col, ' ', Row, ' ', FreeCellsCount);
  297.  
  298. State := UserField[Col, Row];
  299.  
  300. case State of
  301. stFree:;
  302. stImpossible:;
  303. stShortShip:
  304. begin
  305. Ship := tShortShip;
  306. EditFieldAroundShootPlace(UserField, ShootField, FreeCells, CellsIndex, Col, Row, FreeCellsCount);
  307. EditFieldForDestroyedShip(UserField, ShootField, Ship, FreeCells, CellsIndex, Col, Row, FreeCellsCount);
  308. ShootField[Col, Row] := State;
  309. end;
  310. end;
  311.  
  312.  
  313. end;
  314.  
  315. begin
  316. Randomize;
  317. CreateNewField(TempField);
  318. OutputField(TempField);
  319.  
  320. InitializeShips(ShipsArray);
  321. OutputShips(ShipsArray);
  322.  
  323. FillGameField(TempField, ShipsArray);
  324. Writeln;
  325. Writeln;
  326.  
  327. OutputField(TempField);
  328.  
  329.  
  330. CellsArrayInitialize(FreeCells);
  331. CellsArrayInitialize(CellsIndex);
  332. CreateNewField(ShootField);
  333.  
  334. while FreeCellsCount > 0 do
  335. MakeShoot(ShootField, TempField, FreeCells, CellsIndex, FreeCellsCount);
  336. OutputField(ShootField);
  337.  
  338.  
  339. Readln;
  340. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement