Helpers:

 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GameOfLife
{
    public class AsyncObservableCollection<T> : ObservableCollection<T>
    {
        private SynchronizationContext _synchronizationContext = SynchronizationContext.Current;

        public AsyncObservableCollection()
        {
        }

        public AsyncObservableCollection(IEnumerable<T> list)
            : base(list)
        {
        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (SynchronizationContext.Current == _synchronizationContext)
            {
                RaiseCollectionChanged(e);
            }
            else
            {
                _synchronizationContext.Send(RaiseCollectionChanged, e);
            }
        }

        private void RaiseCollectionChanged(object param)
        {
            base.OnCollectionChanged((NotifyCollectionChangedEventArgs)param);
        }

        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (SynchronizationContext.Current == _synchronizationContext)
            {
                RaisePropertyChanged(e);
            }
            else
            {
                _synchronizationContext.Send(RaisePropertyChanged, e);
            }
        }

        private void RaisePropertyChanged(object param)
        {
            base.OnPropertyChanged((PropertyChangedEventArgs)param);
        }
    }
}
 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace GameOfLife
{
    public class ColorToBrushConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return Brushes.Black; // Default color

            Color color = (Color)value;

            return new SolidColorBrush(color);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace GameOfLife

{
    public class RelayCommand : ICommand
    {
        private Action<object> execute;

        private Predicate<object> canExecute;

        private event EventHandler CanExecuteChangedInternal;

        public RelayCommand(Action<object> execute)
            : this(execute, DefaultCanExecute)
        {

        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }

            if (canExecute == null)
            {
                throw new ArgumentNullException("canExecute");
            }

            this.execute = execute;
            this.canExecute = canExecute;
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
                this.CanExecuteChangedInternal += value;
            }

            remove
            {
                CommandManager.RequerySuggested -= value;
                this.CanExecuteChangedInternal -= value;
            }
        }

        public bool CanExecute(object parameter)
        {
            return this.canExecute != null && this.canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            this.execute(parameter);
        }

        public void OnCanExecuteChanged()
        {
            EventHandler handler = this.CanExecuteChangedInternal;
            if (handler != null)
            {
                handler.Invoke(this, EventArgs.Empty);
            }
        }

        public void Destroy()
        {
            this.canExecute = _ => false;
            this.execute = _ => { return; };
        }

        private static bool DefaultCanExecute(object parameter)
        {
            return true;
        }
    }
}




Models:

 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
using EbUserDataFeedbackTestApplication;
using GameOfLife.Model;
using GameOfLife.ViewModel;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace GameOfLife
{
    public class Cell : BaseViewModel
    {
        public Cell()
        {
            ToggleCellStateCmd = new RelayCommand(ToggleCellStateExe);
        }

        public Cell(CellState cellState)
        {
            _cellState = cellState;
        }

        public Cell(Cell cell)
        {
            try
            {
                if (cell == null)
                {
                    throw new Exception("cell can not be null");
                }

                _cellState = cell.CellState;
            }
            catch (Exception ex)
            {
                throw new Exception("Cell(Cell cell): " + ex.ToString());
            }
        }

        public ICommand ToggleCellStateCmd { get; set; }
        private void ToggleCellStateExe(object obj)
        {
            if (this.CellState == CellState.Alive)
            {
                this.CellState = CellState.Dead;
            }
            else
            {
                this.CellState = CellState.Alive;
                Constans.lastX = this.x;
                Constans.lastY = this.y;
            }
        }

        private CellState _cellState;
        public CellState CellState
        {
            get
            {
                return _cellState;
            }
            set
            {
                _cellState = value;
                RaisePropertyChanged("CellState");
            }
        }
        public int x { get; set; }
        public int y { get; set; }
    }

    public enum CellState
    {
        Alive,
        Dead
    }
}
 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
using GameOfLife.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameOfLife.Model
{
    public static class Constans 
    {
        public static int lastX = 1;
        public static int lastY = 1;
        public static string SelectedTemplate = "";

        private static int _numberOfAlive;
        public static  int NumberOfAlive
        {
            get
            {
                return _numberOfAlive;
            }
            set
            {
                _numberOfAlive = value;
            }
        }
    }
}




MainWindow:

  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
