Skip to content

Commit bd65bb2

Browse files
committed
Added a console UI.
1 parent 17022f2 commit bd65bb2

File tree

6 files changed

+211
-10
lines changed

6 files changed

+211
-10
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>GameOfLife.ConsoleApplication</RootNamespace>
12+
<AssemblyName>GameOfLife.ConsoleApplication</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<ProjectReference Include="..\Core\GameOfLife.Core.csproj">
51+
<Project>{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}</Project>
52+
<Name>GameOfLife.Core</Name>
53+
</ProjectReference>
54+
</ItemGroup>
55+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
56+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
57+
Other similar extension points exist, see Microsoft.Common.targets.
58+
<Target Name="BeforeBuild">
59+
</Target>
60+
<Target Name="AfterBuild">
61+
</Target>
62+
-->
63+
</Project>

ConsoleApplication/Program.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using GameOfLife.Core;
6+
using System.Threading;
7+
8+
namespace GameOfLife.ConsoleApplication
9+
{
10+
class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
var grid = new Grid(16, 8);
15+
RandomlySeedGrid(grid);
16+
17+
while (true)
18+
{
19+
grid.Update();
20+
21+
var cells = grid.GetCells();
22+
DisplayCells(cells);
23+
Thread.Sleep(500);
24+
}
25+
}
26+
27+
private static void RandomlySeedGrid(Grid grid)
28+
{
29+
var numberOfCells = grid.NumberOfRows * grid.NumberOfColumns;
30+
var random = new Random();
31+
32+
for (var i = 0; i < numberOfCells; i++)
33+
{
34+
var randomRowNumber = random.Next(1, grid.NumberOfRows);
35+
var randomColumnNumber = random.Next(1, grid.NumberOfColumns);
36+
37+
grid.BringToLife(randomRowNumber, randomColumnNumber);
38+
}
39+
}
40+
41+
private static void DisplayCells(Boolean[,] cells)
42+
{
43+
var numberOfRows = cells.GetLength(0);
44+
var numberOfColumns = cells.GetLength(1);
45+
46+
for (var i = 0; i < numberOfColumns; i++)
47+
{
48+
for (var j = 0; j < numberOfRows; j++)
49+
{
50+
if (cells[j, i])
51+
Console.Write('X');
52+
else
53+
Console.Write('-');
54+
}
55+
56+
Console.Write(Environment.NewLine);
57+
}
58+
59+
Console.Write(Environment.NewLine);
60+
}
61+
}
62+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("GameOfLife.ConsoleApplication")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("GameOfLife.ConsoleApplication")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("9f51016c-fe08-4855-adcf-8554c9baf564")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Core/Grid.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ namespace GameOfLife.Core
77
{
88
public class Grid
99
{
10+
public Int32 NumberOfRows { get; set; }
11+
public Int32 NumberOfColumns { get; set; }
12+
1013
private Boolean[,] cells;
1114

1215
public Grid(Int32 numberOfRows, Int32 numberOfColumns)
1316
{
17+
NumberOfRows = numberOfRows;
18+
NumberOfColumns = numberOfColumns;
19+
1420
cells = new Boolean[numberOfRows, numberOfColumns];
1521

1622
for (var i = 0; i < numberOfRows; i++)
@@ -25,12 +31,12 @@ public void BringToLife(Int32 rowNumber, Int32 columnNumber)
2531

2632
public void Update()
2733
{
28-
var copiedCells = new Boolean[cells.GetLength(0), cells.GetLength(1)];
34+
var copiedCells = new Boolean[NumberOfRows, NumberOfColumns];
2935
Array.Copy(cells, copiedCells, cells.Length);
3036

31-
for (var i = 0; i < cells.GetLength(0); i++)
37+
for (var i = 0; i < NumberOfRows; i++)
3238
{
33-
for (var j = 0; j < cells.GetLength(1); j++)
39+
for (var j = 0; j < NumberOfColumns; j++)
3440
{
3541
var neighbors = GetNeighbors(i, j);
3642
var numberOfLiveNeighbors = neighbors.Count(n => n);
@@ -48,8 +54,6 @@ public void Update()
4854
private IEnumerable<Boolean> GetNeighbors(Int32 rowIndex, Int32 columnIndex)
4955
{
5056
var neighbors = new List<Boolean>();
51-
var numberOfRows = cells.GetLength(0);
52-
var numberOfColumns = cells.GetLength(1);
5357

5458
//for (var i = -1; i <= 1; i++)
5559
//{
@@ -69,23 +73,23 @@ private IEnumerable<Boolean> GetNeighbors(Int32 rowIndex, Int32 columnIndex)
6973
neighbors.Add(cells[rowIndex - 1, columnIndex]);
7074

7175
// Top Right
72-
if (rowIndex > 0 && columnIndex < numberOfColumns - 1)
76+
if (rowIndex > 0 && columnIndex < NumberOfColumns - 1)
7377
neighbors.Add(cells[rowIndex - 1, columnIndex + 1]);
7478

7579
// Right
76-
if (columnIndex < numberOfColumns - 1)
80+
if (columnIndex < NumberOfColumns - 1)
7781
neighbors.Add(cells[rowIndex, columnIndex + 1]);
7882

7983
// Bottom Right
80-
if (rowIndex < numberOfRows - 1 && columnIndex < numberOfColumns - 1)
84+
if (rowIndex < NumberOfRows - 1 && columnIndex < NumberOfColumns - 1)
8185
neighbors.Add(cells[rowIndex + 1, columnIndex + 1]);
8286

8387
// Bottom
84-
if (rowIndex < numberOfRows - 1)
88+
if (rowIndex < NumberOfRows - 1)
8589
neighbors.Add(cells[rowIndex + 1, columnIndex]);
8690

8791
// Bottom Left
88-
if (rowIndex < numberOfRows - 1 && columnIndex > 0)
92+
if (rowIndex < NumberOfRows - 1 && columnIndex > 0)
8993
neighbors.Add(cells[rowIndex + 1, columnIndex - 1]);
9094

9195
// Left
@@ -114,5 +118,13 @@ public Boolean IsCellAlive(Int32 rowNumber, Int32 columnNumber)
114118
{
115119
return cells[rowNumber - 1, columnNumber - 1];
116120
}
121+
122+
public Boolean[,] GetCells()
123+
{
124+
var copiedCells = new Boolean[NumberOfRows, NumberOfColumns];
125+
Array.Copy(cells, copiedCells, cells.Length);
126+
127+
return copiedCells;
128+
}
117129
}
118130
}

GameOfLife.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,51 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
EndProject
1313
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameOfLife.UnitTests", "UnitTests\GameOfLife.UnitTests.csproj", "{E443C582-9CE2-4478-9285-14A07C65E6B6}"
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameOfLife.ConsoleApplication", "ConsoleApplication\GameOfLife.ConsoleApplication.csproj", "{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}"
16+
EndProject
1517
Global
1618
GlobalSection(TestCaseManagementSettings) = postSolution
1719
CategoryFile = GameOfLife.vsmdi
1820
EndGlobalSection
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
23+
Debug|Mixed Platforms = Debug|Mixed Platforms
24+
Debug|x86 = Debug|x86
2125
Release|Any CPU = Release|Any CPU
26+
Release|Mixed Platforms = Release|Mixed Platforms
27+
Release|x86 = Release|x86
2228
EndGlobalSection
2329
GlobalSection(ProjectConfigurationPlatforms) = postSolution
2430
{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2531
{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
33+
{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
34+
{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}.Debug|x86.ActiveCfg = Debug|Any CPU
2635
{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}.Release|Any CPU.ActiveCfg = Release|Any CPU
2736
{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
38+
{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}.Release|Mixed Platforms.Build.0 = Release|Any CPU
39+
{B6EC237D-0F04-4787-9628-ABF4DF9B0D55}.Release|x86.ActiveCfg = Release|Any CPU
2840
{E443C582-9CE2-4478-9285-14A07C65E6B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2941
{E443C582-9CE2-4478-9285-14A07C65E6B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{E443C582-9CE2-4478-9285-14A07C65E6B6}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
43+
{E443C582-9CE2-4478-9285-14A07C65E6B6}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
44+
{E443C582-9CE2-4478-9285-14A07C65E6B6}.Debug|x86.ActiveCfg = Debug|Any CPU
3045
{E443C582-9CE2-4478-9285-14A07C65E6B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
3146
{E443C582-9CE2-4478-9285-14A07C65E6B6}.Release|Any CPU.Build.0 = Release|Any CPU
47+
{E443C582-9CE2-4478-9285-14A07C65E6B6}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
48+
{E443C582-9CE2-4478-9285-14A07C65E6B6}.Release|Mixed Platforms.Build.0 = Release|Any CPU
49+
{E443C582-9CE2-4478-9285-14A07C65E6B6}.Release|x86.ActiveCfg = Release|Any CPU
50+
{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}.Debug|Any CPU.ActiveCfg = Debug|x86
51+
{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
52+
{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}.Debug|Mixed Platforms.Build.0 = Debug|x86
53+
{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}.Debug|x86.ActiveCfg = Debug|x86
54+
{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}.Debug|x86.Build.0 = Debug|x86
55+
{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}.Release|Any CPU.ActiveCfg = Release|x86
56+
{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}.Release|Mixed Platforms.ActiveCfg = Release|x86
57+
{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}.Release|Mixed Platforms.Build.0 = Release|x86
58+
{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}.Release|x86.ActiveCfg = Release|x86
59+
{0FCA3EED-4698-456B-A09D-5602EEA3E8A7}.Release|x86.Build.0 = Release|x86
3260
EndGlobalSection
3361
GlobalSection(SolutionProperties) = preSolution
3462
HideSolutionNode = FALSE

GameOfLife.suo

7.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)