EgonMilanVotrubec

Recursive Connected Nodes

Nov 23rd, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5.  
  6. public class RecursiveFind : MonoBehaviour
  7. {
  8.     private GameBoard gameBoard;
  9.     public int BoardWidth = 5;
  10.     public int BoardHeight = 5;
  11.  
  12.     private List<ConnectedNode> connectedNodes;
  13.  
  14.     private void Start ( )
  15.     {
  16.         gameBoard = new GameBoard ( BoardWidth, BoardHeight );
  17.  
  18.         // This should make a fallen down q shape...
  19.         gameBoard.AddNode ( 0, 0, new Node ( ) { ColourId = 1 } );
  20.         gameBoard.AddNode ( 1, 0, new Node ( ) { ColourId = 1 } );
  21.         gameBoard.AddNode ( 2, 0, new Node ( ) { ColourId = 1 } );
  22.         gameBoard.AddNode ( 3, 0, new Node ( ) { ColourId = 1 } );
  23.         gameBoard.AddNode ( 3, 1, new Node ( ) { ColourId = 1 } );
  24.         gameBoard.AddNode ( 3, 2, new Node ( ) { ColourId = 1 } );
  25.         gameBoard.AddNode ( 2, 2, new Node ( ) { ColourId = 1 } );
  26.         gameBoard.AddNode ( 1, 2, new Node ( ) { ColourId = 1 } );
  27.         gameBoard.AddNode ( 1, 1, new Node ( ) { ColourId = 1 } );
  28.  
  29.         FindConnectedNodes ( 0, 0, 1 );
  30.         Debug.Log ( $"connectedNodes is null: {connectedNodes == null}. connectedNodes.Count = {connectedNodes?.Count}" );
  31.         StringBuilder sb = new StringBuilder ( );
  32.         for ( int i = 0; i < connectedNodes.Count; i++ )
  33.             sb.AppendLine ( $"connectedNodes[{i}] [{connectedNodes[i].Position}]" );
  34.         Debug.Log ( sb.ToString () );
  35.     }
  36.  
  37.     public void FindConnectedNodes ( int x, int y, int colourId )
  38.     {
  39.         gameBoard.ResetChecks ( );
  40.         connectedNodes = new List<ConnectedNode> ( );
  41.         FindPath ( x, y, colourId, Direction.None );
  42.     }
  43.  
  44.     const string NotApplicable = "Not Applicable";
  45.  
  46.     private bool FindPath ( int x, int y, int colourId, Direction enteredFrom )
  47.     {
  48.         var thisNode = gameBoard.GetNode ( x, y );
  49.         Debug.Log ( $"Current Position [{x}, {y}]. Node is null : {thisNode == null}. Node is Checked: {thisNode?.Checked.ToString() ?? NotApplicable }" );
  50.         if ( thisNode == null ) return false;
  51.  
  52.         // Has this node already been checked?
  53.         if ( thisNode.Checked )
  54.             return ( thisNode.ColourId == colourId );
  55.  
  56.         thisNode.Checked = true;
  57.  
  58.         // Is this node the right colour?
  59.         if ( thisNode.ColourId != colourId )
  60.             return false;
  61.  
  62.         // We haven't checked the node, and it's the right colour.
  63.         var newConnectedNode = new ConnectedNode ( )
  64.         {
  65.             Position = new Vector2Int ( x, y )
  66.         };
  67.  
  68.         Bool4 connections = Bool4.False;
  69.  
  70.         if ( enteredFrom != Direction.Up ) connections.Up = FindPath ( x, y + 1, colourId, Direction.Down );
  71.         if ( enteredFrom != Direction.Down ) connections.Down = FindPath ( x, y - 1, colourId, Direction.Up );
  72.         if ( enteredFrom != Direction.Left ) connections.Left = FindPath ( x - 1, y, colourId, Direction.Right );
  73.         if ( enteredFrom != Direction.Right ) connections.Right = FindPath ( x + 1, y, colourId, Direction.Left );
  74.  
  75.         newConnectedNode.Connections = connections;
  76.         connectedNodes.Add ( newConnectedNode );
  77.        
  78.         return true;
  79.     }
  80. }
  81.  
  82.  
  83. public class GameBoard
  84. {
  85.     private Node[] nodes;
  86.     public int Width { get; }
  87.     public int Height { get; }
  88.     public int Count { get; }
  89.     public GameBoard ( int width, int height )
  90.     {
  91.         if ( width <= 0 || height <= 0) throw new ArgumentOutOfRangeException ( "Width and Height cannot be zero." );
  92.         Width = width;
  93.         Height = height;
  94.         Count = width * height;
  95.         nodes = new Node [ width * height ];
  96.     }
  97.  
  98.     public Node GetNode ( int x, int y )
  99.     {
  100.         if ( x < 0 || x >= Width ) return null;
  101.         if ( y < 0 || y >= Height ) return null;
  102.         var index = y * Width + x;
  103.         if ( index >= Count ) return null;
  104.         return nodes [ index ];
  105.     }
  106.  
  107.     public bool AddNode ( int x, int y, Node node )
  108.     {
  109.         if ( x < 0 || x >= Width ) return false;
  110.         var index = y * Width + x;
  111.         if ( index >= Count ) return false;
  112.         nodes [ index ] = node;
  113.         return true;
  114.     }
  115.  
  116.     public Node DeleteNode ( int x, int y, Node node )
  117.     {
  118.         if ( x < 0 || x >= Width ) return null;
  119.         var index = y * Width + x;
  120.         if ( index >= Count ) return null;
  121.         var retunrNode = nodes [ index ];
  122.         nodes [ index ] = null;
  123.         return retunrNode;
  124.     }
  125.  
  126.     public void ResetChecks ( )
  127.     {
  128.         var count = nodes.Length;
  129.         for ( int i = 0; i < count; i++ )
  130.             if ( nodes [ i ] != null ) nodes [ i ].Checked = false;
  131.     }
  132. }
  133.  
  134. public struct ConnectedNode
  135. {
  136.     public Vector2Int Position;
  137.     public Bool4 Connections;
  138. }
  139.  
  140. public enum Direction
  141. {
  142.     None = 0,
  143.     Up,
  144.     Down,
  145.     Left,
  146.     Right
  147. }
  148.  
  149. public struct Bool4
  150. {
  151.     private readonly bool [ ] bools;
  152.  
  153.     public Bool4 ( bool b1, bool b2, bool b3, bool b4 )
  154.     {
  155.         bools = new bool [ 4 ] { b1, b2, b3, b4 };
  156.     }
  157.  
  158.     public static Bool4 False { get { return new Bool4 ( false, false, false, false ); } }
  159.     public static Bool4 True { get { return new Bool4 ( true, true, true, true ); } }
  160.  
  161.     public bool Up { get => bools [ 0 ]; set => bools [ 0 ] = value; }
  162.     public bool Down { get => bools [ 2 ]; set => bools [ 2 ] = value; }
  163.     public bool Left { get => bools [ 3 ]; set => bools [ 3 ] = value; }
  164.     public bool Right { get => bools [ 1 ]; set => bools [ 1 ] = value; }
  165.  
  166.     public bool this [ int index] { get => bools [ index ]; }
  167.  
  168.     public bool IsFalse
  169.     {
  170.         get
  171.         {
  172.             return ( !bools [ 0 ] && ( bools [ 0 ] == bools [ 1 ] == bools [ 2 ] == bools [ 3 ] ) );
  173.         }
  174.     }
  175.  
  176.     public bool IsTrue
  177.     {
  178.         get
  179.         {
  180.             return ( bools [ 0 ] && ( bools [ 0 ] == bools [ 1 ] == bools [ 2 ] == bools [ 3 ] ) );
  181.         }
  182.     }
  183. }
  184.  
  185. public class Node
  186. {
  187.     public bool Checked { get; set; }
  188.     public int ColourId { get; set; }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment