Projektdateien hinzufügen.
This commit is contained in:
137
GameOfLife/Game.cs
Normal file
137
GameOfLife/Game.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameOfLife
|
||||
{
|
||||
public class Game
|
||||
{
|
||||
public Game(int x, int y) {
|
||||
ActiveMap = new(x, y);
|
||||
SwapMap = new(x, y);
|
||||
}
|
||||
public int Iteration => _Iteration;
|
||||
private GameMap ActiveMap, SwapMap;
|
||||
private int _Iteration;
|
||||
|
||||
|
||||
public void Update()
|
||||
{
|
||||
//SwapMap.Clear();
|
||||
|
||||
foreach ( var cell in ActiveMap.GetAllCells())
|
||||
{
|
||||
cell.Update(ref ActiveMap, ref SwapMap);
|
||||
}
|
||||
_Iteration++;
|
||||
|
||||
//Swap Maps
|
||||
ActiveMap.CloneFrom(SwapMap);
|
||||
}
|
||||
|
||||
public GameMap getActiveMap()
|
||||
{
|
||||
return ActiveMap;
|
||||
}
|
||||
|
||||
public string ToString()
|
||||
{
|
||||
return $"""
|
||||
Current Iteration: {Iteration}
|
||||
""";
|
||||
}
|
||||
|
||||
public string RenderToString()
|
||||
{
|
||||
int MapRowSize = ActiveMap.SizeY;
|
||||
int row = 0;
|
||||
int rowCounter = 0;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
stringBuilder.Append("\t0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5\n");
|
||||
stringBuilder.AppendLine();
|
||||
stringBuilder.Append("0\t");
|
||||
|
||||
foreach ( var cell in ActiveMap.GetAllCells())
|
||||
{
|
||||
stringBuilder.Append(cell.ToString() + " ");
|
||||
|
||||
rowCounter++;
|
||||
if (rowCounter == MapRowSize)
|
||||
{
|
||||
rowCounter = 0;
|
||||
row++;
|
||||
stringBuilder.Append($"\n{row}\t");
|
||||
}
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
public string RenderToDebugString()
|
||||
{
|
||||
int row = 0;
|
||||
int MapRowSize = (int)Math.Sqrt(ActiveMap.GetAllCells().Length);
|
||||
int rowCounter = 0;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append("\t0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5\n");
|
||||
stringBuilder.AppendLine();
|
||||
stringBuilder.Append("0\t");
|
||||
foreach (var cell in ActiveMap.GetAllCells())
|
||||
{
|
||||
stringBuilder.Append(cell.GetNeighborVal(ref ActiveMap) + " ");
|
||||
|
||||
|
||||
rowCounter++;
|
||||
if (rowCounter == MapRowSize)
|
||||
{
|
||||
rowCounter = 0;
|
||||
row++;
|
||||
stringBuilder.Append($"\n{row}\t");
|
||||
}
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
public string RenderToSwapDebugString()
|
||||
{
|
||||
int MapRowSize = (int)Math.Sqrt(SwapMap.GetAllCells().Length);
|
||||
int rowCounter = 0;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
foreach (var cell in SwapMap.GetAllCells())
|
||||
{
|
||||
stringBuilder.Append(cell.GetNeighborVal(ref SwapMap));
|
||||
|
||||
rowCounter++;
|
||||
if (rowCounter == MapRowSize)
|
||||
{
|
||||
rowCounter = 0;
|
||||
stringBuilder.Append("\n");
|
||||
}
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
|
||||
public void SetRandomStart(int XSize, int YSize, int Seed = 0,int mod = 2)
|
||||
{
|
||||
//Not using Size args right now
|
||||
for (int i = 0; i < ActiveMap.SizeX - 1; i++)
|
||||
{
|
||||
for (int j = 0; j < ActiveMap.SizeY - 1; j++)
|
||||
{
|
||||
ActiveMap.GetCell(i,j).isAlive = (Random.Shared.Next()%mod == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void GeneraterSwitcher()
|
||||
{
|
||||
ActiveMap.GetCell(ActiveMap.SizeX/2, ActiveMap.SizeY/2).isAlive = true;
|
||||
ActiveMap.GetCell(ActiveMap.SizeX / 2, (ActiveMap.SizeY / 2)+1).isAlive = true;
|
||||
ActiveMap.GetCell(ActiveMap.SizeX / 2, (ActiveMap.SizeY / 2)-1).isAlive = true;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
191
GameOfLife/GameMap.cs
Normal file
191
GameOfLife/GameMap.cs
Normal file
@@ -0,0 +1,191 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameOfLife
|
||||
{
|
||||
public class GameMap
|
||||
{
|
||||
private int _SizeX, _SizeY;
|
||||
|
||||
CellState[,] Cells = new CellState[1,1];
|
||||
|
||||
public int SizeX { get => _SizeX; set => _SizeX = value; }
|
||||
public int SizeY { get => _SizeY; set => _SizeY = value; }
|
||||
|
||||
|
||||
public CellState[,] GetAllCells() => Cells;
|
||||
public GameMap(int xSize, int ySize)
|
||||
{
|
||||
_SizeX = xSize;
|
||||
_SizeY = ySize;
|
||||
InitializeCells();
|
||||
}
|
||||
|
||||
public ref CellState GetCell(int x, int y)
|
||||
{
|
||||
|
||||
|
||||
//if (x > SizeX) { x = SizeX; }
|
||||
//if (x < 0) { x = 0; }
|
||||
//if (y > SizeY) { y = SizeY; }
|
||||
//if (y < 0) { y = 0; }
|
||||
|
||||
//Trying a wrap around
|
||||
if(x >= SizeX) { x = 0; }
|
||||
if(y >= SizeY) { y = 0; }
|
||||
if(x < 0) { x = SizeX-1; }
|
||||
if(y < 0) { y = SizeY-1; }
|
||||
|
||||
|
||||
return ref Cells[x, y];
|
||||
}
|
||||
|
||||
private void InitializeCells()
|
||||
{
|
||||
Cells = new CellState[SizeX,SizeY];
|
||||
for (int i = 0; i < Cells.GetLength(0) -1; i++)
|
||||
{
|
||||
for (int j = 0; j < Cells.GetLength(1) -1; j++)
|
||||
{
|
||||
Cells[i, j].isAlive = false;
|
||||
Cells[i, j].XCoord = i;
|
||||
Cells[i, j].YCoord = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CloneFrom(GameMap source)
|
||||
{
|
||||
for(int i = 0;i < Cells.GetLength(0) - 1;i++)
|
||||
{
|
||||
for(int j = 0; j < Cells.GetLength(1) -1; j++)
|
||||
{
|
||||
this.Cells[i,j] = source.Cells[i,j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int GetAliveCells()
|
||||
{
|
||||
int alive = 0;
|
||||
foreach (var cell in Cells)
|
||||
{ if (cell.isAlive) alive++; }
|
||||
return alive;
|
||||
}
|
||||
|
||||
internal void Clear()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public struct CellState
|
||||
{
|
||||
public bool isAlive;
|
||||
public int XCoord, YCoord;
|
||||
|
||||
|
||||
|
||||
//Debug Function
|
||||
public int GetNeighborVal(ref GameMap map)
|
||||
{
|
||||
return GetAliveNeighbors(ref map);
|
||||
}
|
||||
|
||||
|
||||
public void Update(ref GameMap CurrentMap, ref GameMap SwapMap)
|
||||
{
|
||||
int AliveNeighborCounter = GetAliveNeighbors(ref CurrentMap);
|
||||
|
||||
//Generate new Cell
|
||||
if (AliveNeighborCounter == 3)
|
||||
{
|
||||
SwapMap.GetCell(XCoord, YCoord).isAlive = true;
|
||||
return;
|
||||
}
|
||||
//State unchanged
|
||||
if (AliveNeighborCounter == 2)
|
||||
{
|
||||
SwapMap.GetCell(XCoord, YCoord).isAlive = this.isAlive;
|
||||
return;
|
||||
}
|
||||
//Cell Dies
|
||||
else
|
||||
{
|
||||
SwapMap.GetCell(XCoord, YCoord).isAlive = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private int GetAliveNeighbors(ref GameMap CurrentMap)
|
||||
{
|
||||
int AliveNeighborCounter = 0;
|
||||
|
||||
// 0 0 0
|
||||
// 0 C x
|
||||
// 0 0 0
|
||||
if (CurrentMap.GetCell(this.XCoord + 1, this.YCoord).isAlive)
|
||||
{ AliveNeighborCounter++; }
|
||||
// 0 0 0
|
||||
// 0 C 0
|
||||
// 0 0 x
|
||||
if (CurrentMap.GetCell(this.XCoord + 1, this.YCoord - 1).isAlive)
|
||||
{ AliveNeighborCounter++; }
|
||||
// 0 0 0
|
||||
// 0 C 0
|
||||
// 0 x 0
|
||||
if (CurrentMap.GetCell(this.XCoord, this.YCoord - 1).isAlive)
|
||||
{ AliveNeighborCounter++; }
|
||||
// 0 0 0
|
||||
// 0 C 0
|
||||
// x 0 0
|
||||
if (CurrentMap.GetCell(this.XCoord - 1, this.YCoord - 1).isAlive)
|
||||
{ AliveNeighborCounter++; }
|
||||
|
||||
if (AliveNeighborCounter > 3) return AliveNeighborCounter;
|
||||
// 0 0 0
|
||||
// x C 0
|
||||
// 0 0 0
|
||||
if (CurrentMap.GetCell(this.XCoord - 1, this.YCoord).isAlive)
|
||||
{ AliveNeighborCounter++; }
|
||||
|
||||
if (AliveNeighborCounter > 3) return AliveNeighborCounter;
|
||||
// x 0 0
|
||||
// 0 C 0
|
||||
// 0 0 0
|
||||
if (CurrentMap.GetCell(this.XCoord - 1, this.YCoord + 1).isAlive)
|
||||
{ AliveNeighborCounter++; }
|
||||
|
||||
if (AliveNeighborCounter > 3) return AliveNeighborCounter;
|
||||
// 0 x 0
|
||||
// 0 C 0
|
||||
// 0 0 0
|
||||
if (CurrentMap.GetCell(this.XCoord, this.YCoord + 1).isAlive)
|
||||
{ AliveNeighborCounter++; }
|
||||
|
||||
if (AliveNeighborCounter > 3) return AliveNeighborCounter;
|
||||
// 0 0 x
|
||||
// 0 C 0
|
||||
// 0 0 0
|
||||
if (CurrentMap.GetCell(this.XCoord + 1, this.YCoord + 1).isAlive)
|
||||
{ AliveNeighborCounter++; }
|
||||
|
||||
return AliveNeighborCounter;
|
||||
}
|
||||
|
||||
public string ToString()
|
||||
{
|
||||
return isAlive ? "1" : "0";
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal void SetAlive()
|
||||
{
|
||||
this.isAlive = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
GameOfLife/GameOfLife.csproj
Normal file
10
GameOfLife/GameOfLife.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
77
GameOfLife/Program.cs
Normal file
77
GameOfLife/Program.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using GameOfLife;
|
||||
using System.ComponentModel.Design;
|
||||
using System.Diagnostics;
|
||||
public class Program
|
||||
{
|
||||
public async static Task Main()
|
||||
{
|
||||
Console.WriteLine("Hello");
|
||||
|
||||
|
||||
Game game = new(80, 80);
|
||||
|
||||
game.SetRandomStart(80, 80);
|
||||
//game.GeneraterSwitcher();
|
||||
Console.Write(game.RenderToDebugString());
|
||||
Console.WriteLine();
|
||||
Console.Write(game.RenderToSwapDebugString());
|
||||
//Console.ReadKey();
|
||||
int fpsct = 0;
|
||||
int CurrentFPS = 0;
|
||||
Stopwatch Start = new();
|
||||
Start.Start();
|
||||
PeriodicTimer timer = new(TimeSpan.FromMilliseconds(10));
|
||||
Console.ForegroundColor = ConsoleColor.DarkGreen;
|
||||
while (await timer.WaitForNextTickAsync())
|
||||
{
|
||||
if (Start.ElapsedMilliseconds > 1000)
|
||||
{
|
||||
CurrentFPS = fpsct;
|
||||
fpsct = 0;
|
||||
Start.Restart();
|
||||
}
|
||||
fpsct++;
|
||||
game.Update();
|
||||
Console.Clear();
|
||||
Console.WriteLine($"FPS: {CurrentFPS}\t Iteration: {game.Iteration}");
|
||||
Console.Write(game.RenderToString());
|
||||
if (Console.KeyAvailable)
|
||||
{
|
||||
if (Console.ReadKey(true).Key == ConsoleKey.R)
|
||||
{
|
||||
game = new(80, 80);
|
||||
game.SetRandomStart(80, 80);
|
||||
|
||||
}
|
||||
}
|
||||
//Console.Write(game.RenderToDebugString());
|
||||
//Console.WriteLine();
|
||||
//Console.Write(game.RenderToSwapDebugString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void ColoredWrite(string input)
|
||||
{
|
||||
foreach (var ch in input)
|
||||
{
|
||||
if (ch == '1')
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Black;
|
||||
Console.Write(ch);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Write(ch);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user