<Window x:Class="GameOfLife.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GameOfLife"
        mc:Ignorable="d" 
        MouseMove="Window_MouseMove"
        MouseLeftButtonUp="Window_MouseLeftButtonUp"
        xmlns:z="http://schemas.microsoft.com/expression/2010/interactivity"
        Title="Game Of Life By Linaq" Height="822" Width="950" ResizeMode="NoResize">
    <z:Interaction.Triggers>
        <z:EventTrigger EventName="MouseLeftButtonUp">
            <z:InvokeCommandAction Command="{Binding OnMouseRelaseCmd}" />
        </z:EventTrigger>
    </z:Interaction.Triggers>
    <Window.Resources>
        <Style x:Key="cellStyle" TargetType="{x:Type Rectangle}">
            <Style.Setters>
                <Setter Property="Stroke" Value="Black"></Setter>
                <Setter Property="StrokeThickness" Value="0.25"></Setter>
            </Style.Setters>
            <Style.Triggers>
                <DataTrigger Binding="{Binding CellState}" Value="Alive">
                    <Setter Property="Fill" Value="Green"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding CellState}" Value="Dead">
                    <Setter Property="Fill" Value="White"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Canvas x:Name="canv" Grid.ColumnSpan="11" Grid.RowSpan="11" Panel.ZIndex="999" IsHitTestVisible="False">
            <Image x:Name="Foo" Source="{Binding TemplatePath}"  Width="{Binding TemplateImgWidth}" Height="{Binding TemplateImgHeight}" />
        </Canvas>
        
            <StackPanel Orientation="Vertical" Margin="5">
            
            
            <Button Content="Start" Margin="3" Command="{Binding StartCmd}"/>
            <Button Content="Stop" Margin="3" Command="{Binding StopCmd}"/>
            <Button Content="Next Step" Margin="3" Command="{Binding NextStepCmd}"/>
            <Button Content="Reset" Command="{Binding ResetCmd}" Margin="3"/>
            <Button Content="Generate Random" Command="{Binding FillRandomCellsCmd}" Margin="3"/>
            <StackPanel Orientation="Horizontal">
                <StackPanel>
                    <Button Width="50" Height="50" Margin="10" Command="{Binding TemplateCmd}" CommandParameter="/Resources/1.png" Background="Transparent">
                        <Image Source="/Resources/1.png"></Image>
                    </Button>
                    <Button Width="50" Height="50" Margin="10" Command="{Binding TemplateCmd}" CommandParameter="/Resources/2.png" Background="Transparent">
                        <Image Source="/Resources/2.png"></Image>
                    </Button>
                    <Button Width="50" Height="50" Margin="10" Command="{Binding TemplateCmd}" CommandParameter="/Resources/3.png" Background="Transparent">
                        <Image Source="/Resources/3.png"></Image>
                    </Button>
                    <Button Width="50" Height="50" Margin="10" Command="{Binding TemplateCmd}" CommandParameter="/Resources/4.png" Background="Transparent">
                        <Image Source="/Resources/4.png"></Image>
                    </Button>
                    <Button Width="50" Height="50" Margin="10" Command="{Binding TemplateCmd}" CommandParameter="/Resources/5.png" Background="Transparent">
                        <Image Source="/Resources/5.png"></Image>
                    </Button>
                    <Button Width="50" Height="50" Margin="10" Command="{Binding TemplateCmd}" CommandParameter="/Resources/6.png" Background="Transparent">
                        <Image Source="/Resources/6.png"></Image>
                    </Button>
                    <Button Width="50" Height="50" Margin="10" Command="{Binding TemplateCmd}" CommandParameter="/Resources/7.png" Background="Transparent">
                        <Image Source="/Resources/7.png"></Image>
                    </Button>
                    <Button Width="50" Height="50" Margin="10" Command="{Binding TemplateCmd}" CommandParameter="/Resources/8.png" Background="Transparent">
                        <Image Source="/Resources/8.png"></Image>
                    </Button>
                    <Button Width="50" Height="50" Margin="10" Command="{Binding TemplateCmd}" CommandParameter="/Resources/9.png" Background="Transparent">
                        <Image Source="/Resources/9.png"></Image>
                    </Button>

                </StackPanel>
                <StackPanel>
                    <Button Width="50" Height="50" Margin="10" Command="{Binding TemplateCmd}" CommandParameter="/Resources/10.png" Background="Transparent">
                        <Image Source="/Resources/10.png"></Image>
                    </Button>
                </StackPanel>
            </StackPanel>
           
        </StackPanel>
        <ItemsControl Grid.Column="1" Margin="0" 
                      Name="cellGrid" 
                      ItemsSource="{Binding MainCellsList, Mode=OneWay}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="{Binding Size}" Rows="{Binding Size}"></UniformGrid>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Rectangle Style="{StaticResource cellStyle}"> 
                        <Rectangle.InputBindings>
                            <MouseBinding Gesture="LeftClick" 
                                          Command="{Binding ToggleCellStateCmd}"
                                         CommandParameter="{Binding}">
                            </MouseBinding> 
                        </Rectangle.InputBindings>
                    </Rectangle>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl> 
            <StatusBar  Grid.Row="1" Grid.ColumnSpan="2">
                <StatusBarItem>

                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Frames: "/>
                        <TextBlock Text="{Binding Frames}"/>
                    </StackPanel>
                </StatusBarItem>
            </StatusBar> 
    </Grid>
