Example #1
0
        /// <summary>
        /// Grid generation method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void GenerateGrid(object sender, RoutedEventArgs args)
        {
            //Console.WriteLine("generating grid");
            this.Title = "Game of Life **(Generating grid, hold on a sec...)**";
            CellSpace.Children.Clear();
            int x = (int)xSlider.Value;
            int y = (int)ySlider.Value;

            rule = RuleTextBox.Text;
            cg   = new CellGrid(x, y, rule);
            Binding labelBind = new Binding("RuleString");

            RuleLabel.DataContext = cg;
            RuleLabel.SetBinding(Label.ContentProperty, labelBind);
            for (int i = 0; i < x; i++)
            {
                StackPanel sp = new StackPanel(); //A stack panel of stack panels to keep things organized, and grid's manual placement isn't fun in code
                for (int j = 0; j < y; j++)
                {
                    Rectangle r = new Rectangle()
                    {
                        Height  = CellSpace.ActualHeight != 0 ? CellSpace.ActualHeight / y : 58, //on startup, the stackpanel has an actualwidth/height of 0, which would've made rectangles 0x0, so we force retangle size to force stackpanel size
                        Width   = CellSpace.ActualWidth != 0 ? CellSpace.ActualWidth / x : 58,
                        RadiusX = 5,                                                             //make the rectangle a bit circular to see cells better, since borders dont really exist on them
                        RadiusY = 5,                                                             //also the gap between rectangles is barely visible, and margin messes up formatting
                        //Margin = new Thickness() { Bottom = 1, Top = 1, Left = 1, Right = 1 },
                        DataContext = cg.Grid[i, j],
                    };
                    r.MouseLeftButtonDown += (s, ro) => {   //set left mouse down event with a lambda
                        Cell context = (Cell)r.DataContext;
                        context.IsAlive = !context.IsAlive; //just invert
                    };
                    Binding b = new Binding("IsAlive");
                    b.Converter = B2BConv;                   //set the converter
                    r.SetBinding(Rectangle.FillProperty, b); //set bindind property to the rectangle
                    sp.Children.Add(r);                      //give to stack panel and let that do the formatting
                }
                CellSpace.Children.Add(sp);                  //insert the stackpanel to the main stackpanel, let that do the formatting
            }
            this.Title = "Game of Life";
            //Console.WriteLine($"CellSpace: {CellSpace.ActualWidth}x{CellSpace.ActualHeight}, Rect: {CellSpace.ActualWidth/x}x{CellSpace.ActualHeight/y}");
        }
Example #2
0
        private void drawGrid(string json)
        {
            CellGrid cg = JsonConvert.DeserializeObject <CellGrid>(json);

            managerStage = new Cell[cg.SizeY, cg.SizeX];

            foreach (int[] cell in cg.AliveCells)
            {
                managerStage[cell[0], cell[1]] = new Cell(true);
            }

            for (int y = 0; y < cg.SizeY; y++)
            {
                for (int x = 0; x < cg.SizeX; x++)
                {
                    if (managerStage[y, x] == null)
                    {
                        managerStage[y, x] = new Cell(false);
                    }
                }
            }
            pictureBox1.Invalidate();
        }
Example #3
0
        private void commitToMemory(Cell[,] inputGrid)
        {
            CellGrid cg = new CellGrid();

            cg.SizeX      = inputGrid.GetLength(1);
            cg.SizeY      = inputGrid.GetLength(0);
            cg.AliveCells = new List <int[]>();
            for (int y = 0; y < cg.SizeY; y++)
            {
                for (int x = 0; x < cg.SizeX; x++)
                {
                    if (managerStage[y, x].CurrentState)
                    {
                        cg.AliveCells.Add(new int[2] {
                            y, x
                        });
                    }
                }
            }
            string json = JsonConvert.SerializeObject(cg);

            TrialMemory.Add(json);
        }
Example #4
0
 public Game(CellGrid grid)
 {
     cellGrid = grid;
 }