Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static bool[,] NewWorld(bool[,] currGen, int N)
- {
- Random TrueOrFalse = new Random();
- int RandomAns;
- for (int C = 1; C <= N; C++)
- {
- for(int R = 1; R <= N; R++)
- {
- RandomAns = TrueOrFalse.Next(2);
- if (RandomAns == 0)
- {
- currGen[C, R] = false;
- }else
- {
- currGen[C, R] = true;
- }
- }
- }
- return currGen;
- }
- static int NCount(bool[,] currGen, int C, int R)
- {
- int counter = 0;
- counter += Convert.ToInt32(currGen[C - 1, R]);
- counter += Convert.ToInt32(currGen[C + 1, R]);
- counter += Convert.ToInt32(currGen[C, R - 1]);
- counter += Convert.ToInt32(currGen[C, R + 1]);
- counter += Convert.ToInt32(currGen[C - 1, R - 1]);
- counter += Convert.ToInt32(currGen[C + 1, R + 1]);
- counter += Convert.ToInt32(currGen[C - 1, R + 1]);
- counter += Convert.ToInt32(currGen[C + 1, R - 1]);
- return counter;
- }
- static bool[,] NextGenMaker(bool[,] currGen, bool[,] nextGen, int N)
- {
- for(int C = 1; C <= N; C++)
- {
- for (int R = 1; R <= N; R++)
- {
- if (NCount(currGen, C, R) > 5 || NCount(currGen, C, R) < 3)
- {
- nextGen[C, R] = false;
- }else
- {
- nextGen[C, R] = true;
- }
- }
- }
- return nextGen;
- }
- static void PrintWorld(bool[,] currGen, int N)
- {
- for(int C = 1; C <= N; C++)
- {
- for(int R = 1; R <= N; R++)
- {
- Console.Write(Convert.ToInt32(currGen[C, R]) + " ");
- }
- Console.WriteLine();
- }
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Please Enter The Value Of N");
- int N = int.Parse(Console.ReadLine());
- Console.WriteLine("Please Enter The Amount Of Generations To Simulate");
- int FutureGens = int.Parse(Console.ReadLine());
- bool[,] currGen = new bool[N + 2, N + 2];
- bool[,] nextGen = new bool[N + 2, N + 2];
- currGen = NewWorld(currGen, N);
- for (int Gens = 1; Gens <= FutureGens; Gens++)
- {
- Console.WriteLine("Simulation Of Generation Number {0}:", (Gens));
- PrintWorld(currGen, N);
- if (Gens != FutureGens)
- {
- nextGen = NextGenMaker(currGen, nextGen, N);
- for (int C = 1; C <= N; C++)
- {
- for(int R = 1; R <= N; R++)
- {
- currGen[C, R] = nextGen[C, R];
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement