-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchForm.cs
More file actions
133 lines (116 loc) · 4.99 KB
/
Copy pathSearchForm.cs
File metadata and controls
133 lines (116 loc) · 4.99 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Game_Of_Life
{
public partial class SearchForm : Form
{
private decimal time = 0.0M;
public SearchForm()
{
InitializeComponent();
}
private async void BtnSearch_Click(object sender, EventArgs e)
{
time = 0.0M;
btnSearch.Enabled = false;
int height = (int)numUpDownHeight.Value;
int width = (int)numUpDownWidth.Value;
List<int> B = textBoxB.Text.Split(' ').Select(int.Parse).ToList();
List<int> S = textBoxS.Text.Split(' ').Select(int.Parse).ToList();
int rank = (int)numericUpDownRank.Value;
await Search(width, height, B, S, rank);
}
private async Task Search(int width, int height, List<int> b, List<int> s, int rank = 1)
{
timer1.Start();
List<CellularAutomaton> favoriteFields = new List<CellularAutomaton>();
await Task.Run(() =>
{
float progress = 0;
const int LimitOnSearchSteps = 25;
long numOfCombinations = (long)Math.Pow(2, width * height);
List<int> checkedFields = new List<int>();
_ = Parallel.For(0, numOfCombinations + 1, delegate (long i) // основной цикл с перебором всех вариантов начальных условий
{
progress++;
bool isFound = false;
CellularAutomaton field = new CellularAutomaton(30, 30, b, s, rank);
List<CellularAutomaton> listOfFields = new List<CellularAutomaton>();
// перевод счетчика основного цикла в двоичное число с дополнием справа нулями
string binaryString = Convert.ToString(i, 2).PadRight(width * height, '0');
// заполнение поля
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
bool currentCell = Math.Abs(char.GetNumericValue(binaryString[y + (x * height)]) - 1.0) < 0.001; // берем из строки один элемент
field.field[x + (field.cols / 2), y + (field.rows / 2)] = currentCell;
}
}
int sumOfNeighbours = field.GetSumOfAllNeighbours();
if (checkedFields.Contains(sumOfNeighbours))
{
return;
}
else
{
checkedFields.Add(sumOfNeighbours); // запоминаем начальное условие как проверенное
}
for (int j = 0; j < LimitOnSearchSteps; j++) // поиск состояния, совпадающего хотя бы с одним из предыдущих
{
listOfFields.Add(field.Clone());
field.NextGeneration();
if (field.IsEmpty())
{
break;
}
for (int k = 1; k < listOfFields.Count; k++)
{
if (listOfFields[k] == field && !listOfFields.Last().IsEmpty()) // сравнение с предыдущими состояниями для выявления фигур
{
favoriteFields.Add(listOfFields.Last().Crop());
isFound = true;
break;
}
}
if (isFound)
{
break;
}
}
_ = Invoke((Action)(() =>
{
progressBar1.Value = (int)(progress / numOfCombinations * 100);
}));
});
});
timer1.Stop();
btnSearch.Enabled = true;
progressBar1.Value = 100;
if (favoriteFields.Count > 0)
{
DemonstrateForm form3 = new DemonstrateForm(favoriteFields, b, s, rank);
form3.Show();
}
else
{
_ = MessageBox.Show("Nothing found.");
}
}
private async void Timer1_Tick(object sender, EventArgs e)
{
await Task.Run(() =>
{
_ = Invoke((Action)(() =>
{
time += (decimal)timer1.Interval / 1000;
labelTime.Text = $"Time: {time} s";
}));
});
}
}
}