Tuesday, March 12, 2013

AI Project:Sudoku Source Code in Java


Here is the code that solves a sudoku puzzle by recursion and backtracking method.

package sudoku;

/*********************************************************/
/*                                                       */
/* An applet to demonstrate recursion and backtracking   */
/* ===================================================   */
/*                                                       */
/*********************************************************/
import java.applet.* ;
import java.awt.* ;

//Solves a sudoku puzzle by recursion and backtracking

public class Sudoku extends Applet implements Runnable
{
   // The model
   protected int model[][] ;

   // The view
   protected Button view[][] ;

   // Creates the model and sets up the initial situation
   protected void createModel()
   {
      model = new int[9][9] ;

      // Clear all cells
      for( int row = 0; row < 9; row++ )
         for( int col = 0; col < 9; col++ )
            model[row][col] = 0 ;

      // Create the initial situation
      model[0][0] = 9 ;
      model[0][4] = 2 ;
      model[0][6] = 7 ;
      model[0][7] = 5 ;

      model[1][0] = 6 ;
      model[1][4] = 5 ;
      model[1][7] = 4 ;

      model[2][1] = 2 ;
      model[2][3] = 4 ;
      model[2][7] = 1 ;

      model[3][0] = 2 ;
      model[3][2] = 8 ;

      model[4][1] = 7 ;
      model[4][3] = 5 ;
      model[4][5] = 9 ;
      model[4][7] = 6 ;

      model[5][6] = 4 ;
      model[5][8] = 1 ;

      model[6][1] = 1 ;
      model[6][5] = 5 ;
      model[6][7] = 8 ;

      model[7][1] = 9 ;
      model[7][4] = 7 ;
      model[7][8] = 4 ;

      model[8][1] = 8 ;
      model[8][2] = 2 ;
      model[8][4] = 4 ;
      model[8][8] = 6 ;
   }

   //Creates an empty view
   protected void createView()
   {
      setLayout( new GridLayout(9,9) ) ;

      view = new Button[9][9] ;

      // Create an empty view
      for( int row = 0; row < 9; row++ )
         for( int col = 0; col < 9; col++ )
         {
            view[row][col]  = new Button() ;
            add( view[row][col] ) ;
         }
   }

   //Updates the view from the model
   protected void updateView()
   {
      for( int row = 0; row < 9; row++ )
         for( int col = 0; col < 9; col++ )
            if( model[row][col] != 0 )
               view[row][col].setLabel( String.valueOf(model[row][col]) ) ;
            else
               view[row][col].setLabel( "" ) ;
   }

   // This method is called by the program when the applet is loaded
   public void init()
   {
      createModel() ;
      createView() ;
      updateView() ;
   }

   // Checks if num is an acceptable value for the given row
   protected boolean checkRow( int row, int num )
   {
      for( int col = 0; col < 9; col++ )
         if( model[row][col] == num )
            return false ;

      return true ;
   }

   // Checks if num is an acceptable value for the given column
   protected boolean checkCol( int col, int num )
   {
      for( int row = 0; row < 9; row++ )
         if( model[row][col] == num )
            return false ;

      return true ;
   }

   // Checks if num is an acceptable value for the box around row and col
   protected boolean checkBox( int row, int col, int num )
   {
      row = (row / 3) * 3 ;
      col = (col / 3) * 3 ;

      for( int r = 0; r < 3; r++ )
         for( int c = 0; c < 3; c++ )
         if( model[row+r][col+c] == num )
            return false ;

      return true ;
   }

   // This method is called by the browser to start the applet
   public void start()
   {
      // This statement will start the method 'run' to in a new thread
      (new Thread(this)).start() ;
   }

   // The active part begins here
   public void run()
   {
      try
      {
         // Let the observers see the initial position
         Thread.sleep( 1000 ) ;

         // Start to solve the puzzle in the left upper corner
         solve( 0, 0 ) ;
      }
      catch( Exception e )
      {
      }
   }

   // Recursive function to find a valid number for one single cell
   public void solve( int row, int col ) throws Exception
   {
      // Throw an exception to stop the process if the puzzle is solved
      if( row > 8 )
         throw new Exception( "Solution found" ) ;

      // If the cell is not empty, continue with the next cell
      if( model[row][col] != 0 )
         next( row, col ) ;
      else
      {
         // Find a valid number for the empty cell
         for( int num = 1; num < 10; num++ )
         {
            if( checkRow(row,num) && checkCol(col,num) && checkBox(row,col,num) )
            {
               model[row][col] = num ;
               updateView() ;

               // Let the observer see it
               Thread.sleep( 1000 ) ;

               // Delegate work on the next cell to a recursive call
               next( row, col ) ;
            }
         }

         // No valid number was found, clean up and return to caller
         model[row][col] = 0 ;
         updateView() ;
      }
   }

