Unreal Snake Game 1.0.0
Grid.h
1// Snake Game, Copyright LifeEXE. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Types.h"
7#include "Utils.h"
8
9namespace SnakeGame
10{
11
12class SNAKEGAME_API Grid
13{
14public:
15 Grid(const Dim& dim, const IPositionRandomizerPtr& randomizer = MakeShared<PositionRandomizer>());
16
23 static Position center(uint32 width, uint32 height) { return Position(width / 2 + 1, height / 2 + 1); }
24
29 Dim dim() const { return c_dim; }
30
36 void update(const TPositionPtr* links, CellType cellType);
37
43 void update(const Position& position, CellType cellType);
44
51 bool hitTest(const Position& position, CellType cellType) const;
52
58 UE_NODISCARD bool randomEmptyPosition(Position& position) const;
59
60 void printDebug();
61
62private:
63 const Dim c_dim;
64 TArray<CellType> m_cells;
65 TMap<CellType, TArray<uint32>> m_indByType = {
66 {CellType::Snake, {}}, //
67 {CellType::Wall, {}}, //
68 {CellType::Food, {}}, //
69 };
70
71 TSharedPtr<IPositionRandomizer> m_positionRandomizer;
72
73 void initWalls();
74 void updateInternal(const Position& position, CellType cellType);
75 void freeCellsByType(CellType cellType);
76
77 FORCEINLINE uint32 posToIndex(uint32 x, uint32 y) const;
78 FORCEINLINE uint32 posToIndex(const Position& position) const;
79};
80
81} // namespace SnakeGame
Definition: Grid.h:13
Dim dim() const
Definition: Grid.h:29
static Position center(uint32 width, uint32 height)
Definition: Grid.h:23
Definition: SnakeGame.Build.cs:6
Definition: Types.h:12
Definition: Types.h:18