Snakes N Ladders Game Java

The Code Below Shows How To Develop SnakesNLadder Game

Step1 : Create a directory to store your snakesnladder image

Step2 : Create a java file and place a main JPanel. In This JPanel Place SnakenLadder Image.

Step 3 : Now place a new JLayeredPane. JLayeredPane Helps you to place panel above a panel. This panel will be a player that will move when the dice is played.

Step 4 : Take maximum width and maximum height of the main panel. Use maximum width as maximum X valu and maximum height as Maximum Y value.Initially  keep the players  position at maximum height and minimum width. Eg X=450 Y=450 then player.x = 25 and player.y = 425

Step 5 : Make a button that gives the dice value. Use this value to move the player. Eg if dice gives value as 6 then add 25 to player.x value till it reaches maximum X value.

Step 6 : If player.x reaches the Maximum X value, reduce the player.y value(eg if player.x = 425 then player.y = 400) and start reducing the player.x value till reaches to the minimum X value
Step 7 : Repeat this procedure till the player.x and player.y value reaches to the 100 block

Note : Even thought the code has the login snake bite and climbing ladder still you should use some logic for snake bite and ladder climbing to improve your logics


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import java.io.*;

public class SnakeLadder extends JFrame implements ActionListener
{
JPanel jp,southPanel,jp2,jp3,northPanel,smiley;
JLayeredPane jp1;
JLabel l1,l2,l3,l4,move,position;
private Timer timer,timer1;
private int delay = 50,delay1 = 1000;
int i=1,j=1,counter=1, x = 10, y = 370, swt = 1, score = 500, moves = 0,a=10,b=370,cur_pos=1,cur_pos1=1;
JButton btnStart,btnStop,btnPlay,btnUndo;
public SnakeLadder()
{
jp = (JPanel)getContentPane();
jp.setLayout(new BorderLayout());
southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
northPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
jp1 = new JLayeredPane();
jp2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
jp3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
smiley = new JPanel(new FlowLayout(FlowLayout.CENTER));
jp3.setBackground(Color.BLUE);
smiley.setBounds(-10,-10,15,20);
jp3.setBounds(10,370,20,20);
jp2.setBounds(0,0,400,400);
l1 = new JLabel("Score = 500");
l2 = new JLabel("Ladders");
l3 = new JLabel("Snakes");
l4 = new JLabel("1");
position = new JLabel("Position = 1");
move = new JLabel("Moves = 0");
btnStart = new JButton("Start");
btnStop = new JButton("Stop");
btnPlay = new JButton("New Game");
btnUndo = new JButton("Undo");
timer = new Timer(delay,new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
if(i > 0)
                {
                    if(i != 7)
{
counter = i;
                    l4.setText("" + counter);
i++;
}
else
{
i = 1;
}
                }
}
});

