8 QUEENS program using the JAVA
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
package com.company; import sun.awt.geom.AreaOp; import java.sql.SQLOutput; import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here /* System.out.println("enter digit :"); Scanner scanner = new Scanner(System.in); int A = scanner.nextInt(); System.out.println("digit :"+ A); */ int a = 0, b = 0; int[][] que = new int[8][8]; //It will keep all zero's in chess board for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { que[i][j] = 0; System.out.print(" " + que[i][j]); } System.out.println(); } for (int A=8;A>0;A--) { System.out.println("Enter the digit between 1 to 64 to place the Queen"); System.out.println("you have"+ A +"chances"); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); number = number - 1; // 'a' indicates the columns // 'b' indicates the rows a = number / 8; b = number % 8; if (number < 64) { if (que[a][b] == 0) { que[a][b] = 8; // It will place the "0" with the "8" int k = 1; for (int j = 0; j < 8; j++) // this if loop will place will block all the collems { if (que[a][j] == 0) { if (j > 8) { j = j % 8; 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; } } } if (a > b && a != 7) { AgreaterBse(a, b, que, k); AgreaterBnw(a, b, que, k); if (a + b > 7) { AgreaterBsw(a, b, que, k); AgreaterBne(a, b, que, k); } if (a + b < 7) { AgreaterBsw(a, b, que, k); AgreaterBne(a, b, que, k); } } else if (a > b && a == 7) { AgreaterBnw(a, b, que, k); AgreaterBne(a, b, que, k); } else if (a == b) { if (a != 7 && a != 0) { AgreaterBse(a, b, que, k); AgreaterBnw(a, b, que, k); if (a + b > 7) { AgreaterBsw(a, b, que, k); AgreaterBne(a, b, que, k); } if (a + b < 7) { AgreaterBsw(a, b, que, k); AgreaterBne(a, b, que, k); } } else if (a == 7) { AgreaterBnw(a, b, que, k); } else if (a == 0) { AgreaterBse(a, b, que, k); } } else if (a < b) { if (a + b >= 7) { AgreaterBse(a, b, que, k); AgreaterBnw(a, b, que, k); AgreaterBsw(a, b, que, k); AgreaterBne(a, b, que, k); } if (a + b < 7) { AgreaterBse(a, b, que, k); AgreaterBnw(a, b, que, k); AgreaterBsw(a, b, que, k); AgreaterBne(a, b, que, k); } } else if (a > b && a == 7) { AgreaterBnw(a, b, que, k); AgreaterBne(a, b, que, k); } } } else ; { System.out.println("Enter the digit between 1 to 64 to place the Queen"); } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { System.out.print(" " + que[i][j]); } System.out.println(); } } } public static void AgreaterBse(int a,int b, int [][] que, int k) { if (a>=b) { for (int i = a + 1; i <= 7; ++i) { for (int j = b + 1; j < b + 2; ++j) { que[i][j] = k; } ++b; } } else if(a<b) { for (int i = b+1; i <= 7; ++i) { for (int j = a+1; j < a + 2; ++j) { que[j][i] = k; } ++a; } } } public static void AgreaterBnw(int a,int b, int [][] que, int k) { if (a>=b) { for (int i = b - 1; i >= 0; i--) { for (int j = a - 1; j > a - 2; j--) { que[j][i] = k; } a--; } } else if (a<b) { for (int i = a - 1; i >= 0; i--) { for (int j = b - 1; j > b - 2; j--) { que[i][j] = k; } b--; } } } public static void AgreaterBne(int a,int b, int [][] que, int k) { if (a + b >= 7) { for (int i = b + 1; i <= 7; i++) { for (int j = a - 1; j > a - 2; j--) { que[j][i] = k; } a--; } } else { for (int i = a - 1; i >= 0; --i) { for (int j = b + 1; j < b + 2; ++j) { que[i][j] = k; } ++b; } } } public static void AgreaterBsw(int a,int b, int [][] que, int k) { if (a+b>=7) { for (int i = a + 1; i <= 7; ++i) { for (int j = b - 1; j > b - 2; --j) { que[i][j] = k; } --b; } } else { for (int i = b - 1; i >= 0; --i) { for (int j = a + 1; j < a + 2; ++j) { que[j][i] = k; } ++a; } } } }
No comments:
Post a Comment