inz-00/Assets/Map/LocationGenerator.cs
2019-08-15 20:22:18 +02:00

62 lines
1.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = System.Random;
namespace Assets.Map
{
public class LocationGenerator
{
private Graph<Location> _graph;
private readonly List<(int, Color)> _queue = new List<(int, Color)>();
private Random _random;
public bool Done => _queue.Count == 0;
public Graph<Location> Result => _graph;
public LocationGenerator(Graph<Location> graph, Random random = null)
{
_graph = graph;
_random = random ?? new Random();
}
public void Init(params (int, Color)[] start)
{
foreach (var pair in start)
_queue.Add(pair);
}
private (int, Color) Dequeue()
{
while (_queue.Count > 0)
{
var index = _random.Next(_queue.Count);
var pair = _queue[index];
_queue.RemoveAt(index);
if (_graph.Vertices[pair.Item1].Type == Color.clear) return pair;
}
throw new Exception("No more elements.");
}
public void Step()
{
try
{
var (vertex, color) = Dequeue();
_graph.Vertices[vertex].Type = color;
foreach (var neighbour in _graph.Neighbours(vertex))
if (_graph.Vertices[neighbour].Type == Color.clear)
_queue.Add((neighbour, color));
}
catch (Exception ex)
{
}
}
}
}