Wednesday, 17 April 2019



8 QUEENS program using the C#

                     In this page you can see how to make the 8 queens program in the c# language.

What is 8 queens game  :

      Every one knows how to play the chess in that most power full, dangerous and second most tallest piece in the chess board.

      And know you have to keep the 8 queens in the chess board so that non of the queen can kill the other one. There is a probability that you can keep only 6, 7 and 8 of them maximum number of placing the queens is 8. 

And u can see the code bellow.    







Bellow photo will be the actual output and when you gave the in put between 1 to 64 it will convert in to the rows and columns of the the chess board and replace the number that you gave as "8" and powers of the queen (All the vertical, horizontal and the diagonal rows will be placed with the "1")   



Code starts from here

using System;

namespace ConsoleApp17
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 0, b = 0;
            int[,] que = new int[8, 8];
         


            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    que[i, j] = 0;
                    Console.Write(" " + que[i, j]);
                }

                Console.WriteLine();

            }
           
            //for (int k1 = 8; k1 > 0; k1--)
                for (int k1 = 8; k1 > 0 ; k1--)
            {
                int k = 1;
               

                Console.WriteLine("Select a value between 1 to 64 to plase the qween ");
                Console.WriteLine("number of chancess left is : "+k1   );

                int n = Int32.Parse(Console.ReadLine());
                n = n - 1;
             

                if (n <= 64)
                {
                    b = n % 8;  // It will produce reminder and this will be the columns
                    a = n / 8;    // It will gives the rows for the chess board
                    Console.WriteLine(a);
                    Console.WriteLine(b);
                    int w = a;
                    int x = b;
                    int d = a;
                    int e = b;
                    int f = a;
                    int g = b;
                    int r = a;
                    int s = b;
                    int r1 = 1;
                    int s1 = b;
                    if (que[a, b] == 0)
                    {

                        que[a, b] = 8;                     // It will place the  "0" with the "8"


                        for (int j = 0; j < 8; j++)        // this if loop will place will block all the  columns
                        {
                            if (que[a, j] == 0)
                            {



                                if (j > 8)
                                {
                                    j = j % 8;
                                    //k = r1;
                                    que[a, j] = k;
                                }
                                else
                                {
                                    que[a, j] = k;
                                }

                            }
                        }




                        for (int i = 0; i < 8; i++)   // this if loop will place will block all the  horizontal placess
                        {
                            if (que[i, b] == 0)
                            {


                                if (i > 8)
                                {
                                    i = i % 8;
                                    que[i, b] = k;
                                }
                                else
                                {

                                    que[i, b] = k;
                                }

                            }
                        }
                       


                        // This part of the code will block all the diagonal placess in the the chess board
                       
                        if (a>b)
                        {
                         
                            if (a!=7)
                            {
                                for (int z = 0; z < 2; z++)
                                {

                                    if (z == 0)
                                    {

                                       for (int i = w + 1; i <= 7; ++i)
                                        {
                                            for (int j = x + 1; j < x + 2; ++j)
                                            {
                                                que[i, j] = k;
                                            }
                                            ++x;

                                        }

                                        for (int i = e - 1; i >= 0; i--)
                                        {
                                            for (int j = d - 1; j > d - 2; j--)
                                            {
                                                que[j, i] = k;
                                            }
                                            d--;

                                        }
                                       
                                        if (a + b > 7)
                                        {
                                         

                                            for (int i = g + 1; i <= 7; ++i)
                                            {

                                                for (int j = f - 1; j > f - 2; --j)
                                                {
                                                    que[j, i] = k;
                                                }
                                                --f;

                                            }

                                            for (int i = r + 1; i <= 7; ++i)
                                            {
                                                for (int j = s - 1; j > s - 2; --j)
                                                {
                                                    que[i, j] = k;
                                                }
                                                --s;

                                            }
                                        }

                                        if (a + b <= 7)
                                        {


                                            for (int i = f - 1; i >= 0; --i)
                                            {

                                                for (int j = g + 1; j < g + 2; ++j)
                                                {
                                                    que[i, j] = k;
                                                }
                                                ++g;

                                            }

                                            for (int i = s - 1; i >= 0; --i)
                                            {
                                                for (int j = r + 1; j < r + 2; ++j)
                                                {
                                                    que[j, i] = k;
                                                }
                                                ++r;

                                            }
                                        }
                                    }
                                   
                                   
                                }
                            }

                            else
                            {
                                for (int i = e - 1; i >= 0; i--)
                                {
                                    for (int j = d - 1; j > d - 2; j--)
                                    {
                                        que[j, i] = k;
                                    }
                                    d--;

                                }

                               
                                for (int i = s + 1; i <= 7; ++i)
                                {
                                        for (int j = r - 1; j > r - 2; --j)
                                        {
                                            que[j, i] = k;
                                        }
                                        --r;

                                }
                               
                            }

                        }
                       
                         if (a < b)
                        {
                         
                            if (b != 7)
                            {
                                for (int z = 0; z < 2; z++)
                                {

                                 
                                    if (z == 0)
                                    {
                                       
                                         for (int i = x+1; i <= 7; ++i)
                                         {
                                             for (int j = w+1; j < w + 2; ++j)
                                             {
                                                 que[j, i] = k;
                                             //   Console.WriteLine(que[i, j] = k);

                                            }
                                            ++w;

                                         }
                                     }

                                    if (z == 0)
                                    {
                                        if (a+b <= 7)
                                        {
                                            for (int i = f - 1; i >= 0; --i)
                                            {
                                                for (int j = g + 1; j < g + 2; ++j)
                                                {
                                                    que[i, j] = k;
                                                }
                                                ++g;

                                            }

                                            for (int i = s - 1; i >= 0; --i)
                                            {
                                                for (int j = r + 1; j < r + 2; ++j)
                                                {
                                                    que[j, i] = k;
                                                }
                                                ++r;

                                            }
                                        }

                                       else if(a+b>7)
                                        {
                                            for (int i = f + 1; i <= 7; ++i)
                                            {
                                                for (int j = g - 1; j > g - 2; --j)
                                                {
                                                    que[i, j] = k;
                                                }
                                                --g;

                                            }
                                            for (int i = s + 1; i <= 7; ++i)
                                            {
                                                for (int j = r - 1; j > r - 2; --j)
                                                {
                                                    que[j, i] = k;
                                                }
                                                --r;

                                            }
                                        }

                                     
                                    }


                                    else
                                    {
                                       




                                        for (int i = d - 1; i >= 0; i--)
                                        {
                                            for (int j = e - 1; j > e - 2; j--)
                                            {
                                                que[i, j] = k;
                                            }
                                            e--;
                                        }
                                       

                                     

                                    }
                                }
                            }

                            else
                            {
                               
                                for (int i = f + 1; i <= 7; ++i)
                                {
                                    for (int j = g - 1; j > g - 2; --j)
                                    {
                                        que[i, j] = k;
                                    }
                                    --g;

                                }

                                for (int i = d - 1; i >= 0; i--)
                                {
                                    for (int j = e - 1; j > e - 2; j--)
                                    {
                                        que[i, j] = k;
                                    }
                                    e--;
                                }


                            }
                           
                        }
                       
                       
                        else if (a == b)
                        {
                         
                            if (a != 7)
                            {
                                for (int z = 0; z < 2; z++)
                                {
                                 
                                    if (z == 0)
                                    {

                                         for (int i = w+1; i <= 7; ++i)
                                        {
                                            for (int j = x+1; j < x + 2; ++j)
                                            {
                                                que[i, j] = k;
                                            }
                                            ++x;
                                           
                                        }

                                        for (int i = d - 1; i >= 0; i--)
                                        {
                                            for (int j = e - 1; j > e - 2; j--)
                                            {
                                                que[i, j] = k;
                                            }
                                            e--;

                                        }

                                        if (a+b<=6)
                                        {
                                            for (int i = f - 1; i >= 0; --i)
                                            {
                                                for (int j = g + 1; j < g + 2; ++j)
                                                {
                                                    que[i, j] = k;
                                                }
                                                ++g;

                                            }

                                            for (int i = s - 1; i >= 0; --i)
                                            {
                                                for (int j = r + 1; j < r + 2; ++j)
                                                {
                                                    que[j, i] = k;
                                                }
                                                ++r;

                                            }
                                        }

                                        if (a+b>=8)
                                        {
                                            for (int i = f + 1; i <= 7; ++i)
                                            {
                                                for (int j = g - 1; j > g - 2; --j)
                                                {
                                                    que[i, j] = k;
                                                }
                                                --g;

                                            }
                                            for (int i = s + 1; i <= 7; ++i)
                                            {
                                                for (int j = r - 1; j > r - 2; --j)
                                                {
                                                    que[j, i] = k;
                                                }
                                                --r;

                                            }
                                        }
                                    }

                                 
                                }
                            }

                            else
                            {
                                for (int i = a - 1; i >= 0; --i)
                                {
                                    for (int j = b - 1; j > b - 2; --j)
                                    {
                                        que[i, j] = k;
                                    }
                                    --b;

                                }
                            }
                           
                        }

                        for (int i = 0; i < 8; i++)
                        {
                            for (int j = 0; j < 8; j++)
                            {
                                Console.Write(" " + que[i, j]);
                            }

                            Console.WriteLine();
                        }



                    }

                    else
                    {
                        Console.WriteLine("quween cannot be plased hear");
                        que[a, b] = que[a, b] - 8;
                        k1 = k1 - 1;
                    }

                 


                }


                else
                {
                    Console.WriteLine("Please enter the digits between 1 to 64");
                }

            }
        }
    }
}

No comments:

Post a Comment