   // Calls solve for the next cell
   public void next( int row, int col ) throws Exception
   {
      if( col < 8 )
         solve( row, col + 1 ) ;
      else
         solve( row + 1, 0 ) ;
   }
}

Monday, March 11, 2013

Solution for Laptop heating problem on linux/ubuntu

It was being very difficult for me to switch from windows to linux/ubuntu as my laptop fan go crazy on the ubuntu platform. The heating of cpu was terribly high.
After a long struggle, I found some solutions for the heating problem with my laptop[hp].
1. Use jupiter in power saving mode.
-jupiter not only gives you the power to use your cpu in your desired mode, it also supervises your cpu temperature. It helps to monitor your laptop temperature.
2. Disable your graphic card.
-You can disable your graphic card, this was the best solution for me get hold of my computer and control the heating.
Use the following command.
$ sudo su
- # echo OFF > /sys/kernel/debug/vgaswitcheroo/switch

First login as root in terminal and then turn off secondary card. If after some time fan turns off.

Hope this will help.

Saturday, August 18, 2012

How To Do A Better Google Search

Google has become the one and only search engine in today's world. It solves the problems of all level of users. However, many users still face problems regarding the search keyword. Google has introduced a solution for it for better search techniques. "Power Searching with Google." is an online course introduced by google which enables you to save your time.

Here's the course introduction video:

Saturday, August 11, 2012

AI Project:Checkers[html/javascript]|Source Code Download

<HTML>
<body>
<h1>CHECKERS</h1>
<h2>Project on AI</h2>
<p>

</p>

<script language="JavaScript1.1">

//Defining variables and object functions.
var red = -1; // player is black
var black = 1; // computer is red
var block_length = 45;
var piece_toggled = false;
var my_turn = true;0
var double_jump = false;
var comp_move = false;
var game_is_over = false;
var safe_from = safe_to = null;

var togglers = 0;

function Coord(x,y) {
this.x = x;
this.y = y;
}
function coord(x,y) {
c = new Coord(x,y);
return c;
}


function integ(num) {
if (num != null)
return Math.round(num);
else
return null;
}
function abs(num) {
return Math.abs(num);
}
function sign(num) {
if (num < 0) return -1;
else return 1;
}

//Load up the Board array.
function Board() {
board = new Array();
for (var i=0;i<8; i++) {
board[i] = new Array();
for (var j=0;j<8;j++)
board[i][j] = Board.arguments[8*j+i];
}
board[-2] = new Array(); // prevents errors
board[-1] = new Array(); // prevents errors
board[8] = new Array(); // prevents errors
board[9] = new Array(); // prevents errors
}
var board;
Board(1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,-1,0,-1,0,-1,0,-1,
-1,0,-1,0,-1,0,-1,0,
0,-1,0,-1,0,-1,0,-1);

function moveable_space(i,j) {
return (((i%2)+j)%2 == 0); //Returns true if space is movable else false, adding array index odd=nonmovable.
}

//Load or Create the main table.
document.write("<table border=1 cellspacing=0 cellpadding=0 width="+(block_length*8+8) +" ");
for(var j=0;j<8;j++) {
document.write("<tr><td><img src='whit.gif' width=4 height="+block_length+">");
for(var i=0;i<8;i++) {
if (moveable_space(i,j))
document.write("<a href='javascript:clicked("+i+","+j+")'>"); //Assign Clicked() fucntion to every white array

document.write("<img src='");
if (board[i][j]==1) document.write("you1.gif");
else if (board[i][j]==-1) document.write("me1.gif");
else if (moveable_space(i,j)) document.write("whit.gif");
else document.write("black.gif");
document.write("' width="+block_length+" height="+block_length
+" name='space"+i+""+j+"' border=0>");
if (moveable_space(i,j)) document.write("</a>");
}
document.write("<img src='black.gif' width=4 height="+block_length+"></td></tr>");
}
document.write("</table><br>"
+"<form name='disp'><input "
+"type=button value=\"Play Again\" onClick=\"location.href+=''\"></form>");

function clicked(i,j) {
if (my_turn) {
if (integ(board[i][j])==1) toggle(i,j);
else if (piece_toggled) move(selected,coord(i,j));

}
}

