~cpp
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
class SnakeCell {
public int x;
public int y;
public SnakeCell(int x, int y) {
this.x = x;
this.y = y;
}
};
class Snake {
public static final int LEFT = 1;
public static final int RIGHT = 2;
public static final int UP = 3;
public static final int DOWN = 4;
private final int xRange;
private final int yRange;
private Vector cellVector;
private int headIndex;
private int direction;
public Snake(int length, int xRange, int yRange) {
this.xRange = xRange;
this.yRange = yRange;
cellVector = new Vector();
for(int i = length ; i >= 1 ; i--)
cellVector.addElement(new SnakeCell(i, 1));
headIndex = 0;
direction = Snake.RIGHT;
}
public int length() {
return cellVector.size();
}
public SnakeCell getSnakeCell(int index) {
return (SnakeCell)cellVector.elementAt((headIndex + index) % length());
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public boolean checkGameOver() {
SnakeCell head;
SnakeCell cell;
head = getSnakeCell(0);
if(head.x < 0 || head.x > xRange - 1
|| head.y < 0 || head.y > yRange - 1)
return false;
int length = length();
for(int i = 1 ; i < length ; i++) {
cell = getSnakeCell(i);
if(cell.x == head.x && cell.y == head.y)
return false;
}
return true;
}
public boolean go() {
SnakeCell prevHead = getSnakeCell(0);
headIndex = (headIndex + length() - 1) % length();
SnakeCell currentHead = getSnakeCell(0);
currentHead.x = prevHead.x;
currentHead.y = prevHead.y;
if(direction == Snake.LEFT)
currentHead.x--;
else if(direction == Snake.RIGHT)
currentHead.x++;
else if(direction == Snake.UP)
currentHead.y--;
else if(direction == Snake.DOWN)
currentHead.y++;
return checkGameOver();
}
};
class SnakeBiteCanvas extends Canvas implements Runnable {
private final int boardWallWidth = 4;
private final int snakeCellWidth = 4;
private final int canvasWidth;
private final int canvasHeight;
private final int boardX;
private final int boardY;
private final int boardWidth;
private final int boardHeight;
private final int boardInnerX;
private final int boardInnerY;
private final int boardInnerWidth;
private final int boardInnerHeight;
private Snake snake;
private boolean pause;
private boolean drawAll;
private boolean printScore;
private boolean gameOver;
public Command pauseCommand;
public Command restartCommand;
public SnakeBiteCanvas() {
canvasWidth = getWidth();
canvasHeight = getHeight();
boardWidth = canvasWidth - 6 - (canvasWidth - 6 - boardWallWidth * 2) % snakeCellWidth;
boardHeight = boardWidth / 10 * 8 - (boardWidth / 10 * 8 - boardWallWidth * 2) % snakeCellWidth;
boardX = (canvasWidth - boardWidth) / 2;
boardY = (canvasHeight - boardHeight) /2;
boardInnerX = boardX + boardWallWidth;
boardInnerY = boardY + boardWallWidth;
boardInnerWidth = boardWidth - boardWallWidth * 2;
boardInnerHeight = boardHeight - boardWallWidth * 2;
snake = new Snake(10, boardInnerWidth / snakeCellWidth, boardInnerHeight / snakeCellWidth);
pause = true;
drawAll = true;
printScore = false;
gameOver = false;
}
public void drawBoard(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(0, 0, canvasWidth, canvasHeight);
g.setColor(0x000000);
g.fillRect(boardX, boardY, boardWidth, boardHeight);
}
public void clearBoard(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(boardInnerX - 1, boardInnerY - 1, boardInnerWidth + 2, boardInnerHeight + 2);
}
public void drawSnakeCell(Graphics g, SnakeCell cell) {
g.fillRect(boardInnerX + snakeCellWidth * cell.x,
boardInnerY + snakeCellWidth * cell.y,
snakeCellWidth, snakeCellWidth);
}
public void paint(Graphics g) {
if(drawAll) {
drawBoard(g);
drawAll = false;
}
clearBoard(g);
int length = snake.length();
SnakeCell cell;
g.setColor(0x000000);
for(int i = 0; i < length ; i++) {
cell = snake.getSnakeCell(i);
drawSnakeCell(g, cell);
}
if(printScore) {
}
if(gameOver) {
g.drawString("Game Over!!", canvasWidth / 2, canvasHeight, Graphics.HCENTER | Graphics.BOTTOM);
}
}
public void keyPressed(int keyCode) {
if(!pause && !gameOver) {
int gameAction = getGameAction(keyCode);
if(gameAction == Canvas.LEFT && snake.getDirection() != Snake.RIGHT)
snake.setDirection(Snake.LEFT);
else if(gameAction == Canvas.RIGHT && snake.getDirection() != Snake.LEFT)
snake.setDirection(Snake.RIGHT);
else if(gameAction == Canvas.UP && snake.getDirection() != Snake.DOWN)
snake.setDirection(Snake.UP);
else if(gameAction == Canvas.DOWN && snake.getDirection() != Snake.UP)
snake.setDirection(Snake.DOWN);
}
}
public void start() {
pause = false;
}
public void pause() {
pause = true;
}
public void restart() {
snake = new Snake(5, boardInnerWidth / snakeCellWidth, boardInnerHeight / snakeCellWidth);
pause = true;
drawAll = true;
printScore = false;
gameOver = false;
repaint();
}
public void run() {
try {
for(;;) {
if(!pause && !gameOver) {
if(snake.go() == false) {
this.removeCommand(pauseCommand);
this.addCommand(restartCommand);
gameOver = true;
}
repaint();
Thread.sleep(100);
}
}
} catch(InterruptedException e) {
e.printStackTrace();
}
}
};
public class SnakeBite extends MIDlet implements CommandListener {
private Display display;
private SnakeBiteCanvas canvas;
private Command startCommand;
private Command pauseCommand;
private Command restartCommand;
private Command exitCommand;
public SnakeBite() {
display = Display.getDisplay(this);
canvas = new SnakeBiteCanvas();
startCommand = new Command("Start", Command.SCREEN, 1);
pauseCommand = new Command("Pause", Command.SCREEN, 1);
restartCommand = new Command("Restart", Command.SCREEN, 1);
exitCommand = new Command("Exit", Command.EXIT, 1);
canvas.pauseCommand = pauseCommand;
canvas.restartCommand = restartCommand;
canvas.addCommand(startCommand);
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);
}
public void startApp() {
new Thread(canvas).start();
display.setCurrent(canvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
display = null;
canvas = null;
exitCommand = null;
}
public void commandAction(Command c, Displayable d) {
if(c == startCommand) {
canvas.removeCommand(startCommand);
canvas.addCommand(pauseCommand);
canvas.start();
}
else if(c == pauseCommand) {
canvas.removeCommand(pauseCommand);
canvas.addCommand(startCommand);
canvas.pause();
}
else if(c == restartCommand) {
canvas.removeCommand(restartCommand);
canvas.addCommand(startCommand);
canvas.restart();
}
else if(c == exitCommand) {
destroyApp(true);
notifyDestroyed();
}
}
};