rendered paste bodyimport java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.lang.Math;
import java.io.*;
import javax.swing.*;
import java.util.*;
/*
<applet code = "SnakesAndLadders" width = 800 , height = 700>
</applet>
*/
public class SnakesAndLadders extends Applet implements Runnable , MouseListener
{
final int BRDSZ = 10;
int [][] snakeCells;
int [][] ladderCells;
int nSnakes;
int nLadders;
int nPlayers;
int [][] players ;
Color [] plrColors;
volatile boolean diceToBeRolled = false;
volatile boolean diceBttnVisible = true;
volatile boolean bttnPressed = false;
volatile boolean isPaused = false;
Thread t ;
int currPlayer ;
int currDiceValue;
int maxX , maxY;
int dotRadius , playerRadius;
int mouseX , mouseY;
int bttnX , bttnY , bttnWidth , bttnHeight;
Image backbuff;
Graphics bg;
Color cellColors [] ;
public void init()
{
int [][] temp = {
{8 , 8 , 5 , 9} , {2 , 6 , 2 , 8} , {0 , 2 , 3 , 5} ,
{4 , 3 , 7 , 4} , {8 , 0 , 7 , 5}
};
snakeCells = temp ;
int [][] temp2 = {
{4 , 9 , 5 , 5} , {9 , 8 , 8 , 7} , {1 , 7 , 0 , 3} ,
{9 , 5 , 7 , 3} , {5 , 2 , 6 , 1} , {2 , 2 , 4 , 0}
};
ladderCells = temp2;
nPlayers = 0 ;
while (!(nPlayers > 1))
{
try
{
nPlayers = Integer.parseInt(JOptionPane.showInputDialog (null , "Enter number of players (> 2) : "));
}
catch (Exception e)
{
}
}
players = new int [nPlayers][2];
plrColors = new Color [nPlayers];
Random r = new Random();
for (int i = 0 ; i < nPlayers ; i++)
{
players[i][0] = 0 ;
players[i][1] = BRDSZ - 1;
plrColors[i] = new Color (r.nextInt(256) , r.nextInt(256) , r.nextInt(256));
}
maxX = getSize().width ;
maxY = getSize().height;
setBackground (Color.black);
dotRadius = 7 ;
playerRadius = 15 ;
backbuff = createImage (maxX, maxY);
bg = backbuff.getGraphics();
bttnX = 665 ;
bttnY = 265 ;
bttnWidth = 110 ;
bttnHeight = 50;
cellColors = new Color[100] ;
for (int i = 0 ; i < 100 ; i++)
{
cellColors [i] = new Color (r.nextInt(256) , r.nextInt(256) , r.nextInt(256));
}
addMouseListener (this);
}
public void start()
{
if (t == null)
{
t = new Thread (this);
t.start();
}
}
public void run()
{
Random r = new Random ();
try
{
repaint();
Thread.sleep (1500);
showStatus ("Rolling dices for starting move");
Thread.sleep (1000);
currPlayer = r.nextInt(nPlayers);
showStatus ("Player " + (currPlayer + 1) + " will make first move");
Thread.sleep (1000);
showStatus("Player " + (currPlayer + 1) + " ready ?");
Thread.sleep (1000);
diceToBeRolled = false;
diceBttnVisible = true;
while (true)
{
while (!diceToBeRolled)
{
isPaused = true;
Thread.sleep(100);
}
isPaused = false;
diceBttnVisible = false;
currDiceValue = r.nextInt(6) + 1;
for (int i = 0 ; i < 7 ; i++)
{
currDiceValue = r.nextInt(6) + 1;
showStatus ("Rolling Dice .........");
repaint ();
Thread.sleep ((i+1) * 100);
}
showStatus ("Player " + (currPlayer + 1) + " rolls a " + currDiceValue);
Thread.sleep(1000);
int temp = currDiceValue;
while (temp > 0)
{
if (players[currPlayer][0] == 0 && players[currPlayer][1] == 0)
{
showStatus( "Player " + (currPlayer + 1) + " wins");
repaint();
Thread.sleep (2000);
System.exit(0) ;
}
if (players [currPlayer][1] % 2 != 0)
{
if (players [currPlayer][0] < (BRDSZ - 1))
players [currPlayer][0] ++ ;
else if (players [currPlayer][1] > 0)
players [currPlayer][1] -- ;
}
else
{
if (players [currPlayer][0] > 0)
players [currPlayer][0]--;
else if (players [currPlayer][1] > 0)
players [currPlayer][1] -- ;
}
temp --;
repaint();
Thread.sleep (1000);
}
int x = players[currPlayer][0];
int y = players[currPlayer][1];
int i;
for (i = 0 ; i < snakeCells.length ; i++)
{
if (snakeCells[i][0] == x && snakeCells[i][1] == y)
{
showStatus ("AHHHHHHHH. You stepped on a snake . The snake eats you.");
repaint ();
Thread.sleep (2000);
players [currPlayer][0] = snakeCells[i][2];
players [currPlayer][1] = snakeCells[i][3];
repaint ();
Thread.sleep (2000);
break;
}
}
for (i = 0 ; i < ladderCells.length ; i++)
{
if (ladderCells[i][0] == x && ladderCells[i][1] == y)
{
showStatus ("Ohhhhhhhhh. You climbed a ladder." );
repaint ();
Thread.sleep (2000);
players [currPlayer][0] = ladderCells[i][2];
players [currPlayer][1] = ladderCells[i][3];
repaint ();
Thread.sleep (2000);
}
}
repaint();
Thread.sleep (100);
currPlayer = (currPlayer + 1) % nPlayers;
showStatus( "Player " + (currPlayer + 1) + " ready ?");
diceBttnVisible = true;
diceToBeRolled = false;
repaint();
}
}
catch (InterruptedException e)
{
System.out.println ("Game Interrupted.\n Stacktrace : ");
e.printStackTrace();
}
}
public void mouseEntered( MouseEvent e )
{}
public void mouseExited( MouseEvent e )
{}
public void mouseClicked( MouseEvent e )
{}
public void mousePressed( MouseEvent e )
{
int x = e.getX();
int y = e.getY();
if (x >= bttnX && x <= (bttnX + bttnWidth) && y >= bttnY && y <= (bttnY + bttnHeight))
{
bttnPressed = true;
repaint();
}
e.consume();
}
public void mouseReleased( MouseEvent e )
{
int x = e.getX();
int y = e.getY();
if (x >= bttnX && x <= (bttnX + bttnWidth) && y >= bttnY && y <= (bttnY + bttnHeight))
{
bttnPressed = false;
repaint();
diceToBeRolled = true;
}
e.consume();
}
public void update(Graphics g)
{
//Clear Screen
bg.clearRect(0, 0, maxX, maxY);
//Draw board
bg.translate (20 , 20);
int boardWidth = 3 * maxX / 4 ;
int boardHeight = boardWidth ;
int gridSize = boardWidth / 10 ;
int i , j ;
bg.setColor(Color.white);
bg.drawRect (0 , 0 , boardWidth , boardHeight);
for (i = 1 ; i < 10 ; i++)
{
bg.drawLine (i * gridSize , 0 , i * gridSize , boardHeight);
bg.drawLine (0 , i * gridSize , boardWidth , i * gridSize);
}
//Draw Numbers
for (int n = 1 ; n < BRDSZ * BRDSZ ; n++)
{
int x , y ;
x = (n % 10 ) - 1;
if ((n / 10) % 2 != 0 && (n % 10) != 0)
x = 9 - x ;
if (n % 10 == 0)
{
if (n % 20 == 0)
x = 0 ;
else
x = 9 ;
}
y = 0 ;
while ((100 - (y + 1) * 10) > n)
y++ ;
if (n % 10 == 0)
y++ ;
bg.setColor(cellColors [n - 1]);
bg.drawString(n + "" , x * gridSize + gridSize / 2 - 3, y * gridSize + gridSize / 2 + 3);
}
bg.setColor(cellColors [99]);
bg.drawString(100 + "", gridSize / 2 - 3, gridSize / 2 + 3);
//Draw snakes and ladders
bg.setColor(Color.red);
for (i = 0 ; i < snakeCells.length ; i++)
{
bg.drawLine(gridSize * snakeCells[i][0] + gridSize / 2 , gridSize * snakeCells[i][1] + gridSize / 2 , gridSize * snakeCells[i][2] + gridSize / 2 , gridSize * snakeCells[i][3] + gridSize / 2);
}
bg.setColor(Color.green);
for (i = 0 ; i < ladderCells.length ; i++)
{
bg.drawLine(gridSize * ladderCells[i][0] + gridSize / 2 , gridSize * ladderCells[i][1] + gridSize / 2 , gridSize * ladderCells[i][2] + gridSize / 2 , gridSize * ladderCells[i][3] + gridSize / 2);
}
//Draw Players
int x , y ;
for (i = 0 ; i < nPlayers ; i++)
{
x = players [i][0] * gridSize + gridSize / 2;
y = players [i][1] * gridSize + gridSize / 2;
bg.setColor(plrColors[i]);
bg.fillOval(x - playerRadius, y - playerRadius, 2 * playerRadius , 2 * playerRadius);
bg.setColor (Color.white);
bg.drawString(i + 1 + "", x - 5 , y + 5);
}
//Draw dice
bg.translate(boardWidth + 50, 20);
bg.setColor(Color.white);
bg.fillRoundRect(0, 0, 100, 100 , 20 , 20);
bg.translate (50 , 50);
bg.setColor (Color.black);
switch (currDiceValue)
{
case 1 :
bg.fillOval (-dotRadius , -dotRadius , 2 * dotRadius , 2 * dotRadius );
break;
case 2 :
bg.fillOval (-30 , -dotRadius , 2 * dotRadius , 2 * dotRadius );
bg.fillOval ( 30 - 2 * dotRadius , -dotRadius , 2 * dotRadius , 2 * dotRadius );
break;
case 3 :
bg.fillOval (-40 , -dotRadius , 2 * dotRadius , 2 * dotRadius );
bg.fillOval ( 40 - 2 * dotRadius , -dotRadius , 2 * dotRadius , 2 * dotRadius );
bg.fillOval (-dotRadius , -dotRadius , 2 * dotRadius , 2 * dotRadius );
break;
case 4 :
bg.fillOval (-dotRadius - 20 , -40 +dotRadius , 2 * dotRadius , 2 * dotRadius );
bg.fillOval (-dotRadius + 20 , -40 +dotRadius , 2 * dotRadius , 2 * dotRadius );
bg.fillOval (-dotRadius - 20 , dotRadius + 10 , 2 * dotRadius , 2 * dotRadius );
bg.fillOval (dotRadius + 5 , dotRadius + 10 , 2 * dotRadius , 2 * dotRadius );
break;
case 5 :
bg.fillOval (-dotRadius - 20 , -dotRadius - 20 , 2 * dotRadius , 2 * dotRadius );
bg.fillOval ( dotRadius + 10 , -dotRadius - 20 , 2 * dotRadius , 2 * dotRadius );
bg.fillOval (-dotRadius - 20 , dotRadius + 10 , 2 * dotRadius , 2 * dotRadius );
bg.fillOval (dotRadius + 10 , dotRadius + 10 , 2 * dotRadius , 2 * dotRadius );
bg.fillOval (-dotRadius + 1, -dotRadius , 2 * dotRadius , 2 * dotRadius );
break;
case 6 :
bg.fillOval (-40 , -dotRadius - 25 , 2 * dotRadius , 2 * dotRadius );
bg.fillOval ( 40 - 2 * dotRadius , -dotRadius - 25, 2 * dotRadius , 2 * dotRadius );
bg.fillOval (-dotRadius , -dotRadius - 25, 2 * dotRadius , 2 * dotRadius );
bg.fillOval (-40 , dotRadius + 15 , 2 * dotRadius , 2 * dotRadius );
bg.fillOval ( 40 - 2 * dotRadius , dotRadius + 15, 2 * dotRadius , 2 * dotRadius );
bg.fillOval (-dotRadius , dotRadius + 15, 2 * dotRadius , 2 * dotRadius );
break;
default:
}
//Draw Button
bg.translate (0 , 200);
if (diceBttnVisible)
{
bg.setColor (Color.gray);
bg.fill3DRect (-bttnWidth / 2 , -bttnHeight / 2 , bttnWidth , bttnHeight , !bttnPressed);
bg.setColor (Color.white);
bg.drawString("Press to roll dice" , -47 , 5);
}
//Reset back to original position
bg.translate (0 , -200);
bg.translate (-50 , -50);
bg.translate(-boardWidth - 50, -20);
bg.translate (-20 , -20);
g.drawImage(backbuff , 0 , 0 , this);
}
public void paint(Graphics g)
{
update (g);
}
}