示例#1
0
        // takes a list of tiles and reveals them and expands any zeros
        private List <ActionResult> Reveal(List <MinesweeperTile> firstTiles)
        {
            List <ActionResult> actionResults = new List <ActionResult>();

            var soFar = 0;

            foreach (MinesweeperTile firstTile in firstTiles)
            {
                firstTile.SetCovered(false);
            }

            int safety = 1000000;

            while (soFar < firstTiles.Count)
            {
                MinesweeperTile tile = firstTiles[soFar];

                actionResults.Add(new ActionResult(tile.GetX(), tile.GetY(), ResultType.Cleared, tile.GetValue()));

                this.tilesLeft--;

                // if the value is zero then for each adjacent tile not yet revealed add it to the list
                if (tile.GetValue() == 0)
                {
                    foreach (int adjIndex in GetAdjacentIndex(tile.GetIndex()))
                    {
                        MinesweeperTile adjTile = this.tiles[adjIndex];

                        if (adjTile.IsCovered() && !adjTile.IsFlagged()) // if not covered and not a flag
                        {
                            adjTile.SetCovered(false);                   // it will be uncovered in a bit
                            firstTiles.Add(adjTile);
                        }
                    }
                }

                soFar++;
                if (safety-- < 0)
                {
                    Write("MinesweeperGame: Reveal Safety limit reached !!");
                    break;
                }
            }

            // if there are no tiles left to find then set the remaining tiles to flagged and we've won
            if (this.tilesLeft == 0)
            {
                for (var i = 0; i < this.tiles.Length; i++)
                {
                    MinesweeperTile tile = this.tiles[i];
                    if (tile.IsMine() && !tile.IsFlagged())
                    {
                        minesLeft--;
                        tile.ToggleFlagged();
                        actionResults.Add(new ActionResult(tile.GetX(), tile.GetY(), ResultType.Flagged));  // auto set remaining flags
                    }
                }

                this.gameStatus = GameStatus.Won;
            }

            return(actionResults);
        }