Example #1
0
        public Unit(int id, CustomGrid grid)
        {
            this.id = id;

            //Spawn unit at a random, valid location
            bool   unitSet = false;
            Random rand    = new Random();

            //Loop while unit is not set at location
            while (unitSet == false)
            {
                int x = rand.Next(grid.GetGridX());
                int y = rand.Next(grid.GetGridY());

                //If the location is valid, set unit to that location
                if (grid.GetGridPosition(x, y).GetSpaceType() == 0 && grid.GetGridPosition(x, y).GetSpaceOccupant() < 0)
                {
                    this.positionX = x;
                    this.positionY = y;
                    grid.GetGridPosition(x, y).SetSpaceOccupant(id);
                    Console.WriteLine(x + y + " OCCUPIED");
                    unitSet = true;
                }
            }
        }
        /**
         * Button_Click - Called when the "Generate Grid" button is clicked
         * This could be refactored into smaller functions in the future...
         */
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Get user input
            //TODO - Make sure input can be converted to int
            int x           = Convert.ToInt32(TB_GridX.Text);
            int y           = Convert.ToInt32(TB_GridY.Text);
            int probability = Convert.ToInt32(TB_Probability.Text);
            int units       = Convert.ToInt32(TB_Units.Text);

            //Initialise grid and units
            this.grid  = new CustomGrid(x, y, probability, units);
            this.units = new Unit[units];

            //Create unique unit for each unit in units & place it on grid
            for (int unit = 0; unit < units; unit++)
            {
                this.units[unit] = new Unit(unit, this.grid);
                int positionX = this.units[unit].GetPositionX();
                int positionY = this.units[unit].GetPositionY();
                this.grid.GetGridPosition(positionX, positionY).SetSpaceOccupant(unit);
            }

            //Print the populated grid, show the UI
            this.grid.PrintGrid();
            this.gridWindow = new GridWindow();
            var grid          = gridWindow.grid;
            var populatedGrid = PopulateGrid(grid);

            gridWindow.Owner = this;
            gridWindow.Show();
        }