//Change the colour of a block as selected ,green
function toggle(x,y) {
if (my_turn) {
if (piece_toggled)
draw(selected.x,selected.y,"you1"+((board[selected.x][selected.y]==1.1)?"k":"")+".gif");
if (piece_toggled && (selected.x == x) && (selected.y == y)) {
piece_toggled = false;
if (double_jump) { my_turn = double_jump = false; computer(); }
} else {

piece_toggled = true;
draw(x,y,"you2"+((board[x][y]==1.1)?"k":"")+".gif");
}
selected = coord(x,y);
}
else {
if ((piece_toggled) && (integ(board[selected_c.x][selected_c.y])==-1))
draw(selected_c.x,selected_c.y,"me1"+((board[selected_c.x][selected_c.y]==-1.1)?"k":"")+".gif");
if (piece_toggled && (selected_c.x == x) && (selected_c.y == y)) {
piece_toggled = false;
} else {
piece_toggled = true;
draw(x,y,"me2"+((board[x][y]==-1.1)?"k":"")+".gif");
}
selected_c = coord(x,y);
}
}

function draw(x,y,name) {
document.images["space"+x+""+y].src = name;
}


function legal_move(from,to) {
if ((to.x < 0) || (to.y < 0) || (to.x > 7) || (to.y > 7)) return false;
piece = board[from.x][from.y];
distance = coord(to.x-from.x,to.y-from.y);
if ((distance.x == 0) || (distance.y == 0)) {
alert("You may only move diagonally.");
return false;
}
if (abs(distance.x) != abs(distance.y)) {
alert("Invalid move.");
return false;
}
if (abs(distance.x) > 2) {
alert("Invalid move.");
return false;
}
if ((abs(distance.x) == 1) && double_jump) {
return false;
}
if ((board[to.x][to.y] != 0) || (piece == 0)) {
return false;
}
if ((abs(distance.x) == 2)
&& (integ(piece) != -integ(board[from.x+sign(distance.x)][from.y+sign(distance.y)]))) {
return false;
}
if ((integ(piece) == piece) && (sign(piece) != sign(distance.y))) {
return false;
}

return true;
}
function move(from,to) {
my_turn = true;
if (legal_move(from,to)) {
piece = board[from.x][from.y];
distance = coord(to.x-from.x,to.y-from.y);
if ((abs(distance.x) == 1) && (board[to.x][to.y] == 0)) {
swap(from,to);
} else if ((abs(distance.x) == 2)
&& (integ(piece) != integ(board[from.x+sign(distance.x)][from.y+sign(distance.y)]))) {
double_jump = false;
swap(from,to);
remove(from.x+sign(distance.x),from.y+sign(distance.y));
if ((legal_move(to,coord(to.x+2,to.y+2)))
|| (legal_move(to,coord(to.x+2,to.y-2)))
|| (legal_move(to,coord(to.x-2,to.y-2)))
|| (legal_move(to,coord(to.x-2,to.y+2)))) {
double_jump = true;
alert("You may complete the double jump or click on your piece to stay still.");
}
}
if ((board[to.x][to.y] == 1) && (to.y == 7)) king_me(to.x,to.y);
selected = to;
if (game_over() && !double_jump) {
setTimeout("toggle("+to.x+","+to.y+");my_turn = double_jump = false;computer(); ",1000);

}
}
return true;
}
function king_me(x,y) {
if (board[x][y] == 1) {
board[x][y] = 1.1; // king you
draw(x,y,"you2k.gif");
} else if (board[x][y] == -1) {
board[x][y] = -1.1; // king me
draw(x,y,"me2k.gif");
}
}

function swap(from,to) {
if (my_turn || comp_move) {
dummy_src = document.images["space"+to.x+""+to.y].src;
document.images["space"+to.x+""+to.y].src = document.images["space"+from.x+""+from.y].src;
document.images["space"+from.x+""+from.y].src = dummy_src;
}
dummy_num = board[from.x][from.y];
board[from.x][from.y] = board[to.x][to.y];
board[to.x][to.y] = dummy_num;
}
function remove(x,y) {
if (my_turn || comp_move)
draw(x,y,"whit.gif");
board[x][y] = 0;
}

