I'm trying to get better at java by making a Sudoku puzzle, however I'm running in to some issues. As it currently stands, if I put any "illegal" input in the Sudoku, it just continues, but I'm trying to make the script ask for a value again if it is "illegal". As seen below in the code, it is when my public boolean "moves" is false I'm trying to start the while loop once again.
public static void main(String[] args) throws Exception {
Sudoku s = new Sudoku("C:\\Users\\caspe\\Downloads\\Sudoku001.sdk");
s.printBoard();
s.errorCheck();
s.getNum();
while(getNum() > 0) {
System.out.println("Next move, please (row , column , value )");
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt();
int column = scanner.nextInt() ;
int value = scanner.nextInt();
if (s.moves(row, column, value) == false); {
//Try again
}
s.printBoard();
}
}
I still can't figure out an solution after several different attempts, so help is very much appreciated!
This is my getNum method for further understanding:
public static int getNum() {
int getNumb = 0;
for(int j = 0; j < list.size(); j++) {
for (int i=0; i < list.get(j).length(); i++) {
if(list.get(i).charAt(j) == '.') {
getNumb++;
}
}
}
return getNumb;
}
The way you have written your code it is going to print the Sudoku board whether s.move is true or false. The way you would fix that is by add in an else statement to your if statement.
For example:
if(s.moves(row, column, value) == false){
//print error message
}else {
s.printBoard();
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Here's a little code I wrote; in short, I'm plain stuck and can't figure out what Im doing wrong. Basically what my intention for the code is, is to check my boolean array; find out if true is listed more consecutive or if false is. False is of course listed more so it then should return false to my main method.
public class FalseBoolean
{
public static void main(String [] args) {
boolean[] guess = {false,true,false,false,false,true,true};
boolean result = longerTF(guess);
}
public static boolean longerTF(boolean[] guess) {
int variableTrue = 0;
int variableFalse = 0;
for(int x = 0; x < guess.length; x++) {
if(guess[x] == true) {
variableTrue++;
} else {
variableFalse++;
}
return variableFalse;
}
}
}
The immediate problem should be returning a boolean, not an integer. You can do this by simply checking which count is greatest.
Your second problem is to return after you looked at the entire array, not just the first element.
public static boolean longerTF(boolean[] guess) {
int variableTrue = 0;
int variableFalse = 0;
for(int x = 0; x < guess.length; x++) {
if(guess[x]) {
variableTrue++;
} else {
variableFalse++;
}
}
return variableTrue >= variableFalse;
}
Note: you only need one counter for an array of two possible values ... For example,
int variableFalse = guess.length - variableTrue;
Your question asked for consecutive elements, but this code only returns which occurs the most in the entire array, so keep working on the logic
Try This(Made some logic change)
public class FalseBoolean
{
public static void main(String [] args)
{
boolean[] guess =
{false,true,true,true,true,false,false,false,true,true};
boolean result = longerTF(guess);
System.out.println(result);
}
public static boolean longerTF(boolean[] guess)
{
int consecutiveVariableTrue = 0, maxConsecutiveVariableTrue = 0;
int consecutiveVariableFalse = 0, maxConsecutiveVariableFalse = 0;
for(int x = 0; x < guess.length; x++)
{
if(guess[x] == true) {
consecutiveVariableTrue++;
if (maxConsecutiveVariableTrue < consecutiveVariableTrue)
maxConsecutiveVariableTrue = consecutiveVariableTrue;
} else {
consecutiveVariableTrue = 0;
}
}
for(int x = 0; x < guess.length; x++)
{
if(guess[x] == false) {
consecutiveVariableFalse++;
if (maxConsecutiveVariableFalse < consecutiveVariableFalse)
maxConsecutiveVariableFalse = consecutiveVariableFalse;
} else {
consecutiveVariableFalse = 0;
}
}
if (maxConsecutiveVariableTrue >= maxConsecutiveVariableFalse) {
return true;
}
return false;
}
}
You are trying to return an int variable when you obviously declared your method as a boolean type. Do this instead:
if(variableFalse > variableTrue){
return false;
}else if(variableTrue > variableFalse){
return true;
}else{
//Default return statement
}
Also, in this specific case (because you are processing an array), don't include the return statement in the for loop.
I would explain this better if I wasn't using mobile :P
getting compile errors and just cannot get this code to work the way I need it to, it's beginning to drive me mad! Basically I am trying to get my boolean array in my method to iterate through the array, find out if False is listed more consecutively or if True is and return either true or false. In my program the array i have listed should return false. Any ideas? thanks in advance.
public class trueOrfalse
{
public static void main(String [] args)
{
boolean[] guess = {false,true,false,false,false,true,true};
boolean result = longerTF(guess);
}
public static boolean longerTF(boolean[] guess)
{
int variable = 0;
for(int x = 0; x < guess.length; x++)
{
if(guess[x] > true)
{
return true;
}
else
{
return false;
}
}
}
i don't know where to start
first of all this is wrong
if(guess[x] > true)
it should be
if(guess[x]==true)
since an if statement expects a boolean result and you have boolean values in your array this will have the same effect
if(guess[x])
you also missed a case. when the array is empty you would never run into the for loop, but your method still needs to return a boolean value. you could throw a runtime-exception or a default value like return false; at the end of your method
your for-loop does not make sense, since your method will return a result within the first iteration (if the array is not empty). your longerTF method could be also looking like this
public static boolean longerTF(boolean[] guess) {
if(guess.length>0)
return guess[0];
throw new IllegalArgumentException("The array must not be empty");
}
i'd suggest a general book like "programming: Learn the Fundamentals of Computer Programming Languages". you need to understand first the basics of programming before you try to implement anything.
The comparison​ operators >, etc., are neither legal nor meaningful for boolean operands. What did you intend guess[x] > true to accomplish?
Since guess is a boolean[] you are allowed to test
if (guess[x])
or
if (! guess[x])
and to
return guess[x];
EDIT
You want the loop to count consecutive values. This loop does not, but it shows how such a structure works for a simpler problem.
public boolean dominant(boolean[] guess) {
int tCount = 0;
for (int ix = 0; ix < guess.length; ++ix) {
if (guess[ix]) {
++tCount;
}
}
return tCount >= guess.length / 2;
}
Here is a "corrected" version:
public class trueOrfalse {
public static void main(String[] args) {
boolean[] guess = { false, true, false, false, false, true, true };
boolean result = longerTF(guess);
}
public static boolean longerTF(boolean[] guess) {
int variable = 0;
for (int x = 0; x < guess.length; x++) {
if (guess[x]) {
variable++;
}
}
return (variable > (guess.length-variable));
}
}
You forgot one closing braket }, a return statement and boolean compare cannot be with < or >.
As mentioned by everyone above. You can't use > to compare two booleans.
For your purpose to count the number of consecutive true/false, you need two different counters. You can run a loop and keep incrementing the counter as you encounter repeated item of true/false, if not you can just reset the counter to 1. I have put on a hasty solution below to give you an idea. I haven't tested it well it seems to work. Hope this helps.
public class trueOrfalse {
public static void main(String[] args) {
boolean[] guess = { false,true,false,false,false,true,true };
boolean result = longerTF(guess);
System.out.println("result: " +result);
}
public static boolean longerTF(boolean[] guess) {
int consecutiveFalseCount = 1;
int consecutiveTrueCount = 1;
for (int x = 0; x < guess.length; x++) {
if (guess[x] == true) {
if(x!=0 && x<guess.length){
if(guess[x-1] == true){
consecutiveTrueCount = consecutiveTrueCount + 1;
} else {
consecutiveTrueCount = 1;
}
}
} else {
if(x!=0 && x<guess.length-1){
if(guess[x-1] == false){
consecutiveFalseCount = consecutiveFalseCount + 1;
} else {
consecutiveFalseCount = 1;
}
}
}
}
System.out.println("Consecutive True count: " +consecutiveTrueCount);
System.out.println("Consecutive False count: " +consecutiveFalseCount);
if(consecutiveTrueCount>consecutiveFalseCount){
return true;
} else {
return false;
}
}
}
I am working on a little personal Sudoku and trying to expand it.
So far I got the "Solve" part working, using a recursive backtracking method, which returns true whenever it manages to solve the recursion.
Now I am trying to build a unique solution board generator, and I've found quite a bit of info online about how it can be implemented.
However, I am struggling on the first step, which is my boolean recursive backtracking algorithm into a recursive algorithm that keeps a count of a possible solutions. This is essential to check whether my generated board is unique.
On a larger note, I've realized that I've struggled with this problem before when implementing some recursive sorts: How to transform a boolean recursive function into a recursive function that returns some kind of count (int/long), without losing the functionality? Is there any sort of guidelines or technique to follow?
Attached is the working code so far.
import java.util.Scanner;
public class Sudoku {
int[][] board;
public Sudoku(){}
public Sudoku(int n){
this.board=new int[n][n];
}
/* Creates an NxN game.board in a two-dimensional array*/
public static int[][] createBoard(int n)
{
int[][] board = new int[n][n];
for (int i=0; i<board.length; i++)
for (int j=0; j<board[i].length; j++)
board[i][j]=0;
return board;
}
/* prints the game.board*/
public static void printBoard(int[][] b)
{
int buffer=(int)Math.sqrt(b.length);
// fitting the bottom line into any size of game.board
String btm=new String(new char[buffer*buffer*3+buffer+1]).replace("\0", "_");
for (int i=0; i<b.length; i++)
{
if (i%buffer==0)
System.out.println(btm);
for (int j=0; j<b[i].length; j++)
{
if (j%buffer==0)
System.out.print("|");
if (b[i][j]==0)
System.out.print(" _ ");
else
System.out.print(" " + b[i][j] + " ");
}
System.out.println("|");
}
System.out.println(btm);
}
/* returns true if a number can be inserted in a row, otherwise returns false. */
public static boolean checkLegalRow(int[][] b, int row, int num)
{
for (int i=0; i<b.length; i++)
{
if (b[row][i]==num)
return false;
}
return true;
}
/* returns true if a number can be inserted in a column, otherwise returns false.*/
public static boolean checkLegalCol(int[][] b, int col, int num)
{
for (int i=0; i<b.length; i++)
{
if (b[i][col]==num)
return false;
}
return true;
}
/*returns true if number can be inserted in its local box.*/
public static boolean checkLegalBox(int[][] b, int row, int col, int num)
{
int buffer=(int)Math.sqrt(b.length);
for (int i=0, adjRow=row-(row%buffer); i<buffer; i++, adjRow++)
{
for (int j=0, adjCol=col-(col%buffer); j<buffer; j++, adjCol++)
{
if (b[adjRow][adjCol]==num)
return false;
}
}
return true;
}
/*allows user input for a sudoku game.board*/
public static void fillInBoardConsole(int[][] b)
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a row: ");
int r=sc.nextInt();
System.out.print("Please enter a column: ");
int c=sc.nextInt();
System.out.print("Please enter a number from 1 to "+b.length+": ");
int num=sc.nextInt();
while (num>b.length || num<1)
{
System.out.print("Please enter a number from 1 to "+b.length+": ");
num=sc.nextInt();
}
b[r][c]=num;
sc.close();
}
/* returns true if all the conditions for sudoku legal move are met: there is no
* number on the same row, column, box, and the cell isn't taken*/
public static boolean legalMove(int[][] b, int row, int col, int num)
{
return checkLegalRow(b,row,num) && checkLegalCol(b,col,num) && checkLegalBox(b,row,col,num) && b[row][col]==0;
}
/* returns true if the initial board setting is legal*/
public static boolean initialLegal(int[][] b)
{
int num;
for (int i=0; i<b.length; i++)
{
for (int j=0; j<b[i].length; j++)
{
if (b[i][j]!=0)
{
num=b[i][j];
b[i][j]=0;
if (!(checkLegalRow(b,i,num) && checkLegalCol(b,j,num) && checkLegalBox(b,i,j,num)))
{
b[i][j]=num;
return false;
}
else
b[i][j]=num;
}
}
}
return true;
}
/* using backtrack algorithm and recursion to solve the sudoku*/
public static boolean solveBacktrack(int[][] b, int row, int col)
{
/*If the cell is already taken by a number:
* case 1: if its the last cell (rightmost, lowest) is already taken, sudoku solved
* case 2: if its the rightmost cell not on the if it is the rightmost column but not
* the lowest row, go to the leftmost cell in next row
* case 3: if it's a regular cell, go for the next cell*/
if (b[row][col]!=0)
{
if (col==b.length-1)
if (row==b.length-1)
{
//printgame.board(b); // case 1
return true;
}
else
return solveBacktrack(b,row+1,0); // case 2
else
return solveBacktrack(b,row,col+1); // case 3
}
boolean solved=false;
for (int k=1; k<=b.length; k++) //iterates through all numbers from 1 to N
{
// If a certain number is a legal for a cell - use it
if (legalMove(b,row,col,k))
{
b[row][col]=k;
if (col==b.length-1) // if it's the rightmost column
{
if (row==b.length-1) // and the lowest row - the sudoku is solved
{
//printgame.board(b);
return true;
}
else
solved=solveBacktrack(b,row+1,0); // if its not the lowest row - keep solving for next row
}
else // keep solving for the next cell
solved=solveBacktrack(b,row,col+1);
}
if (solved)
return true;
else //if down the recursion sudoku isn't solved-> remove the number (backtrack)
{
b[row][col]=0;
}
}
return solved;
}
/* public static long solveCountSolutions(int[][]b, int row, int col, long counter)
{
}
*/
public static void main(String[] args)
{
Sudoku game = new Sudoku(9);
game.board[0][2]=5;game.board[0][1]=3; game.board[0][0]=1;
game.board[8][2]=4;game.board[8][4]=3;game.board[8][6]=6;
printBoard(game.board);
if (initialLegal(game.board))
System.out.println(solveBacktrack(game.board,0,0));
else
System.out.println("Illegal setting");
printBoard(game.board);
}
}
Such a function could be implemented by not exiting from the recursion when a solution is found, but instead dump that solution to an external structure (if you only need counts, make a counter somewhere outside of the function but visible to it, and increment it once a solution is found), and then continue searching like if you've hit a dead end. Something in line of this (abstract code):
static int solutions=0;
bool recursiveSolver(TYPE data) {
TYPE newData;
while (!nextChoice(data)) {
if (solved(data)) {
// return true; not now, we count instead
solutions++;
}
newData=applyNextChoice(data); // for recursion
if (recursiveSolver(newData)) {
return true; // will never hit, but checking is needed for solver to work
}
}
// all choices checked, no solution
return false;
}
applyNextChoice() is a placeholder for "select next number, put into this cell" in case of sudoku. TYPE is a placeholder for whatever structure that represents an incomplete solution, in your case a combined int[][] b, int row, int col.
I'm doing a program where user input five numbers and in the end the numbers are printed out which is working fine. What I can't get to work is a boolean function to check for duplicates. It should check for duplicates as the user write them in, so e.g. if number one is 5 and the second numbers is also 5, you should get an error until you write in a different number. Meaning if the user input a duplicate it should NOT be saved in the array. This is obviously an assignment, so I'm just asking for a hint or two.
This program is written based on pseudo-code given to me, and therefore I have to use a boolean to check for duplicates with the public boolean duplicate( int number ) class.
I've tried getting my head around it and tried something by myself, but obviously I'm doing a stupid mistake. E.g.:
if(int i != myNumbers[i])
checkDuplicates = false
else
checkDuplicates = true;
return checkDuplicates;
DuplicatesTest class:
public class DuplicatesTest {
public final static int AMOUNT = 5;
public static void main(String[] args) {
Duplicates d = new Duplicates(AMOUNT);
d.inputNumber();
d.duplicate(AMOUNT);
d.printInputNumbers();
}
}
Duplicates class:
public class Duplicates {
private int amount;
private int[] myNumbers;
private boolean checkDuplicates;
public Duplicates(int a) {
amount = a;
myNumbers = new int[amount];
}
public void inputNumber() {
for(int i = 0; i < amount; i++ ) {
int input = Integer.parseInt(JOptionPane.showInputDialog("Input 5 numbers"));
myNumbers[i] = input;
}
}
public boolean duplicate( int number ) {
<BOOLEAN TO CHECK FOR DUPLICATES, RETURN FALSE OR TRUE>
}
public void printInputNumbers() {
JTextArea output = new JTextArea();
output.setText("Your numbers are:" + "\n");
for(int i = 0; i < myNumbers.length; i++) {
if (i % 5 == 0) {
output.append("\n");
}
output.append(myNumbers[i] + "\t");
}
JOptionPane.showMessageDialog(null, output, "Numbers", JOptionPane.PLAIN_MESSAGE);
}
}
Sorry if the code tag is messy, I had some trouble with white fields in between and such. I'm new here.
Don't store the numbers in an array. Use a Set<Integer> instead. And then do a Set#contains() operation. It's O(1) operation which is actually far better than iterating over the array to search for duplicates.
Ok, if it's a compulsion to use an array, then you should modify your current approach, to return true as soon as you find a duplicate, instead of iterating over the array again. In your current approach, since you are setting the boolean variable to false in the else block, your method will return false if the last element of the array is not the same as what you are checking. So, just modify your approach to:
// loop over the array
if (number == myNumbers[i])
return true;
// outside the loop, if you reach, return false
return false;
Note that your current if statement will not compile. You are declaring an int variable there, which you can't do.
if (int i == myNumbers[i]) // this is not a valid Java code.
int nums[] = new int[5];
int count = 0;
public boolean duplicate(int number)
{
boolean isDup = false;
for (int i = 0; i <= count; i++)
{
if (number == nums[i])
{
isDup = true;
break;
}
}
if (!isDup)
{
count++;
nums[count] = number;
}
return isDup;
}
I am trying to have a method (duplicates) return true if a given array called x (entered by user in another method), contains duplicate values. Otherwise it would return false. Rather then checking the entire array, which is initialized to 100, it will check only the amount of values entered, which is kept track of with a global counter: numElementsInX.
What is the best way to accomplish this?
public static boolean duplicates (int [] x)
I am prompting for user data like so:
public static void readData (int [] x, int i){
Scanner input = new Scanner(System.in);
System.out.println("Please enter integers, enter -999 to stop");
while (i <= 99) {
int temp = input.nextInt();
if(temp == -999){
break;
}
else {
x[i++]=temp;
}
// else
}//end while
printArray(x,i);
}//end readData
public static void printArray(int [] x, int numElementsInX){
int n = numElementsInX;
for (int i = 0; i < n; i++){
System.out.print(x[i] + " ");
}//end for
System.out.println();
}//end printArray
I am sure there is a better way to do this, but this is how I have been taught so far.
Here is a solution that:
Compiles and executes without throwing.
Uses numElementsInX as you requested.
Returns as soon as it finds a duplicate.
This approach tests whether each member of the array has been seen before. If it has, the method can return immediately. If it hasn't, then the member is added to the set seen before.
public static boolean duplicates (int [] x, int numElementsInX ) {
Set<Integer> set = new HashSet<Integer>();
for ( int i = 0; i < numElementsInX; ++i ) {
if ( set.contains( x[i])) {
return true;
}
else {
set.add(x[i]);
}
}
return false;
}
Here's a sample program containing the above code.
this should do it.
public boolean containsDuplicates(Integer[] x) {
return new HashSet<Integer>(Arrays.asList(x)).size() != x.length
}
You dont need numElementsInX as this is the same as x.length
edit after comment from Louis. Arrays.asList does not work with int arrays.
To convert int[] to Integer try this question How to convert int[] to Integer[] in Java?
or do soemthing like this (not tested but from memory)
Integer[] newArray = new Integer[a.length];
System.arraycopy(a, 0, newArray, 0, a.length);
This certainly isn't the most efficient way, but since you don't know about Sets yet, you can use two loops:
public static boolean duplicates (int [] x){
for (int i=0; i<numElementsInX; i++){
for (int j=i+1; j<numElementsInX; j++){
if (x[j]==x[i]) return true;
}
}
return false;
}
"set.add()" returns true if the element is not already present in the set and false otherwise. We could make use of that and get rid of "set.contains()" as in the above solution.
public static boolean duplicates (int[] x, int numElementsInX) {
Set<Integer> myset = new HashSet<>();
for (int i = 0; i < numElementsInX; i++) {
if (!myset.add(x[i])) {
return true;
}
}
return false;
}
For java, return true if the array contains a duplicate value,
boolean containsDuplicates(int[] a) {
HashSet<Integer> hs = new HashSet<>();
for(int i = 0; i<a.length; i++) {
if(!hs.add(a[i])){
return true;
}
}
return false;
}