コード例 #1
0
 private void FillHiddenArray()
 {
     hiddenArray.ForEachMineCell((x, y) =>
     {
         hiddenArray[x, y] = new MineCell(MineCellState.Hidden, false);
     });
 }
コード例 #2
0
        public IEnumerable <MineCell> TryDigAt(MineCell cell)
        {
            if (cell == null)
            {
                throw new ArgumentNullException("cell");
            }

            VerifyStateIs(GameState.Initialized, GameState.Started);

            var result = new List <MineCell>();

            // Hit the mine on first hit.
            if (GameState == GameState.Initialized)
            {
                // Don't dig at a Marked cell.
                if (cell.State != CellState.MarkAsMine)
                {
                    this.cells.ClearMineAround(cell);
                    GameState = GameState.Started;
                    result    = this.cells.ExpandFrom(cell).ToList();
                    if (cells.CheckWinning())
                    {
                        GameState = GameState.Success;
                    }
                }
                else
                {
                    // Acturally, the dig operation doesn't really performed, so no need to send dig message here.
                    return(result);
                }
            }

            // Change Game State Once! Don't change it more than one time.
            if (cell.HasMine && cell.State != CellState.MarkAsMine)
            {
                cell.IsTerminator = true;
                GameState         = GameState.Failed;
            }
            // Cannot dig at a flagged cell.
            else if (cell.State == CellState.Normal || cell.State == CellState.Question)
            {
                result = this.cells.ExpandFrom(cell).ToList();
                if (cells.CheckWinning())
                {
                    GameState = GameState.Success;
                }
                else if (GameState == GameState.Initialized)
                {
                    GameState = GameState.Started;
                }
            }
            MessageManager.SendMessage <UserOperationMessage>(this, GameOperation.Dig, result);

            return(result);
        }
コード例 #3
0
        public void FirstMoveWithFlagUncoverTheFirstCell()
        {
            var firstMove = new MineCell(4, 4);
            var minefield = new MineField(9, 9, 10);

            minefield.UncoverCell(firstMove.Row, firstMove.Col);
            Assert.Equal(GameState.InProgress, minefield.GameState);

            var uncoveredCell = minefield.GetCell(firstMove.Row, firstMove.Col);
            Assert.Equal(MineState.Uncovered, uncoveredCell.GetState());
            Assert.Equal(0, uncoveredCell.NeighbourhoodMineCount);
        }
コード例 #4
0
        public void FirstMoveWithFlagUncoverTheFirstCell()
        {
            var firstMove = new MineCell(4, 4);
            var minefield = new MineField(9, 9, 10);

            minefield.UncoverCell(firstMove.Row, firstMove.Col);
            Assert.Equal(GameState.InProgress, minefield.GameState);

            var uncoveredCell = minefield.GetCell(firstMove.Row, firstMove.Col);

            Assert.Equal(MineState.Uncovered, uncoveredCell.GetState());
            Assert.Equal(0, uncoveredCell.NeighbourhoodMineCount);
        }
コード例 #5
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value == null || !(value is string))
            {
                return(null);
            }

            var str  = Convert.ToString(value);
            var grid = new MinesGrid();
            var rows = str.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            int rowNum    = 0;
            int columnNum = 0;

            foreach (var rowStr in rows)
            {
                columnNum = 0;
                var cells = rowStr.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var cellstr in cells)
                {
                    var cell = new MineCell(columnNum, rowNum);
                    int cellState;
                    if (Int32.TryParse(cellstr.Substring(0, 1), out cellState))
                    {
                        cell.State = (CellState)cellState;
                    }
                    else
                    {
                        cell.State = CellState.Normal;
                    }

                    cell.HasMine = cellstr[1] == 'T' ? true : false;
                    grid.Add(cell);

                    ++columnNum;
                }

                ++rowNum;
            }

            grid.Size = new Size(columnNum, rowNum);

            return(grid);
        }
コード例 #6
0
ファイル: GameExtension.cs プロジェクト: hugogu/ClearMine
        /// <summary>
        ///
        /// </summary>
        /// <param name="game"></param>
        /// <param name="cell"></param>
        /// <returns></returns>
        public static bool AutoMarkAt(this IClearMine game, MineCell cell)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }

            if (cell == null)
            {
                throw new ArgumentNullException("cell");
            }

            if (game.GameState == GameState.Initialized || game.GameState == GameState.Started)
            {
                if (cell.State == CellState.Normal)
                {
                    game.TryMarkAt(cell, CellState.MarkAsMine);
                }
                else if (cell.State == CellState.MarkAsMine)
                {
                    if (Settings.Default.ShowQuestionMark)
                    {
                        game.TryMarkAt(cell, CellState.Question);
                    }
                    else
                    {
                        game.TryMarkAt(cell, CellState.Normal);
                    }
                }
                else if (cell.State == CellState.Question)
                {
                    game.TryMarkAt(cell, CellState.Normal);
                }
                else
                {
                    // Do nothing.
                }

                return(true);
            }

            return(false);
        }
