Snake and Ladder game code in java.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
class Main implements ActionListener,MouseListener
{
JFrame f;
JTabbedPane tabPane;
JPanel mainPanel,introPanel,gamePanel,playerPassPanel,diePanel;
JPanel gameCenter,gameEast,gameWest,gameNorth,gameSouth;
Icon p1,p2,header;
Icon up,down;
Icon icon[][]= new Icon[10][10];
Icon winericon[][]= new Icon[10][10];
Icon dieIcon;
JButton introB[]= new JButton[5];
JButton b[][]=new JButton[10][10];
JButton start,restart;
JButton JBplayer,JBcomputer;
JLabel die;
Random randomNo;
int imageFlag;
int i,j,num;
int prevIp1,prevJp1;
int path;
int p1value,p2value;
int player,computer;
int gameover;
int cimageFlag=0;
int cnoFlag=0;
String str;
int n[][]={ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{10,11,12,13,14,15,16,17,18,19},
{20,21,22,23,24,25,26,27,28,29},
{30,31,32,33,34,35,36,37,38,39},
{40,41,42,43,44,45,46,47,48,49},
{50,51,52,53,54,55,56,57,58,59},
{60,61,62,63,64,65,66,67,68,69},
{70,71,72,73,74,75,76,77,78,79},
{80,81,82,83,84,85,86,87,88,89},
{90,91,92,93,94,95,96,97,98,99},
};
int game[][]={
{100,99,98,97,96,95,94,93,92,91},
{81,82,83,84,85,86,87,88,89,90},
{80,79,78,77,76,75,74,73,72,71},
{61,62,63,64,65,66,67,68,69,70},
{60,59,58,57,56,55,54,53,52,51},
{41,42,43,44,45,46,47,48,49,50},
{40,39,38,37,36,35,34,33,32,31},
{21,22,23,24,25,26,27,28,29,30},
{20,19,18,17,16,15,14,13,12,11},
{ 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
};
int winer[][]={
{ 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
{11,12,13,14,15,16,17,18,19,20},
{21,22,23,24,25,26,27,28,29,30},
{31,32,33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48,49,50},
{51,52,53,54,55,56,57,58,59,60},
{61,62,63,64,65,66,67,68,69,70},
{71,72,73,74,75,76,77,78,79,80},
{81,82,83,84,85,86,87,88,89,90},
{91,92,93,94,95,96,97,98,99,100},
};
Main()
{
f= new JFrame("Snakes and Ladders 1.0");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel= new JPanel();
tabPane =new JTabbedPane(JTabbedPane.LEFT);
f.setLayout(new BorderLayout());
for(int i=0;i<introB.length;i++)
{
introB[i]= new JButton(Integer.toString(i));
}
randomNo= new Random();
JBplayer= new JButton("player1");
JBcomputer= new JButton("player2");
JBplayer.setEnabled(false);
JBcomputer.setEnabled(false);
JBplayer.addActionListener(this);
JBcomputer.addActionListener(this);
die = new JLabel();
game();
introduction();
f.add(tabPane,BorderLayout.CENTER);
f.setResizable(false);
f.setSize(750,700);
//f.pack();
f.setVisible(true);
}
void introduction()
{
introPanel= new JPanel();
JPanel main= new JPanel();
JLabel l1= new JLabel();
//JPanel about= new JPanel();
JLabel l2=new JLabel("<html><body><Font color='red' Size='14'> Hi,</Font><Font color='green' Size='4'>every one this is Snake and Ladders version 1.0 , plz mail if u find any type of problem/error and ur sugession.<br></Font> <Font color='red' Size='3'>thank you visit for more Programs:</Font> <Font color='gray' Size='6'>gauravsiwach.blogspot.com</Font> </body></html>");
Icon mypic;
mypic=new ImageIcon("images/itsme.jpg");
l1.setIcon(mypic);
introPanel.setLayout(new BorderLayout());
introPanel.add(l1,BorderLayout.CENTER);
l2.addMouseListener(this);
l2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
introPanel.add(l2,BorderLayout.SOUTH);
tabPane.addTab("About me",introPanel);
}
void game()
{
JLabel gameheader= new JLabel();
JLabel logo = new JLabel();
JLabel l1= new JLabel();
JLabel l2= new JLabel();
restart= new JButton("Restart");
restart.setEnabled(false);
restart.addActionListener(this);
start= new JButton("Start");
start.addActionListener(this);
gamePanel= new JPanel();
gameCenter=new JPanel();
gameWest= new JPanel();
gameNorth=new JPanel();
gameSouth= new JPanel();
gameCenter.setLayout(new GridLayout(10,10));
gameEast= new JPanel(new GridLayout(2,1));
gameWest.setLayout(new FlowLayout());
gameNorth=new JPanel(new FlowLayout());
gameSouth= new JPanel(new FlowLayout());
gamePanel.setLayout(new BorderLayout());
p1= new ImageIcon("images/p1.gif");
p2= new ImageIcon("images/p2.gif");
up= new ImageIcon("images/up.gif");
down= new ImageIcon("images/down.gif");
l1.setIcon(p1);
l2.setIcon(p2);
header=new ImageIcon("images/header.jpg");
gameheader.setIcon(header);
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
b[i][j]= new JButton();
//b[i][j].addActionListener(this);
path=game[i][j];
str=Integer.toString(path);
icon[i][j]=new ImageIcon("images/"+str+".jpg");
b[i][j].setIcon(icon[i][j]);
}
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
path=winer[i][j];
str=Integer.toString(path);
winericon[i][j]=new ImageIcon("images/winer/"+str+".jpg");
// b[i][j].setIcon(winericon[i][j]);
}
str=Integer.toString(1);
dieIcon= new ImageIcon("images/Game dies/"+str+".jpg");
die.setIcon(dieIcon);
gameSouth.add(l1);
gameSouth.add(JBplayer);
gameSouth.add(die);
gameSouth.add(JBcomputer);
gameSouth.add(l2);
gameNorth.add(start);
gameNorth.add(gameheader);
gameNorth.add(restart);
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
gameCenter.add(b[i][j]);
//System.out.print("["+i+"]["+j+"],");
}
//System.out.println();
}
gamePanel.add(gameCenter,BorderLayout.CENTER);
gamePanel.add(gameWest,BorderLayout.WEST);
gamePanel.add(gameNorth,BorderLayout.NORTH);
gamePanel.add(gameSouth,BorderLayout.SOUTH);
tabPane.addTab("Game",gamePanel);
}
void chance()
{
int n=randomNo.nextInt(3);
int i=0;
//System.out.println("Chacne is :"+n) ;
if(n==0)
chance();
if(n==1)
{
player=1;
computer=0;
}
else if(n==2)
{
player=0;
computer=1;
}
if(n==1)
{
JBcomputer.setEnabled(false);
JBplayer.setEnabled(true);
}
else if(n==2)
{
JBcomputer.setEnabled(true);
JBplayer.setEnabled(false);
}
}
public void actionPerformed(ActionEvent e)
{
int n=0;
try{
if(e.getSource()==JBplayer)
{
//System.out.println("JB Player");
do{
n =playerPassNumber();
}while(n==0);
//System.out.println("UR playerPass value is :"+n);
p1value=p1value+n;
if(p1value>=100)
{
p1value=100;
}
playerimageTraval(p1value);
JBcomputer.setEnabled(true);
JBplayer.setEnabled(false);
setBothImage();
}
else if(e.getSource()==JBcomputer)
{
//System.out.println("JB Computer");
do{
n =playerPassNumber();
}while(n==0);
//System.out.println("Computer Pass Value is "+n);
p2value=p2value+n;
if(p2value>=100)
{
p2value=100;
}
computerimageTraval(p2value);
JBcomputer.setEnabled(false);
JBplayer.setEnabled(true);
setBothImage();
}
else if(e.getSource()==restart)
{
p1value=0;
p2value=0;
cnoFlag=0;
cimageFlag=0;
chance();
rePrint();
}
else if(e.getSource()==start)
{
gameover=0;
p1value=0;
p2value=0;
cnoFlag=0;
cimageFlag=0;
rePrint();
start.setEnabled(false);
restart.setEnabled(true);
chance();
}
else
{
imageChange(e);
}
}catch(Exception ee)
{
//System.out.println("Error in BUTTON:"+ee);
}
}
int playerPassNumber()
{
int i= randomNo.nextInt(7);
str=Integer.toString(i);
dieIcon= new ImageIcon("images/Game dies/"+str+".jpg");
die.setIcon(dieIcon);
return i;
}
void winerdraw()
{
int i=0;
start.setEnabled(true);
restart.setEnabled(false);
JBplayer.setEnabled(false);
JBcomputer.setEnabled(true);
JBcomputer.setEnabled(false);
for(int k=0;k<10;k++)
for(int l=0;l<10;l++)
{
gameover=1;
b[k][l].setIcon(winericon[k][l]);
}
}
void rePrint()
{
try{
for( i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
// b[i][j].setIcon(null);
b[i][j].setIcon(icon[i][j]);
}
}
}catch(Exception e)
{
//System.out.println("error in repaint"+e);
}
}
void playerimageTraval(int n)
{
int i=j=0;
int imageFlag=0;
int noFlag=0;
if(gameover==0)
{
try{
rePrint();
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(n==game[i][j])
{
noFlag=1;
break;
}
}
if(noFlag==1)
break;
}
if(noFlag==1)
{
if(imageFlag==0)
{
Thread.sleep(200);
b[i][j].setIcon(p1);
imageFlag=1;
prevIp1=i;
prevJp1=j;
}
else
{
path=game[prevIp1][prevJp1];
str=Integer.toString(path);
Thread.sleep(200);
icon[prevIp1][prevJp1]=new ImageIcon("images/"+str+".jpg");
Thread.sleep(200);
b[prevIp1][prevJp1].setIcon(icon[prevIp1][prevJp1]);
b[i][j].setIcon(p1);
prevIp1=i;
prevJp1=j;
}
/*****************checks for staires**********************/
if(n==10)
{
n=28;
playerimageTraval(n);
p1value=n;
}
else if(n==17)
{
n=37;
playerimageTraval(n);
p1value=n;
}
else if(n==31)
{
n=70;
playerimageTraval(n);
p1value=n;
}
else if(n==45)
{
n=84;
playerimageTraval(n);
p1value=n;
}
else if(n==78)
{
n=97;
playerimageTraval(n);
p1value=n;
}
/*****************Checks for Snakes***************************/
if(n==95)
{
n=73;
playerimageTraval(n);
p1value=n;
}
else if(n==79)
{
n=59;
playerimageTraval(n);
p1value=n;
}
else if(n==68)
{
n=48;
playerimageTraval(n);
p1value=n;
}
else if(n==44)
{
n=21;
playerimageTraval(n);
p1value=n;
}
else if(n==34)
{
n=16;
playerimageTraval(n);
p1value=n;
}
}
if(n>=100)
{
//System.out.println("Congr u r win");
winerdraw();
}
}
catch(Exception e)
{
}
}
}
void setBothImage()
{
int i=0;
int j=0;
int noFlag=0;
if(gameover==0)
{
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(p1value==game[i][j])
{
noFlag=1;
break;
}
}
if(noFlag==1)
break;
}
if(noFlag==1)
{
b[i][j].setIcon(p1);
}
noFlag=0;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(p2value==game[i][j])
{
noFlag=1;
break;
}
}
if(noFlag==1)
break;
}
if(noFlag==1)
{
b[i][j].setIcon(p2);
}
}
}
void computerimageTraval(int n)
{
int i=j=0;
int imageFlag=0;
int noFlag=0;
try{
rePrint();
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(n==game[i][j])
{
noFlag=1;
break;
}
}
if(noFlag==1)
break;
}
if(noFlag==1)
{
if(imageFlag==0)
{
Thread.sleep(200);
b[i][j].setIcon(p2);
imageFlag=1;
prevIp1=i;
prevJp1=j;
}
else
{
path=game[prevIp1][prevJp1];
str=Integer.toString(path);
Thread.sleep(200);
icon[prevIp1][prevJp1]=new ImageIcon("images/"+str+".jpg");
Thread.sleep(200);
b[prevIp1][prevJp1].setIcon(icon[prevIp1][prevJp1]);
b[i][j].setIcon(p1);
prevIp1=i;
prevJp1=j;
}
/*****************checks for staires**********************/
if(n==10)
{
n=28;
computerimageTraval(n);
p2value=n;
}
else if(n==17)
{
n=37;
computerimageTraval(n);
p2value=n;
}
else if(n==31)
{
n=70;
computerimageTraval(n);
p2value=n;
}
else if(n==45)
{
n=84;
computerimageTraval(n);
p2value=n;
}
else if(n==78)
{
n=97;
computerimageTraval(n);
p2value=n;
}
/*****************Checks for Snakes***************************/
if(n==95)
{
n=73;
computerimageTraval(n);
p2value=n;
}
else if(n==79)
{
n=59;
computerimageTraval(n);
p2value=n;
}
else if(n==68)
{
n=48;
computerimageTraval(n);
p2value=n;
}
else if(n==44)
{
n=21;
computerimageTraval(n);
p2value=n;
}
else if(n==34)
{
n=16;
computerimageTraval(n);
p2value=n;
}
}
if(n>=100)
{
//System.out.println("Congr u r win");
gameover=1;
winerdraw();
}
}
catch(Exception e)
{
}
}
void imageChange(ActionEvent e)
{
int i=j=0;
int flag=0;
try{
for( i=0;i<10;i++)
{
for( j=0;j<10;j++)
{
if(e.getSource()==b[i][j])
{
//System.out.println("Button Found");
//System.out.println("b["+i+"]["+j+"]");
flag=1;
break;
}
//System.out.println(j);
}
if(flag==1)
break;
}
if(imageFlag==0)
{
Thread.sleep(200);
b[i][j].setIcon(p1);
imageFlag=1;
prevIp1=i;
prevJp1=j;
}
else
{
path=game[prevIp1][prevJp1];
str=Integer.toString(path);
Thread.sleep(200);
icon[prevIp1][prevJp1]=new ImageIcon("images/"+str+".jpg");
Thread.sleep(200);
b[prevIp1][prevJp1].setIcon(icon[prevIp1][prevJp1]);
b[i][j].setIcon(p1);
prevIp1=i;
prevJp1=j;
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
/******************************Methods of MouseListener****************************/
public void mouseClicked(MouseEvent ae)
{
openURL("http://gauravsiwach.blogspot.com");
System.out.println("Clicked");
}
public void mouseEntered(MouseEvent ae)
{
}
public void mouseExited(MouseEvent ae)
{
}
public void mousePressed(MouseEvent ae)
{
}
public void mouseReleased(MouseEvent ae)
{
}
/*******************************BrowserController*********************************/
public static void openURL(String url)
{
String osName = System.getProperty("os.name");
try {
if (osName.startsWith("Windows"))
Runtime.getRuntime().exec(
"rundll32 url.dll,FileProtocolHandler " + url);
else {
String[] browsers = { "firefox", "opera"};
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++)
if (Runtime.getRuntime().exec(
new String[] { "which", browsers[count] })
.waitFor() == 0)
browser = browsers[count];
Runtime.getRuntime().exec(new String[] { browser, url });
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error in opening browser"
+ ":\n" + e.getLocalizedMessage());
}
}
public static void main(String args[])
{
Main m= new Main();
}
}
Game running
you can download Source code
Snake And Ladders code
hello sir,i need a snake and ladder complete document sir....Snake And Ladders version 1.0 Game in java
ReplyDeletethanx for comment , give me ur email id m will forward u soon... happy programming
ReplyDeleteCan u send the codes to me also with complete docu??
Deletelokibisko@yahoo.com Thanks a lot :D
Can you send me the Code pls for my Final project :'( Urbuda.venchlouie04@gmail.com or venchlouieuy@yahoo.com Ty
Deletecan you send me the code please for a school project :/ dylanberetta@hotmail.co.uk or 13berettad@highcliffeschool.com Ty
Deletehello sir, thnx for ths code
Deleteur downlod link is not working sir, so can you plz send me the complete code & documentaion file on srchavhan12@gmail.com
ths'll realy help me a lot sir...Thanx in advance
hello there sir, what if every time i restart the game the snakes and ladder are placed in the different location? is there a easy way to do that? thx
ReplyDeleteyaa, basically these snakes and ladder board is a full image that's divided into 100 pies , so if u want randomly generated (show differently every time when you restart the game.) then have need to save this image into our source code.
Deletethank you Gaurav
Deletewelcome Emmanuel Santiago.. happy programming..
Deletehello sir,i need a snake and ladder complete document (Snake And Ladders version 1.0 )
Delete& what the logic behind the travaling d image,& and why did u use repaint
ReplyDeletebasically the board is array(10*10=100) of button, that's contain(10*10=100) images,the code is remind counting score of every player(p1,p2). then according to score set the player icon on a particular button. if u have need any kind of document or help then you can freely send mail to my id, gauravsiwach2008@gmail.com, and you also can like the my facebook page for get more update https://www.facebook.com/techadhyan
Deletehello , sir can i have a complete document of your snakes and ladders?. :) ? this is my email brionesbryle@yahoo.com or brionesbryle181995@gmail.com
ReplyDeleteThank you :)
thanx for visit Bryle, which kind of document have you need. you can download source code with all resources form given links.
DeleteI am looking to create a sales board game based on an existing board game used to help train people on sales techniques.
ReplyDeleteWould I be able to talk to you about having you create this game for us?
My email address is terry.lewis@tmhrconsulting.com
check you mail
Deletecan i get a complete codes ? so that i can learn more of this . please
ReplyDeletehi @jingle, yaa, you can download from attached zip file in the below of the code..
Deletehiii sir...can u forward whole project and source code for snakes and laders....
ReplyDeleteakhilashlakma534@gmail.com
DeleteHi Akhilash
Deleteyou can download source code from https://www.dropbox.com/s/e53jzvk73sdynwb/GameDesgin%28800-600%29.zip
and also like our facebook page
https://www.facebook.com/techadhyan
So i am new to java i was just wondering what does the (n) stand for - new counter or what
ReplyDeleteif(n==95)
{
n=73;
computerimageTraval(n);
p2value=n;
}
else if(n==79)
{
n=59;
computerimageTraval(n);
p2value=n;
}
else if(n==68)
{
hey Edd,
DeletecomputerimageTraval(int n){} this is function for travel image on board. so n is counter number where your image will render, and if(n==95) this is the check for snake, if you are on 95 then u will fall down on snake tail at 73.
Sir can you please email me the source code as well. Rockytabi@gmail.com. that would be very help thanks.
ReplyDeleteSir can you please email me the source code as well. Rockytabi@gmail.com. that would be very help thanks.
ReplyDeleteHey Tabi, source code also available here. u can find in link just below of blog ending.
Deletesir can you please share the code on ninayjain03@gmail.com
Deleteor
jainbrothers339@gmail.com
or
nityat950@gmail.com
or
ninayjain06@gmail.xom
Sir ! Why can't I see the graphics when I execute the program ? I only see blue boxes but no images.
ReplyDeletesir cam you give me the full version of this program? i badly needed this for my project ....i only see boxes ...please sir help me....
ReplyDeletesir cam you give me the full version of this program? i badly needed this for my project ....i only see boxes ...please sir help me....
ReplyDeleteHi Bliss, download full source code that is attached on link below of the post. there is images, if u copy and paste this program then u see only boxes,(they are buttons). so u have to download whole code.
Deletesir can u plz send the report of the project also
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehello sir,i need a snake and ladder complete document can u send to me email (skyrios26@outlook.com)
ReplyDeletehello sir i have a question can i put that game on a cellphone / android cellphone???
ReplyDeleteemail me at janbuizon@gmail.com
hello sir i have a question can i put that game on a cellphone / android cellphone???
ReplyDeleteemail me at janbuizon@gmail.com
hello sir i need to know if i can insert or put that game into a phone / android?
ReplyDeleteemail me at janbuizon@gmail.com
Hello Gaurav Sir
ReplyDeleteCan you send me the complete documentation of this code?
iamramesh214@gmail.com
Hello Gaurav Sir
ReplyDeleteCan you send me the complete documentation of this code
keleychy1@gmail.com
sir the download link is not working can you mail the whole code at shubhamsingh28061996@gmail.com
ReplyDeletesir the download link is not working can you mail the whole code at shubhamsingh28061996@gmail.com
ReplyDeleteSir, the download link is not working. mind send it to yeongjong.jong@gmail.com
ReplyDeletehttps://drive.google.com/open?id=0B1Y2bTzMvCSyU2lBd2tFLXg0b1k
DeleteSeƱor me podria ayudar con el codigo de este juego cristfernando94@gmail.com gracias de antemano
ReplyDeletehttps://drive.google.com/open?id=0B1Y2bTzMvCSyU2lBd2tFLXg0b1k
DeleteHell Sir,
ReplyDeleteI was wondering if I can get he documentation?
smnhasan@gmail.com
Kind Regards,
Naz
Hi can you send me the code plz?
ReplyDeleteHi, I was wondering on how to separate this one main file into relevant classes for example a dice class and when the program is run the class will be called something like that as my project requires me to not put all the codes in one class as it is hard to keep track and my report also requires separate classes and not one whole main file. My email is pdash41@gmail.com
ReplyDeleteSorry for late reply, you have to write separate class for every object. Board, Dice, Player with there needed property and methods and have to handle from Main Class. little busy these days, i will also upload new code with separate and relevant classes.
DeletecnoFlag=0; what does this code do?
ReplyDeletecould you plz send me the full source code. when i'm running that source code ,it is showing only the blocks, nothing else is there. my mail id raunakmir@yahoo.com
ReplyDeleteSir, I will be highly benefited if I could run the code. could you plz send me the full documentation for snake and ladder game. when i'm running that source code,it is showing only the blocks. My mail id rubelsu1@gmail.com.
ReplyDeletehello sir,i need a snake and ladder complete document sir....Snake And Ladders Game in java swing
ReplyDeleteSnake and Ladder Game always gives a fun and enjoyment. Playing Snake & Ladder Game Online become the new trend.
ReplyDeleteya but its not working
DeleteI recently saw your video on You tube. That was so amazing. Sir Can you please send me the Source code file. in PDF it asks Permission to download
ReplyDeleteI recently saw your video on You tube. That was so amazing. Sir Can you please send me the Source code file. in PDF it asks Permission to download.
ReplyDeletemy mail is- sukantasamal471@gmail.com
Plz sir send me this source code and folder on my gmail account
ReplyDeletegamingbd987@gmail.com