Ejemplo n.º 1
0
        private void SetupBombs(int numOfBombs, int seed, BaseCell firstCell)
        {
            //var numOfBombs = 20;
            random = random ?? new Random(seed);
            //create bombs
            for (int i = 0; i < numOfBombs; i++)
            {
                var cells = Grid.Cells.Where(p => p.Bomb != true && p != firstCell).ToList();
                cells[random.Next(0, cells.Count - 1)].Bomb = true;
            }

            //set values
            foreach (var item in Grid.Cells)
            {
                if (item.Bomb != true)
                {
                    CalculateBombs(item);
                }
            }
        }
Ejemplo n.º 2
0
        public BaseCell[] SquareCells(BaseCell cell)
        {
            var cells = new List <BaseCell>();

            for (int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                    var column = cell.Column + i;
                    var row    = cell.Row + j;
                    if (row >= 0 && row < Rows && column >= 0 && column < Columns)
                    {
                        var neighbor = Cells.FirstOrDefault(p => p.Row == row && p.Column == column);

                        cells.Add(neighbor);
                    }
                }
            }
            return(cells.ToArray());
        }
Ejemplo n.º 3
0
        private void CalculateBombs(BaseCell cell)
        {
            int numOfBombs = 0;

            for (int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                    var column = cell.Column + i;
                    var row    = cell.Row + j;
                    if (row >= 0 && row <= Rows && column >= 0 && column <= Columns)
                    {
                        if (Grid.Cells.FirstOrDefault(p => p.Row == row && p.Column == column)?.Bomb == true)
                        {
                            numOfBombs++;
                        }
                    }
                }
            }
            cell.Value = numOfBombs;
        }
Ejemplo n.º 4
0
        private void ShowOthers(BaseCell cell)
        {
            for (int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                    var column = cell.Column + i;
                    var row    = cell.Row + j;
                    if (row >= 0 && row < Grid.Rows && column >= 0 && column < Grid.Columns)
                    {
                        var neighbor = Grid.Cells.FirstOrDefault(p => p.Row == row && p.Column == column);
                        if (neighbor?.Value == 0 && neighbor.Visible != true && neighbor.Bomb != true)
                        {
                            neighbor.Visible = true;
                            ShowOthers(neighbor);
                        }
                    }
                }
            }

            //cell.Show();
        }
Ejemplo n.º 5
0
        public bool ClickOnCell(BaseCell item, bool placeAsFlag)
        {
            if (item.Visible)
            {
                return(false);
            }

            if (!placeAsFlag)
            {
                ShowCell(item);
            }
            else
            {
                ToggleFlagCell(item);
            }
            if (Win)
            {
                OnHasWon();
                OnGameEnded(true);
            }
            return(true);
        }
Ejemplo n.º 6
0
 public void ToggleFlagCell(BaseCell item)
 {
     item.Flag = !item.Flag;
 }
Ejemplo n.º 7
0
 public Cell(BaseCell cell, HTMLCanvasElement canvas)
 {
     _cell               = cell;
     _canvas             = canvas;
     _cell.VisualChange += (sender, args) => Show();
 }