Skip to content

Commit ef6ad4d

Browse files
authored
Merge pull request #15 from MrMame/develop
v0.1.0
2 parents 554b2f7 + ea05ec6 commit ef6ad4d

17 files changed

+977
-6
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
# GameOfLife
1+
# GameOfLife
2+
3+
Conways Game of Life
4+
5+
6+
Wiki Site
7+
https://conwaylife.com/wiki/Main_Page
8+

docs/System.odg

7.63 KB
Binary file not shown.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using GameOfLife.Helpers;
2+
using GameOfLife.Models;
3+
using GameOfLife.Views;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace GameOfLife.Controllers {
12+
class GameController {
13+
14+
iView _GameView;
15+
Grid _GameGrid;
16+
GameTimer _GameTimer;
17+
18+
public GameController(iView GameView, Grid GameGrid,Helpers.GameTimer GameTimer) {
19+
_GameGrid = GameGrid;
20+
_GameView = GameView;
21+
_GameTimer = GameTimer;
22+
_GameView.GridCellClick += onGridCellClick;
23+
_GameTimer.GametimerTick += onGameTimerTick;
24+
DrawGameGridInView();
25+
}
26+
27+
public void StartGeneration() {
28+
_GameTimer.StartGameTimer();
29+
}
30+
public void StopGeneration() {
31+
_GameTimer.StopGameTimer();
32+
}
33+
34+
35+
/// <summary>
36+
/// Draws the actual gamegrid into the View
37+
/// </summary>
38+
private void DrawGameGridInView() {
39+
_GameView.Draw(_GameGrid);
40+
}
41+
/// <summary>
42+
/// Calculates the next Generation of the Cells inside the gamegrid.
43+
/// </summary>
44+
private void CalculateNextGeneration() {
45+
// Create a Copy of teh Array to calculate the next geenration
46+
// BUG : This is not cloning the Pointers target Objects. only the pointer Array is geting cloned. poiters to celss
47+
// are still the same.
48+
//AbstractCell[,] nextGeneration = (AbstractCell[,])_GameGrid._Cells.Clone();
49+
50+
// Calulcate the next Geerations
51+
for(int r = 0;r<_GameGrid._Cells.GetLength(0);r++){
52+
for(int c = 0;c<_GameGrid._Cells.GetLength(1);c++){
53+
_GameGrid._NextGenerationCells[r, c].CellIsLiving = _GameGrid._Cells[r, c].CheckNextLivingState();
54+
}
55+
}
56+
// Set the new Generation to the latest of the grid. That means copy the Next generation Lifestates to
57+
// the actual CellGrid.
58+
for (int r = 0; r < _GameGrid._Cells.GetLength(0); r++) {
59+
for (int c = 0; c < _GameGrid._Cells.GetLength(1); c++) {
60+
_GameGrid._Cells[r, c].CellIsLiving = _GameGrid._NextGenerationCells[r, c].CellIsLiving;
61+
}
62+
}
63+
64+
65+
// Take the New geenration Array for latest
66+
//_GameGrid._Cells = nextGeneration;
67+
}
68+
/// <summary>
69+
/// Toggles the Life State of the GridCell
70+
/// </summary>
71+
/// <param name="cell"></param>
72+
private void ToggleGridCellLifeState(GridCell cell) {
73+
cell.CellIsLiving = !cell.CellIsLiving;
74+
}
75+
76+
#region EventHandlers
77+
/// <summary>
78+
/// Toggles the Lifestate of the Clicked GridCell and refreshs the view to draw latest grid
79+
/// </summary>
80+
/// <param name="sender"></param>
81+
/// <param name="e"></param>
82+
private void onGridCellClick(object sender, GridCellClickEventArgs e) {
83+
ToggleGridCellLifeState(e.GridCell);
84+
DrawGameGridInView();
85+
}
86+
/// <summary>
87+
/// Calculates next Generation of cells and refrshs the view to draw latest grid
88+
/// </summary>
89+
/// <param name="sender"></param>
90+
/// <param name="e"></param>
91+
private void onGameTimerTick(object sender, EventArgs e) {
92+
this.CalculateNextGeneration();
93+
this.DrawGameGridInView();
94+
}
95+
#endregion
96+
97+
98+
}
99+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+

2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using System.Windows.Controls;
9+
using System.Windows.Data;
10+
using System.Windows.Documents;
11+
using System.Windows.Input;
12+
using System.Windows.Media;
13+
using System.Windows.Media.Imaging;
14+
using System.Windows.Navigation;
15+
using System.Windows.Shapes;
16+
17+
18+
19+
20+
21+
namespace GameOfLife.Demos {
22+
class WriteableBitmapDemo {
23+
24+
public static void Draw(Grid grdMain){
25+
const int width = 240;
26+
const int height = 240;
27+
28+
29+
30+
WriteableBitmap wbitmap = new WriteableBitmap(
31+
width, height, 96, 96, PixelFormats.Bgra32, null);
32+
byte[, ,] pixels = new byte[height, width, 4];
33+
34+
// Clear to black.
35+
for (int row = 0; row < height; row++)
36+
{
37+
for (int col = 0; col < width; col++)
38+
{
39+
for (int i = 0; i < 3; i++)
40+
pixels[row, col, i] = 0;
41+
pixels[row, col, 3] = 255;
42+
}
43+
}
44+
45+
// Blue.
46+
for (int row = 0; row < 80; row++)
47+
{
48+
for (int col = 0; col <= row; col++)
49+
{
50+
pixels[row, col, 0] = 255;
51+
}
52+
}
53+
54+
// Green.
55+
for (int row = 80; row < 160; row++)
56+
{
57+
for (int col = 0; col < 80; col++)
58+
{
59+
pixels[row, col, 1] = 255;
60+
}
61+
}
62+
63+
// Red.
64+
for (int row = 160; row < 240; row++)
65+
{
66+
for (int col = 0; col < 80; col++)
67+
{
68+
pixels[row, col, 2] = 255;
69+
}
70+
}
71+
72+
// Copy the data into a one-dimensional array.
73+
byte[] pixels1d = new byte[height * width * 4];
74+
int index = 0;
75+
for (int row = 0; row < height; row++)
76+
{
77+
for (int col = 0; col < width; col++)
78+
{
79+
for (int i = 0; i < 4; i++)
80+
pixels1d[index++]= pixels[row, col, i];
81+
}
82+
}
83+
84+
// Update writeable bitmap with the colorArray to the image.
85+
Int32Rect rect = new Int32Rect(0, 0, width, height);
86+
int stride = 4 * width;
87+
wbitmap.WritePixels(rect, pixels1d, stride, 0);
88+
89+
// Create an Image to display the bitmap.
90+
Image image = new Image();
91+
image.Stretch = Stretch.None;
92+
image.Margin = new Thickness(0);
93+
94+
grdMain.Children.Add(image);
95+
96+
//Set the Image source.
97+
image.Source = wbitmap;
98+
99+
}
100+
101+
}
102+
}

src/GameOfLife/GameOfLife.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555
<Generator>MSBuild:Compile</Generator>
5656
<SubType>Designer</SubType>
5757
</ApplicationDefinition>
58+
<Compile Include="Controllers\GameController.cs" />
59+
<Compile Include="Helpers\GameTimer.cs" />
60+
<Compile Include="Demos\WriteableBitmapDemo.cs" />
61+
<Compile Include="Models\GridFactory.cs" />
62+
<Compile Include="Views\GridCellClickEventArgs.cs" />
63+
<Compile Include="Views\iView.cs" />
64+
<Compile Include="Views\NormalGridView.cs" />
65+
<Compile Include="Views\WritableBitmapView.cs" />
5866
<Page Include="MainWindow.xaml">
5967
<Generator>MSBuild:Compile</Generator>
6068
<SubType>Designer</SubType>
@@ -69,6 +77,10 @@
6977
</Compile>
7078
</ItemGroup>
7179
<ItemGroup>
80+
<Compile Include="Models\Grid.cs" />
81+
<Compile Include="Models\GridCell.cs" />
82+
<Compile Include="Models\AbstractCell.cs" />
83+
<Compile Include="Models\OutOfBorderCell.cs" />
7284
<Compile Include="Properties\AssemblyInfo.cs">
7385
<SubType>Code</SubType>
7486
</Compile>
@@ -94,5 +106,6 @@
94106
<ItemGroup>
95107
<None Include="App.config" />
96108
</ItemGroup>
109+
<ItemGroup />
97110
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
98111
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace GameOfLife.Helpers {
8+
class GameTimer {
9+
10+
11+
public event EventHandler GametimerTick;
12+
13+
14+
System.Windows.Threading.DispatcherTimer _Timer;
15+
16+
public GameTimer(int MSIntervall) {
17+
_Timer = new System.Windows.Threading.DispatcherTimer();
18+
_Timer.Tick += new EventHandler(TimerTickEventHandler);
19+
_Timer.Interval = new TimeSpan(0, 0, 0, 0, MSIntervall);
20+
}
21+
22+
public void StartGameTimer() {
23+
_Timer.Start();
24+
}
25+
public void StopGameTimer() {
26+
_Timer.Stop();
27+
}
28+
29+
private void doGameTimerTick(object sender, EventArgs e){
30+
if (GametimerTick != null) GametimerTick.Invoke(this, e);
31+
}
32+
private void TimerTickEventHandler(object sender, EventArgs e) {
33+
doGameTimerTick(sender,e);
34+
}
35+
36+
37+
}
38+
}

src/GameOfLife/MainWindow.xaml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,27 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:GameOfLife"
77
mc:Ignorable="d"
8-
Title="MainWindow" Height="450" Width="800">
9-
<Grid>
10-
8+
Title="MainWindow" Height="622" Width="619" Background="Red" MinWidth="416" MinHeight="526">
9+
<Grid x:Name="MainWindowGrid" Margin="0,0,0,0">
10+
<Canvas x:Name="DrawingCanvas" Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}" Margin="10,142,10,10"/>
11+
<ToggleButton Name="ToggleRun" Height="33"
12+
HorizontalAlignment="Left"
13+
Margin="311,32,0,0"
14+
VerticalAlignment="Top"
15+
Width="81"
16+
IsEnabled="{Binding Path=IsGameControllerInitialized}"
17+
Checked="ToggleRun_Checked"
18+
Unchecked="ToggleRun_Unchecked">Run</ToggleButton>
19+
<Label Content="Rows" HorizontalAlignment="Left" Height="24" Margin="43,6,0,0" VerticalAlignment="Top" Width="41"/>
20+
<TextBox Name ="txtRows" HorizontalAlignment="Left" Height="23" Margin="89,10,0,0" TextWrapping="Wrap" Text="{Binding Path=GridRows}" VerticalAlignment="Top" Width="104" PreviewTextInput="numText_PreviewTextInput" TextChanged="txtRows_TextChanged" />
21+
<Label Content="Columns" HorizontalAlignment="Left" Height="24" Margin="27,35,0,0" VerticalAlignment="Top" Width="57"/>
22+
<TextBox Name ="txtColumns" HorizontalAlignment="Left" Height="23" Margin="89,38,0,0" TextWrapping="Wrap" Text="{Binding Path=GridColumns}" VerticalAlignment="Top" Width="104" PreviewTextInput="numText_PreviewTextInput" TextChanged="txtColumns_TextChanged"/>
23+
<Label Content="Speed [ms]" HorizontalAlignment="Left" Height="24" Margin="10,64,0,0" VerticalAlignment="Top" Width="79"/>
24+
<TextBox Name ="txtSpeed" HorizontalAlignment="Left" Height="23" Margin="89,66,0,0" TextWrapping="Wrap" Text="{Binding Path=Speed}" VerticalAlignment="Top" Width="104" PreviewTextInput="numText_PreviewTextInput" TextChanged="txtSpeed_TextChanged"/>
25+
<Button x:Name="btnCreateNewGrid" Content="New Grid" HorizontalAlignment="Left" Height="23" Margin="198,10,0,0" VerticalAlignment="Top" Width="89" Click="btnCreateNewGrid_Click"/>
26+
27+
28+
29+
1130
</Grid>
1231
</Window>

0 commit comments

Comments
 (0)