Apologies for the recent interruption in service; I accidentally allowed the domain to expire.

Cascade Source

(Last Update 16:14 on Thursday, 21 June 2001)

// Cascade applet
// Neil Rashbrook
// Last modified June 21 2001
import java.awt.Color;
import java.awt.Container;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageConsumer;
import java.util.Hashtable;
public class cascade extends java.applet.Applet {
private Image[] pebbles;
private int x, y, score, cols, rows, colours, size;
private int[][] board;
private static java.util.Random random = new java.util.Random();
/**
 * Clear pebble from cell and check all neighbours.
 * Returns count of cells cleared.
 */
private int clear(int x, int y, int colour) {
if (x < 0 || y <= 0 || x >= cols || y > rows || board[x] == null || board[x][y] != colour) return 0;
board[x][y] = 0;
return 1 + clear(x + 1, y, colour) + clear(x - 1, y, colour) + clear(x, y + 1, colour) + clear(x, y - 1, colour);
}
/**
 * Information displayed in AppletViewer 1.1
 */
public String getAppletInfo() {
return "Cascade Applet © 2001 Neil Rashbrook";
}
/**
 * Information displayed in AppletViewer 1.1
 * N.B. Does not compile in JDK 1.0.2
 */
public String[][] getParameterInfo() {
return new String[][] {
{"background", "000000-FFFFFF", "Background Colour"},
{"size", "int", "Pebble Size"},
{"height", "int", "Applet Height"},
{"width", "int", "Applet Width"},
{"colours", "int", "Number of Pebble Colours"},
{"colour1", "(00|55|AA|FF){3}", "Pebble Colour 1"},
{"colour2", "(00|55|AA|FF){3}", "Pebble Colour 2"},
{"colour3", "(00|55|AA|FF){3}", "Pebble Colour 3"}
};
}
/**
 * Fill board with random cells.
 * Also add 500 points to score.
 */
private void fill() {
int i = cols;
board = new int[i + 1][rows + 1];
board[i] = null;
while (i > 0) {
int[] col = board[--i];
for (int j = rows; j > 0; j--) col[j] = Math.abs(random.nextInt() % colours) + 1;
}
score += 500;
}
/**
 * Initialise variables and create pebbles.
 */
public final void init() {
int background = Integer.parseInt(super.getParameter("background"), 16);
super.setBackground(new Color(background));
size = Integer.parseInt(super.getParameter("size"));
cols = Integer.parseInt(super.getParameter("width")) / size;
rows = Integer.parseInt(super.getParameter("height")) / size;
colours = Integer.parseInt(super.getParameter("colours"));
pebbles = new Image[colours + 1];
int[] squares = new int[size];
for (int i = size, j = --i; i >= 0; i--, j -= 2) squares[i] = 0x55 * 2 * j * j;
int square = size * size, square2 = square + square, square171 = (0x55 * 2 + 1) * square;
byte[] pixels = new byte[square];
for (int pos = 0, i = 0; i < size; i++) for (int j = 0; j < size; j++) pixels[pos++] = (byte)((square171 - squares[i] - squares[j]) / square2);
/* Unrolled
for (int i = 0; i + i < size; i++) for (int j = 0; j + j < size; j++)
pixels[i * size + j] =
pixels[j * size + i] =
pixels[i * size + size - j - 1] =
pixels[j * size + size - i - 1] =
pixels[square - i * size - j - 1] =
pixels[square - j * size - i - 1] =
pixels[square - i * size - size + j] =
pixels[square - j * size - size + i] = (byte)((square171 - squares[i] - squares[j]) / square2);
*/
byte[] r = newarray(background >> 16), g = newarray(background >> 8), b = newarray(background);
for (int i = 0; ; ) {
pebbles[i] = super.createImage(new java.awt.image.MemoryImageSource(size, size, new java.awt.image.IndexColorModel(8, 256, r, g, b), pixels, 0, size));
if (i == colours) break;
int rgb = Integer.parseInt(super.getParameter("colour" + ++i), 16);
int red = ((rgb & 0xC00000) >> 22) - 2;
int green = ((rgb & 0xC000) >> 14) - 2;
int blue = ((rgb & 0xC0) >> 6) - 2;
for (int j = 0; j <= 0x55; j++) {
r[j] = (byte)(red * j + 0xAA);
g[j] = (byte)(green * j + 0xAA);
b[j] = (byte)(blue * j + 0xAA);
}
}
}
/**
 * Find pebble clicked on
 */
public final boolean mouseDown(Event evt, int x, int y) {
this.x = x / size;
this.y = y / size + 1;
return true;
}
/**
 * Update status bar
 */
public final boolean mouseEnter(Event evt, int x, int y) {
super.showStatus("Score: " + score);
return true;
}
/**
 * If blank area clicked, restart game.
 * If pebble clicked, fix up holes (if any) and calculate score (if any).
 */
public final boolean mouseUp(Event evt, int x, int y) {
int colour, count, i;
if (this.x == (x /= size) && this.y == (y = y / size + 1)) {
if (board[x] == null || (colour = board[x][y]) == 0) start();
else if ((count = clear(x + 1, y, colour) + clear(x - 1, y, colour) + clear(x, y + 1, colour) + clear(x, y - 1, colour)) > 0) {
for (i = 0; board[i] != null; i++);
while (--i >= 0) {
int[] col = board[i];
int j = 1;
while (j <= rows && col[j] == 0) j++;
if (j > rows) System.arraycopy(board, i + 1, board, i, cols - i);
else while (++j <= rows) if (col[j] == 0) System.arraycopy(col, 0, col, 1, j);
}
if (board[0] == null) fill();
super.showStatus("Score: " + (score += count * (count + 1) >> 1));
}
super.repaint();
}
return true;
}
/**
 * Create and initialize a new 256 byte array.
 */
private static byte[] newarray(int value) {
byte[] a = new byte[256];
a[3] = a[2] = a[1] = a[0] = (byte)value;
System.arraycopy(a, 0, a, 4, 4);
System.arraycopy(a, 0, a, 8, 8);
System.arraycopy(a, 0, a, 16, 16);
System.arraycopy(a, 0, a, 32, 32);
System.arraycopy(a, 0, a, 64, 64);
System.arraycopy(a, 0, a, 128, 128);
return a;
}
/**
 * Draw board.
 * Only draw columns with pebbles.
 * Clear rest of board.
 */
public final void paint(Graphics g) {
int[] col;
int x = 0;
for (int i = 0; (col = board[i]) != null; i++, x += size) for (int j = 1, y = 0; j <= rows; j++, y += size) g.drawImage(pebbles[col[j]], x, y, null);
g.clearRect(x, 0, cols * size - x, rows * size);
/*
for (int i = 0, x = 0; i < cols; i++, x += size) {
if ((col = board[i]) != null) for (int j = 1, y = 0; j <= rows; j++, y += size) g.drawImage(pebbles[col[j]], x, y, null);
else {
g.setColor(Color.white);
g.fillRect(x, 0, (cols - i) * size, rows * size);
break;
}
}
*/
}
/**
 * Fill board and reset score
 */
public final void start() {
fill();
score = 0;
}
/**
 * Board is not transparent so just paint it.
 */
public final void update(Graphics g) {
paint(g);
}
}

Frames Version
Customization Page
Back to Cascade Page


640661
38.107.179.227