/// <summary>
        /// Runs the game until it's either lost or won.
        /// </summary>
        /// <returns>The result of the game as a string.</returns>
        private static string StartNewGame( )
        {
            Console.Clear();
            var minefield = new Minefield(FIELD_SIZE);

            minefield.AddBombs(NBR_OF_BOMBS);
            minefield.PrintField();
            bool bombSelected   = false;
            bool gameInProgress = true;

            while (gameInProgress)
            {
                Console.WriteLine("Enter coordinates on the form \"X Y\": ");
                Tuple <int, int> inputRes;
                try
                {
                    inputRes       = Util.ParseInput(FIELD_SIZE, Console.ReadLine());
                    bombSelected   = minefield.UncoverNode(inputRes.Item1, inputRes.Item2);
                    gameInProgress = !(bombSelected || minefield.AllNonBombsUncovered());
                    Console.Clear();
                    minefield.PrintField();
                }
                catch (ArgumentException)
                {
                    Console.WriteLine("Wrong input, please try again.");
                }
            }

            return(bombSelected ? "KABOOM! You hit a mine!" : "Congratulations, you won!");
        }
        public static string RenderMinefield(Minefield minefield)
        {
            var output = new StringBuilder();

            GenerateColumnTitles(minefield.Width, output);
            for (var rowIndex = 0; rowIndex < minefield.Height; rowIndex++)
            {
                output.Append(GenerateRowTitle(rowIndex));
                for (var columnIndex = 0; columnIndex < minefield.Width; columnIndex++)
                {
                    //check if the minefield has been explored at index.
                    if (minefield.IsExplored(rowIndex, columnIndex))
                    {
                        output.Append(" ");
                    }
                    else
                    {
                        output.Append("#");

                    }
                }
                output.Append("\n");
            }
            return output.ToString();
        }
 private void CreatePlayfield(int rows, int cols, int numBombs)
 {
     Mines = new Minefield(cols, rows, numBombs);
     Game.Initialize(Mines);
     Game.Visibility = Visibility.Visible;
     Menu.Visibility = Visibility.Hidden;
 }
Exemple #4
0
        public static string RenderMinefield(Minefield minefield)
        {
            var output = new StringBuilder();


            GenerateColumnTitles(minefield.Width, output);
            for (var rowIndex = 0; rowIndex < minefield.Height; rowIndex++)
            {
                output.Append(GenerateRowTitle(rowIndex));
                for (var columnIndex = 0; columnIndex < minefield.Width; columnIndex++)
                {
                    //check if the minefield has been explored at index.
                    if (minefield.IsExplored(rowIndex, columnIndex))
                    {
                        output.Append(" ");
                    }
                    else
                    {
                        output.Append("#");
                    }
                }
                output.Append("\n");
            }
            return(output.ToString());
        }
        internal void Initialize(Minefield mines)
        {
            ClearMinefield();

            Field.Rows            = mines.Height;
            Field.Columns         = mines.Width;
            game                  = mines;
            game.PropertyChanged += Game_PropertyChanged;
            int cellCount = mines.Height * mines.Width;
            int i         = 0;

            for (; i < cellCount && i < Field.Children.Count; ++i)
            {
                GameCell cell = Field.Children[i] as GameCell;
                cell.GameElement = game.Playfield[i];
                cell.OnSelected += Child_OnSelected;
            }

            for (; i < cellCount; ++i)
            {
                GameCell cell = new GameCell()
                {
                    GameElement = game.Playfield[i]
                };
                Field.Children.Add(cell);
                cell.OnSelected += Child_OnSelected;
            }
        }
