Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7764883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:01:59+00:00 2026-06-01T15:01:59+00:00

I am writing up a Game of Life program in C#. I am using

  • 0

I am writing up a Game of Life program in C#. I am using a 2D array of structs. It seems when I display the Random method that the algorithm of the neighbours or something is wrong. When it goes through generations random cells are coming “alive” when they are not supposed to. Any help?

public struct cellDetail
{
    public int curGenStatus;
    public int nextGenStatus;
    public int age;

}
public class Class1
{

    static cellDetail[,] Generations(cellDetail[,] cells)
    {

        int neighbours = 0;

        for (int i = 0; i < 40; i++)
            for (int j = 0; j < 60; j++)
                cells[i, j].nextGenStatus = 0;

        for (int row = 0; row < 39; row++)
            for (int col = 0; col < 59; col++)
            {
                neighbours = 0;

                if (row > 0 && col > 0)
                {
                    if (cells[row - 1, col - 1].curGenStatus > 0)
                        neighbours++;
                    if (cells[row - 1, col].curGenStatus > 0)
                        neighbours++;
                    if (cells[row - 1, col + 1].curGenStatus > 0)
                        neighbours++;

                    if (cells[row, col - 1].curGenStatus > 0)
                        neighbours++;
                    if (cells[row, col + 1].curGenStatus > 0)
                        neighbours++;

                    if (cells[row + 1, col - 1].curGenStatus > 0)
                        neighbours++;
                }

                if (cells[row + 1, col].curGenStatus > 0)
                    neighbours++;
                if (cells[row + 1, col + 1].curGenStatus > 0)
                    neighbours++;

                if (neighbours < 2)
                    cells[row, col].nextGenStatus = 0;
                if (neighbours > 3)
                    cells[row, col].nextGenStatus = 0;
                if ((neighbours == 2 || neighbours == 3) && cells[row, col].curGenStatus > 0)
                    cells[row, col].nextGenStatus = 1;
                if (neighbours == 3 && cells[row, col].curGenStatus == 0)
                    cells[row, col].nextGenStatus = 1;
            }

        for (int i = 0; i < 40; i++)
            for (int j = 0; j < 60; j++)
                cells[i, j].curGenStatus = cells[i, j].nextGenStatus;

        return cells;
    }

    static void PrintCells(cellDetail[,] cells)
    {
        for (int row = 0; row < 40; row++)
        {
            for (int col = 0; col < 60; col++)
            {
                if (cells[row, col].curGenStatus != 0)
                {
                    cells[row, col].curGenStatus = (char)30;
                    Console.Write((char)cells[row, col].curGenStatus);
                }
                else
                    Console.Write(" ");
            }
        }
    }

    public static void Random(int numOfGenerations)
    {
        cellDetail[,] cells = new cellDetail[40,60];
        Random rand = new Random();

        for (int row = 0; row < 40; row++)
            for (int col = 0; col < 60; col++)
                cells[row, col].curGenStatus = rand.Next(0, 2);

        for (int i = 0; i < numOfGenerations; i++)
        {
            Console.ForegroundColor = (ConsoleColor.Green);
            Generations(cells);
            Console.SetCursorPosition(0, 3);
            PrintCells(cells);
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:02:00+00:00Added an answer on June 1, 2026 at 3:02 pm

    The random object should be created only once and used by all objects of the class.By declaring it as a static member of the class this can be achieved.A better option would be to create a singleton helper class for random object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program in WPF application that simulates the game of life. How
I'm writing a simple Game of Life program in Java and am having a
I'm writing a game of life program in mathematica however there is a caveat
I'm writing Conway's game of life using Tkinter, and I want to have a
I'm writing a game which uses a border layout with a JPanel using BorderLayout.CENTER.
I'm writing a game for iPhone, and I want an online leaderboard using mySQL,
I'm writing an implementation of Conway's Game of Life in C#. This is the
I'm writing a game in cocos2dx, and i'm trying to refactor a method thats
I'm writing a game using android ndk. I'm using picking to select objects in
Alright so I writing Conways Game of Life in C++, and so far I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.