import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; public class snakoban extends BApplet { // Snakoban // Kevan Davis, 26/8/03 int[][] square; int[][] tailage; int gamemode = 1; int speed = 150; int level,step; int head = 1; int tail = 2; int block = 3; int hole = 4; int fill = 5; int headcolor,tailcolor,blockcolor,holecolor,fillcolor; int boardsize=10; int squaresize=20; int snakelength; int offx=0,offy=0; // Setup function, automatically called once at the start. void setup() { size(boardsize*squaresize,boardsize*squaresize); square = new int[boardsize][boardsize]; tailage = new int[boardsize][boardsize]; headcolor = color(0,200,0); tailcolor = color(0,100,0); holecolor = color(0,0,0); fillcolor = color(100,0,0); background(51); // Fill the board with blank squares, except for the edges, which // start as filled holes. for(int x=0; x198) { square[rx][ry] = block; i=200; } } } for (i=0; i<500; i++) { // Same again, to place a hole. rx = (int)random(boardsize); ry = (int)random(boardsize); if (square[rx][ry] == 0) { if ((square[rx-1][ry-1] != fill && square[rx][ry-1] != fill && square[rx+1][ry-1] != fill && square[rx-1][ry] != fill && square[rx+1][ry] != fill && square[rx-1][ry+1] != fill && square[rx][ry+1] != fill && square[rx+1][ry+1] != fill && square[rx-1][ry-1] != block && square[rx][ry-1] != block && square[rx+1][ry-1] != block && square[rx-1][ry] != block && square[rx+1][ry] != block && square[rx-1][ry+1] != block && square[rx][ry+1] != block && square[rx+1][ry+1] != block) || i>200) { square[rx][ry] = hole; i=500; } } } } // This functiong gets called by Proce55ing whenever a key is // pressed... void keyPressed() { int x,y; // If playing as Sokoban, reset the offsets each keypress. if(gamemode == 1) { offy = 0; offx=0; } // See what grid offset the key provokes. if(key == 'w' || key == 'W') { offy = -1; offx=0; } if(key == 's' || key == 'S') { offy = 1; offx=0; } if(key == 'a' || key == 'A') { offx = -1; offy=0; } if(key == 'd' || key == 'D') { offx = 1; offy=0; } // Toggle mode (1=Sokoban, 2=Snake) if(key == 'm' || key == 'M') { gamemode = 3-gamemode; } // Restart if(key == 'q' || key == 'Q') { setup(); } // Restart if(key == '-' && speed < 200) { speed = speed + 10; } if(key == '+' && speed > 0) { speed = speed - 10; } // If in Sokoban mode, move the snake after each keypress. if (gamemode == 1) { moveSnake(); } } void moveSnake() { int x,y,headx=0,heady=0; int oldest=snakelength,oldestx=0,oldesty=0; int finished=1; // Run through the board to see where the snake's head is, and // store its coordinates in headx and heady. for(x=0; x= oldest) { oldest = tailage[x][y]; oldestx = x; oldesty = y; } } } } // If the oldest bit is longer than the snake, remove that bit. if (oldest > snakelength) { square[oldestx][oldesty] = 0; } } } // And that's it! Gosh. }