Skip to content

Commit 3cb7e84

Browse files
committed
Added a new Console .Net Framework project ConsoleLife
1 parent b6c24c8 commit 3cb7e84

File tree

6 files changed

+268
-0
lines changed

6 files changed

+268
-0
lines changed

ConsoleLife/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
5+
</startup>
6+
</configuration>

ConsoleLife/ConsoleLife.csproj

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{7EEC3CA0-75AD-4D01-BFFE-ED027C5D38E8}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>ConsoleLife</RootNamespace>
10+
<AssemblyName>ConsoleLife</AssemblyName>
11+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="GameEngine.cs" />
47+
<Compile Include="Program.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<None Include="App.config" />
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
</Project>

ConsoleLife/GameEngine.cs

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ConsoleLife
8+
{
9+
public class GameEngine
10+
{
11+
public uint CurrentGeneration { get; private set; }
12+
private bool[,] field;
13+
private readonly int rows;
14+
private readonly int cols;
15+
16+
public GameEngine(int rows, int cols, int density)
17+
{
18+
this.rows = rows;
19+
this.cols = cols;
20+
field = new bool[cols, rows];
21+
Random random = new Random();
22+
for (int x = 0; x < cols; x++)
23+
{
24+
for (int y = 0; y < rows; y++)
25+
{
26+
field[x, y] = random.Next(density) == 0;
27+
}
28+
}
29+
}
30+
public void NextGeneration()
31+
{
32+
var newField = new bool[cols, rows];
33+
for (int x = 0; x < cols; x++)
34+
{
35+
for (int y = 0; y < rows; y++)
36+
{
37+
var neighboursCount = CountNeighbours(x, y);
38+
var hasLife = field[x, y];
39+
if (!hasLife && neighboursCount == 3)
40+
{
41+
newField[x, y] = true;
42+
}
43+
else if (hasLife && neighboursCount < 2 || neighboursCount > 3)
44+
{
45+
newField[x, y] = false;
46+
}
47+
else
48+
{
49+
newField[x, y] = field[x, y];
50+
}
51+
}
52+
}
53+
field = newField;
54+
CurrentGeneration++;
55+
}
56+
public bool[,] GetCurrentGeneration()
57+
{
58+
var result = new bool[cols, rows];
59+
for (int x = 0; x < cols; x++)
60+
{
61+
for (int y = 0; y < rows; y++)
62+
{
63+
result[x, y] = field[x, y];
64+
}
65+
}
66+
return result;
67+
}
68+
private int CountNeighbours(int x, int y)
69+
{
70+
int count = 0;
71+
for (int i = -1; i < 2; i++)
72+
{
73+
for (int j = -1; j < 2; j++)
74+
{
75+
var col = (x + i + cols) % cols;
76+
var row = (y + j + rows) % rows;
77+
var isSelfChecking = col == x && row == y;
78+
var hasLife = field[col, row];
79+
if (hasLife && !isSelfChecking)
80+
{
81+
count++;
82+
}
83+
}
84+
85+
}
86+
return count;
87+
}
88+
private bool ValidateCellPosition(int x, int y)
89+
{
90+
return x >= 0 && y >= 0 && x < cols && y < rows;
91+
}
92+
private void UpdateCell(int x, int y, bool state)
93+
{
94+
if (ValidateCellPosition(x, y))
95+
{
96+
field[x, y] = state;
97+
}
98+
}
99+
public void AddCell(int x, int y)
100+
{
101+
UpdateCell(x, y, state: true);
102+
}
103+
public void RemoveCell(int x, int y)
104+
{
105+
UpdateCell(x, y, state: false);
106+
}
107+
}
108+
}

