-
Notifications
You must be signed in to change notification settings - Fork 2
/
PuzzleConverter.cs
101 lines (90 loc) · 2.96 KB
/
PuzzleConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using lib.Models;
namespace lib.Puzzles
{
public static class PuzzleConverter
{
public static List<V> ConvertMapToPoints(Map<PuzzleCell> map)
{
V start = null;
for (int y = 0; y < map.SizeY && start == null; y++)
{
for (int x = 0; x < map.SizeX && start == null; x++)
if (map[new V(x, y)] == PuzzleCell.Inside)
start = new V(x, y);
}
var p = start;
var d = Direction.Up;
var result = new List<V>();
while (p != start || result.Count == 0)
{
result.Add(p);
int x = p.X, y = p.Y;
if (d == Direction.Up)
{
if (Has(x, y))
d = Direction.Right;
else if (Has(x - 1, y))
d = Direction.Up;
else
d = Direction.Left;
}
else
if (d == Direction.Down)
{
if (Has(x - 1, y - 1))
d = Direction.Left;
else if (Has(x, y - 1))
d = Direction.Down;
else
d = Direction.Right;
}
else
if (d == Direction.Right)
{
if (Has(x, y - 1))
d = Direction.Down;
else if (Has(x, y))
d = Direction.Right;
else
d = Direction.Up;
}
else
if (d == Direction.Left)
{
if (Has(x - 1, y))
d = Direction.Up;
else if (Has(x - 1, y - 1))
d = Direction.Left;
else
d = Direction.Down;
}
p = p.Shift((int)d);
}
bool Has(int x, int y)
{
var pp = new V(x, y);
return pp.Inside(map) && map[pp] == PuzzleCell.Inside;
}
int sz = 0;
while (sz != result.Count)
{
sz = result.Count;
for (int i = 0; i < result.Count; i++)
{
var a = i == 0 ? result.Last() : result[i - 1];
var b = result[i];
var c = result[(i + 1) % result.Count];
if ((b - a) * (c - b) == 0)
{
result.RemoveAt(i);
break;
}
}
}
return result;
}
}
}