0

I have a program that use 2 arrays, 1 is the cells array and the other is temporary array

this program generate 20 rows and 64 columns(cells)
each cell compare the itself with the cell on it left and right.

The problem is, the program only work from row 1 to row 2.
row 3 until 20 did not work.
the cell changes it state correctly from row 1 to 2 only.
i think it is because of the temp array.
can someone check it?

the problem is in method operateCell()

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

/*******************************************************************\
       Program that generates a simple 1 dimendional automata.
          The automata is described by a line of cells that
	   can have 4 possible states; space,".", "+", "#".
       Each cell examines the cell on either side of it.
       The following rules are then applied to each cell:
1. If either one of the neighbouring cells has a state equal to 
    the next state then the cell is changed to the next stage.
2. If both neighours have the same state as the cell then the 
    cell is changed to the next state.
3. If neither rurle 1 or 2 applies then the cell remains unchanged.
\*******************************************************************/

namespace cell_autom
{
    class Automata
    {
        private const int CELLSIZE = 64;
        private const int GENERATION = 20;
        private Cell[] Cells;
        private Cell[] tempCells;

        public Automata()
        {
            Cells = new Cell[CELLSIZE];
            tempCells = new Cell[CELLSIZE];
            for (int i = 0; i < CELLSIZE; i++)
            {
                Cells[i] = new Cell();
                tempCells[i] = new Cell();
            }
        }

        /// <summary>
        /// Generates random number and assign the symbol to each number
        /// </summary>
        public void GenerateCell(int seed)
        {
            int rand;

            // set up random object and initialise with seed
            Random r = new Random(seed);
            for (int i = 0; i < CELLSIZE; i++)
            {
                rand = r.Next(4);

                // applying symbol to generated number
                if (rand == 0)
                {
                    Cells[i].DefaultCell = CellState.SPACE;
                    tempCells[i].DefaultCell = CellState.SPACE;
                }
                else if (rand == 1)
                {
                    Cells[i].DefaultCell = CellState.DOT;
                    tempCells[i].DefaultCell = CellState.DOT;
                }
                else if (rand == 2)
                {
                    Cells[i].DefaultCell = CellState.PLUS;
                    tempCells[i].DefaultCell = CellState.PLUS;
                }
                else
                {
                    Cells[i].DefaultCell = CellState.HASH;
                    tempCells[i].DefaultCell = CellState.HASH;
                }
            }
            Console.Write("Gen   1  ");
        }

        /// <summary>
        /// Print out the cells using symbol
        /// </summary>
        public void PrintCell()
        {
            // print the cells in the row
            for (int i = 0; i < CELLSIZE; 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();
        }

        /// <summary>
        /// Create a copy array
        /// </summary>
        public void CopyArray()
        {
            for (int i = 0; i < CELLSIZE; i++)
            {
                tempCells[i] = Cells[i];
            }
        }

        public void OperateCell()
        {
            for (int j = 0; j < GENERATION - 1; j++)
            {
                Console.Write("Gen {0,3}  ", j + 2);
                for (int i = 0; i < CELLSIZE; i++)
                {
                    if (i == 0)
                    {
                        if (Cell.NextCellState(tempCells[i]) == tempCells[i + 1].DefaultCell || Cell.NextCellState(tempCells[i]) == tempCells[63].DefaultCell)
                            Cells[i].ChangeState();
                        if (tempCells[i].DefaultCell == tempCells[i + 1].DefaultCell && tempCells[i].DefaultCell == tempCells[63].DefaultCell)
                            Cells[i].ChangeState();
                    }
                    else if (i == 63)
                    {
                        if (Cell.NextCellState(tempCells[i]) == tempCells[0].DefaultCell || Cell.NextCellState(tempCells[i]) == tempCells[i - 1].DefaultCell)
                            Cells[i].ChangeState();
                        if (tempCells[i].DefaultCell == tempCells[0].DefaultCell && tempCells[i].DefaultCell == tempCells[i - 1].DefaultCell)
                            Cells[i].ChangeState();
                    }
                    else if (Cell.NextCellState(tempCells[i]) == tempCells[i + 1].DefaultCell || Cell.NextCellState(tempCells[i]) == tempCells[i - 1].DefaultCell)
                    {
                        Cells[i].ChangeState();
                    }
                    else
                    {
                        if (tempCells[i].DefaultCell == tempCells[i + 1].DefaultCell && tempCells[i].DefaultCell == tempCells[i - 1].DefaultCell)
                            Cells[i].ChangeState();
                    }
                }
                CopyArray();
                PrintCell();
            }
        }
    }
}
2
Contributors
1
Reply
4
Views
8 Years
Discussion Span
Last Post by DdoubleD
0

STEP 1: Place several comments in your OperateCell method (aka operation1 method). Place the comments above:
* every loop
* every logical comparison
* every method called within the nested loop

When doing this, provide a good explanation of what the statement's purpose is--what is it supposed to do exactly.

By doing this, you will:
* have a better chance of discovering what is wrong yourself
* give others a much better chance of helping you to figure it out
* help to understand the program later on when it may not still be fresh in yours and/or someone else's mind

STEP 2 (can be done in conjunction with STEP 1): Refer to the rules/logic of program stated at the beginning your program:
* Compare it to the descriptive comments you just made in OperateCell
* update these rules to reflect any detail not already defined.
* update comments in OperateCell too if a detail already defined in the rules is not explained in the statement comments in OperateCell

Cheers!

This topic has been dead for over six months. 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.