5#include "CoreMinimal.h"
13 virtual bool generatePosition(
const Dim& dim,
const TArray<CellType>& cells,
Position& position)
const = 0;
20 virtual bool generatePosition(
const Dim& dim,
const TArray<CellType>& cells,
Position& position)
const override
22 const uint32 startX = FMath::RandRange(1, dim.width - 2);
23 const uint32 startY = FMath::RandRange(1, dim.height - 2);
24 Position randomPosition = {startX, startY};
28 const uint32 currentIndex = posToIndex(randomPosition, dim);
29 if (cells[currentIndex] == CellType::Empty)
31 position = randomPosition;
35 if (++randomPosition.x > dim.width - 2)
38 if (++randomPosition.y > dim.height - 2)
43 }
while (randomPosition.x != startX || randomPosition.y != startY);
49 FORCEINLINE
Position indexToPos(uint32 index,
const Dim& dim)
const {
return Position(index % dim.width, index / dim.width); }
50 FORCEINLINE uint32 posToIndex(
const Position& position,
const Dim& dim)
const {
return position.x + position.y * dim.width; }
53using IPositionRandomizerPtr = TSharedPtr<IPositionRandomizer>;
Definition: SnakeGame.Build.cs:6