コード例 #7
0
        public void TryMarkAt(MineCell cell, CellState newState)
        {
            if (cell == null)
            {
                throw new ArgumentNullException("cell");
            }

            VerifyStateIs(GameState.Initialized, GameState.Started);
            if (!new[] { CellState.MarkAsMine, CellState.Normal, CellState.Question }.Contains(newState))
            {
                throw new InvalidOperationException(ResourceHelper.FindText("InvalidTargetCellState", newState));
            }

            cell.State = newState;
            if (cells.CheckWinning())
            {
                GameState = GameState.Success;
            }

            MessageManager.SendMessage <UserOperationMessage>(this, GameOperation.MarkAs, new[] { cell });
        }
コード例 #8
0
        public IEnumerable <MineCell> TryExpandAt(MineCell cell)
        {
            VerifyStateIs(GameState.Initialized, GameState.Started);

            try
            {
                var result = cells.TryExpandFrom(cell).ToList();
                if (cells.CheckWinning())
                {
                    GameState = GameState.Success;
                }
                MessageManager.SendMessage <UserOperationMessage>(this, GameOperation.Expand, result);

                return(result);
            }
            catch (ExpandFailedException)
            {
                GameState = GameState.Failed;
                return(new List <MineCell>());
            }
        }
コード例 #9
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="game"></param>
        /// <param name="cell"></param>
        /// <returns></returns>
        public static bool AutoMarkAt(this IClearMine game, MineCell cell)
        {
            if (game == null)
                throw new ArgumentNullException("game");

            if (cell == null)
                throw new ArgumentNullException("cell");

            if (game.GameState == GameState.Initialized || game.GameState == GameState.Started)
            {
                if (cell.State == CellState.Normal)
                {
                    game.TryMarkAt(cell, CellState.MarkAsMine);
                }
                else if (cell.State == CellState.MarkAsMine)
                {
                    if (Settings.Default.ShowQuestionMark)
                    {
                        game.TryMarkAt(cell, CellState.Question);
                    }
                    else
                    {
                        game.TryMarkAt(cell, CellState.Normal);
                    }
                }
                else if (cell.State == CellState.Question)
                {
                    game.TryMarkAt(cell, CellState.Normal);
                }
                else
                {
                    // Do nothing.
                }

                return true;
            }

            return false;
        }
コード例 #10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="cell"></param>
 public CellStateMessage(MineCell cell, CellState oldState)
 {
     Cell     = cell;
     OldState = oldState;
 }
コード例 #11
0
        public void TryMarkAt(MineCell cell, CellState newState)
        {
            if (cell == null)
                throw new ArgumentNullException("cell");

            VerifyStateIs(GameState.Initialized, GameState.Started);
            if (!new[] { CellState.MarkAsMine, CellState.Normal, CellState.Question }.Contains(newState))
            {
                throw new InvalidOperationException(ResourceHelper.FindText("InvalidTargetCellState", newState));
            }

            cell.State = newState;
            if (cells.CheckWinning())
            {
                GameState = GameState.Success;
            }

            MessageManager.SendMessage<UserOperationMessage>(this, GameOperation.MarkAs, new[] { cell });
        }
コード例 #12
0
        public IEnumerable<MineCell> TryExpandAt(MineCell cell)
        {
            VerifyStateIs(GameState.Initialized, GameState.Started);

            try
            {
                var result = cells.TryExpandFrom(cell).ToList();
                if (cells.CheckWinning())
                {
                    GameState = GameState.Success;
                }
                MessageManager.SendMessage<UserOperationMessage>(this, GameOperation.Expand, result);

                return result;
            }
            catch (ExpandFailedException)
            {
                GameState = GameState.Failed;
                return new List<MineCell>();
            }
        }