timer1 = new Timer(delay1,new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
//jp3.setBounds(x,y,20,20);
timer1.stop();
}
});
jp.add(jp1,BorderLayout.CENTER);
jp2.setOpaque(true);
JLabel background=new JLabel(new ImageIcon("Snakes-Ladders.jpg"));
JLabel background1=new JLabel(new ImageIcon("smileyevil.png"));
jp2.add(background);
smiley.add(background1);
jp1.add(jp2, new Integer(0),0);
jp1.add(jp3, new Integer(1),0);
jp1.add(smiley, new Integer(2),0);
//smiley.setVisible(false);
jp.add(northPanel,"North");
jp.add(l2,"East");
jp.add(l3,"West");
jp.add(southPanel,"South");
southPanel.add(btnStart);
southPanel.add(btnStop);
southPanel.add(btnPlay);
southPanel.add(btnUndo);
southPanel.add(l4);
northPanel.add(l1);
northPanel.add(move);
northPanel.add(position);
btnStart.addActionListener(this);
btnStop.addActionListener(this);
btnPlay.addActionListener(this);
btnUndo.addActionListener(this);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == btnUndo)
{
x = a;
y = b;
moves++;
jp3.setBounds(x,y,20,20);
score = score - 5;
if(y == 330 || y == 250 || y == 170 || y == 90 || y == 10)
{
swt = 2;
}
else //if(x == 370 || x == 290 || x == 210 || x == 130 || x == 80)
{
swt = 1;
}
System.out.println("X = " + x + " Y = " + y);
}
if(ae.getSource() == btnPlay)
{
        timer.stop();
btnStop.setEnabled(true);
btnStart.setEnabled(true);
i=1;j=1;counter=1; x = 10; y = 370; swt = 1; score = 500; moves = 0;a=10;b=370;cur_pos=1;
jp3.setBounds(x,y,20,20);
l1.setText("Score = 500");
l4.setText("1");
position.setText("Postion = 1");
move.setText("Moves = 0");
}
if(ae.getSource() == btnStart)
{
smiley.setVisible(false);
        timer.start();
btnStart.setEnabled(false);
btnStop.setEnabled(true);
score= score - 5;
l1.setText("Score = " + score);
}
if(ae.getSource() == btnStop)
{
        timer.stop();
System.out.println("Counter = " + counter);
btnStart.setEnabled(true);
btnStop.setEnabled(false);
moves++;
a = x;
b = y;
if(x == 10 && y == 370 && counter != 6)
{
counter = 1;
}
if(x == 10 && y == 370)
{
counter--;
}
if(x == 210 && y == 10 && counter > 5)
{}
else if(x == 170 && y == 10 && counter > 4)
{}
else if(x == 130 && y == 10 && counter > 3)
{}
else if(x == 90 && y == 10 && counter > 2)
{}
else if(x == 50 && y == 10 && counter > 1)
{}
else
{
while(j <= counter)
{
if(swt == 1)
{
if(x == 370)
{
if(counter == 1)
{
y = y - 40;
swt = 2;
}
else
{
x = 370;
y = y - 40;
swt = 2;
}
}
else
{
x = x + 40;
}
j++;
}
if(swt == 2)
{

if(x == 10)
{
if(counter == 1)
{
y = y - 40;
swt = 1;
}
else
{
x = 10;
y = y - 40;
swt = 1;
}
}
else
{
x = x - 40;
}
j++;
}
}
j = 1;
timer1.start();
jp3.setBounds(x,y,20,20);
}

move.setText("Moves = " + moves);

// LADDERS

if(x == 130 && y == 370)
{
x = 250;
y = 330;
jp3.setBounds(x,y,20,20);
swt = 2;
}
if(x == 330 && y == 370)
{
x = 370;
y = 250;
jp3.setBounds(x,y,20,20);
swt = 2;
}
if(x == 50 && y == 330)
{
x = 90;
y = 250;
jp3.setBounds(x,y,20,20);
swt = 2;
}
if(x == 10 && y == 290)
{
x = 50;
y = 210;
jp3.setBounds(x,y,20,20);
swt = 1;
}
if(x == 290 && y == 290)
{
x = 130;
y = 50;
jp3.setBounds(x,y,20,20);
swt = 1;
}
if(x == 370 && y == 170)
{
x = 250;
y = 130;
jp3.setBounds(x,y,20,20);
swt = 2;
}
if(x == 370 && y == 90)
{
x = 370;
y = 10;
jp3.setBounds(x,y,20,20);
swt = 2;
}
if(x == 10 && y == 90)
{
x = 10;
y = 10;
jp3.setBounds(x,y,20,20);
//swt = 2;
}
// SNAKES
if(x == 130 && y == 330)
{
smiley.setVisible(true);
smiley.setBounds(x,y,15,20);
x = 250;
y = 370;
jp3.setBounds(x,y,20,20);
swt = 1;
}
if(x == 250 && y == 170)
{
smiley.setVisible(true);
smiley.setBounds(x,y,15,20);
x = 250;
y = 250;
jp3.setBounds(x,y,20,20);
swt = 2;
}
if(x == 50 && y == 130)
{
smiley.setVisible(true);
smiley.setBounds(x,y,15,20);
x = 50;
y = 330;
jp3.setBounds(x,y,20,20);
swt = 2;
}
if(x == 130 && y == 130)
{
smiley.setVisible(true);
smiley.setBounds(x,y,15,20);
x = 10;
y = 170;
jp3.setBounds(x,y,20,20);
swt = 2;
}
if(x == 250 && y == 50)
{
smiley.setVisible(true);
smiley.setBounds(x,y,15,20);
x = 130;
y = 290;
jp3.setBounds(x,y,20,20);
swt = 1;
}
if(x == 290 && y == 10)
{
smiley.setVisible(true);
smiley.setBounds(x,y,15,20);
x = 290;
y = 90;
jp3.setBounds(x,y,20,20);
swt = 2;
}
if(x == 210 && y == 10)
{
smiley.setVisible(true);
smiley.setBounds(x,y,15,20);
x = 210;
y = 90;
jp3.setBounds(x,y,20,20);
swt = 2;
}
if(x == 90 && y == 10)
{
smiley.setVisible(true);
smiley.setBounds(x,y,15,20);
x = 50;
y = 90;
jp3.setBounds(x,y,20,20);
swt = 2;
}
System.out.println("X = " + x + " Y = " + y);

// Position

if(y == 370)
{
cur_pos = (x - 10)/40;
cur_pos = cur_pos + 1;
position.setText("Position = " + cur_pos);

}
if(y == 330)
{
cur_pos = (x - 10)/40;
cur_pos = 10 - cur_pos + 10;
position.setText("Position = " + cur_pos);

}
if(y == 290)
{
if(x == 10)
{
cur_pos = (x - 10)/40;
cur_pos = cur_pos + 1 + 20;
position.setText("Position = " + cur_pos);
}
else
{
cur_pos = (x - 10)/40;
cur_pos = cur_pos + 21;
position.setText("Position = " + cur_pos);
}

}
if(y == 250)
{
cur_pos = (x - 10)/40;
cur_pos = 10 - cur_pos + 30;
position.setText("Position = " + cur_pos);

}
if(y == 210)
{
if(x == 10)
{
cur_pos = (x - 10)/40;
cur_pos = cur_pos + 1 + 40;
position.setText("Position = " + cur_pos);
}
else
{
cur_pos = (x - 10)/40;
cur_pos = cur_pos + 41;
position.setText("Position = " + cur_pos);
}

}
if(y == 170)
{
cur_pos = (x - 10)/40;
cur_pos = 10 - cur_pos + 50;
position.setText("Position = " + cur_pos);

}
if(y == 130)
{
if(x == 10)
{
cur_pos = (x - 10)/40;
cur_pos = cur_pos + 1 + 60;
position.setText("Position = " + cur_pos);
}
else
{
cur_pos = (x - 10)/40;
cur_pos = cur_pos + 61;
position.setText("Position = " + cur_pos);
}

}
if(y == 90)
{
cur_pos = (x - 10)/40;
cur_pos = 10 - cur_pos + 70;
position.setText("Position = " + cur_pos);

}
if(y == 50)
{
if(x == 10)
{
cur_pos = (x - 10)/40;
cur_pos = cur_pos + 1 + 80;
position.setText("Position = " + cur_pos);
}
else
{
cur_pos = (x - 10)/40;
cur_pos = cur_pos + 81;
position.setText("Position = " + cur_pos);
}

}
if(y == 10)
{
cur_pos = (x - 10)/40;
cur_pos = 10 - cur_pos + 90;
position.setText("Position = " + cur_pos);

}
if(score == 0)
{
JOptionPane.showMessageDialog(this,"Game Over. Your Score is " + score);
int ans = JOptionPane.showConfirmDialog(this,"New Game","Game",JOptionPane.YES_NO_OPTION);
if(ans == JOptionPane.YES_OPTION)
{
        timer.stop();
btnStop.setEnabled(true);
btnStart.setEnabled(true);
i=1;j=1;counter=1; x = 10; y = 370; swt = 1; score = 500; moves = 0;a=10;b=370;
jp3.setBounds(x,y,20,20);
l1.setText("Score = 500");
l4.setText("1");
move.setText("Moves = 0");
position.setText("Position = 1");
}
else if(ans ==JOptionPane.NO_OPTION)
{
System.exit(0);
}
}
if(x == 10 && y == 10)
{
JOptionPane.showMessageDialog(this,"You Won. Your Score Is " + score);
int ans = JOptionPane.showConfirmDialog(this,"New Game","Game",JOptionPane.YES_NO_OPTION);
if(ans == JOptionPane.YES_OPTION)
{
        timer.stop();
btnStop.setEnabled(true);
btnStart.setEnabled(true);
i=1;j=1;counter=1; x = 10; y = 370; swt = 1; score = 500; moves = 0;a=10;b=370;
jp3.setBounds(x,y,20,20);
l1.setText("Score = 500");
l4.setText("1");
move.setText("Moves = 0");
position.setText("Position = 1");
}
else if(ans ==JOptionPane.NO_OPTION)
{
System.exit(0);
}
}
}
}

public static void main(String[] args)
{
SnakeLadder sl = new SnakeLadder();
sl.setSize(500,500);
sl.setVisible(true);
sl.setResizable(false);
}
}

2 comments:

  1. hey all you can take this code to expand the working of the game
    Thank You

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete