Minesweeper Nearby Mine Counting - java

I'm trying to create a 10x10 Minesweeper-esque board made of _'s, with 10 mines randomly placed as *'s. No actual gameplay is involved, just the making of the board. I've been able to (somewhat) successfully place the mines randomly but I can't get the nearby mine counting aspect to work. I've tried many different things but this is what I've come up with so far.
import java.util.Random;
public class Mines {
public static final int BOARD_SIZE = 10;
enum Space {Empty, Mine, MineCount};
public static void main(String[] args) {
Random rand = new Random();
//Creates board
Space[][] board = new Space[BOARD_SIZE][BOARD_SIZE];
for (int y=0;y<board.length;y++)
{
for (int x=0;x<board.length;x++)
{
board[x][y] = Space.Empty;
}
}
System.out.println("Creating empty board");
//Draws the board
for(int y=0;y<board.length;y++)
{
for(int x=0;x<board.length;x++)
{
switch(board[y][x])
{
case Empty:
System.out.print("_");
break;
default:
System.out.println("?");
}
}
System.out.println();
}
System.out.println("Placing mines");
//Sets mines
for(int i=0;i<board.length;i++)
{
int mX = rand.nextInt(BOARD_SIZE);
int mY = rand.nextInt(BOARD_SIZE);
if(board[mX][mY] == Space.Empty)
{
board[mX][mY] = Space.Mine;
}
}
for(int y=0;y<board.length;y++)
{
for(int x=0;x<board.length;x++)
{
switch(board[y][x])
{
case Empty:
System.out.print("_");
break;
case Mine:
System.out.print("*");
break;
}
}
System.out.println();
}
//Count mines
System.out.println("Counting the mines");
//Prints board again
for(int y=0;y<board.length;y++)
{
for(int x=0;x<board.length;x++)
{
switch(board[y][x])
{
case Empty:
System.out.print("_");
break;
case Mine:
System.out.print("*");
break;
case MineCount:
int mineCount = 0;
if(board[x-1][y-1] == Space.Mine)
{
mineCount++;
board[y][x] = Space.MineCount;
System.out.print(mineCount);
}
}
}
System.out.println();
}
}
}

Try this code (Not an object oriented approach), but is easily understandable:
import java.util.Random;
public class Mines {
public static final int BOARD_SIZE = 5;
enum Space {
Empty, Mine
};
public static void main(String[] args) {
Random rand = new Random();
// Creates board
System.out.println("Empty board");
Space[][] board = new Space[BOARD_SIZE][BOARD_SIZE];
for (int y = 0; y < board.length; y++) {
for (int x = 0; x < board.length; x++) {
board[x][y] = Space.Empty;
System.out.print("_");
}
System.out.println();
}
// Sets mines
for (int i = 0; i < BOARD_SIZE; i++) {
int mX = rand.nextInt(BOARD_SIZE);
int mY = rand.nextInt(BOARD_SIZE);
// Condition if random number combination [mX, mY] generated previously. Guarantees BOARD_SIZE mines always.
if(Space.Mine.equals(board[mX][mY])) {
i--;
continue;
}
board[mX][mY] = Space.Mine;
}
System.out.println("\nPlacing mines");
for (int y = 0; y < board.length; y++) {
for (int x = 0; x < board.length; x++) {
switch (board[y][x]) {
case Empty :
System.out.print("_");
break;
case Mine :
System.out.print("*");
break;
}
}
System.out.println();
}
// Count mines
System.out.println("\nCounting mines");
for (int y = 0; y < board.length; y++) {
for (int x = 0; x < board.length; x++) {
if(Space.Mine.equals(board[y][x])) {
System.out.print("*");
} else {
System.out.print(findAdjCount(y, x, board));
}
}
System.out.println();
}
}
private static int findAdjCount(int row, int col, Space[][] board) {
int cnt = 0;
// Check 8 adjacent positions
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if(i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE) {
if(Space.Mine.equals(board[i][j])) {
cnt++;
}
}
}
}
return cnt;
}
}

Related

