Skip to content

Core.GridSystem.GridAnalyzerYZ

Dennis Steffen edited this page Jan 4, 2026 · 1 revision

GridAnalyzerYX

The class is a utility designed for navigating and searching through a 2D grid structure. It treats the grid as a "YX" system, where the outer list represents rows (Y) and the inner list represents columns (X). GridAnalyzerYX<T> Here is a breakdown of its primary responsibilities:

1. Grid Management

  • Data Structure: It wraps a , providing a safer and more convenient API than raw nested list indexing. List<List<T>>
  • Validation: The constructor ensures the grid is rectangular (all rows have the same length) and contains data, preventing null or empty reference errors later.
  • Bounds Checking: It includes internal logic () to prevent when accessing coordinates. InBounds``ArgumentOutOfRangeException

2. Navigation and State

Unlike a simple lookup table, this class maintains an internal "cursor" via the field. _position

  • / :SetPosition``GetPosition Allows you to move the analyzer's focus to a specific cell.
  • methods:Neighbor These are used to "look" or "move" relative to the current position (Up, Down, Left, Right). One overload even updates the internal position automatically, allowing you to "walk" through the grid programmatically.

3. Search Capabilities

The class provides several ways to find data within the grid:

  • :Search Finds the first occurrence of a specific value.
  • :SearchAll Uses C# iterators () to find every occurrence of a value. yield return
  • :IterateAll A helper to loop through every cell in the grid, returning both the coordinate and the value.

4. (The "Path-Finder")NeighborEqualSearch

This is the most complex method in the class. It performs a search to find a sequence of adjacent cells that all share the same value.

  • It starts at a given position and looks at the cardinal neighbors (Up, Down, Left, Right).
  • If a neighbor matches the target and hasn't been visited yet, it "jumps" there and continues the search. value
  • This is useful for logic like finding connected "blobs" of the same tile type or tracing a path in a puzzle game.

Key Technical Details

  • Generic ():<T> It can handle any data type (integers, strings, or custom Tile objects).
  • :PointInt It heavily relies on a type (from your namespace) to handle 2D coordinates as a single object. PointInt``Meatcorps.Engine.Core.Data
  • Equality: It uses to ensure it correctly compares the generic type . EqualityComparer<T>.Default``T

Summary: You would use this class if you have a 2D map or board and need to perform logic like "find the nearest empty space," "check what is to the left of the player," or "find all connected water tiles."

Implementation

SourceCode

Examples

Check for walkable locations. This is used in CyberMaze:

var analyzer = new GridAnalyzerYX<char>(levelData);
var allowedDirections = new List<PointInt>();
var allDirections = new PointInt[]
            { new PointInt(-1, 0), new PointInt(1, 0), new PointInt(0, -1), new PointInt(0, 1) };
foreach (var direction in allDirections)
{
    var exist = analyzer.Neighbor(position, direction, out var neighbor);
    if (((!exist) || neighbor != '#') && walkable)
        allowedDirections.Add(direction);
}                            

Search for a specific char used in Snake

var analyzer = new GridAnalyzerYX<char>(levelData);
foreach (var position in analyzer.NeighborEqualSearch('2', player2Position))
                    playerPositions.Add(position);

Clone this wiki locally