Minesweeper Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MineSweeper
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Write("Enter number of rows: ");
                int M = int.Parse(Console.ReadLine());
                Console.Write("Enter number of columns: ");
                int N = int.Parse(Console.ReadLine());
                int[,] mineMatrix = new int[M, N];
                for (int i = 0; i < M; i++)
                {
                    for (int j = 0; j < N; j++)
                    {
                        Console.Write("Enter element (" + (i + 1) + "," + (j + 1) + "): ");
                        mineMatrix[i, j] = int.Parse(Console.ReadLine());
                    }
                }

                Display(mineMatrix);
               
                //Mark Mines
                for (int i = 0; i < mineMatrix.GetLength(0); i++)
                {
                    for (int j = 0; j < mineMatrix.GetLength(1); j++)
                    {
                        if (mineMatrix[i, j] > 0)
                        {
                            int count = CountNearByUncoveredCells(mineMatrix, i, j);
                            if (count == mineMatrix[i, j])
                            {
                                MarkMines(mineMatrix, i, j);
                            }
                        }
                    }
                }
                //Mark Free Cells
                for (int i = 0; i < mineMatrix.GetLength(0); i++)
                {
                    for (int j = 0; j < mineMatrix.GetLength(1); j++)
                    {
                        if (mineMatrix[i, j] > 0)
                        {
                            int count1 = CountNearByMines(mineMatrix, i, j);
                            int count2 = CountNearByUncoveredCells(mineMatrix, i, j);
                            if (count2 > count1 && count1 == mineMatrix[i, j])
                            {
                                MarkFreeCells(mineMatrix, i, j);
                            }
                        }
                    }
                }
                //Mark Left Over Mines
                for (int i = 0; i < mineMatrix.GetLength(0); i++)
                {
                    for (int j = 0; j < mineMatrix.GetLength(1); j++)
                    {
                        if (mineMatrix[i, j] > 0)
                        {
                            int count = CountNearByUndetectedCells(mineMatrix, i, j);
                            int count1 = CountNearByMines(mineMatrix, i, j);
                            if (count > 0 && count + count1 == mineMatrix[i, j])
                            {
                                MarkMines(mineMatrix, i, j);
                            }
                        }
                    }
                }
                //Mark Left Over Free Cells
                for (int i = 0; i < mineMatrix.GetLength(0); i++)
                {
                    for (int j = 0; j < mineMatrix.GetLength(1); j++)
                    {
                        if (mineMatrix[i, j] > 0)
                        {
                            int count1 = CountNearByMines(mineMatrix, i, j);
                            int count2 = CountNearByUndetectedCells(mineMatrix, i, j);
                            if (count1 == mineMatrix[i, j] && count2 != 0)
                            {
                                MarkFreeCells(mineMatrix, i, j);
                            }
                        }
                    }
                }
                Display(mineMatrix);
                Console.Write("Cells with mines: ");
                //Print Cells With Mines
                int flag = 0;
                for (int i = 0; i < mineMatrix.GetLength(0); i++)
                {
                    for (int j = 0; j < mineMatrix.GetLength(1); j++)
                    {
                        if (mineMatrix[i, j] == -2)
                        {
                            if (flag == 0)
                            {
                                Console.Write("(" + (i + 1) + "," + (j + 1) + ")");
                                flag = 1;
                            }
                            else
                            {
                                Console.Write(", (" + (i + 1) + "," + (j + 1) + ")");
                            }
                        }
                    }
                }
                Console.WriteLine();
                Console.Write("Cells without mines: ");
                //Print Cells Without Mines
                flag = 0;
                for (int i = 0; i < mineMatrix.GetLength(0); i++)
                {
                    for (int j = 0; j < mineMatrix.GetLength(1); j++)
                    {
                        if (mineMatrix[i, j] == -3)
                        {
                            if (flag == 0)
                            {
                                Console.Write("(" + (i + 1) + "," + (j + 1) + ")");
                                flag = 1;
                            }
                            else
                            {
                                Console.Write(", (" + (i + 1) + "," + (j + 1) + ")");
                            }
                        }
                    }
                }
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        //Mark Free Cells
        public static void MarkFreeCells(int[,] mineMatrix, int x, int y)
        {
            int rows = mineMatrix.GetLength(0);
            int cols = mineMatrix.GetLength(1);
            //Top left corner
            if (x != 0 && y != 0)
            {
                if (mineMatrix[x – 1, y – 1] == -1)
                {
                    mineMatrix[x – 1, y – 1] = -3;
                }
            }
            //Top corner
            if (x != 0)
            {
                if (mineMatrix[x – 1, y] == -1)
                {
                    mineMatrix[x – 1, y] = -3;
                }
            }
            //Top right corner
            if (x != 0 && y != cols – 1)
            {
                if (mineMatrix[x – 1, y + 1] == -1)
                {
                    mineMatrix[x – 1, y + 1] = -3;
                }
            }
            //Bottom left corner
            if (x != rows – 1 && y != 0)
            {
                if (mineMatrix[x + 1, y – 1] == -1)
                {
                    mineMatrix[x + 1, y – 1] = -3;
                }
            }
            //Bottom corner
            if (x != rows – 1)
            {
                if (mineMatrix[x + 1, y] == -1)
                {
                    mineMatrix[x + 1, y] = -3;
                }
            }
            //Bottom right corner
            if (x != rows – 1 && y != cols – 1)
            {
                if (mineMatrix[x + 1, y + 1] == -1)
                {
                    mineMatrix[x + 1, y + 1] = -3;
                }

            }
            //Left corner
            if (y != 0)
            {
                if (mineMatrix[x, y – 1] == -1)
                {
                    mineMatrix[x, y – 1] = -3;
                }
            }
            //Right corner
            if (y != cols – 1)
            {
                if (mineMatrix[x, y + 1] == -1)
                {
                    mineMatrix[x, y + 1] = -3;
                }
            }
        }
       
        //Count Near By Mines
        public static int CountNearByMines(int[,] mineMatrix, int x, int y)
        {
            int count = 0;
            int rows = mineMatrix.GetLength(0);
            int cols = mineMatrix.GetLength(1);
            //Top left corner
            if (x != 0 && y != 0)
            {
                if (mineMatrix[x – 1, y – 1] == -2)
                {
                    count++;
                }
            }
            //Top corner
            if (x != 0)
            {
                if (mineMatrix[x – 1, y] == -2)
                {
                    count++;
                }
            }
            //Top right corner
            if (x != 0 && y != cols – 1)
            {
                if (mineMatrix[x – 1, y + 1] == -2)
                {
                    count++;
                }
            }
            //Bottom left corner
            if (x != rows – 1 && y != 0)
            {
                if (mineMatrix[x + 1, y – 1] == -2)
                {
                    count++;
                }
            }
            //Bottom corner
            if (x != rows – 1)
            {
                if (mineMatrix[x + 1, y] == -2)
                {
                    count++;
                }
            }
            //Bottom right corner
            if (x != rows – 1 && y != cols – 1)
            {
                if (mineMatrix[x + 1, y + 1] == -2)
                {
                    count++;
                }

            }
            //Left corner
            if (y != 0)
            {
                if (mineMatrix[x, y – 1] == -2)
                {
                    count++;
                }
            }
            //Right corner
            if (y != cols – 1)
            {
                if (mineMatrix[x, y + 1] == -2)
                {
                    count++;
                }
            }
            return count;
        }
       
        //Mark Mines
        public static void MarkMines(int[,] mineMatrix, int x, int y)
        {
            int rows = mineMatrix.GetLength(0);
            int cols = mineMatrix.GetLength(1);
            //Top left corner
            if (x != 0 && y != 0)
            {
                if (mineMatrix[x – 1, y – 1] == -1)
                {
                    mineMatrix[x – 1, y – 1] = -2;
                }
            }
            //Top corner
            if (x != 0)
            {
                if (mineMatrix[x – 1, y] == -1)
                {
                    mineMatrix[x – 1, y] = -2;
                }
            }
            //Top right corner
            if (x != 0 && y != cols – 1)
            {
                if (mineMatrix[x – 1, y + 1] == -1)
                {
                    mineMatrix[x – 1, y + 1] = -2;
                }
            }
            //Bottom left corner
            if (x != rows – 1 && y != 0)
            {
                if (mineMatrix[x + 1, y – 1] == -1)
                {
                    mineMatrix[x + 1, y – 1] = -2;
                }
            }
            //Bottom corner
            if (x != rows – 1)
            {
                if (mineMatrix[x + 1, y] == -1)
                {
                    mineMatrix[x + 1, y] = -2;
                }
            }
            //Bottom right corner
            if (x != rows – 1 && y != cols – 1)
            {
                if (mineMatrix[x + 1, y + 1] == -1)
                {
                    mineMatrix[x + 1, y + 1] = -2;
                }

            }
            //Left corner
            if (y != 0)
            {
                if (mineMatrix[x, y – 1] == -1)
                {
                    mineMatrix[x, y – 1] = -2;
                }
            }
            //Right corner
            if (y != cols – 1)
            {
                if (mineMatrix[x, y + 1] == -1)
                {
                    mineMatrix[x, y + 1] = -2;
                }
            }
        }
       
        //Count Near By Uncovered Cells
        public static int CountNearByUncoveredCells(int[,] mineMatrix, int x, int y)
        {
            int count = 0;
            int rows = mineMatrix.GetLength(0);
            int cols = mineMatrix.GetLength(1);
            //Top left corner
            if (x != 0 && y != 0)
            {
                if (mineMatrix[x – 1, y – 1] < 0)
                {
                    count++;
                }
            }
            //Top corner
            if (x != 0)
            {
                if (mineMatrix[x – 1, y] < 0)
                {
                    count++;
                }
            }
            //Top right corner
            if (x != 0 && y != cols – 1)
            {
                if (mineMatrix[x – 1, y + 1] < 0)
                {
                    count++;
                }
            }
            //Bottom left corner
            if (x != rows – 1 && y != 0)
            {
                if (mineMatrix[x + 1, y – 1] < 0)
                {
                    count++;
                }
            }
            //Bottom corner
            if (x != rows – 1)
            {
                if (mineMatrix[x + 1, y] < 0)
                {
                    count++;
                }
            }
            //Bottom right corner
            if (x != rows – 1 && y != cols – 1)
            {
                if (mineMatrix[x + 1, y + 1] < 0)
                {
                    count++;
                }

            }
            //Left corner
            if (y != 0)
            {
                if (mineMatrix[x, y – 1] < 0)
                {
                    count++;
                }
            }
            //Right corner
            if (y != cols – 1)
            {
                if (mineMatrix[x, y + 1] < 0)
                {
                    count++;
                }
            }
            return count;
        }

        //Count Near By Undetected Cells
        public static int CountNearByUndetectedCells(int[,] mineMatrix, int x, int y)
        {
            int count = 0;
            int rows = mineMatrix.GetLength(0);
            int cols = mineMatrix.GetLength(1);
            //Top left corner
            if (x != 0 && y != 0)
            {
                if (mineMatrix[x – 1, y – 1] == -1)
                {
                    count++;
                }
            }
            //Top corner
            if (x != 0)
            {
                if (mineMatrix[x – 1, y] == -1)
                {
                    count++;
                }
            }
            //Top right corner
            if (x != 0 && y != cols – 1)
            {
                if (mineMatrix[x – 1, y + 1] == -1)
                {
                    count++;
                }
            }
            //Bottom left corner
            if (x != rows – 1 && y != 0)
            {
                if (mineMatrix[x + 1, y – 1] == -1)
                {
                    count++;
                }
            }
            //Bottom corner
            if (x != rows – 1)
            {
                if (mineMatrix[x + 1, y] == -1)
                {
                    count++;
                }
            }
            //Bottom right corner
            if (x != rows – 1 && y != cols – 1)
            {
                if (mineMatrix[x + 1, y + 1] == -1)
                {
                    count++;
                }

            }
            //Left corner
            if (y != 0)
            {
                if (mineMatrix[x, y – 1] == -1)
                {
                    count++;
                }
            }
            //Right corner
            if (y != cols – 1)
            {
                if (mineMatrix[x, y + 1] == -1)
                {
                    count++;
                }
            }
            return count;
        }
       
        //Matrix Display
        public static void Display(int[,] tempMatrix)
        {
            int rows = tempMatrix.GetLength(0);
            int cols = tempMatrix.GetLength(1);
            //Displaying the reservoir matrix
            Console.WriteLine("Displaying reservoir matrix:");
            Console.WriteLine("============================");
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Console.Write(tempMatrix[i, j]);
                    Console.Write(" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("============================");
        }
    }
}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s