Creating a non-Attacking Queens game that is supposed to print out all 92 solutions of 8 queens on a chess board that cannot attack eachother

I made the 8x8 chess board and have a lot of the code done, but for some reason it only print out one solution, does anyone know why this may be and how I can fix it?
public class NonAttackingQueens {
private int[][] board;
private int solutionCount = 0;
private boolean solutionFound = false;
public NonAttackingQueens() {
board = new int[8][8];
}
public boolean canPlace(int x, int y) {
// Check if a queen is already placed at position (x, y)
if (board[x][y] == 1) {
return false;
}
// Check horizontal positions
for (int i = 0; i < 8; i++) {
if (board[x][i] == 1) {
return false;
}
}
// Check vertical positions
for (int i = 0; i < 8; i++) {
if (board[i][y] == 1) {
return false;
}
}
// Check diagonal positions
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 1 && (Math.abs(i - x) == Math.abs(j - y))) {
return false;
}
}
}
return true;
}
public void solve() {
// Check if the solutionCount has reached 92
if (solutionCount == 92) {
return;
}
// Check if all 8 queens have been placed
int queensPlaced = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 1) {
queensPlaced++;
}
}
}
if (queensPlaced == 8) {
// All positions have been checked, so we have found a solution
solutionCount++;
System.out.println("Solution " + solutionCount + ":");
print();
return;
}
// Try to place a queen at each position on the board
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (canPlace(i, j)) {
// Place a queen at position (i, j) and try to solve the rest of the board
board[i][j] = 1;
solve();
// Backtrack: remove the queen from position (i, j) and try the next position
board[i][j] = 0;
}
}
}
}
public void print() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 1) {
System.out.print(" X");
} else {
System.out.print(" O");
}
}
System.out.println();
}
System.out.println("---------------");
}
}
I'm doing this in blueJ, so I tried to run the void solve(); method and it runs, but it only prints out the first of 92 solutions 92 times. It should print out all 92 different solutions.

How I can read how many mines are around each empty cell.Game minesweeper

The program below ask the user how many mines he wants to see on the field and then display the field with mines.
In next step I need to calculate how many mines are around each empty cell. And I know that I
need to check 8 cells if the cell is in the middle, 5 cells if the cell is in the side, and 3
cells if the cell is in the corner. If there are from 1 to 8 mines around the cell, I need to
output the number of mines instead of the symbol representing an empty cell.
import java.util.Scanner;
import java.util.Random;
public class Minesweeper {
char[][] minesweeper = new char[9][9];
Random randNum = new Random();
Scanner sc = new Scanner(System.in);
public Minesweeper() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
minesweeper[i][j] = '*';
}
}
}
public void printMinesweeper() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(minesweeper[i][j]);
}
System.out.println();
}
}
public void randomX() {
System.out.print("How many mines do you want on the field?: ");
int numberOfMines = sc.nextInt();
int i = 0;
while (i < numberOfMines) {
int x = randNum.nextInt(9);
int y = randNum.nextInt(9);
if (minesweeper[x][y] == '*') {
minesweeper[x][y] = 'X';
i++;
}
}
printMinesweeper();
}
}
You can do it like this:
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
public static void main(String[] args) {
Minesweeper minesweeper = new Minesweeper();
minesweeper.randomX();
minesweeper.printMinesweeper();
}
char[][] minesweeper = new char[9][9];
Random randNum = new Random();
Scanner sc = new Scanner(System.in);
public Minesweeper() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
minesweeper[i][j] = '*';
}
}
}
public void printMinesweeper() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(getCharAt(i, j));
}
System.out.println();
}
}
private String getCharAt(int i, int j) {
if (mineAt(i, j)) {
return "X";
}
int minesNear = countMinesNear(i, j);
return Integer.toString(minesNear);
}
private boolean mineAt(int i, int j) {
return minesweeper[i][j] == 'X';
}
private int countMinesNear(int i, int j) {
int mines = 0;
for (int x = -1; x <= 1; x++) {//near fields in x direction
for (int y = -1; y <= 1; y++) {//near fields in y direction
if (x + i >= 0 && x + i < minesweeper.length && y + j >= 0 && y + j < minesweeper.length) {//check whether the field exists
if (minesweeper[x+i][y+j] == 'X') {//check whether the field is a mine
mines++;
}
}
}
}
return mines;
}
public void randomX() {
System.out.print("How many mines do you want on the field?: ");
int numberOfMines = sc.nextInt();
int i = 0;
while (i < numberOfMines) {
int x = randNum.nextInt(9);
int y = randNum.nextInt(9);
if (minesweeper[x][y] == '*') {
minesweeper[x][y] = 'X';
i++;
}
}
printMinesweeper();
}
}
The countMinesNear(int, int) method check whether the field near exists (to prevent index errors on the edges) and counts the mines if the fields exist.