function move_comp(from,to) {
toggle(from.x,from.y);
comp_move = true;
swap(from,to);
if (abs(from.x-to.x) == 2) {
remove(from.x+sign(to.x-from.x),from.y+sign(to.y-from.y));
}
if ((board[to.x][to.y] == -1) && (to.y == 0)) king_me(to.x,to.y);
setTimeout("selected_c = coord("+to.x+","+to.y+");piece_toggled = true;",900);
setTimeout("bak=my_turn;my_turn=false;toggle("+to.x+","+to.y+");my_turn=bak;",1000);
if (game_over()) {
setTimeout("comp_move = false;my_turn = true;togglers=0;",600);
}
return true;
}
function game_over() { // make sure game is not over (return false if game is over)
comp = you = false;
for(var i=0;i<8;i++) {
for(var j=0;j<8;j++) {
if(integ(board[i][j]) == -1) comp = true;
if(integ(board[i][j]) == 1) you = true;
}
}
if (!comp) alert("You Win!!");
if (!you) alert("You Lose!!");
game_is_over = (!comp || !you)
return (!game_is_over);
}


function computer() {
// step one - prevent any jumps
for(var j=0;j<8;j++) {
for(var i=0;i<8;i++) {
if (integ(board[i][j]) == 1) {
if ((legal_move(coord(i,j),coord(i+2,j+2))) && (prevent(coord(i+2,j+2),coord(i+1,j+1)))) {
return true;
} if ((legal_move(coord(i,j),coord(i-2,j+2))) && (prevent(coord(i-2,j+2),coord(i-1,j+1)))) {
return true;
}
} if (board[i][j] == 1.1) {
if ((legal_move(coord(i,j),coord(i-2,j-2))) && (prevent(coord(i-2,j-2),coord(i-1,j-1)))) {
return true;
} if ((legal_move(coord(i,j),coord(i+2,j-2))) && (prevent(coord(i+2,j-2),coord(i+1,j-1)))) {
return true;
}
}
}
}
// step two - if step one not taken, look for jumps
for(var j=7;j>=0;j--) {
for(var i=0;i<8;i++) {
if (jump(i,j))
return true;
}
}
safe_from = null;
// step three - if step two not taken, look for safe single space moves
for(var j=0;j<8;j++) {
for(var i=0;i<8;i++) {
if (single(i,j))
return true;
}
}
// if no safe moves, just take whatever you can get
if (safe_from != null) {
move_comp(safe_from,safe_to);
} else {
alert("You win!!");
game_is_over = true;
}
safe_from = safe_to = null;
return false;
}
function jump(i,j) {

if (board[i][j] == -1.1) {
if (legal_move(coord(i,j),coord(i+2,j+2))) {
move_comp(coord(i,j),coord(i+2,j+2));
setTimeout("jump("+(i+2)+","+(j+2)+");",500);
return true;
} if (legal_move(coord(i,j),coord(i-2,j+2))) {
move_comp(coord(i,j),coord(i-2,j+2));
setTimeout("jump("+(i-2)+","+(j+2)+");",500);
return true;
}
} if (integ(board[i][j]) == -1) {
if (legal_move(coord(i,j),coord(i-2,j-2))) {
move_comp(coord(i,j),coord(i-2,j-2));
setTimeout("jump("+(i-2)+","+(j-2)+");",500);
return true;
} if (legal_move(coord(i,j),coord(i+2,j-2))) {
move_comp(coord(i,j),coord(i+2,j-2));
setTimeout("jump("+(i+2)+","+(j-2)+");",500);
return true;
}
}
return false;
}
function single(i,j) {

if (board[i][j] == -1.1) {
if (legal_move(coord(i,j),coord(i+1,j+1))) {
safe_from = coord(i,j);
safe_to = coord(i+1,j+1);
if (wise(coord(i,j),coord(i+1,j+1))) {
move_comp(coord(i,j),coord(i+1,j+1));
return true;
}
} if (legal_move(coord(i,j),coord(i-1,j+1))) {
safe_from = coord(i,j);
safe_to = coord(i-1,j+1);
if (wise(coord(i,j),coord(i-1,j+1))) {
move_comp(coord(i,j),coord(i-1,j+1));
return true;
}
}
} if (integ(board[i][j]) == -1) {
if (legal_move(coord(i,j),coord(i+1,j-1))) {
safe_from = coord(i,j);
safe_to = coord(i+1,j-1);
if (wise(coord(i,j),coord(i+1,j-1))) {
move_comp(coord(i,j),coord(i+1,j-1));
return true;
}
} if (legal_move(coord(i,j),coord(i-1,j-1))) {
safe_from = coord(i,j);
safe_to = coord(i-1,j-1);
if (wise(coord(i,j),coord(i-1,j-1))) {
move_comp(coord(i,j),coord(i-1,j-1));
return true;
}
}
}
return false;
}
function possibilities(x,y) {
if (!jump(x,y))
if (!single(x,y))
return true;
else
return false;
else
return false;
}
function prevent(end,s) {

i = end.x;
j = end.y;
if (!possibilities(s.x,s.y))
return true;
else if ((integ(board[i-1][j+1])==-1) && (legal_move(coord(i-1,j+1),coord(i,j)))) {
return move_comp(coord(i-1,j+1),coord(i,j));
} else if ((integ(board[i+1][j+1])==-1) && (legal_move(coord(i+1,j+1),coord(i,j)))) {
return move_comp(coord(i+1,j+1),coord(i,j));
} else if ((board[i-1][j-1]==-1.1) && (legal_move(coord(i-1,j-1),coord(i,j)))) {
return move_comp(coord(i-1,j-1),coord(i,j));
} else if ((board[i+1][j-1]==-1.1) && (legal_move(coord(i+1,j-1),coord(i,j)))) {
return move_comp(coord(i+1,j-1),coord(i,j));
} else {
return false;
}

}
function wise(from,to) {
i = to.x;
j = to.y;
n = (j>0);
s = (j<7);
e = (i<7);
w = (i>0);
if (n&&e) ne = board[i+1][j-1]; else ne = null;
if (n&&w) nw = board[i-1][j-1]; else nw = null;
if (s&&e) se = board[i+1][j+1]; else se = null;
if (s&&w) sw = board[i-1][j+1]; else sw = null;
eval(((j-from.y != 1)?"s":"n")+((i-from.x != 1)?"e":"w")+"=0;");
if ((sw==0) && (integ(ne)==1)) return false;
if ((se==0) && (integ(nw)==1)) return false;
if ((nw==0) && (se==1.1)) return false;
if ((ne==0) && (sw==1.1)) return false;
return true;
}

