All pastes #824209 Raw Edit

Something

public java v1 · immutable
·published 2007-12-19 21:14 UTC
rendered paste body
public class Horse{    private int dimension;    private int row;    private int col;    private int gameBoard[][];    private int[][] moves = { {-2,-1}, {-2,1}, {1,-2}, {1,2}, {2,1}, {2,-1}, {1,-2}, {-1,-2} };    public Horse( int dimension, int row, int col )    {        this.dimension = dimension;        this.row = row;        this.col = col;    }        //SpelBord aanmaken en paard plaatsen.    public void createBoard()    {        gameBoard = new int[dimension][dimension];        for( int y = 0; y < dimension; y++)            {                for( int x = 0; x < dimension; x++)                    {                        gameBoard[y][x] = 0; //Alle lege vakjes krijgen waarde 0.                    }            }            gameBoard[row][col] = 1; //Het paard krijgt waarde 1.     }          public boolean possibleSolution( int i , int row , int col )     {         System.out.println("Move " + i + ", coordinates: " + row + " , " + col);         if( i == dimension*dimension )         {             return true;         }         else         {             for( int x = 0; x < moves.length; x++ )             {                int newRow = row + moves[x][1];                int newCol = col + moves[x][0];                System.out.println( newRow + " , " + newCol );                                if( ( newRow >= 0 && newRow < dimension ) && ( newCol >= 0 && newCol < dimension ) )                {                    if( gameBoard[newRow][newCol] == 0 )                    {                        putHorse( i+1 , newRow , newCol );                        if( possibleSolution( i+1 , newRow , newCol ) )                        {                        return true;                        }                        removeHorse( newRow , newCol );                    }                                    }            }           return false;          }     }               public void putHorse( int i , int row , int col )     {         gameBoard[row][col] = i;     }          public void removeHorse( int row , int col )     {         gameBoard[row][col] = 0;     }          public void solve()     {         createBoard();         if( possibleSolution( 1 , row , col ) )         {             for (int r = 0; r < dimension; r++)             {                 for (int k = 0; k < dimension; k++)                 {                     System.out.print(gameBoard[row][col] +", ");			     }			     System.out.println("");		     }		 }     }}