Print star pattern based on coordinate

I am new to java, I want to print a reversed star pattern based on the coordinate. After I set the coordinate, it will then print the star pattern
import java.util.Scanner;
public class coorTest {
public static void main(String[] args) {
int max = 5;
for (int y = 0; y < max; y += 2) {
int left_spacing = (int) Math.floor(y * 1.0 / 2.0);
for (int space = 0; space < left_spacing; space++) {
System.out.print(" ");
}
for (int x = 0; x < (max - y); x++) {
System.out.print("x");
}
System.out.println();
}
}
}
Try this. I am using (0,0) as a first coordinate.
public static void printStar(int x, int y) {
int starCount = 5;
int space = 0;
int count = 0;
// y-axis line move
for(int i=0; i<y; i++) {
System.out.println();
}
for (int i = 0; i < starCount; i++) {
// x-axis space move
for(int xAxis=0; xAxis<x; xAxis++) {
System.out.print(" ");
}
for (int j = 0; j < starCount; j++) {
if (count < space) {
System.out.print(" ");
count++;
continue;
} else if (j >= starCount - count) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
space++;
count = 0;
}
}

Eight Queens Array out of bounds (Java)

I'm trying to make a program that does Eight Queens with recursion but I keep on getting an array out of bounds error. I've been having problems with this for awhile now and I can't really seem to pinpoint the problem. Here's my code:
public class Queens {
public int currColumn = 0;
public static final int BOARD_SIZE = 8;
public static final int EMPTY = 0;
public static final int QUEEN = 1;
private int board[][];
public Queens() {
board = new int[BOARD_SIZE][BOARD_SIZE];
}
public void clearBoard() {
for (int x = 0; x <= BOARD_SIZE; x++) {
for (int y = 0; y <= BOARD_SIZE; y++) {
board[x][y] = 0;
}
}
}
public void displayBoard() {
for (int x = 0; x < BOARD_SIZE; x++) {
System.out.print("\n");
for (int y = 0; y < BOARD_SIZE; y++) {
System.out.print(board[x][y]);
}
}
}
public boolean placeQueens(int column) {
if (column > BOARD_SIZE) {
return true;
} else {
boolean queenPlaced = false;
int row = 1;
while (!queenPlaced && (row <= BOARD_SIZE)) {
if (isUnderAttack(row, column)) {
++row;
} else {
setQueen(row, column);
queenPlaced = placeQueens(column + 1);
if (!queenPlaced) {
removeQueen(row, column);
++row;
}
}
}
return queenPlaced;
}
}
public void setQueen(int row, int column) //SET BACK TO PRIVATE
{
board[row][column] = 1;
}
private void removeQueen(int row, int column) {
board[row][column] = 0;
}
private boolean isUnderAttack(int row, int column) {
if (column == 0) {
return false;
}
int prevColumn = column - 1;
int prevRow = index(prevColumn);
while (prevColumn >= 0) {
prevRow = index(prevColumn);
for (int i = 0; i > BOARD_SIZE; i++) {
if (prevRow == row && prevColumn + i == column) //Going right
{
return true;
}
if (prevRow + i == row && prevColumn + i == column) //Going up/right
{
return true;
}
if (prevRow - i == row && prevColumn + i == column) //Going down/right
{
return true;
}
}
prevColumn--;
}
return false;
}
public int index(int column) //BACK TO PRIVATE
{
for (int i = 0; i < 8; i++) {
if (board[i][column] == 1) {
return i;
}
}
return 0;
}
public static void main(String[] args) {
Queens x = new Queens();
if (x.placeQueens(1) == true) {
x.displayBoard();
} else {
System.out.println("No solution found");
}
}
}
First Problem:
public void clearBoard()
{
for(int x = 0; x < BOARD_SIZE; x++) // changed from x <= BOARD_SIZE
{
for(int y = 0; y < BOARD_SIZE; y++) // changed from y <= BOARD_SIZE
{
board[x][y] = 0;
}
}
}
Array index counting starts from 0 upto size-1. There is nothing in board[8][8].
Another Problem
/* ... */
while(prevColumn >= 0)
{
prevRow = index(prevColumn);
for(int i = 0; i > BOARD_SIZE; i++) // i=0; i> BOARD_SIZE is always false, so no looping mechanism
{ /* ... */ }
Change it to
for(int i = 0; i < BOARD_SIZE; i++) // corrected
Another Problem ?
if(column > BOARD_SIZE) when `column = 8`
Make it
if(column >= BOARD_SIZE)
Another Problem:
while(!queenPlaced && (row <= BOARD_SIZE))
Make it
row < BOARD_SIZE
Yet Another ?
queenPlaced = placeQueens(column + 1); // What if column = 7
for(int x = 0; x <= BOARD_SIZE; x++)
Change that to:
for(int x = 0; x < BOARD_SIZE; x++)
Since arrays are 0-indexed.
You try to access more elements than the declared array actually has:
// this means you can access index in [0, BOARD_SIZE - 1].
board = new int[BOARD_SIZE][BOARD_SIZE];
// this means you access index in [0, BOARD_SIZE].
for(int x = 0; x <= BOARD_SIZE; x++)
So, a good practice is to always use < when referring to the declared size of the array.
In java array indexes starts from zero.
for(int x = 0; x <= BOARD_SIZE; x++)
should be
for(int x = 0; x < BOARD_SIZE; x++)
{
for(int y = 0; y < BOARD_SIZE; y++)
{
board[x][y] = 0;
}
Your BOARD_SIZE is 8 .so array initialized for 8 elelements. So avaliable indexes are 0 to 7.
in loop
for(int x = 0; x <= BOARD_SIZE; x++)
when x=8 ArrayIndexOutOfBound Exception throws.
Check this point in all your arrays,where you are looping.

Recursive solution to Sudoku generator

I'm trying to code an algorithm that creates a legal Sudoku board in either Java or Javascript. Neither work, and I'm not entirely sure why.
Essentially, the problem in both programs is that either x or y is getting incremented more than it should (skipping the square). I can't for the life of me figure out how this is happening. I can provide the HTML that completes the JS solution if need be.
My best guess is it has to do with how I've created a stack using recursion, but as far as I can tell, it should work.
In my old code there was an incorrect for loop, I'm aware of this. I pasted an old version, it's fixed now.
Java:
import java.util.*;
public class SudokuGenerator
{
//credit:cachao
//http://stackoverflow.com/questions/9959172/recursive-solution-to-sudoku-generator
public static final int BOARD_WIDTH = 9;
public static final int BOARD_HEIGHT = 9;
public SudokuGenerator() {
board = new int[BOARD_WIDTH][BOARD_HEIGHT];
}
//Recursive method that attempts to place every number in a square
public int[][] nextBoard()
{
nextBoard(0,0);
return board;
}
public void nextBoard(int x, int y)
{
int nextX = x;
int nextY = y;
//int[] toCheck = Collections.shuffle(Arrays.asList({1,2,3,4,5,6,7,8,9}));
int[] toCheck = {1,2,3,4,5,6,7,8,9};
Collections.shuffle(Arrays.asList(toCheck));
for(int i=0;i<toCheck.length;i++)
{
if(legalMove(x, y, toCheck[i]))
{
board[x][y] = toCheck[i];
if(x == 8)
{
if(y == 8)
break;//We're done! Yay!
else
{
nextX = 0;
nextY++;
}
}
else
{
nextX++;
}
nextBoard(nextX, nextY);
}
}
board[x][y] = 0;
}
public boolean legalMove(int x, int y, int current) {
for(int i=0;i<9;i++) {
if(current == board[x][i])
return false;
}
for(int i=0;i<9;i++) {
if(current == board[i][y])
return false;
}
int cornerX = 0;
int cornerY = 0;
if(x > 2)
if(x > 5)
cornerX = 6;
else
cornerX = 3;
if(y > 2)
if(y > 5)
cornerY = 6;
else
cornerY = 3;
for(int i=cornerX;i<10 && i<cornerX+3;i++)
for(int j=cornerY;j<10 && j<cornerY+3;j++)
if(current == board[i][j])
return false;
return true;
}
public void print()
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
System.out.print(board[i][j] + " ");
System.out.println();
}
}
public static void main(String[] args)
{
SudokuGenerator sg = new SudokuGenerator();
sg.nextBoard();
sg.print();
}
int[][] board;
}
Javascript:
//Recursive method that attempts to place every number in a square
function driver()
{
board = new Array(10);
for(var i=0;i<9;i++)
board[i] = new Array(10);
nextBoard(0,0);
print();
}
function nextBoard(x, y)
{
var nextX = x;
var nextY = y;
for(var i=1;i<10;i++) {
console.log(y + " " + x + " " + i);
document.getElementById(y + " " + x).innerHTML = i;
if(legalMove(x, y, i)) {
board[x][y] = i;
if(x === 8) {
if(y === 8)
return board;//We're done! Yay!
else {
nextX = 0;
nextY++;
}
}
else
nextX++;
nextBoard(nextX, nextY);
}
}
//This is needed for legalMove to work, otherwise [x][y] == 9
board[x][y] = undefined;
}
function legalMove(x, y, current) {
for(var i=0;i<9;i++) {
if(current === board[x][i])
return false;
}
for(var i=0;i<9;i++) {
if(current === board[i][y])
return false;
}
var cornerX = 0;
var cornerY = 0;
if(x > 2)
if(x > 5)
cornerX = 6;
else
cornerX = 3;
if(y > 2)
if(y > 5)
cornerY = 6;
else
cornerY = 3;
for(var i=cornerX;i<10 && i<cornerX+3;i++)
for(var j=cornerY;j<10 && j<cornerY+3;j++)
if(current === board[i][j])
return false;
return true;
}
function print() {
for(var i=0;i<9;i++)
for(var j=0;j<9;j++)
{
document.getElementById(i + " " + j).innerHTML = board[i][j];
console.log(board[i][j]);
}
}
var board;
In the Java code:
I'll translate it to psuedocode:
for all z values:
If for current (x,y), the number 'z' is legal then:
insert z to current (x,y)
if finished
hooray!
else
go to next square
else try next number
But what if you can't put any number there as it ends up being illegal (aka a board where you can't insert any number in a specific square)?
You don't address that. What you need to do is implement it via backtracking:
for all z values:
If for current (x,y) the number 'z' is legal then:
insert z to current (x,y)
go to next(x,y)
try to complete the board // recursive call
if you completed the board // == the result of the recursion is legal
return the completed board
If all z values have been attempted
return "cannot complete board"
increment z, try again with current (x,y)
Java:
You should initialize your board variable, you may want to initialize it in a constructor:
public class SudokuGenerator {
public static final int BOARD_WIDTH = 9;
public static final int BOARD_HEIGHT = 9;
public SudokuGenerator() {
board = new int[BOARD_WIDTH][BOARD_HEIGHT];
}
}
I believe that your loop iterator in the function nextBoard it is wrong:
for(int i=1;i<10;i++){ ... }
I think that you want to iterate from 0 to 9.
In the function nextBoard, you also need to check the variable:
int[] toCheck = {1,2,3,4,5,6,7,8,9};
You get an java.lang.ArrayIndexOutOfBoundsException, you should initialize it from 0 to 8, otherwise you try to access the board row number 9 and you get a runtime error.
Another problem that you need to solve is that x is being set to nine in nextBoard() function. Call the function nextBoard(int x, int y) "manually" with these parameteres: nextBoard(7,3) and you will understand why x is being set to nine. Check specifically the values of the variable nextX.
I believe it will really help you if you use a debugger to check this kind of errors, here you have a nice tutorial with a video explanation(in case your are using the Eclipse IDE).
Hope it helps.
Java:
Your loop iterator in nextBoard range from 1 to 9. I don't think you meant that. Same in the function legalMove.... initialize cornerX and cornerY to 0.
interesting question, I just noticed this one bug in the Java code: isn't the call to Collection.shuffle() useless since the toCheck array will remain unmodifed (unshuffled) after this call? Here is my quick fix (and I'm sure there are more clever ways to do it):
List<Integer> lst = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Collections.shuffle(lst);
for (int i=0; i<lst.size(); i++)
toCheck[i] = lst.get(i);
In Java array indexes are zero-based. In nextBoard you loop over 1..9 for i and use it as an index into toCheck which will skip the first element at index 0 and go past the end of the array. This will throw ArrayIndexOutOfBoundsException if the line containing toCheck[i] is reached with i equal to 9.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SudokuNrupen {
public static int[][] p;
public static int tmp[] ;
public static int tmp2D[][] ;
public static void main(String[] args){
tmp = new int[9];
tmp2D = new int[3][3];
p = new int[9][9];
System.out.print("Enter the name of he file below ");
Scanner scan = new Scanner (System.in);
String name = scan.nextLine();
File file = new File( name );
if ( file.exists()){
try {
Scanner inFile = new Scanner( file );
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
if(inFile.hasNextInt()){
p[i][j] = inFile.nextInt();
}
}
}
inFile.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(SudokuNrupen.class.getName()).log(Level.SEVERE, null, ex);
}
}
display(p);
solve(p);
System.out.println("Solved Sudoku is:");
display(p);
}
public static void display(int [][] p)
{
for(int i=0; i<p.length;i++)
{
for(int j=0; j<p[i].length;j++)
{
System.out.print(" ");
if(p[i][j]<10) System.out.print(p[i][j] + " ");
else System.out.print(p[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
public static boolean solve(int [][] p)
{
if(!isValidSudoku(p))
{
return false;
}
if(isComplete(p)==true)
{
return true;
}
for(int i=0; i<9; i++)
{
for(int j=0 ; j<9 ; j++)
{
if(p[i][j]==0)
{
int k=1;
while(k<=9)
{
p[i][j]=k;
if(solve(p))
{
return true;
}
else k++;
}
p[i][j]=0;
return false;
}
}
}
return true;
}
public static boolean isComplete(int [][]p)
{
for(int i=0; i<9; i++)
{
for(int j=0 ; j<9 ; j++)
{
if(p[i][j]==0){
return false;
}
}
}
return true;
}
public static boolean isRepeated(int [] a)
{
for(int i=0; i<8; i++)
{
if((a[i]!=0 || a[i+1]!=0))
{
if(a[i]==a[i+1]){
return true;
}
}
}
return false;
}
public static boolean isDuplicateEx0(int [][]p)
{
for(int i=0; i<p[0].length; i++)
{
for(int j=0 ; j<9 ; j++)
{
tmp[j]=p[i][j];
}
Arrays.sort(tmp);
System.out.println(Arrays.toString(tmp));
if(isRepeated(tmp)==true)
{
System.out.println("Duplicates are found in row");
return true;
}
}
display(p);
for(int j=0; j<p[0].length; j++)
{
for(int i=0 ; i<9 ; i++)
{
tmp[i]=p[i][j];
}
Arrays.sort(tmp);
if(isRepeated(tmp)==true)
{
System.out.println("Duplicates are found in columns");
return true;
}
}
display(p);
for(int z=0;z<9;z++){
tmp[z]=0;
}
int x=0,y=0;
for(int i=0; i<3;i++)
{
y=0;
for(int j=0;j<3;j++)
{
tmp2D[x][y]=p[i][j];
y++;
}
x++;
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tmp[(i*tmp2D.length) + j] = tmp2D[i][j];
}
}
Arrays.sort(tmp);
if(isRepeated(tmp)==true)
{
return true;
}
for(int z=0;z<9;z++){
tmp[z]=0;
}
x=0;
y=0;
for(int i=0; i<3;i++)
{
y=0;
for(int j=3;j<6;j++)
{
tmp2D[x][y]=p[i][j];
y++;
}
x++;
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tmp[(i*tmp2D.length) + j] = tmp2D[i][j];
}
}
Arrays.sort(tmp);
if(isRepeated(tmp)==true)
{
return true;
}
for(int z=0;z<9;z++){
tmp[z]=0;
}
x=0;
y=0;
for(int i=0; i<3;i++)
{
y=0;
for(int j=6;j<9;j++)
{
tmp2D[x][y]=p[i][j];
y++;
}
x++;
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tmp[(i*tmp2D.length) + j] = tmp2D[i][j];
}
}
Arrays.sort(tmp);
if(isRepeated(tmp)==true)
{
return true;
}
for(int z=0;z<9;z++){
tmp[z]=0;
}
x=0;
y=0;
for(int i=3; i<6;i++)
{
y=0;
for(int j=0;j<3;j++)
{
tmp2D[x][y]=p[i][j];
y++;
}
x++;
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tmp[(i*tmp2D.length) + j] = tmp2D[i][j];
}
}
Arrays.sort(tmp);
if(isRepeated(tmp)==true)
{
return true;
}
for(int z=0;z<9;z++){
tmp[z]=0;
}
x=0;
y=0;
for(int i=3; i<6;i++)
{
y=0;
for(int j=3;j<6;j++)
{
tmp2D[x][y]=p[i][j];
y++;
}
x++;
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tmp[(i*tmp2D.length) + j] = tmp2D[i][j];
}
}
Arrays.sort(tmp);
if(isRepeated(tmp)==true)
{
return true;
}
for(int z=0;z<9;z++){
tmp[z]=0;
}
x=0;
y=0;
for(int i=3; i<6;i++)
{
y=0;
for(int j=6;j<9;j++)
{
tmp2D[x][y]=p[i][j];
y++;
}
x++;
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tmp[(i*tmp2D.length) + j] = tmp2D[i][j];
}
}
Arrays.sort(tmp);
if(isRepeated(tmp)==true)
{
return true;
}
for(int z=0;z<9;z++){
tmp[z]=0;
}
x=0;
y=0;
for(int i=6; i<9;i++)
{
y=0;
for(int j=0;j<3;j++)
{
tmp2D[x][y]=p[i][j];
y++;
}
x++;
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tmp[(i*tmp2D.length) + j] = tmp2D[i][j];
}
}
Arrays.sort(tmp);
if(isRepeated(tmp)==true)
{
return true;
}
for(int z=0;z<9;z++){
tmp[z]=0;
}
x=0;
y=0;
for(int i=6; i<9;i++)
{
y=0;
for(int j=3;j<6;j++)
{
tmp2D[x][y]=p[i][j];
y++;
}
x++;
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tmp[(i*tmp2D.length) + j] = tmp2D[i][j];
}
}
Arrays.sort(tmp);
if(isRepeated(tmp)==true)
{
return true;
}
for(int z=0;z<9;z++){
tmp[z]=0;
}
x=0;
y=0;
for(int i=6; i<9;i++)
{
y=0;
for(int j=6;j<9;j++)
{
tmp2D[x][y]=p[i][j];
y++;
}
x++;
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tmp[(i*tmp2D.length) + j] = tmp2D[i][j];
}
}
Arrays.sort(tmp);
if(isRepeated(tmp)==true)
{
return true;
}
return false;
}
public static boolean isValidSudoku(int [][] p)
{
return (!isDuplicateEx0(p));
}
}

Categories

Resources