Exemple #6
0
        public bool tryToSolve(ref Space[] _field, Size _fieldSize, Point _solverOrigin, Minefield _minefield = null)
        {
            flippedNumbers      = new LinkedList <int>();
            fullyFlaggedNumbers = new LinkedList <int>();
            field        = _field;
            fieldSize    = _fieldSize;
            solverOrigin = _solverOrigin;
            minefield    = _minefield;

            //flipFirstUnflippedZero();

            if (minefield == null)
            {
                flipField(_solverOrigin.X + _solverOrigin.Y * fieldSize.Width);
            }
            else
            {
                for (int i = 0; i < field.Length; i++)
                {
                    if (0 < field[i].number && field[i].state == Space.STATE_FACE_UP)
                    {
                        flippedNumbers.AddFirst(i);
                    }
                }
            }
            solveViaFlippedNumbers();

            return(CheckWin());
        }
 internal static void Main(string[] args)
 {
     var minefield = new Minefield(Width, Height);
     Console.Write(MinefieldRenderer.RenderMinefield(minefield));
     //format A2
     var input = Console.ReadLine();
     var coordinates = UserInputReader.ReadCoordinates(input);
     var newMinefield = minefield.Explore(coordinates);
     Console.Write(MinefieldRenderer.RenderMinefield(newMinefield));
     Console.ReadLine();
 }
Exemple #8
0
        internal static void Main(string[] args)
        {
            var minefield = new Minefield(Width, Height);

            Console.Write(MinefieldRenderer.RenderMinefield(minefield));
            //format A2
            var input        = Console.ReadLine();
            var coordinates  = UserInputReader.ReadCoordinates(input);
            var newMinefield = minefield.Explore(coordinates);

            Console.Write(MinefieldRenderer.RenderMinefield(newMinefield));
            Console.ReadLine();
        }
Exemple #9
0
        /*
         * Resets for a new game
         */
        public void NewGame()
        {
            uncovered = 0;
            Newgame   = true;
            GameOver  = false;
            flagged   = 0;

            // set up minefield
            minefield = new Minefield(Rows, Cols);
            form.doubleBufferedPanel1.Size = new Size(Cols * 16 * scale, Rows * 16 * scale);
            faceinator.state  = GraphicsLibrary.SMILE_UP;
            mineCounter.count = Mines;
            timerinator.Reset();
            form.Refresh();
        }
        public static string RenderMinefield(Minefield minefield)
        {
            var output = new StringBuilder();
            GenerateColumnTitles(minefield.Width, output);

            for (var rowIndex = 0; rowIndex < minefield.Height; rowIndex++)
            {
                output.Append(GenerateRowTitle(rowIndex));
                for (var columnIndex = 0; columnIndex < minefield.Width; columnIndex++)
                {
                    output.Append("#");
                }
                output.Append("\n");
            }
            return output.ToString();
        }
        private void ClearMinefield()
        {
            FinalScreen.Visibility = Visibility.Hidden;
            Field.Visibility       = Visibility.Visible;

            if (game != null)
            {
                game.PropertyChanged -= Game_PropertyChanged;
                game = null;
            }

            foreach (GameCell child in Field.Children)
            {
                child.OnSelected -= Child_OnSelected;
                child.GameElement = null;
            }
        }
Exemple #12
0
        public static string RenderMinefield(Minefield minefield)
        {
            var output = new StringBuilder();

            GenerateColumnTitles(minefield.Width, output);

            for (var rowIndex = 0; rowIndex < minefield.Height; rowIndex++)
            {
                output.Append(GenerateRowTitle(rowIndex));
                for (var columnIndex = 0; columnIndex < minefield.Width; columnIndex++)
                {
                    output.Append("#");
                }
                output.Append("\n");
            }
            return(output.ToString());
        }
        public void Run()
        {
            var content   = _reader.Read();
            var minefield = new Minefield();

            _listOfMinefields.Add(minefield);

            foreach (var line in content)
            {
                if (line == "")
                {
                    minefield = new Minefield();
                    _listOfMinefields.Add(minefield);
                    continue;
                }
                minefield.AddLine(line);
            }

            PrintMinefield();
        }
 public void NewGame(int width, int height, int numMines)
 {
     theMinefield = new Minefield(width, height, numMines);
 }