alert("Start the Game, select your piece to move.");
my_turn = true;

// -->
</script>



</body>
</html>

Monday, August 6, 2012

"Baghchal"[Tiger and Goat] Project Source code Download


             Baghchal is a traditional board game of Nepal. It is quite simple to setup and play. The board is drawn by a combination of horizontal, vertical, and slanting lines. Each intersecting point can hold a piece, a tiger or a goat. The pieces can only move one step along the lines, except for the case in which a tiger can eat a goat.
The game is played between two players, one represents tiger, four in number, and the other represents goats, twenty in number to start with. The tiger can eat goats by jumping above it, while the goats tries to limit the movement of a tiger.
This is a javascript version of the game, which can be played on the web browser

Game Explanation.


At the start of the game all four tigers are placed on the four corners of the grid, facing the center. All goats start off the board.
The pieces must be put at the intersections of the board lines and moves follow these lines.
The player controlling the goats moves first by placing a goat onto a free intersection on the board. Then it is the tigers' turn. One tiger is then moved to an adjacent position along the lines that indicate the valid moves. Moves are alternate between players.
Tigers capture goats by jumping over them to an adjacent free position. Goats cannot move until all 20 have been put on the board.
The tigers must move according to these rules:
1.    They can start capturing goats any moment after the match has started.
2.    They can capture only one goat at a time.
3.    They can jump over a goat in any direction, but it must be to an adjacent intersection following any of the lines drawn on the board.
4.    A tiger cannot jump over another tiger.
The goats must move according to these rules:
1.    They must leave the board when captured.
2.    They can not jump over the tigers or other goats.
3.    They can only move after all 20 have been put on the board.
The tigers win once they have captured five goats. Goats try to avoid being captured (jumped over) and they win by blocking the tigers' moves till they are unable to move.

Tuesday, July 3, 2012

No Code Completion Suggestion in Netbeans

I was working on a tutorial on netbeans which required to add a DB Report on the jsp page but the code completion tool[ctrl+space] shows no suggestions. If you are having the same problem of code completion in netbeans then here is the solution for it. 

Steps:
1. Open the jsp page, you are working in the editor window.
2. Choose  Window -> Palette
3. In the Palette, Choose the expand the Database option.
4. Click DB Report from the database option and without Releasing your mouse, drag the feature to the position in your file where you want it.
5.Your Insert DB Report dialog appears


You can follow this steps for other components also.

Saturday, May 26, 2012

Installing Apache Ant In Windows

While I was working on my project, I had to use Apache ant to build sample programs. The processes described in apache ant documentation  http://ant.apache.org/manual/install.html is really a tedious task for a beginner like me when it comes to setting up environment variables and path.

Here is a real nice solution for problems like:
 'ant’ is not recognized as an internal or external command, operable program or batch file.

You can simply download the installation file called WinAnt which does all the job for you.
Click Here To Download WinAnt