</Window>
 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
using GameOfLife.Model;
using GameOfLife.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GameOfLife
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }
        //private void Canvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        //{
        //    var pos = e.GetPosition((Canvas)sender); 

        //    Foo.SetValue(Canvas.LeftProperty, pos.X);
        //    Foo.SetValue(Canvas.TopProperty, pos.Y);
        //}

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            Point position = Mouse.GetPosition(this);


            Foo.SetValue(Canvas.LeftProperty, position.X);
            Foo.SetValue(Canvas.TopProperty, position.Y);
        }

        private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            
        }
    }
}




ViewModels:

 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameOfLife.ViewModel
{
     public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
        }

        public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
        public static void RaiseStaticPropertyChanged(string propName)
        {
            EventHandler<PropertyChangedEventArgs> handler = StaticPropertyChanged;
            if (handler != null)
                handler(null, new PropertyChangedEventArgs(propName));

        }
    }
}
  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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
using EbUserDataFeedbackTestApplication;
using GameOfLife.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace GameOfLife.ViewModel
{
    public class MainViewModel : BaseViewModel
    {
        private static object _updateLock = new object();
        private static object _drawTemplateLock = new object();
        private DispatcherTimer _timer;
        bool IsRunning = false;
        public MainViewModel()
        {
            ResetCmd = new RelayCommand(ResetExe);
            NextStepCmd = new RelayCommand(NextStepExe);
            StartCmd = new RelayCommand(StartExe);
            StopCmd = new RelayCommand(StopExe);
            FillRandomCellsCmd = new RelayCommand(FillRandomCellsExe);
            TemplateCmd = new RelayCommand(TemplateExe);
            OnMouseRelaseCmd = new RelayCommand(OnMouseRelaseExe);
            Size = 80;
            GenrateField(Size, Size);

        }
        

        private void OnMouseRelaseExe(object obj)
        {
            if (!string.IsNullOrWhiteSpace(TemplatePath))
            {
                string tmpID = Path.GetFileNameWithoutExtension(TemplatePath);
                 DrawTemplate(Constans.lastX,Constans.lastY,tmpID);
                TemplatePath = "";
            }
        }

        private void TemplateExe(object obj)
        {
            if (string.IsNullOrEmpty(TemplatePath) && obj is String)
                TemplatePath = obj as String;
            else
                TemplatePath = "";
        }

        private string _templatePath;
        public string TemplatePath
        {
            get
            {
                return _templatePath;
            }
            set
            {
                _templatePath = value;
                switch (Path.GetFileNameWithoutExtension(value))
                {
                    case "1":
                        TemplateImgWidth = "40";
                        TemplateImgHeight = "40";
                        break;
                    case "2":
                        TemplateImgWidth = "60";
                        TemplateImgHeight = "60";
                        break;
                    case "3":
                        TemplateImgWidth = "60";
                        TemplateImgHeight = "80";
                        break;
                    case "4":
                        TemplateImgWidth = "60";
                        TemplateImgHeight = "80";
                        break;
                    case "5":
                        TemplateImgWidth = "60";
                        TemplateImgHeight = "60";
                        break;
                    case "6":
                        TemplateImgWidth = "80";
                        TemplateImgHeight = "40";
                        break;
                    case "7":
                        TemplateImgWidth = "20";
                        TemplateImgHeight = "60";
                        break;
                    case "8":
                        TemplateImgWidth = "60";
                        TemplateImgHeight = "60";
                        break;
                    case "9":
                        TemplateImgWidth = "100";
                        TemplateImgHeight = "80";
                        break;
                    case "10":
                        TemplateImgWidth = "240";
                        TemplateImgHeight = "240";
                        break;
                    default:
                        TemplateImgWidth = "60";
                        TemplateImgHeight = "60";
                        break;
                }
                    
                RaisePropertyChanged("TemplatePath");
            }
        }

        private string _templateImgWidth;
        public string TemplateImgWidth
        {
            get
            {
                return _templateImgWidth;
            }
            set
            {
                _templateImgWidth = value;
                RaisePropertyChanged("TemplateImgWidth");
            }
        }

        private string _templateImgHeight;
        public string TemplateImgHeight
        {
            get
            {
                return _templateImgHeight;
            }
            set
            {
                _templateImgHeight = value;
                RaisePropertyChanged("TemplateImgWidth");
            }
        }

        private void FillRandomCellsExe(object obj)
        {
            if (IsRunning)
            {
                _timer.Stop();
                IsRunning = false;
            }
            MainCellsList = new ObservableCollection<Cell>();
            FillRandomCells();
        } 

        public ICommand ResetCmd { get; set; }
        public ICommand NextStepCmd { get; set; }
        public ICommand StartCmd { get; set; }
        public ICommand StopCmd { get; set; }
        public ICommand FillRandomCellsCmd { get; set; }
        public ICommand TemplateCmd { get; set; }
        public ICommand OnMouseRelaseCmd { get; set; }
        private void StopExe(object obj)
        {
            if (IsRunning)
            {
                _timer.Stop();
                IsRunning = false;
            } 
        }

        private void StartExe(object obj)
        {
            if (!IsRunning && !IsFieldEmpty())
            {
                IsRunning = true;
                _timer = new DispatcherTimer();
                _timer.Interval = new TimeSpan(0, 0, 0, 0, 150);
                _timer.Tick += new EventHandler(StepTimerEventHandler);
                _timer.Start();
            } 
        }

        private async void StepTimerEventHandler(object sender, EventArgs e)
        {
            await UpdateField(); 
        }

        private async void NextStepExe(object obj)
        {
            if (!IsRunning && !IsFieldEmpty())
            {
                await UpdateField();
            }
        }

        private void ResetExe(object obj)
        {
            StopExe(obj);
            MainCellsList = new ObservableCollection<Cell>();
            GenrateField(Size, Size);
            Frames = 0;
        }

        private ObservableCollection<Cell> _mainCellsList = new ObservableCollection<Cell>();
        public ObservableCollection<Cell> MainCellsList
        {
            get
            {
                return _mainCellsList;
            }
            set
            {
                _mainCellsList = value; 
                RaisePropertyChanged("MainCellsList");
            }
        }
         
        private int _size;
        public int Size
        {
            get
            {
                return _size;
            }
            set
            {
                _size = value;
                RaisePropertyChanged("Size");
            }
        }

        private int _frames =0;
        public int Frames
        {
            get
            {
                return _frames;
            }
            set
            {
                _frames = value;
                RaisePropertyChanged("Frames");
            }
        }

        private int _numberOfRandomCells;
        public int NumberOfRandomCells
        {
            get
            {
                return _numberOfRandomCells;
            }
            set
            {
                _numberOfRandomCells = value;
                RaisePropertyChanged("NumberOfRandomCells");
            }
        }

        private void GenrateField(int sizeX, int sizeY)
        {
            for (int ix = 0; ix < sizeX; ix++)
            {
                for (int iy = 0; iy < sizeY; iy++)
                {
                    MainCellsList.Add(new Cell()
                    {
                        x = ix,
                        y = iy,
                        CellState = CellState.Dead
                    });
                }
            }
        }

        private void FillRandomCells()
        {
            Random rand = new Random();
            for (int ix = 0; ix < Size; ix++)
            {
                for (int iy = 0; iy < Size; iy++)
                {
                    if (rand.NextDouble() >= 0.5)
                    {
                        MainCellsList.Add(new Cell()
                        {
                            x = ix,
                            y = iy,
                            CellState = CellState.Alive
                        });
                    }
                    else
                    {
                        MainCellsList.Add(new Cell()
                        {
                            x = ix,
                            y = iy,
                            CellState = CellState.Dead
                        });
                    }
                }
            }
        }

        private async Task UpdateField()
        {
            try
            {
                await Task.Run(() =>
                {
                    lock (_updateLock)
                    {
                        ObservableCollection<Cell> updatedGrid = new ObservableCollection<Cell>();    // The updated grid.

                        foreach (Cell gridCell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(gridCell);

                            int topIndex = cellIndex - Size;
                            int topRightIndex = cellIndex - Size + 1;
                            int rightIndex = cellIndex + 1;
                            int bottomRightIndex = cellIndex + Size + 1;
                            int bottomIndex = cellIndex + Size;
                            int bottomLeftIndex = cellIndex + Size - 1;
                            int leftIndex = cellIndex - 1;
                            int topLeftIndex = cellIndex - Size - 1;

                            // Determine if the cell is on the top/right/bottom/left edge of the grid - certain neighbours must be ignored if a cell is on an edge.
                            bool topEdge = cellIndex < Size ? true : false;
                            bool rightEdge = ((cellIndex + 1) % Size) == 0 ? true : false;
                            bool leftEdge = (cellIndex % Size) == 0 ? true : false;
                            bool bottomEdge = (cellIndex + Size) >= (Size * Size) ? true : false;

                            // Check each neighbour - determine the number of living neighbours.
                            int numberOfNeighbours = 0;
                            if (!topEdge && IsCellAlive(topIndex))
                            {
                                numberOfNeighbours++;
                            }
                            if (!rightEdge && IsCellAlive(topRightIndex))
                            {
                                numberOfNeighbours++;
                            }
                            if (!rightEdge && IsCellAlive(rightIndex))
                            {
                                numberOfNeighbours++;
                            }
                            if (!rightEdge && IsCellAlive(bottomRightIndex))
                            {
                                numberOfNeighbours++;
                            }
                            if (!bottomEdge && IsCellAlive(bottomIndex))
                            {
                                numberOfNeighbours++;
                            }
                            if (!leftEdge && IsCellAlive(bottomLeftIndex))
                            {
                                numberOfNeighbours++;
                            }
                            if (!leftEdge && IsCellAlive(leftIndex))
                            {
                                numberOfNeighbours++;
                            }
                            if (!leftEdge && IsCellAlive(topLeftIndex))
                            {
                                numberOfNeighbours++;
                            }

                            Cell updatedCell = new Cell(gridCell); 
                            if (gridCell.CellState == CellState.Alive)
                            { 
                                switch (numberOfNeighbours)
                                {
                                    case 0:
                                    case 1:
                                        updatedCell.CellState = CellState.Dead; // Lonely.
                                        break;
                                    case 2:
                                    case 3:
                                        updatedCell.CellState = CellState.Alive; // Lives.
                                        break;
                                    case 4:
                                    case 5:
                                    case 6:
                                    case 7:
                                    case 8:
                                        updatedCell.CellState = CellState.Dead; // Overcrowded.
                                        break;
                                }
                            }
                            else if (gridCell.CellState == CellState.Dead)
                            { 
                                switch (numberOfNeighbours)
                                {
                                    case 3:
                                        updatedCell.CellState = CellState.Alive; // Takes 3 to give birth.
                                        break;
                                    case 0:
                                    case 1:
                                    case 2:
                                    case 4:
                                    case 5:
                                    case 6:
                                    case 7:
                                    case 8:
                                        updatedCell.CellState = CellState.Dead; // Barren.
                                        break;
                                }
                            }
                            updatedGrid.Add(updatedCell); 
                        }
                         
                        if (updatedGrid.Count == _mainCellsList.Count)
                        {
                            foreach (Cell gridCell in _mainCellsList)
                            {
                                CellState updatedCellState = updatedGrid.ElementAt(_mainCellsList.IndexOf(gridCell)).CellState;
                                if (updatedCellState != gridCell.CellState)
                                {
                                    gridCell.CellState = updatedCellState;
                                }
                            }
                        }

                        if (updatedGrid.Count == 0)
                        {
                            StopExe(null);
                            return;
                        }

                        RaisePropertyChanged("MainCellsList");
                        Frames++;
                    } 
                });
            }
            catch (Exception)
            {
                
            }
        }

        public void DrawTemplate(int startPosX, int startPosY, string templateId)
        {
            try
            {
                ObservableCollection<Cell> updatedGrid = new ObservableCollection<Cell>();
                int startCellIndex = _mainCellsList.IndexOf(_mainCellsList.FirstOrDefault(xx => xx.x == startPosX && xx.y == startPosY));

                switch (templateId)
                {
                    case "1":
                        foreach (Cell _cell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(_cell);
                            Cell updatedCell = updatedCell = new Cell(_cell);
                            if (cellIndex == startCellIndex)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + 1))
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + Size + 1))
                                updatedCell.CellState = CellState.Alive;
                            updatedGrid.Add(updatedCell);
                        }
                        break;
                    case "2":
                        foreach (Cell _cell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(_cell);
                            Cell updatedCell = updatedCell = new Cell(_cell);
                            if (cellIndex == startCellIndex)
                                updatedCell.CellState = CellState.Dead;
                            if (cellIndex == startCellIndex + 1)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + 2 * Size))
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + 2 * Size + 1))
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + 2 * Size + 2))
                                updatedCell.CellState = CellState.Alive;
                            updatedGrid.Add(updatedCell);
                        }
                        break;
                    case "3":
                        foreach (Cell _cell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(_cell);
                            Cell updatedCell = updatedCell = new Cell(_cell);
                            if (cellIndex == startCellIndex)
                                updatedCell.CellState = CellState.Dead;
                            if (cellIndex == startCellIndex + 1)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + 2))
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + Size))
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + Size + 1))
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + 2 * Size + 1))
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + 3 * Size + 1))
                                updatedCell.CellState = CellState.Alive;
                            updatedGrid.Add(updatedCell);
                        }
                        break;
                    case "4":
                        foreach (Cell _cell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(_cell);
                            Cell updatedCell = updatedCell = new Cell(_cell);
                            if (cellIndex == startCellIndex)
                                updatedCell.CellState = CellState.Dead;
                            if (cellIndex == startCellIndex + 1)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size + 2)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 2 * Size)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 2 * Size + 2)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 3 * Size + 1)
                                updatedCell.CellState = CellState.Alive;
                            updatedGrid.Add(updatedCell);
                        }
                        break;
                    case "5":
                        foreach (Cell _cell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(_cell);
                            Cell updatedCell = updatedCell = new Cell(_cell);
                            if (cellIndex == startCellIndex)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + 1))
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + Size))
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + Size + 2))
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == (startCellIndex + 2 * Size + 1))
                                updatedCell.CellState = CellState.Alive;
                            updatedGrid.Add(updatedCell);
                        }
                        break;
                    case "6":
                        foreach (Cell _cell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(_cell);
                            Cell updatedCell = updatedCell = new Cell(_cell);
                            if (cellIndex == startCellIndex)
                                updatedCell.CellState = CellState.Dead;
                            if (cellIndex == startCellIndex + 1)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 2)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 3)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size + 1)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size + 2)
                                updatedCell.CellState = CellState.Alive;
                            updatedGrid.Add(updatedCell);
                        }
                        break;
                    case "7":
                        foreach (Cell _cell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(_cell);
                            Cell updatedCell = updatedCell = new Cell(_cell);
                            if (cellIndex == startCellIndex)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 2 * Size)
                                updatedCell.CellState = CellState.Alive;
                            updatedGrid.Add(updatedCell);
                        }
                        break;
                    case "8":
                        foreach (Cell _cell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(_cell);
                            Cell updatedCell = updatedCell = new Cell(_cell);
                            if (cellIndex == startCellIndex)
                                updatedCell.CellState = CellState.Dead;
                            if (cellIndex == startCellIndex + 1)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 2)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size + 2)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 2 * Size + 2)
                                updatedCell.CellState = CellState.Alive;
                            updatedGrid.Add(updatedCell);
                        }
                        break;
                    case "9":
                        foreach (Cell _cell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(_cell);
                            Cell updatedCell = updatedCell = new Cell(_cell);
                            if (cellIndex == startCellIndex)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 3)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size + 4)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 2 * Size)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 2 * Size + 4)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 3 * Size + 1)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 3 * Size + 2)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 3 * Size + 3)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 3 * Size + 4)
                                updatedCell.CellState = CellState.Alive;
                            updatedGrid.Add(updatedCell);
                        }
                        break;
                    case "10":
                        foreach (Cell _cell in _mainCellsList)
                        {
                            int cellIndex = _mainCellsList.IndexOf(_cell);
                            Cell updatedCell = updatedCell = new Cell(_cell);
                            if (cellIndex == startCellIndex)
                                updatedCell.CellState = CellState.Dead;

                            if (cellIndex == startCellIndex + 6)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 7)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size + 6)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + Size + 7)
                                updatedCell.CellState = CellState.Alive;

                            if (cellIndex == startCellIndex + 4 * Size)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 4 * Size + 1)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 5 * Size)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 5 * Size + 1)
                                updatedCell.CellState = CellState.Alive;

                            if (cellIndex == startCellIndex + 6 * Size + 10)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 6 * Size + 11)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 7 * Size + 10)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 7 * Size + 11)
                                updatedCell.CellState = CellState.Alive;

                            if (cellIndex == startCellIndex + 10 * Size + 4)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 10 * Size + 5)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 11 * Size + 4)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 11 * Size + 5)
                                updatedCell.CellState = CellState.Alive;

                            if (cellIndex == startCellIndex + 3 * Size + 4)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 3 * Size + 5)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 3 * Size + 6)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 3 * Size + 7)
                                updatedCell.CellState = CellState.Alive;

                            if (cellIndex == startCellIndex + 8 * Size + 4)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 8 * Size + 5)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 8 * Size + 6)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 8 * Size + 7)
                                updatedCell.CellState = CellState.Alive;

                            if (cellIndex == startCellIndex + 4 * Size + 3)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 5 * Size + 3)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 6 * Size + 3)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 7 * Size + 3)
                                updatedCell.CellState = CellState.Alive;

                            if (cellIndex == startCellIndex + 4 * Size + 8)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 5 * Size + 8)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 6 * Size + 8)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 7 * Size + 8)
                                updatedCell.CellState = CellState.Alive;

                            if (cellIndex == startCellIndex + 5 * Size + 4)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 6 * Size + 5)
                                updatedCell.CellState = CellState.Alive;
                            if (cellIndex == startCellIndex + 6 * Size + 6)
                                updatedCell.CellState = CellState.Alive;
                            updatedGrid.Add(updatedCell);
                        }
                        break;
                    default:
                        break;

                }

                if (updatedGrid.Count == _mainCellsList.Count)
                {
                    foreach (Cell gridCell in _mainCellsList)
                    {
                        CellState updatedCellState = updatedGrid.ElementAt(_mainCellsList.IndexOf(gridCell)).CellState;
                        if (updatedCellState != gridCell.CellState)
                        {
                            gridCell.CellState = updatedCellState;
                        }
                    }
                }
                RaisePropertyChanged("MainCellsList");
            }
            catch
            {

            }
        }

        private bool IsCellAlive(int cellIndex)
        {
            return IsCellIndexValid(cellIndex) && _mainCellsList.ElementAt(cellIndex).CellState == CellState.Alive;
        }

        private bool IsCellIndexValid(int cellIndex)
        {
            return cellIndex >= 0 && cellIndex < _mainCellsList.Count;
        }

        private bool IsFieldEmpty()
        {
            foreach (var item in _mainCellsList)
            {
                if (item.CellState == CellState.Alive)
                    return false;
            }
            return true;
        }
    }
}




Packages:

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="MicrosoftExpressionInteractions" version="3.0.40218.0" targetFramework="net472" />
  <package id="System.Windows.Interactivity.WPF" version="2.0.20525" targetFramework="net472" />
</packages>




TOP50 Gry