Skip to content

Commit 6535c33

Browse files
committed
Add breadthsearch alg
1 parent 2e79b8a commit 6535c33

File tree

3 files changed

+76
-24
lines changed

3 files changed

+76
-24
lines changed

Assets/Scripts/BoardSystem/Board/BoardManipulationOddR.cs

+68-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using HexBoardGame.SharedData;
1+
using System.Collections.Generic;
2+
using HexBoardGame.SharedData;
23
using Tools.Extensions.Arrays;
34
using UnityEngine;
45

56
namespace HexBoardGame.Runtime
67
{
78
/// <summary>
89
/// The way to manipulate a board in the Odd-Row layout.
10+
/// TODO: Open for many memory/cache optimizations and algorithms improvements.
911
/// </summary>
1012
public class BoardManipulationOddR : IBoardManipulation
1113
{
@@ -30,8 +32,7 @@ public Hex[] GetNeighbours(Vector3Int cell)
3032
foreach (var direction in NeighboursDirections)
3133
{
3234
var neighbour = Hex.Add(center[0], direction);
33-
var array = new[] {neighbour};
34-
neighbours = neighbours.Append(array);
35+
neighbours = neighbours.Append(GetIfExistsOrEmpty(neighbour));
3536
}
3637

3738
return neighbours;
@@ -42,33 +43,34 @@ public Hex[] GetNeighbours(Vector3Int cell)
4243
/// </summary>
4344
private Hex[] GetIfExistsOrEmpty(Hex hex)
4445
{
45-
foreach (var i in _hexPoints)
46-
if (i == hex)
47-
return new[] {i};
48-
49-
return new Hex[] { };
46+
var cell = GetCellCoordinate(hex);
47+
return Contains(cell) ? new[] {GetHexCoordinate(cell)} : new Hex[] { };
5048
}
5149

5250
#region Operations
5351

5452
public bool Contains(Vector3Int cell)
5553
{
56-
var center = GetHexCoordinate(cell);
57-
return GetIfExistsOrEmpty(center).Length > 0;
54+
var hex = GetHexCoordinate(cell);
55+
foreach (var i in _hexPoints)
56+
if (i == hex)
57+
return true;
58+
return false;
5859
}
5960

6061
public Hex[] GetVertical(Vector3Int cell, int length)
6162
{
63+
//For Odd-R the vertical is always empty.
6264
return new Hex[] { };
6365
}
6466

6567
public Hex[] GetHorizontal(Vector3Int cell, int length)
6668
{
67-
var center = GetHexCoordinate(cell);
69+
var point = GetHexCoordinate(cell);
6870
var halfLength = length / 2;
69-
var points = GetIfExistsOrEmpty(center);
70-
var x = center.q;
71-
var y = center.r;
71+
var points = GetIfExistsOrEmpty(point);
72+
var x = point.q;
73+
var y = point.r;
7274

7375
for (var i = 1; i <= halfLength; i++)
7476
points = points.Append(GetIfExistsOrEmpty(new Hex(x + i, y)));
@@ -81,11 +83,11 @@ public Hex[] GetHorizontal(Vector3Int cell, int length)
8183

8284
public Hex[] GetDiagonalAscendant(Vector3Int cell, int length)
8385
{
84-
var center = GetHexCoordinate(cell);
86+
var point = GetHexCoordinate(cell);
8587
var halfLength = length / 2;
86-
var points = GetIfExistsOrEmpty(center);
87-
var x = center.q;
88-
var y = center.r;
88+
var points = GetIfExistsOrEmpty(point);
89+
var x = point.q;
90+
var y = point.r;
8991

9092
for (var i = 1; i <= halfLength; i++)
9193
points = points.Append(GetIfExistsOrEmpty(new Hex(x, y + i)));
@@ -98,11 +100,11 @@ public Hex[] GetDiagonalAscendant(Vector3Int cell, int length)
98100

99101
public Hex[] GetDiagonalDescendant(Vector3Int cell, int length)
100102
{
101-
var center = GetHexCoordinate(cell);
103+
var point = GetHexCoordinate(cell);
102104
var halfLength = length / 2;
103-
var points = GetIfExistsOrEmpty(center);
104-
var x = center.q;
105-
var y = center.r;
105+
var points = GetIfExistsOrEmpty(point);
106+
var x = point.q;
107+
var y = point.r;
106108

107109
for (var i = 1; i <= halfLength; i++)
108110
points = points.Append(GetIfExistsOrEmpty(new Hex(x - i, y + i)));
@@ -113,6 +115,50 @@ public Hex[] GetDiagonalDescendant(Vector3Int cell, int length)
113115
return points;
114116
}
115117

118+
public Hex[] GetPathBreadthSearch(Vector3Int begin, Vector3Int end)
119+
{
120+
var beginHex = GetHexCoordinate(begin);
121+
var endHex = GetHexCoordinate(end);
122+
var frontier = new Queue<Hex>();
123+
frontier.Enqueue(beginHex);
124+
var visited = new Dictionary<Hex, Hex>();
125+
126+
127+
//Creating the breadcrumbs
128+
129+
while (frontier.Count > 0)
130+
{
131+
var current = frontier.Dequeue();
132+
if (current == endHex)
133+
break;
134+
135+
var currentCell = GetCellCoordinate(current);
136+
var neighbours = GetNeighbours(currentCell);
137+
foreach (var next in neighbours)
138+
{
139+
if (!visited.ContainsKey(next))
140+
{
141+
frontier.Enqueue(next);
142+
visited[next] = current;
143+
}
144+
}
145+
}
146+
147+
//Backtracking from the ending point
148+
149+
150+
var path = new List<Hex>();
151+
while(endHex != beginHex)
152+
{
153+
path.Add(endHex);
154+
endHex = visited[endHex];
155+
}
156+
157+
path.Add(beginHex);
158+
path.Reverse();
159+
return path.ToArray();
160+
}
161+
116162
/// <summary>
117163
/// Unity by default makes use the R-Offset Odd to reference tiles inside a TileMap with a vector3Int cell.
118164
/// The internal board manipulation works with HexCoordinates, this method converts vector3int cell to hex.

Assets/Scripts/BoardSystem/Board/IBoardManipulation.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public interface IBoardManipulation
1313
Hex[] GetHorizontal(Vector3Int cell, int length);
1414
Hex[] GetDiagonalAscendant(Vector3Int cell, int length);
1515
Hex[] GetDiagonalDescendant(Vector3Int cell, int length);
16-
16+
Hex[] GetPathBreadthSearch(Vector3Int begin, Vector3Int end);
1717
//TODO:
1818
//1. Range
1919
//2. Path finding
20-
//3. More useful methods...
20+
//3. More useful methods ...
2121
}
2222
}

Assets/Scripts/BoardSystem/BoardController.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using HexBoardGame.SharedData;
3+
using Tools.Extensions.Arrays;
34
using UnityEngine;
45
using UnityEngine.Tilemaps;
56

@@ -16,6 +17,11 @@ public class BoardController : MonoBehaviour
1617
private void Start()
1718
{
1819
CreateBoard();
20+
21+
var start = new Vector3Int(0, 0, 0);
22+
var end = new Vector3Int(3, 0, -3);
23+
var path = BoardManipulation.GetPathBreadthSearch(start, end);
24+
path.Print();
1925
}
2026

2127
private void CreateBoard()

0 commit comments

Comments
 (0)