Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- public class RecursiveFind : MonoBehaviour
- {
- private GameBoard gameBoard;
- public int BoardWidth = 5;
- public int BoardHeight = 5;
- private List<ConnectedNode> connectedNodes;
- private void Start ( )
- {
- gameBoard = new GameBoard ( BoardWidth, BoardHeight );
- // This should make a fallen down q shape...
- gameBoard.AddNode ( 0, 0, new Node ( ) { ColourId = 1 } );
- gameBoard.AddNode ( 1, 0, new Node ( ) { ColourId = 1 } );
- gameBoard.AddNode ( 2, 0, new Node ( ) { ColourId = 1 } );
- gameBoard.AddNode ( 3, 0, new Node ( ) { ColourId = 1 } );
- gameBoard.AddNode ( 3, 1, new Node ( ) { ColourId = 1 } );
- gameBoard.AddNode ( 3, 2, new Node ( ) { ColourId = 1 } );
- gameBoard.AddNode ( 2, 2, new Node ( ) { ColourId = 1 } );
- gameBoard.AddNode ( 1, 2, new Node ( ) { ColourId = 1 } );
- gameBoard.AddNode ( 1, 1, new Node ( ) { ColourId = 1 } );
- FindConnectedNodes ( 0, 0, 1 );
- Debug.Log ( $"connectedNodes is null: {connectedNodes == null}. connectedNodes.Count = {connectedNodes?.Count}" );
- StringBuilder sb = new StringBuilder ( );
- for ( int i = 0; i < connectedNodes.Count; i++ )
- sb.AppendLine ( $"connectedNodes[{i}] [{connectedNodes[i].Position}]" );
- Debug.Log ( sb.ToString () );
- }
- public void FindConnectedNodes ( int x, int y, int colourId )
- {
- gameBoard.ResetChecks ( );
- connectedNodes = new List<ConnectedNode> ( );
- FindPath ( x, y, colourId, Direction.None );
- }
- const string NotApplicable = "Not Applicable";
- private bool FindPath ( int x, int y, int colourId, Direction enteredFrom )
- {
- var thisNode = gameBoard.GetNode ( x, y );
- Debug.Log ( $"Current Position [{x}, {y}]. Node is null : {thisNode == null}. Node is Checked: {thisNode?.Checked.ToString() ?? NotApplicable }" );
- if ( thisNode == null ) return false;
- // Has this node already been checked?
- if ( thisNode.Checked )
- return ( thisNode.ColourId == colourId );
- thisNode.Checked = true;
- // Is this node the right colour?
- if ( thisNode.ColourId != colourId )
- return false;
- // We haven't checked the node, and it's the right colour.
- var newConnectedNode = new ConnectedNode ( )
- {
- Position = new Vector2Int ( x, y )
- };
- Bool4 connections = Bool4.False;
- if ( enteredFrom != Direction.Up ) connections.Up = FindPath ( x, y + 1, colourId, Direction.Down );
- if ( enteredFrom != Direction.Down ) connections.Down = FindPath ( x, y - 1, colourId, Direction.Up );
- if ( enteredFrom != Direction.Left ) connections.Left = FindPath ( x - 1, y, colourId, Direction.Right );
- if ( enteredFrom != Direction.Right ) connections.Right = FindPath ( x + 1, y, colourId, Direction.Left );
- newConnectedNode.Connections = connections;
- connectedNodes.Add ( newConnectedNode );
- return true;
- }
- }
- public class GameBoard
- {
- private Node[] nodes;
- public int Width { get; }
- public int Height { get; }
- public int Count { get; }
- public GameBoard ( int width, int height )
- {
- if ( width <= 0 || height <= 0) throw new ArgumentOutOfRangeException ( "Width and Height cannot be zero." );
- Width = width;
- Height = height;
- Count = width * height;
- nodes = new Node [ width * height ];
- }
- public Node GetNode ( int x, int y )
- {
- if ( x < 0 || x >= Width ) return null;
- if ( y < 0 || y >= Height ) return null;
- var index = y * Width + x;
- if ( index >= Count ) return null;
- return nodes [ index ];
- }
- public bool AddNode ( int x, int y, Node node )
- {
- if ( x < 0 || x >= Width ) return false;
- var index = y * Width + x;
- if ( index >= Count ) return false;
- nodes [ index ] = node;
- return true;
- }
- public Node DeleteNode ( int x, int y, Node node )
- {
- if ( x < 0 || x >= Width ) return null;
- var index = y * Width + x;
- if ( index >= Count ) return null;
- var retunrNode = nodes [ index ];
- nodes [ index ] = null;
- return retunrNode;
- }
- public void ResetChecks ( )
- {
- var count = nodes.Length;
- for ( int i = 0; i < count; i++ )
- if ( nodes [ i ] != null ) nodes [ i ].Checked = false;
- }
- }
- public struct ConnectedNode
- {
- public Vector2Int Position;
- public Bool4 Connections;
- }
- public enum Direction
- {
- None = 0,
- Up,
- Down,
- Left,
- Right
- }
- public struct Bool4
- {
- private readonly bool [ ] bools;
- public Bool4 ( bool b1, bool b2, bool b3, bool b4 )
- {
- bools = new bool [ 4 ] { b1, b2, b3, b4 };
- }
- public static Bool4 False { get { return new Bool4 ( false, false, false, false ); } }
- public static Bool4 True { get { return new Bool4 ( true, true, true, true ); } }
- public bool Up { get => bools [ 0 ]; set => bools [ 0 ] = value; }
- public bool Down { get => bools [ 2 ]; set => bools [ 2 ] = value; }
- public bool Left { get => bools [ 3 ]; set => bools [ 3 ] = value; }
- public bool Right { get => bools [ 1 ]; set => bools [ 1 ] = value; }
- public bool this [ int index] { get => bools [ index ]; }
- public bool IsFalse
- {
- get
- {
- return ( !bools [ 0 ] && ( bools [ 0 ] == bools [ 1 ] == bools [ 2 ] == bools [ 3 ] ) );
- }
- }
- public bool IsTrue
- {
- get
- {
- return ( bools [ 0 ] && ( bools [ 0 ] == bools [ 1 ] == bools [ 2 ] == bools [ 3 ] ) );
- }
- }
- }
- public class Node
- {
- public bool Checked { get; set; }
- public int ColourId { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment