// Board.java: draw Chutes and Ladders board
import java.applet.Applet;
import java.awt.*;
public class Board {
public final int X_CORNER = 30;
public final int Y_CORNER = 70;
public final int SIDE = 40;
private int[] cLArray =
{-1, 38, 0, 0, 14, 0, 0, 0, 0, 31, 0,
0, 0, 0, 0, 0, 6, 0, 0, 0, 0,
42, 0, 0, 0, 0, 0, 0, 84, 0, 0,
0, 0, 0, 0, 0, 44, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 26, 0, 11, 0,
67, 0, 0, 0, 0, 53, 0, 0, 0, 0,
0, 19, 0, 60, 0, 0, 0, 0, 0, 0,
91, 0, 0, 0, 0, 0, 0, 0, 0, 100,
0, 0, 0, 0, 0, 0, 24, 0, 0, 0,
0, 0, 73, 0, 75, 0, 0, 78, 0, 0};
public Board() {
}
private class BPoint {
int x;
int y;
}
private BPoint p1, p2;
public void drawBoard(Graphics g) {
// horizontal lines
for (int j = 0; j <= 10; j++) {
g.drawLine(X_CORNER, j*SIDE + Y_CORNER,
X_CORNER + 10*SIDE, j*SIDE + Y_CORNER);
}
// vertical lines
for (int i = 0; i <= 10; i++) {
g.drawLine(X_CORNER + i*SIDE, Y_CORNER,
X_CORNER + i*SIDE, Y_CORNER + 10*SIDE);
}
int boardCount = 100;
for (int i = 1; i <= 10; i++)
if (i%2 == 0)
for (int j = 10; j >= 1; j--) {
g.drawString(boardCount-- + "",
X_CORNER + (j-1)*SIDE + 4,
Y_CORNER + (i-1)*SIDE + 12);
}
else
for (int j = 1; j <= 10; j++) {
g.drawString(boardCount-- + "",
X_CORNER + (j-1)*SIDE + 4,
Y_CORNER + (i-1)*SIDE + 12);
}
for (int i = 1; i <= 100; i++) {
if (cLArray[i] != 0) {
p1 = getCoords(i); p2 = getCoords(cLArray[i]);
int x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y;
if (cLArray[i] > i)
drawLine(g, x1, y1, x2, y2, Color.green);
else if (cLArray[i] != 0 && cLArray[i] < i)
drawLine(g, x1, y1, x2, y2, Color.red);
}
}
g.setColor(Color.black);
g.drawString("CHUTES AND LADDERS",
X_CORNER, Y_CORNER - 30);
g.setColor(Color.green);
g.drawString("Ladders are green", X_CORNER, Y_CORNER + 10*SIDE + 20);
g.setColor(Color.red);
g.drawString("Chutes are red", X_CORNER, Y_CORNER + 10*SIDE + 35);
g.setColor(Color.magenta);
g.drawString("Player 1 is magenta",
X_CORNER + 130, Y_CORNER + 10*SIDE + 20);
g.setColor(Color.cyan);
g.drawString("Player 2 is cyan",
X_CORNER + 130, Y_CORNER + 10*SIDE + 35);
}
private void drawLine(Graphics g, int x1, int y1, int x2, int y2, Color c) {
g.setColor(c);
g.drawLine(x1, y1, x2, y2);
g.drawLine(x1-1, y1-1, x2-1, y2-1);
g.drawLine(x1+1, y1+1, x2+1, y2+1);
g.drawLine(x1-1, y1+1, x2-1, y2+1);
g.drawLine(x1+1, y1-1, x2+1, y2-1);
g.drawLine(x1-1, y1-1, x2-1, y2-1);
g.drawLine(x1+1, y1+1, x2+1, y2+1);
g.drawLine(x1, y1, x2, y2);
g.drawLine(x1, y1, x2, y2);
g.fillOval(x1-3, y1-3, 6, 6);
g.fillOval(x2-3, p2.y-3, 6, 6);
}
private void drawLineNoOval(Graphics g, int x1, int y1,
int x2, int y2, Color c) {
g.setColor(c);
g.drawLine(x1, y1, x2, y2);
g.drawLine(x1-1, y1-1, x2-1, y2-1);
g.drawLine(x1+1, y1+1, x2+1, y2+1);
g.drawLine(x1-1, y1+1, x2-1, y2+1);
g.drawLine(x1+1, y1-1, x2+1, y2-1);
g.drawLine(x1-1, y1-1, x2-1, y2-1);
g.drawLine(x1+1, y1+1, x2+1, y2+1);
g.drawLine(x1, y1, x2, y2);
g.drawLine(x1, y1, x2, y2);
}
public void drawLinePlay1(Graphics g,int boardPos1,int boardPos2, Color c) {
p1 = getCoords(boardPos1); p2 = getCoords(boardPos2);
int x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y;
drawLineNoOval(g, x1+8, y1+8, x2+8, y2+8, c);
}
public void drawLinePlay2(Graphics g, int boardPos1, int boardPos2, Color c) {
p1 = getCoords(boardPos1); p2 = getCoords(boardPos2);
int x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y;
drawLineNoOval(g, x1-12, y1+8, x2-12, y2+8, c);
}
public void placeToken1(Graphics g, int boardNum, int counter, Color c) {
p1 = getCoords(boardNum);
g.setColor(c);
g.fillOval(p1.x, p1.y, 16, 16);
g.setColor(Color.black);
g.drawString(counter + "" , p1.x + 2, p1.y +12);
}
public void placeToken2(Graphics g, int boardNum, int counter, Color c) {
p1 = getCoords(boardNum);
g.setColor(c);
g.fillOval(p1.x - 20, p1.y, 16, 16);
g.setColor(Color.black);
g.drawString(counter + "" , p1.x - 18, p1.y +12);
}
private BPoint getCoords(int boardNum) {
int i = 10 - (boardNum - 1)/10;
int j;
if (((boardNum - 1)/10)%2 == 0) j = (boardNum - 1)%10 + 1;
else j = 10 - (boardNum - 1)%10;
BPoint p = new BPoint();
p.x = X_CORNER + (j-1)*SIDE + SIDE/2;
p.y = Y_CORNER + (i-1)*SIDE + SIDE/2;
return p;
}
public int getEnd(int position) { // of chute or ladder
if (1 <= position && position <= 100)
return cLArray[position];
else return -1;
}
}
// Chutes.java: play Chutes and Ladders
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Chutes extends Applet
implements ActionListener {
Board board = new Board();
private Dice dice = new Dice();
private Button nextMove, newGame, oneTwo;
private boolean onePlayer = true;
private int die, dieq;
private int playCount = 0;
private int position = 0, position2 = 0,
oldPosition = 0, position3 = 0;
private int qusition = 0, qusition2 = 0,
oldQusition = 0, qusition3 = 0;
Color play1Color = new Color(255, 0, 255); // Color.magenta
Color play2Color = new Color(0, 210, 210); // darker than Color.cyan
Color play1ColorLight = new Color(255, 190, 255); // lighter than magenta
Color play2ColorLight = new Color(190, 255, 255); // lighter than cyan
public void init() {
setBackground(Color.white);
nextMove = new Button("Next Move");
nextMove.addActionListener(this);
add(nextMove);
newGame = new Button("New Game");
newGame.addActionListener(this);
add(newGame);
oneTwo = new Button("One/Two");
oneTwo.addActionListener(this);
add(oneTwo);
}
public void paint(Graphics g) {
board.drawBoard(g);
if (position == 100 || qusition == 100) {
if (position == 100) {
g.setColor(Color.magenta);
g.drawString("GAME OVER, Player 1 wins in " + playCount + " moves",
board.X_CORNER, board.Y_CORNER - 15);
}
else {
g.setColor(Color.cyan);
g.drawString("GAME OVER, Player 2 wins in " + playCount + " moves",
board.X_CORNER, board.Y_CORNER - 15);
}
board.placeToken1(g, position, playCount, play1Color);
if (!onePlayer)
board.placeToken2(g, qusition, playCount, play2Color);
}
else {
g.setColor(Color.black);
g.drawString("Dice rolled: ", board.X_CORNER, board.Y_CORNER - 15);
if (onePlayer)
g.drawString("ONE-PERSON GAME",
board.X_CORNER + 200, board.Y_CORNER - 30);
else
g.drawString("TWO-PERSON GAME",
board.X_CORNER + 200, board.Y_CORNER - 30);
g.setColor(play1Color);
g.drawString(die + "",
board.X_CORNER + 90, board.Y_CORNER - 15);
if (!onePlayer) {
g.setColor(play2Color);
g.drawString(dieq + "",
board.X_CORNER + 120, board.Y_CORNER - 15);
}
// draw lines for first player
if (position3 != 0) {
board.drawLinePlay1(g, position3, position, play1ColorLight);
if (oldPosition != 0)
board.drawLinePlay1(g, oldPosition, position3, play1ColorLight);
}
else {
if (oldPosition != 0)
board.drawLinePlay1(g, oldPosition, position, play1ColorLight);
}
// only in case of two players
if (!onePlayer) {
// draw lines
if (qusition3 != 0) {
board.drawLinePlay2(g, qusition3, qusition, play2ColorLight);
if (oldQusition != 0)
board.drawLinePlay2(g, oldQusition, qusition3,
play2ColorLight);
}
else {
if (oldQusition != 0)
board.drawLinePlay2(g, oldQusition, qusition,
play2ColorLight);
}
// put in tokens
if (oldQusition != 0)
board.placeToken2(g, oldQusition, playCount-1, play2ColorLight);
if (qusition3 != 0)
board.placeToken2(g, qusition3, playCount, play2ColorLight);
if (qusition != 0)
board.placeToken2(g, qusition, playCount, play2Color);
}
// Tokens for first player
if (oldPosition != 0)
board.placeToken1(g, oldPosition, playCount - 1, play1ColorLight);
if (position3 != 0)
board.placeToken1(g, position3, playCount, play1ColorLight);
if (position != 0)
board.placeToken1(g, position, playCount, play1Color);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nextMove) {
if (position != 100 && qusition != 100) playCount++;
oldPosition = position;
oldQusition = qusition;
if (position != 100 && qusition != 100) {
die = dice.getDie();
position3 = 0;
if (position + die <= 100) {
position += die;
position2 = board.getEnd(position);
if (position2 != 0) {
position3 = position;
position = position2;
}
}
}
if (!onePlayer && position != 100 && qusition != 100) {
dieq = dice.getDie();
qusition3 = 0;
if (qusition + dieq <= 100) {
qusition += dieq;
qusition2 = board.getEnd(qusition);
if (qusition2 != 0) {
qusition3 = qusition;
qusition = qusition2;
}
}
}
repaint();
}
else if (e.getSource() == newGame) {
playCount = 0; position = 0; position2 = 0;
oldPosition = 0; position3 = 0;
qusition = 0; qusition2 = 0;
oldQusition = 0; qusition3 = 0;
repaint();
}
else if (e.getSource() == oneTwo) {
if (onePlayer) onePlayer = false;
else onePlayer = true;
playCount = 0; position = 0; position2 = 0;
oldPosition = 0; position3 = 0;
qusition = 0; qusition2 = 0;
oldQusition = 0; qusition3 = 0;
repaint();
}
}
}
// Dice.java: roll dice
import java.util.Random;
public class Dice {
private Random ranNumGen;
// create an instance of the generator, with random seed
public Dice () {
ranNumGen = new Random();
}
// return random ints between 1 and 6 inclusive
public int getDie() {
return (int)(6*ranNumGen.nextDouble()+1);
}
}
<html>
<applet code="Chutes.class" width=500 height=550>
</applet>
</html>