コード例 #13
0
        public IEnumerable<MineCell> TryDigAt(MineCell cell)
        {
            if (cell == null)
                throw new ArgumentNullException("cell");

            VerifyStateIs(GameState.Initialized, GameState.Started);

            var result = new List<MineCell>();

            // Hit the mine on first hit.
            if (GameState == GameState.Initialized)
            {
                // Don't dig at a Marked cell.
                if (cell.State != CellState.MarkAsMine)
                {
                    this.cells.ClearMineAround(cell);
                    GameState = GameState.Started;
                    result = this.cells.ExpandFrom(cell).ToList();
                    if (cells.CheckWinning())
                    {
                        GameState = GameState.Success;
                    }
                }
                else
                {
                    // Acturally, the dig operation doesn't really performed, so no need to send dig message here.
                    return result;
                }
            }

            // Change Game State Once! Don't change it more than one time.
            if (cell.HasMine && cell.State != CellState.MarkAsMine)
            {
                cell.IsTerminator = true;
                GameState = GameState.Failed;
            }
            // Cannot dig at a flagged cell.
            else if (cell.State == CellState.Normal || cell.State == CellState.Question)
            {
                result = this.cells.ExpandFrom(cell).ToList();
                if (cells.CheckWinning())
                {
                    GameState = GameState.Success;
                }
                else if (GameState == GameState.Initialized)
                {
                    GameState = GameState.Started;
                }
            }
            MessageManager.SendMessage<UserOperationMessage>(this, GameOperation.Dig, result);

            return result;
        }
コード例 #14
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="cell"></param>
 public CellStateMessage(MineCell cell, CellState oldState)
 {
     Cell = cell;
     OldState = oldState;
 }
コード例 #15
0
        /// <summary>
        /// Creates an instance of the class MineCell
        /// </summary>
        /// <param name="row">An integer for the row of the cell</param>
        /// <param name="col">An integer for the col of the cell</param>
        /// <returns>An instance of the MineCell class</returns>
        public override Cell CreateMineCell(Position pos)
        {
            var mineCell = new MineCell(pos);

            return mineCell;
        }
コード例 #16
0
ファイル: MainWindow.xaml.cs プロジェクト: vabc3/MineStudio
        private void Canvas1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var p = e.GetPosition(Canvas1);
            int x = (int)((p.X - _boardLeft)/dw);
            int y = (int)((p.Y - _boardTop)/dh);
            if (_mineTable.IndexValid(x, y)) {
                DataGrid1.SelectedIndex = _mineTable.GetIndex(x, y);

                var b = mm.GetGrid(x, y);
                b.Save(string.Format("Grid_{0}_{1}.png", x, y));
            } else {
                DataGrid1.SelectedIndex = -1;
                Selected=null;
            }
        }
コード例 #17
0
 public CellStateChangedEventArgs(MineCell cell, CellState oldState)
 {
     Cell = cell;
     OldState = oldState;
 }
コード例 #18
0
ファイル: MainWindow.xaml.cs プロジェクト: vabc3/MineStudio
        private void DrawCell(MineCell cell)
        {
            double px = (cell.X + .5) * dw+_boardLeft;
            double py = (cell.Y + .5)* dh+_boardTop;

            string str = "";
            switch (cell.Status) {
                case CellStatus.Undef:
                    str = "U";
                    break;
                case CellStatus.Covered:
                    str = "C";
                    break;
                case CellStatus.Mine:
                    str = "M";
                    break;
                case CellStatus.Ground:
                    if (cell.N!=0)
                        str = cell.N.ToString();
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            var formattedText = new FormattedText(
            str,
            CultureInfo.GetCultureInfo("en-us"),
            FlowDirection.LeftToRight,
            new Typeface("Verdana"),
            16,
            Brushes.Black);
            var tb = formattedText.BuildGeometry(new Point(px, py));
            var pa = new Path
                {
                    Stroke = Brushes.Black,
                    Data = tb
                };
            Canvas1.Children.Add(pa);

            if (Selected == cell) {
                double pw=.4*dw;
                double ph=.4*dh;
                RectangleGeometry rg =
                    new RectangleGeometry(new Rect(new Point(px - pw, py - ph), new Point(px + pw, py + ph)));
                var pc = new Path
                {
                    Stroke = Brushes.Blue,
                    Data = rg
                };
                Canvas1.Children.Add(pc);
            }

            if (cell.PredStatus==CellStatus.Covered) return;

            EllipseGeometry eg = new EllipseGeometry(
                new Point(px, py), 10, 10
                );
            var pb = new Path
                {
                    Fill = (cell.PredStatus == CellStatus.Mine ? Brushes.Red : Brushes.Green),
                    Data = eg,

                };
            Canvas1.Children.Add(pb);
        }
コード例 #19
0
ファイル: MainWindow.xaml.cs プロジェクト: vabc3/MineStudio
 private void DataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     DataGrid dt = sender as DataGrid;
     if (dt.SelectedIndex > 0) {
         Selected = _mineTable.Table[dt.SelectedIndex];
     }
     Canvas_Update();
 }