Exemple #15
0
        public static void Main()
        {
            Minefield mf       = new Minefield();
            var       field    = new Minefield();
            bool      runGame  = true;
            bool      newState = true;



            //set the bombs...



            mf.SetBomb(0, 0);
            mf.SetBomb(0, 1);
            mf.SetBomb(1, 1);
            mf.SetBomb(1, 4);
            mf.SetBomb(4, 2);


            //the mine field should look like this now:
            //  01234
            //4|1X1
            //3|11111
            //2|2211X
            //1|XX111
            //0|X31

            // Game code...

            Console.WriteLine("Minesweeper!");
            mf.InitializeMineField();



            while (runGame)
            {
                int x;
                int y;
                newState = true;


                mf.PrintMineField(newState);

                Console.WriteLine("Enter x-coordinate: ");
                string xInput = Console.ReadLine();
                Console.WriteLine("Enter y-coordinate: ");
                string yInput = Console.ReadLine();

                if (String.IsNullOrEmpty(xInput) || String.IsNullOrEmpty(yInput))
                {
                    Console.WriteLine("Please select a coordinate");
                }
                else
                {
                    try
                    {
                        x = Convert.ToInt32(xInput);
                        y = Convert.ToInt32(yInput);

                        if (x > 4 || y > 4)
                        {
                            Console.WriteLine("Position is out of bounds, try again");
                        }
                        else
                        {
                            mf.HandleInput(x, y);
                        }
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Please select an integer");
                    }
                }
            }

            Console.Read();
        }
        /// <summary>
        /// Makes out of the Input File containing the Minefield the Cheat Note
        /// </summary>
        /// <param name="filepath"></param>
        public void MakeCheatNote(string filepath)
        {
            int rows    = 0;
            int columns = 0;

            string[]  lines;
            Minefield minefield;
            Regex     input = new Regex("^[.*]*$");

            try
            {
                if (File.Exists(filepath))
                {
                    //read input
                    lines = File.ReadAllLines(filepath);

                    //set Dimensions
                    rows    = lines.GetLength(0);
                    columns = lines[0].Length;
                    if (rows < 1 || columns < 1)
                    {
                        throw new ArgumentException("Board size can not be less than 1x1!");
                    }
                    minefield = new Minefield();
                    minefield.setDimensions(rows, columns);

                    //set mines
                    for (int r = 0; r < rows; r++)
                    {
                        //catch eventual faulty input charachters
                        if (!input.IsMatch(lines[r]))
                        {
                            throw new ArgumentException("Input File contains unallowed charachters!");
                        }
                        int c = 0;
                        foreach (char ch in lines[r])
                        {
                            switch (ch)
                            {
                            case '*':
                                minefield.setMine(r, c);
                                break;

                            default:
                                break;
                            }
                            c++;
                        }
                    }
                    //calculate fields
                    minefield.calculateTileMineNeighbours();

                    //start Making Cheat Note
                    string[]       cheatNote = minefield.GetCheatNote();
                    SaveFileDialog sfd       = new SaveFileDialog();
                    sfd.Filter           = "txt File (*.txt)|*.txt";
                    sfd.RestoreDirectory = true;
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        File.WriteAllLines(sfd.FileName, cheatNote);
                    }
                    else
                    {
                        throw new ArgumentException("Cheat Note Creation cancled!");
                    }

                    MessageBox.Show("Cheat Note Successfuly Created!", "Success", MessageBoxButtons.OK);
                    Process.Start("explorer.exe", "/select, \"" + sfd.FileName + "\""); //Open File Explorer with selection on new File
                }
                else
                {
                    throw new ArgumentException("File not found!");
                }
            }
            catch (ArgumentException ae)
            {
                MessageBox.Show(ae.Message, "Error", MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Runtime Error", MessageBoxButtons.OK);
            }
        }
        private Random r = new Random(); //Generates random locations

        #endregion Fields

        #region Constructors

        //constructor
        public Minefields(int rows, int columns)
        {
            //Initializes the cells
            grid = new Minefield[rows, columns];

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    grid[i, j] = new Minefield();

                }
            }
            //Creates Mines
            for (int k = 0; k < Math.Pow(grid.GetLength(0),2)/10; k++)
            {
                //Continuously creates mines at random locations until a certain amount are created
                int i, j;
                do
                {
                    i = r.Next(0, grid.GetLength(0));
                    j = r.Next(0, grid.GetLength(1));
                } while (grid[i, j].IsMine == true);
                grid[i, j].IsMine = true; //Changes the status of the square to have a mine
                //Changes the values (makes other squares know that they are touching a mine)
                if (i == 0) //First Row
                {
                    if (j == 0) //Left Corner
                    {
                        grid[i + 1, j].GetValue += 1;
                        grid[i + 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue += 1;
                    }
                    else if (j == grid.GetLength(0)-1) //Right Corner
                    {
                        grid[i + 1, j].GetValue += 1;
                        grid[i + 1, j - 1].GetValue += 1;
                        grid[i, j - 1].GetValue += 1;
                    }
                    else if (j >0 && j < grid.GetLength(0)-1) //Anywhere in that row
                    {
                        grid[i, j-1].GetValue += 1;
                        grid[i + 1, j - 1].GetValue += 1;
                        grid[i+1, j].GetValue += 1;
                        grid[i + 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue += 1;
                    }
                }
                if (i == grid.GetLength(0)-1) //Last row
                {
                    if (j == 0) //Left Corner
                    {
                        grid[i - 1, j].GetValue += 1;
                        grid[i - 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue += 1;
                    }
                    else if (j == grid.GetLength(0) - 1) //Right Corner
                    {
                        grid[i - 1, j].GetValue += 1;
                        grid[i - 1, j - 1].GetValue += 1;
                        grid[i, j - 1].GetValue += 1;
                    }
                    else if (j > 0 && j < grid.GetLength(0) - 1) //Anywhere in that row
                    {
                        grid[i, j - 1].GetValue += 1;
                        grid[i - 1, j - 1].GetValue += 1;
                        grid[i - 1, j].GetValue += 1;
                        grid[i - 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue += 1;
                    }
                }
                if (i > 0 && i < grid.GetLength(0) - 1) //Anywhere in between
                {
                    if (j == 0) //Left Column
                    {
                        grid[i - 1, j].GetValue += 1;
                        grid[i - 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue += 1;
                        grid[i + 1, j + 1].GetValue += 1;
                        grid[i + 1, j].GetValue += 1;
                    }
                    else if (j == grid.GetLength(0) - 1) //Right Column
                    {
                        grid[i - 1, j].GetValue += 1;
                        grid[i - 1, j - 1].GetValue += 1;
                        grid[i, j - 1].GetValue += 1;
                        grid[i + 1, j - 1].GetValue += 1;
                        grid[i + 1, j].GetValue += 1;
                    }
                    else if (j > 0 && j < grid.GetLength(0) - 1) //In the Middle
                    {
                        grid[i, j - 1].GetValue += 1;
                        grid[i - 1, j - 1].GetValue += 1;
                        grid[i - 1, j].GetValue += 1;
                        grid[i - 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue += 1;
                        grid[i + 1, j + 1].GetValue += 1;
                        grid[i + 1, j].GetValue += 1;
                        grid[i + 1, j - 1].GetValue += 1;
                    }
                }
            }
        }
Exemple #18
0
        static void Main()
        {
            var           field       = new Minefield();
            string        playerInput = "";
            int           X;
            int           Y;
            List <String> inputLog = new List <String>();

            //set the bombs...
            field.SetBomb(0, 0);
            field.SetBomb(0, 1);
            field.SetBomb(1, 1);
            field.SetBomb(1, 4);
            field.SetBomb(4, 2);

            //the mine field should look like this now:
            //  01234
            //4|1X1
            //3|11111
            //2|2211X
            //1|XX111
            //0|X31

            // Game code...

            //populate board with "?"
            field.Populate();
            //visualize the field behind the board
            field.CreateGameField();
            //print starting board
            field.PrintBoard();

            //keep the game running until game over
            while (field.end == 0)
            {
                System.Console.WriteLine();
                playerInput = Console.ReadLine().ToString();


                //split input
                string[] instructions = playerInput.Split(' ');


                //make sure the input isn't too long or too short. It should always be 2 values (without the space in the initial input)
                if (instructions.Length == 2)
                {
                    //make sure this input haven't already been used, check the inputLog!
                    for (int c = 0; c <= inputLog.Count; c++)
                    {
                        //make sure values are numbers
                        if (Int32.TryParse(instructions[0], out int tryInt) && Int32.TryParse(instructions[1], out tryInt))
                        {
                            if (inputLog.Contains(playerInput))
                            {
                                System.Console.WriteLine("You have already checked these coordinates.");
                                break;
                            }
                        }
                        else
                        {
                            System.Console.WriteLine("Those are not numbers. Incorrect input. Try again.");
                            //clear the instructions variable or it carries to next round!
                            instructions[0] = "";
                            instructions[1] = "";
                        }

                        if (Int32.TryParse(instructions[0], out tryInt) && Int32.TryParse(instructions[1], out tryInt))
                        {
                            //Log the input data!
                            inputLog.Add(playerInput.ToString());

                            //convert to int
                            X = Int32.Parse(instructions[0]);
                            Y = Int32.Parse(instructions[1]);

                            //Make sure there's no out of bounds!
                            if (X >= 0 && X < 5 && Y >= 0 && Y < 5)
                            {
                                //Call game function
                                field.PlayGame(X, Y);
                                if (field.end == 0)
                                {
                                    field.PrintBoard();
                                }
                                break;
                            }
                            else
                            {
                                System.Console.WriteLine("Incorrect input. Try again.");
                                break;
                            }
                        }
                        //clear the instructions variable or it carries to next round!
                        instructions[0] = "";
                        instructions[1] = "";
                    }
                }
                else
                {
                    System.Console.WriteLine("Incorrect input. Did you use numbers? Try again.");
                }
            }


            //make the hidden field visible here on game over
            if (field.end == 1)
            {
                System.Console.WriteLine();
                System.Console.WriteLine("+++++ BOOOOOOOM! +++++");
                System.Console.WriteLine("  01234");
                int h = field.gameField.GetLength(1);
                int w = field.gameField.GetLength(0);
                int o = field.gameField.GetLength(1) - 1;
                for (int q = w - 1; q >= 0; q--)
                {
                    for (int p = 0; p < h; p++)
                    {
                        if (p == 0)
                        {
                            System.Console.Write(o + "|".Replace("\n", Environment.NewLine));
                            o--;
                        }

                        System.Console.Write("{0}", field.gameField[p, q]);
                    }
                    System.Console.WriteLine();
                }
                System.Console.WriteLine("\nGAME OVER. Thanks for playing!".Replace("\n", Environment.NewLine));
            }

            if (field.end == 2)
            {
                System.Console.WriteLine(". o O * VICTORY !* O o .");
                System.Console.WriteLine("  01234");
                int h = field.gameField.GetLength(1);
                int w = field.gameField.GetLength(0);
                int o = field.gameField.GetLength(1) - 1;
                for (int q = w - 1; q >= 0; q--)
                {
                    for (int p = 0; p < h; p++)
                    {
                        if (p == 0)
                        {
                            System.Console.Write("\n" + o + "|".Replace("\n", Environment.NewLine));
                            o--;
                        }

                        System.Console.Write("{0}", field.gameField[p, q]);
                    }
                }
                System.Console.WriteLine("\nYOU WON. Thanks for playing!".Replace("\n", Environment.NewLine));
            }
        }
Exemple #19
0
        private int mGameState = 0;            //Keeps track of Game State that the user is in

        //constructor

        public Minefields(int rows, int columns)
        {
            //Initializes the cells
            grid = new Minefield[rows, columns];

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    grid[i, j] = new Minefield();
                }
            }
            //Creates Mines
            for (int k = 0; k < Math.Pow(grid.GetLength(0), 2) / 10; k++)
            {
                //Continuously creates mines at random locations until a certain amount are created
                int i, j;
                do
                {
                    i = r.Next(0, grid.GetLength(0));
                    j = r.Next(0, grid.GetLength(1));
                } while (grid[i, j].IsMine == true);
                grid[i, j].IsMine = true; //Changes the status of the square to have a mine
                //Changes the values (makes other squares know that they are touching a mine)
                if (i == 0)               //First Row
                {
                    if (j == 0)           //Left Corner
                    {
                        grid[i + 1, j].GetValue     += 1;
                        grid[i + 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue     += 1;
                    }
                    else if (j == grid.GetLength(0) - 1) //Right Corner
                    {
                        grid[i + 1, j].GetValue     += 1;
                        grid[i + 1, j - 1].GetValue += 1;
                        grid[i, j - 1].GetValue     += 1;
                    }
                    else if (j > 0 && j < grid.GetLength(0) - 1) //Anywhere in that row
                    {
                        grid[i, j - 1].GetValue     += 1;
                        grid[i + 1, j - 1].GetValue += 1;
                        grid[i + 1, j].GetValue     += 1;
                        grid[i + 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue     += 1;
                    }
                }
                if (i == grid.GetLength(0) - 1) //Last row
                {
                    if (j == 0)                 //Left Corner
                    {
                        grid[i - 1, j].GetValue     += 1;
                        grid[i - 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue     += 1;
                    }
                    else if (j == grid.GetLength(0) - 1) //Right Corner
                    {
                        grid[i - 1, j].GetValue     += 1;
                        grid[i - 1, j - 1].GetValue += 1;
                        grid[i, j - 1].GetValue     += 1;
                    }
                    else if (j > 0 && j < grid.GetLength(0) - 1) //Anywhere in that row
                    {
                        grid[i, j - 1].GetValue     += 1;
                        grid[i - 1, j - 1].GetValue += 1;
                        grid[i - 1, j].GetValue     += 1;
                        grid[i - 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue     += 1;
                    }
                }
                if (i > 0 && i < grid.GetLength(0) - 1) //Anywhere in between
                {
                    if (j == 0)                         //Left Column
                    {
                        grid[i - 1, j].GetValue     += 1;
                        grid[i - 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue     += 1;
                        grid[i + 1, j + 1].GetValue += 1;
                        grid[i + 1, j].GetValue     += 1;
                    }
                    else if (j == grid.GetLength(0) - 1) //Right Column
                    {
                        grid[i - 1, j].GetValue     += 1;
                        grid[i - 1, j - 1].GetValue += 1;
                        grid[i, j - 1].GetValue     += 1;
                        grid[i + 1, j - 1].GetValue += 1;
                        grid[i + 1, j].GetValue     += 1;
                    }
                    else if (j > 0 && j < grid.GetLength(0) - 1) //In the Middle
                    {
                        grid[i, j - 1].GetValue     += 1;
                        grid[i - 1, j - 1].GetValue += 1;
                        grid[i - 1, j].GetValue     += 1;
                        grid[i - 1, j + 1].GetValue += 1;
                        grid[i, j + 1].GetValue     += 1;
                        grid[i + 1, j + 1].GetValue += 1;
                        grid[i + 1, j].GetValue     += 1;
                        grid[i + 1, j - 1].GetValue += 1;
                    }
                }
            }
        }