ConsoleLife/Program.cs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace ConsoleLife
9+
{
10+
class Program
11+
{
12+
[DllImport("kernel32.dll", ExactSpelling = true)]
13+
private static extern IntPtr GetConsoleWindow();
14+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
15+
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
16+
17+
private const int MAXIMIZE = 3;
18+
19+
static void Main(string[] args)
20+
{
21+
Console.ReadKey();
22+
Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
23+
ShowWindow(GetConsoleWindow(), MAXIMIZE);
24+
Console.CursorVisible = false;
25+
Console.SetCursorPosition(0, 0);
26+
var gameEngine = new GameEngine
27+
(
28+
rows: 168,
29+
cols: 630,
30+
density: 2
31+
);
32+
while (true)
33+
{
34+
Console.Title = gameEngine.CurrentGeneration.ToString();
35+
var field = gameEngine.GetCurrentGeneration();
36+
37+
for (int y = 0; y < field.GetLength(1); y++)
38+
{
39+
var str = new char[field.GetLength(0)];
40+
for (int x = 0; x < field.GetLength(0); x++)
41+
{
42+
if (field[x, y])
43+
{
44+
str[x] = '#';
45+
}
46+
else
47+
{
48+
str[x] = ' ';
49+
}
50+
}
51+
Console.WriteLine(str);
52+
}
53+
Console.SetCursorPosition(0, 0);
54+
gameEngine.NextGeneration();
55+
}
56+
}
57+
}
58+
}
+36
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+
// Общие сведения об этой сборке предоставляются следующим набором
6+
// набора атрибутов. Измените значения этих атрибутов для изменения сведений,
7+
// связанные с этой сборкой.
8+
[assembly: AssemblyTitle("ConsoleLife")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ConsoleLife")]
13+
[assembly: AssemblyCopyright("Copyright © 2022")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Установка значения False для параметра ComVisible делает типы в этой сборке невидимыми
18+
// для компонентов COM. Если необходимо обратиться к типу в этой сборке через
19+
// из модели COM задайте для атрибута ComVisible этого типа значение true.
20+
[assembly: ComVisible(false)]
21+
22+
// Следующий GUID представляет идентификатор typelib, если этот проект доступен из модели COM
23+
[assembly: Guid("7eec3ca0-75ad-4d01-bffe-ed027c5d38e8")]
24+
25+
// Сведения о версии сборки состоят из указанных ниже четырех значений:
26+
//
27+
// Основной номер версии
28+
// Дополнительный номер версии
29+
// Номер сборки
30+
// Номер редакции
31+
//
32+
// Можно задать все значения или принять номера сборки и редакции по умолчанию
33+
// используя "*", как показано ниже:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Lessons.sln

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkLesson45", "Benchm
2727
EndProject
2828
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneNeuron", "OneNeuron\OneNeuron.csproj", "{E0D0E12A-B1F0-4F57-9868-6CB731EC6D00}"
2929
EndProject
30+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleLife", "ConsoleLife\ConsoleLife.csproj", "{7EEC3CA0-75AD-4D01-BFFE-ED027C5D38E8}"
31+
EndProject
3032
Global
3133
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3234
Debug|Any CPU = Debug|Any CPU
@@ -81,6 +83,10 @@ Global
8183
{E0D0E12A-B1F0-4F57-9868-6CB731EC6D00}.Debug|Any CPU.Build.0 = Debug|Any CPU
8284
{E0D0E12A-B1F0-4F57-9868-6CB731EC6D00}.Release|Any CPU.ActiveCfg = Release|Any CPU
8385
{E0D0E12A-B1F0-4F57-9868-6CB731EC6D00}.Release|Any CPU.Build.0 = Release|Any CPU
86+
{7EEC3CA0-75AD-4D01-BFFE-ED027C5D38E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
87+
{7EEC3CA0-75AD-4D01-BFFE-ED027C5D38E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
88+
{7EEC3CA0-75AD-4D01-BFFE-ED027C5D38E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
89+
{7EEC3CA0-75AD-4D01-BFFE-ED027C5D38E8}.Release|Any CPU.Build.0 = Release|Any CPU
8490
EndGlobalSection
8591
GlobalSection(SolutionProperties) = preSolution
8692
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)