Example #1
0
        public StateMachine(DataTypes.BitmapUnsafe bitmap, List<State> states)
        {
            _bitmap = bitmap;
            _states = states;

            startx = rand.Next(0, bitmap.Width);
            starty = rand.Next(0, bitmap.Height);
            startstate = states[rand.Next(0, states.Count)];

            Reset();
        }
Example #2
0
        public void FromString(string value)
        {
            try
            {
                boring = int.MinValue;
                boredom = 0;

                int index;

                if (value[0] == 'w')
                {
                    value = value.Substring(1);
                    this.numeric_executionrate.Value = 100.0m;
                }
                else if (value[0] == 'r')
                {
                    value = value.Substring(1);
                    index = value.IndexOf('w');

                    decimal rate = Convert.ToDecimal(value.Substring(0, index));

                    numeric_executionrate_override = true;
                    this.numeric_executionrate.Value = rate;
                    numeric_executionrate_override = false;

                    value = value.Substring(index + 1);
                }
                else throw new Exception("unrecognized initial character");

                index = value.IndexOf('h');

                int width = Convert.ToInt32(value.Substring(0, index));
                this.numeric_width.Value = width;
                value = value.Substring(index + 1);

                index = value.IndexOf('v');

                int height = Convert.ToInt32(value.Substring(0, index));
                this.numeric_height.Value = height;
                value = value.Substring(index + 1);

                this.textbox_boredom.Text = boredom.ToString();
                this.pictureBox1.BackgroundImage = null;
                image = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                _image = new DataTypes.BitmapUnsafe(image);

                numeric_executionrate_ValueChanged(null, null);

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        image.SetPixel(x, y, Color.FromArgb(255, 0, 0, 0));
                    }
                }

                int indexm, indexi;

                indexm = value.IndexOf('m');
                indexi = value.IndexOf('i');

                if (indexm < 0) indexm = int.MaxValue;
                if (indexi < 0) indexi = int.MaxValue;

                bool samemachine;
                samemachine = indexm < indexi;
                this.checkbox_samemachine.Checked = samemachine;

                index = indexm < indexi ? indexm : indexi;

                int numeric_values = Convert.ToInt32(value.Substring(0, index));
                this.numeric_values.Value = numeric_values;
                value = value.Substring(index + 1);

                if (samemachine)
                {
                    List<TuringMachine.State> states = new List<TuringMachine.State>();

                    index = value.IndexOf('s');

                    int statecount = Convert.ToInt32(value.Substring(0, index));
                    this.numeric_states.Value = statecount;
                    value = value.Substring(index + 1);

                    for (int n = 0; n < statecount; n++)
                    {
                        index = value.IndexOf(']');

                        TuringMachine.State state = TuringMachine.State.FromString(value.Substring(0, index + 1), numeric_values);
                        value = value.Substring(index + 1);
                        states.Add(state);
                    }

                    instances = new List<TuringMachine.StateMachine>();

                    value = value.Substring(1);

                    index = value.IndexOf('(');

                    int instancecount = Convert.ToInt32(value.Substring(0, index));
                    this.numeric_instances.Value = instancecount;
                    value = value.Substring(index);

                    for (int n = 0; n < this.numeric_instances.Value; n++)
                    {
                        index = value.IndexOf(')');

                        TuringMachine.StateMachine what = TuringMachine.StateMachine.FromString(_image, value.Substring(0, index + 1), states);
                        value = value.Substring(index + 1);
                        instances.Add(what);
                    }
                }
                else
                {
                    instances = new List<TuringMachine.StateMachine>();

                    index = value.IndexOf('(');

                    int instancecount = Convert.ToInt32(value.Substring(0, index));
                    this.numeric_instances.Value = instancecount;
                    value = value.Substring(index);

                    for (int n = 0; n < this.numeric_instances.Value; n++)
                    {
                        index = value.IndexOf(')');

                        TuringMachine.StateMachine what = TuringMachine.StateMachine.FromString(_image, value.Substring(0, index + 1), numeric_values);
                        value = value.Substring(index + 1);
                        instances.Add(what);
                    }

                    this.numeric_states.Value = instances[0]._states.Count;

                }

                this.checkbox_executionstate.Enabled = true;
                this.button_singlestep.Enabled = !this.checkbox_executionstate.Checked;
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed to parse string."+Environment.NewLine+Environment.NewLine+"Error given:" + Environment.NewLine + e.Message);
            }
        }
Example #3
0
        void button_randomize_Click(object sender, EventArgs e)
        {
            this.checkbox_executionstate.Enabled = true;
            this.button_singlestep.Enabled = !this.checkbox_executionstate.Checked;

            boring = int.MinValue;
            boredom = 0;

            this.textbox_boredom.Text = boredom.ToString();
            this.pictureBox1.BackgroundImage = null;

            string text = "r" + this.numeric_executionrate.Value.ToString();

            text += "w" + this.numeric_width.Value.ToString() + "h" + this.numeric_height.Value.ToString();

            image = new Bitmap((int)this.numeric_width.Value, (int)this.numeric_height.Value, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            numeric_executionrate_ValueChanged(null, null);

            _image = new DataTypes.BitmapUnsafe(image);

            for (int y = 0; y < this.numeric_height.Value; y++)
            {
                for (int x = 0; x < this.numeric_width.Value; x++)
                {
                    image.SetPixel(x, y, Color.FromArgb(255, 0, 0, 0));
                }
            }

            text += "v" + this.numeric_values.Value.ToString();

            if (this.checkbox_samemachine.Checked)
            {
                List<TuringMachine.State> states = new List<TuringMachine.State>();

                text += "m" + this.numeric_states.Value.ToString() + "s";

                for (int n = 0; n < this.numeric_states.Value; n++)
                {
                    TuringMachine.State state = new TuringMachine.State((int)this.numeric_values.Value);
                    states.Add(state);

                    text += state.ToString();
                }

                instances = new List<TuringMachine.StateMachine>();

                text += "i" + this.numeric_instances.Value.ToString();

                for (int n = 0; n < this.numeric_instances.Value; n++)
                {
                    TuringMachine.StateMachine what = new TuringMachine.StateMachine(_image, states);
                    instances.Add(what);
                    text += what.ToString(true);
                }
            }
            else
            {
                instances = new List<TuringMachine.StateMachine>();

                text += "i" + this.numeric_instances.Value.ToString();

                for (int n = 0; n < this.numeric_instances.Value; n++)
                {
                    List<TuringMachine.State> states = new List<TuringMachine.State>();

                    for (int subn = 0; subn < this.numeric_states.Value; subn++)
                    {
                        TuringMachine.State state = new TuringMachine.State((int)this.numeric_values.Value);
                        states.Add(state);
                    }

                    TuringMachine.StateMachine what = new TuringMachine.StateMachine(_image, states);
                    instances.Add(what);

                    text += what.ToString(false);
                }
            }

            this.textbox_machines.Text = text;
        }