0

Hi all, i need help to build up a cellular automata program.
So, there is 4 state of cells, 0 = space, 1 = ".", 2 = "+", 3 = "#"
This state is continuous from 0 - 3, after 3, it will be back to 0.

For example, an array is listed as following:

0123456
#. .+++

This is the rule:

  1. If both of the neighbouring cells have a state equal to the next state then the cell is
    changed to the previous state.
  2. If only one of the neighbouring cells has a state equal to the next state then the cell is
    changed to the next state
  3. If both neighbours have the same state as the cell then the cell is changed to the next
    state
  4. If neither rule 1, 2 or 3 applies then the cell remains unchanged.

So, after applying the rule, the example array will be :

0123456
#.#++##

I have write 2 .cs file : Cell.cs and program.cs, but there are some bugs in my job, so if anybody can help me to look through the code?

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cell1
{
    class Program
    {

        static void Main(string[] args)
        {
            string seed = Console.ReadLine();

            int seed_R = Convert.ToInt32(seed);

            Demo demo = new Demo();

            demo.GenerateCell(seed_R);

            Console.ReadLine();
        }
    }

    public class Demo
    {
        private const int ARRAYSIZE = 64;
        private const int GENERATION = 20;
        private Cell[] Cells;
        private Cell[] temp;
        public void scramble()
        {
            Cells = new Cell[ARRAYSIZE];
            for (int i = 0; i < ARRAYSIZE; i++)
            {
                Cells[i] = new Cell();
            }
        }

        public void GenerateCell(int seed)
        {
            int rand;

            Random r = new Random(seed);
            for (int i = 0; i < ARRAYSIZE; i++)
            {
                rand = r.Next(4);

                if (rand == 0) 
                    Cells[i].DefaultCell = CellState.SPACE;
                else if (rand == 1) 
                    Cells[i].DefaultCell = CellState.DOT;
                else if (rand == 2) 
                    Cells[i].DefaultCell = CellState.PLUS;
                else 
                    Cells[i].DefaultCell = CellState.HASH;
            }
            Console.Write("gen  " + " 1 ");
        }

        public void PrintCell()
        {
            for (int i = 0; i < ARRAYSIZE; i++)                
            {
                if (Cells[i].DefaultCell == CellState.SPACE)
                    Console.Write(" ");
                else if (Cells[i].DefaultCell == CellState.DOT)
                    Console.Write(".");
                else if (Cells[i].DefaultCell == CellState.PLUS)
                    Console.Write("+");
                else
                    Console.Write("#");
            }
            Console.WriteLine();
        }
    }
}

Cell.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cell1
{
    enum CellState
    {
        SPACE,
        DOT,
        PLUS,
        HASH
    }

    class Cell
    {
        private CellState defaultCell;

        public Cell()
        {
            defaultCell = CellState.SPACE;
        }

        public CellState DefaultCell
        {
            get { return defaultCell; }
            set { defaultCell = value; }
        }


       /*
        public void ChangeRule1
        {

        }

        public void ChangeRule2
        {

        }

        public void ChangeRule3
        {

        }

        public void ChangeRule4
        {

        }
        */
    }
}

Edited by ryanzeng: edit

2
Contributors
1
Reply
13
Views
1 Year
Discussion Span
Last Post by JOSheaIV
0

Question, doesn't the logic apply recurssively, so after you change one index, you have to proceed to checking all the following? (Also aren't rule 1 and 3 the same? or am I misreading it?)

I say recurssively, as that's how I would see doing it. Change one index then going left to right, checking the rest, wrapping back around to the beginning and continuing to check till no more changes remain. Also, is this the whole code? I never see your print function even called? And speaking of that print, and relating back to my earlier comment, it literally looks like you index through once, left to right, changing only the current index, and not caring for the surrounding indexes. So unless I am misreading something, it appears we are only looking at a partial piece of code

(By the way, recurssive, might not have been the best word to use, but maybe I should say looping through, until all the possibilities have been fixed).

Those rules to, I don't know if it's early or what, but for some reason I can only partially wrap my head around what they are expecting ... okay I could blame lack of sleep for that to (with an early wake up)

Edited by JOSheaIV

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.