В настоящее время я пытаюсь воссоздать «Игру жизни» Конвея в консольном приложении .NET.
Я жестко запрограммировал этот шаблон
который по сути не перемещается и просто остается таким на протяжении поколений; теперь, хотя я пришел к проблеме, что после одного поколения все умирает.
using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
internal class Program
{
private static Cell[,] cells;
private const int MAX = 6;
public static void Main()
{
var rand = new Random();
cells = new Cell[MAX, MAX];
for (int row = 0; row < cells.GetLength(0); row++)
{
for (int collum = 0; collum < cells.GetLength(1); collum++)
{
// cells[row,collum] = new Cell((rand.Next(0,3) == 1) ? '#' : '0');
cells[row, collum] = new Cell('0');
}
}
cells[1, 0] = new Cell('#');
cells[0, 1] = new Cell('#');
cells[2, 1] = new Cell('#');
cells[1, 2] = new Cell('#');
updateConsole();
Thread.Sleep(1000);
while (true)
{
Thread.Sleep(1000);
nextGeneration();
}
}
static void updateConsole()
{
Console.Clear();
for (int y = 0; y < cells.GetLength(1); y++)
{
for (int x = 0; x < cells.GetLength(0); x++)
{
if (cells[x, y].state == '#') Console.ForegroundColor = ConsoleColor.DarkYellow;
else Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.Write(cells[x, y].state + " ");
}
Console.Write("\n");
}
}
static void nextGeneration()
{
for (int _x = 0; _x < MAX; _x++) for (int _y = 0; _y < MAX; _y++) cells[_x, _y].lastState = cells[_x, _y].state;
for (int y = 0; y < cells.GetLength(1); y++)
{
for (int x = 0; x < cells.GetLength(0); x++)
{
//cells[x, y].lastState = cells[x, y].state;
int alive = 0;
int dx = 0, dy = 0;
for (int i = 0; i < 6; i++)
{
dx = (int)Math.Round(Math.Cos(i * 45)) + x;
dy = (int)Math.Round(Math.Sin(i * 45)) + y;
if ((dx >= 0) && (dx < MAX) && (dy >= 0) && (dy < MAX)) { if (cells[dx, dy].lastState == '#') alive++; }
}
if (cells[x, y].state == '0')
{
if (alive == 3) cells[x, y].state = '#';
}
if (cells[x, y].lastState == '#')
{
if (alive == 2 || alive == 3) { cells[x, y].state = '#'; }
else { cells[x, y].state = '0'; }
}
//Console.WriteLine("x:" +x +" y:" +y + " = alive:" + alive);
}
updateConsole();
}
}
class Cell
{
public Cell(char _state)
{
this.state = _state;
this.lastState = _state;
}
public char state { get; set; }
public char lastState { get; set; }
}
}
// 0 = Dead
// # = alive
I tried to make sure the directions weren't unnecessarily counted twice and checked via debuging if it counted the living neighbours right; So far, no problem. After that I looked if I implemented the game rules right; seemed like it.
В настоящее время я пытаюсь воссоздать «Игру жизни» Конвея в консольном приложении .NET. Я жестко запрограммировал этот шаблон [img]https://i.stack.imgur.com/OpyYd.png[/img] который по сути не перемещается и просто остается таким на протяжении поколений; теперь, хотя я пришел к проблеме, что после одного поколения все умирает. [code]using System.Linq.Expressions; using System.Security.Cryptography.X509Certificates;
internal class Program { private static Cell[,] cells; private const int MAX = 6; public static void Main() { var rand = new Random(); cells = new Cell[MAX, MAX]; for (int row = 0; row < cells.GetLength(0); row++) { for (int collum = 0; collum < cells.GetLength(1); collum++) { // cells[row,collum] = new Cell((rand.Next(0,3) == 1) ? '#' : '0'); cells[row, collum] = new Cell('0'); }
} cells[1, 0] = new Cell('#'); cells[0, 1] = new Cell('#'); cells[2, 1] = new Cell('#'); cells[1, 2] = new Cell('#');
updateConsole(); Thread.Sleep(1000); while (true) { Thread.Sleep(1000);
nextGeneration(); }
} static void updateConsole() { Console.Clear(); for (int y = 0; y < cells.GetLength(1); y++) { for (int x = 0; x < cells.GetLength(0); x++) { if (cells[x, y].state == '#') Console.ForegroundColor = ConsoleColor.DarkYellow; else Console.ForegroundColor = ConsoleColor.DarkBlue; Console.Write(cells[x, y].state + " "); } Console.Write("\n"); } } static void nextGeneration() { for (int _x = 0; _x < MAX; _x++) for (int _y = 0; _y < MAX; _y++) cells[_x, _y].lastState = cells[_x, _y].state; for (int y = 0; y < cells.GetLength(1); y++) { for (int x = 0; x < cells.GetLength(0); x++) { //cells[x, y].lastState = cells[x, y].state; int alive = 0; int dx = 0, dy = 0; for (int i = 0; i < 6; i++) { dx = (int)Math.Round(Math.Cos(i * 45)) + x; dy = (int)Math.Round(Math.Sin(i * 45)) + y;
public Cell(char _state) { this.state = _state; this.lastState = _state; } public char state { get; set; } public char lastState { get; set; } } }
// 0 = Dead // # = alive
[/code] I tried to make sure the directions weren't unnecessarily counted twice and checked via debuging if it counted the living neighbours right; So far, no problem. After that I looked if I implemented the game rules right; seemed like it.
I'm new to this forum.
I'm currently try to recreate Conway's Game of Life in a .Net console application.
I hardcoded this pattern enter image description here which does basically not move and just stays that way through generations; now tho I came...
Я пытался бросить себе вызов и написать программу на C#, которая будет моделировать игру жизни Конвея в консоли, используя словари, где каждому ключу присваивается значение перечисления Life, внутри которого есть живые и мертвые. Я хочу, чтобы доска...
NullReferenceException в разработке мода Unity с Bepinex для стримерного симулятора Life 2
post content:
Я разработал мод для симулятора жизни Streater Life 2, используя Bepinex и столкнулся с NullReferenceException Ошибка при создании кнопок...
Я хочу получить или рассчитать это значение «оставшаяся жизнь», которое можно найти в Windows 11 в условиях хранения, или в Crystaldiskinfo, как «состояние здоровья», здесь у меня есть 99% в Windows, это также 99%
Я хочу получить или рассчитать это значение «оставшаяся жизнь», которое можно найти в Windows 11 в условиях хранения, или в Crystaldiskinfo, как «состояние здоровья», здесь у меня есть 99% в Windows, это также 99%