diff --git a/.idea/.idea.Life/.idea/contentModel.xml b/.idea/.idea.Life/.idea/contentModel.xml
index b440801..c6f9d81 100644
--- a/.idea/.idea.Life/.idea/contentModel.xml
+++ b/.idea/.idea.Life/.idea/contentModel.xml
@@ -13,6 +13,7 @@
+
diff --git a/Life/Automaton/BasicField.cs b/Life/Automaton/BasicField.cs
index ac32053..bf196df 100644
--- a/Life/Automaton/BasicField.cs
+++ b/Life/Automaton/BasicField.cs
@@ -40,9 +40,9 @@ namespace Life.Automaton
public abstract IEnumerable Neighbours(int x, int y);
- public void Transform(Rule transform)
+ public void Transform(IRule rule)
{
- _cells = transform(this);
+ _cells = rule.Next(this);
Notify();
}
diff --git a/Life/Automaton/GameOfLife.cs b/Life/Automaton/GameOfLife.cs
index 136063a..ac5e891 100644
--- a/Life/Automaton/GameOfLife.cs
+++ b/Life/Automaton/GameOfLife.cs
@@ -1,24 +1,27 @@
using System;
-using System.IO;
using System.Linq;
-using System.Runtime.Serialization;
namespace Life.Automaton
{
[Serializable]
public class GameOfLife : IAutomaton
{
- public GameOfLife(int width, int height)
+ [Serializable]
+ public class StandardRule : IRule
{
- Field = new MooreField(width, height);
- }
+ private string survive;
+ private string born;
+
+ public StandardRule(string born, string survive)
+ {
+ this.born = born;
+ this.survive = survive;
+ }
- public static Rule MakeRule(string born, string survive)
- {
bool WillSurvive(int neighbours) => survive.Contains(neighbours.ToString());
bool WillBorn(int neighbours) => born.Contains(neighbours.ToString());
- return field =>
+ public Cell[,] Next(IField field)
{
var @new = new Cell[field.Size.Width, field.Size.Height];
@@ -36,10 +39,15 @@ namespace Life.Automaton
}
return @new;
- };
+ }
+ }
+
+ public GameOfLife(int width, int height)
+ {
+ Field = new MooreField(width, height);
}
- public Rule Rule { get; set; } = MakeRule("3", "32");
+ public IRule Rule { get; set; } = new StandardRule("3", "32");
public int Iteration { get; private set; }
diff --git a/Life/Automaton/IField.cs b/Life/Automaton/IField.cs
index 1107314..11b4865 100644
--- a/Life/Automaton/IField.cs
+++ b/Life/Automaton/IField.cs
@@ -4,7 +4,6 @@ using Life.Misc;
namespace Life.Automaton
{
- public delegate Cell[,] Rule(IField old);
public interface IField : IObservable
{
@@ -16,7 +15,7 @@ namespace Life.Automaton
IEnumerable Neighbours(int x, int y);
- void Transform(Rule rule);
+ void Transform(IRule rule);
void Notify();
void Resize(int width, int height);
diff --git a/Life/Automaton/IRule.cs b/Life/Automaton/IRule.cs
new file mode 100644
index 0000000..8f6398a
--- /dev/null
+++ b/Life/Automaton/IRule.cs
@@ -0,0 +1,7 @@
+namespace Life.Automaton
+{
+ public interface IRule
+ {
+ Cell[,] Next(IField old);
+ }
+}
\ No newline at end of file
diff --git a/Life/ViewModel/AutomatonViewModel.cs b/Life/ViewModel/AutomatonViewModel.cs
index 1657ef1..5e68e3e 100644
--- a/Life/ViewModel/AutomatonViewModel.cs
+++ b/Life/ViewModel/AutomatonViewModel.cs
@@ -147,11 +147,7 @@ namespace Life.ViewModel
if (dialog.ShowDialog() == true)
{
using var file = dialog.OpenFile();
- var field = _formatter.Deserialize(file) as IField;
-
- Automaton.ReplaceField(field);
-
- OnPropertyChanged(nameof(Field));
+ Automaton = _formatter.Deserialize(file) as IAutomaton;
}
}
@@ -162,7 +158,7 @@ namespace Life.ViewModel
if (dialog.ShowDialog() == true)
{
using var file = dialog.OpenFile();
- _formatter.Serialize(file, Field);
+ _formatter.Serialize(file, Automaton);
}
}
}
| |