I've recently been working on a backtracking sudoku solving algorithm and currently I'd like to ask on how I should go about to change my solve() method from void to a boolean.
I'm using a very simple backtracking algorithm, and it's currently working fine, but I'd rather have a boolean instead of a void, because having a printstack isn't very nice...
Thanks!
public class Backtracking{
static int backtrack = 0;
//check if valid in row
protected static boolean validInRow(int row, int value)
{
for( int col = 0; col < 9; col++ )
if( board[row][col] == value )
return false ;
return true ;
}
//check if valid in column
protected static boolean validInCol(int col, int value)
{
for( int row = 0; row < 9; row++ )
if( board[row][col] == value )
return false ;
return true ;
}
//check if valid in 3*3
protected static boolean validInBlock(int row, int col, int value)
{
row = (row / 3) * 3 ;
col = (col / 3) * 3 ;
for( int r = 0; r < 3; r++ )
for( int c = 0; c < 3; c++ )
if( board[row+r][col+c] == value )
return false ;
return true ;
}
//call other methods
public void solve(int row, int col) throws Exception
{
if(row > 8)
throw new Exception("Solution found") ;
else
{
while(board[row][col] != 0)
{
if( ++col > 8 )
{
col = 0 ;
row++ ;
if( row > 8 )
throw new Exception( "Solution found" ) ;
}
}
for(int value = 1; value < 10; value++)
{
if(validInRow(row,value) && validInCol(col,value) && validInBlock(row,col,value))
{
board[row][col] = value;
new PrintEvent(board);
if( col < 8 )
solve(row, col + 1);
else
solve(row + 1, 0);
backtrack++;
}
}
board[row][col] = 0;
}
}
}
Well, you could catch the exception to avoid the stack trace, but that's still not very pretty. What you can do after changing the return type to boolean is:
if( col < 8 ) {
if (solve(row, col + 1)) {
return true;
}
} else {
if (solve(row + 1, 0)) {
return true;
}
}
and then of course, change the throw statements to return true;.
public boolean solve(int row, int col) throws Exception
{
{
while(board[row][col] != 0)
{
if( ++col > 8 )
{
col = 0 ;
row++ ;
if( row > 8 )
return true
}
}
for(int value = 1; value < 10; value++)
{
if(validInRow(row,value) && validInCol(col,value) && validInBlock(row,col,value))
{
board[row][col] = value;
new PrintEvent(board);
if( col < 8 )
solve(row, col + 1);
else
solve(row + 1, 0);
backtrack++;
}
}
board[row][col] = 0;
}
}
return false;
}
Related
I am using the Sieve of Eratosthenes algorithm to find the prime numbers in a range (from a min value to a max value). However, I cannot seem to get it to work if I include a min value.
Here is my code in Java:
protected static List<Integer> getSievePrimes(int a, int b) {
List<Integer> primeNumbers = new ArrayList();
boolean [] isComposite = new boolean [b + 1];
isComposite[1] = true;
// Mark all composite numbers
for (int i = 2; i <= b; i++) {
// for (int i = a == 1 ? 2 : a; i <= b; i++) {
if (!isComposite[i]) {
// 'i' is a prime number
//if (i >= a) {
primeNumbers.add(i);
//}
int multiple = 2;
while (i * multiple <= b) {
isComposite [i * multiple] = true;
multiple++;
}
}
}
return primeNumbers;
}
As you can see it currently only caters for the max value (b), and not the min value (a).
Question
How can I modify the method above to cater for both min and max?
public boolean isPrime( int test )
{
int k;
if( test < 2 )
return false;
else if( test == 2 )
return true;
else if( ( test > 2 ) && ( test % 2 == 0 ) )
return false;
else
{
for( k = 3; k < ( test/2 ); k += 2 )
{
if( test % k == 0 )
return false;
}
}
return true;
}
public void sieveOfEratosthenes( int first, int last )
{
boolean[ ] sieve = new boolean[ ( last - first ) + 1 ];
int count = 0;
int index = 0;
int cursor = first;
int end = last;
while( cursor <= end )
{
if( isPrime( cursor ) != sieve[ index ] )
{
System.out.println( cursor+" " );
count++;
}
cursor++;
index++;
}
cursor = first;
if( count == 0 )
System.out.println( "There are "+count+" primes from "+cursor+" to "+end+"." );
else if( count == 1 )
System.out.println( "is the "+count+" prime from "+cursor+" to "+end+"." );
else
System.out.println( "are the "+count+" primes from "+cursor+" to "+end+"." );
}
My check vertical win and check horizontal win work perfectly fine, however i dont know what to do with my check diagonal code to make it actually check diagonal. Some guidance would be much appreciated and this is in java. Thank you.
private boolean checkVerticalWin()
{
PieceType type = myBoard[myLastPoint.x][myLastPoint.y];
System.out.println("check vert");
for(int j = 0; j < myNumColumns; j++)
{
for(int i = 0; i < myNumRows; i++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
if(count == 1)
{
myWinBegin = new Point(i,j);
}
}
else
{
myWinBegin = null;
count = 0;
}
System.out.println(count);
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
myWinBegin = null;
return false;
}
private boolean checkHorizontalWin()
{
System.out.println("test");
PieceType type = myBoard[myLastPoint.x][myLastPoint.y];
for(int i = 0; i < myNumRows; i++)
{
for(int j = 0; j < myNumColumns; j++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null)
{
count++;
if (count == 1)
{
myWinBegin = new Point(i,j);
}
}
else
{
myWinBegin = null;
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
myWinBegin = null;
return false;
}
private boolean checkDiagonalWin()
{
PieceType type = myBoard[myLastPoint.x][myLastPoint.y];
for(int i = 0; i < myNumRows; i++)
{
for (int j = 0; j < myNumColumns; j++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
myWinBegin = new Point(i,j);
}
else
{
count = 0;
myWinEnd = new Point(i,j);
}
if(count == myWinLength)
{
return true;
}
}
}
for(int j = 0; j < myNumColumns; j--)
{
for (int i = 0; i < myNumRows; i--)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
myWinBegin = new Point(i,j);
}
else
{
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
for(int j = 0; j < myNumColumns; j++)
{
for (int i = 0; i < myNumRows; i--)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
}
else
{
myWinBegin = new Point(i,j);
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
for(int j = 0; j < myNumColumns; j--)
{
for (int i = 0; i < myNumRows; i++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
myWinBegin = new Point(i,j);
}
else
{
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
return false;
}
So basically, you need a start point, you then need to determine in which direction to move
With that idea in hand, you could use something like...
boolean win = true;
for (int count = 0; count < 4; count++) {
if (row < myNumRows && row >= 0 && col < myNumColumns && col >= 0) {
int test = myBoard[row][col];
if (test != check) {
win = false;
break;
}
} else {
break;
}
row += rowDelta;
col += colDelta;
}
As the basic algorithm. All this does is checks each cell from a start point, to a total of 4 cells, the algorithm moves by the specified delta/direction and keeps checking while each cell matches the check value.
Now, I'd wrap this in a simple method
public boolean didWin(int[][] grid, int check, int row, int col, int rowDelta, int colDelta) {
boolean win = true;
for (int count = 0; count < 4; count++) {
if (row < ROWS && row >= 0 && col < COLUMNS && col >= 0) {
int test = grid[row][col];
if (test != check) {
win = false;
break;
} else {
break;
}
}
row += rowDelta;
col += colDelta;
}
return win;
}
which makes it simpler to call, know given any point, you can do something like...
int startRow = ...;
int startCol = ...;
int player = ...;
if (didWin(myBoard, player, startRow, startCol, 1, 0) || // Vertical, down
didWin(myBoard, 1, startRow, startCol, 0, 1) || // Right
didWin(myBoard, 1, startRow, startCol, 0, -1) || // Left
didWin(myBoard, 1, startRow, startCol, 1, 1) || // Right/down
didWin(myBoard, 1, startRow, startCol, -1, -1) || // Left/Up
didWin(myBoard, 1, startRow, startCol, 1, -1) || // Down/Left
didWin(myBoard, 1, startRow, startCol, -1, 1) // Up/Right
) {
// You be the winner
}
nb: I've left out check a vertical up direction, because it's unlikely that you could actually win this way
ps: I'd be even more lazy and would just have a didWin method, which did the above checks and returned true or false, but I'm lazy
So, the startRow and startCol would represent the anchor point around which you want to check and would, in this example, represent the last drop.
This example uses a int to represent the player/token, but you could use anything, all this does is compares the token you supply with the values in the array
I'm trying to create a strategy that will play against human or another strategy for the game Gomoku. I already have some kind of minimax function, although i don't quite understand how does it work and i also have a getscore function, which should send back the best score to minimax ? But the problem is that my getScore function does not recognize if there are 4 or 5 circles in a row/col/diagonally.
Here is my code:
public class JarmoStrategyV1 implements ComputerStrategy {
public static int Lrow = 0;
public static int Lcol = 0;
public static int Drow = 0;
public static int Dcol = 0;
public static final int E = 0;
public static final int X = 1; // black
public static final int O = -1; // white
public static final int WINSCORE = 100;
private static final int WINCOUNT = 5;
public Location getMove(SimpleBoard board, int player) {
// let's operate on 2-d array
int[][] b = board.getBoard();
System.out.println(getScore(b, player));
for (int row = 0; row < b.length; row++) {
for (int col = 0; col < b[0].length; col++) {
if (b[row][col] == SimpleBoard.EMPTY) {
// first empty location
return new Location(row, col);
}
}
}
return null;
}
#Override
public String getName() {
return "Student name";
}
public static int minimax(int[][] board, int player, int depth) {
if (getScore(board, player) == WINSCORE) {
//
return WINSCORE;
}
if (depth == 2) {
return getScore(board, player);
}
int max = Integer.MIN_VALUE;
if (player == -1){
max = Integer.MAX_VALUE;
}
System.out.println(max);
List<Location> possibleMoves = getPossibleMoves(board);
for (Location loc : possibleMoves) {
board[loc.getRow()][loc.getColumn()] = player;
int newplayer = 0 - player;
if(newplayer == 1){
int value = minimax(board, newplayer,depth + 1);
if(value < max) {
max = value;
}
}
if (newplayer == -1){
int value = minimax(board, newplayer, depth + 1);
if (value > max) {
max = value;
}
}
board[loc.getRow()][loc.getColumn()] = E;
}
return max;
}
public static int getScore(int[][] board, int muutuja) {
//int yks = 0;
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] == muutuja) {
if (row <= (board.length - 5)) {
if (col <= board.length && getCount(board, row, col, 0, 1, muutuja, WINCOUNT) >= (WINCOUNT - 1) && getCount(board, row, (col + 4), 0, 1, E, 1 ) >= 1) return 1; // - 4 in a row
if (row >= 1 && getCount(board, row, col, 1, 0, muutuja, WINCOUNT) >= (WINCOUNT -1) && getCount(board, (row - 1), col, 1, 0, E, 1) == 1) return 1;
if (getCount(board, row, col, 1, 0, muutuja, WINCOUNT) >= WINCOUNT) return 100; // | 5 in a row
if (col <= WINCOUNT && getCount(board, row, col, 1, 1, muutuja, WINCOUNT) >= WINCOUNT) return 100; // \
if (col >= WINCOUNT && getCount(board, row, col, 1, -1, muutuja, WINCOUNT) >= WINCOUNT) return 100; // /
}
if (col <= WINCOUNT && getCount(board, row, col, 0, 1, muutuja, WINCOUNT) >= WINCOUNT) return 100; // -
}
}
}
return 0;
}
public static int getCount(int[][] board, int row, int col, int rowd, int cold, int player, int test) {
int count = 0;
for (int i = 0; i < test; i++) {
if (board[row + i * rowd][col + i * cold] == player) count++;
else break;
}
return count;
}
public static ArrayList<Location> getPossibleMoves(int[][] board) {
ArrayList<Location> availableMoves = new ArrayList<Location>();
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] == E) {
availableMoves.add(new Location(row, col));
}
}
}
return availableMoves;
}
}
There seems to be some kind of a problem with getScore, when i run this code then i can only play for a while until my Gomoku app crashes.
If you want to try this out yourself then you can open this via Eclipse.
Download the project file: http://www68.zippyshare.com/v/feWl2QwC/file.html
Import it to eclipse projects/workspace.
And you also have to build path to those 2 jar files in lib folder.
NOTE: I can only edit files in gomoku.strategies package.
the stacktrace shows an exception. add debug print or run with debugger
java.lang.IllegalArgumentException: It's computer's turn, pass the player to makeMove()
at gomoku.Game.makeMove(Game.java:476)
i'm a bit stuck with the Sudoku algorithm, i coded it using backtrack, and following the theorical steps this should work, and i tried to debuge it, but is too hard (and yes, it solve some numbers and does things)
i paste the code, i hope that you can help me, i really can't see where the problem is...
public void backtracking(int row,int col){
if(row > 8){
System.out.println("Solution Found!!");
printSudoku();
}
if (m[row][col] != 0){
next(row, col);
}
else {
for(int i =1; i < n;i++)
if(row(row, i) && col(col, i)) {
m[row][col] =i;
next(row, col);
}
m[row][col] = 0;
}
}
public void next( int row, int col ) {
if( col < 8)
backtracking( row, col + 1 ) ;
else
backtracking( row+ 1, 0 ) ;
}
public boolean region(int x, int y, int numReg) {
x = (x / 3) * 3 ;
y = (y / 3) * 3 ;
for( int r = 0; r < 3; r++ )
for( int c = 0; c < 3; c++ )
if( m[x+r][y+c] == numReg )
return false ;
return true ;
}
public boolean row(int x, int k){
for(int i =0; i < 9; i++)
if(m[x][i] == k)
return false;
return true;
}
public boolean col(int x, int k){
for(int i =0; i < 9; i++)
if(m[i][x] == k)
return false;
return true;
}
I ommited the "printSudoku" method, is just a double for and you know.
The code seems almost right.
As far as i can see you just forgot to call the region method. And I can't see where the variable n is coming from.
Try it with this slightly modified backtracking method:
public static void backtracking(int row, int col) {
if (row > 8) {
System.out.println("Solution Found!!");
printSudoku();
System.exit(0); //exiting after solution is found
}
if (m[row][col] != 0) {
next(row, col);
} else {
for (int i = 1; i <= 9; i++) //replaced i < n with i<=9
if (row(row, i) && col(col, i) && region(row, col, i)) { //calling region method too
m[row][col] = i;
next(row, col);
}
m[row][col] = 0;
}
}
I'm trying to implement The Game of Life in java, as an exercise to learn this language.
Unfortunately I have a problem, as I don't seem able to make this program run correctly.
I implemented a torodial sum (the plane is a donut) with no problem:
int SumNeighbours (int i, int j) {
int value = 0;
value = world[( i - 1 + row ) % row][( j - 1 + column ) % column]+world[( i - 1 + row ) % row][j]+world[( i - 1 + row ) % row][( j + 1 ) % column];
value = value + world[i][( j - 1 + column ) % column] + world[i][( j + 1 ) % column];
value = value + world[( i + 1 ) % row][( j - 1 + column ) % column] + world[( i + 1 ) % row][j]+world[ ( i+1 ) % row ][( j + 1 ) % column];
return value;
}
And it sums correctly when I test it:
void NextWorldTest () {
int count;
int [][] nextWorld = new int[row][row];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
}
System.out.println();
}
world=nextWorld;
}
Unfortunately when I add the conditions of game of life (born/death) the program stop working correctly, as it seems not able anymore to count correctly the alive cells in the neighborhood. It counts where there are none, and it doesn't count when there are some. E.g.: it doesn't count the one below some living cells.
It's a very odd behaviour, and it's been giving me a headache for 3 days now... maybe I'm missing something basic about variables?
Here you can find the class.
void NextWorld () {
int count;
int [][] nextWorld = new int[row][column];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
if ( ( world[i][j] == 0) && ( count == 3 ) ) {
nextWorld[i][j] = 1;
} else if ( ( world[i][j] == 1 ) && ( (count == 3) || (count == 2) )) {
nextWorld[i][j] = 1;
} else {
nextWorld[i][j]=0;
}
}
System.out.println();
}
world=nextWorld;
}
}
Am I doing something wrong?
Below you can find the full package.
package com.GaOL;
public class GameWorld {
int [][] world;
int row;
int column;
public int GetRow() {
return row;
}
public int GetColumn() {
return column;
}
public int GetWorld (int i, int j) {
return world[i][j];
}
void RandomGen (int size, double p1) {
double randomCell;
row = size;
column = size;
world = new int[row][column];
for (int i = 0; i<row; i++ ) {
for (int j = 0; j<column; j++ ) {
randomCell=Math.random();
if (randomCell < 1-p1) {
world[i][j] = 0;
} else {
world[i][j] = 1;
}
}
}
}
void printToConsole() {
double test = 0;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
if ( world[i][j] == 0 ) {
System.out.print(" ");
} else {
System.out.print(" * ");
test++;
}
}
System.out.println("");
}
System.out.println("ratio is " + test/(row*column));
}
int SumNeighbours (int i, int j) {
int value = 0;
value = world[( i - 1 + row ) % row][( j - 1 + column ) % column]+world[( i - 1 + row ) % row][j]+world[( i - 1 + row ) % row][( j + 1 ) % column];
value = value + world[i][( j - 1 + column ) % column] + world[i][( j + 1 ) % column];
value = value + world[( i + 1 ) % row][( j - 1 + column ) % column] + world[( i + 1 ) % row][j]+world[ ( i+1 ) % row ][( j + 1 ) % column];
return value;
}
void NextWorldTest () {
int count;
int [][] nextWorld = new int[row][row];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
}
System.out.println();
}
world=nextWorld;
}
void NextWorld () {
int count;
int [][] nextWorld = new int[row][column];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
if ( ( world[i][j] == 0) && ( count == 3 ) ) {
nextWorld[i][j] = 1;
} else if ( ( world[i][j] == 1 ) && ( (count == 3) || (count == 2) )) {
nextWorld[i][j] = 1;
} else {
nextWorld[i][j]=0;
}
}
System.out.println();
}
world=nextWorld;
}
}
and here the test class:
package com.GaOL;
public class GameTestClass {
public static void main(String[] args) {
GameWorld prova = new GameWorld();
prova.RandomGen(10, 0.02);
for (int i=0; i<3; i++) {
prova.printToConsole();
prova.NextWorld();
}
}
}
I think, problem is in arrays assignments. What are you doing here?
int [][] nextWorld = new int[row][row];
nextWorld = world;
you should create new array [row][column] and then fill it according data from world.
And more
in Java you can do smt like anArray.length and anArray[0].length, so you do not need row and column variables
you should name all you method starting with low letter, like sumNeighbours() instead of SumNeighbours()
upd:there was typo in second advise: fixed