package colorlinez;
import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class ClCanvas extends Canvas {
Ball[][] balls = new Ball[Cfg.SIZE][Cfg.SIZE];
Image curPos = Image.createImage(Cfg.BALL_DIM + Cfg.EDGE * 2, Cfg.BALL_DIM
+ Cfg.EDGE * 2);
Image markPos = Image.createImage(Cfg.BALL_DIM + Cfg.EDGE * 2, Cfg.BALL_DIM
+ Cfg.EDGE * 2);
int cx, cy;
int mx, my; // marked x and y, -1 for no marking
int score;
int[] nextBallColors;
Random random = new Random(System.currentTimeMillis());
public ClCanvas() {
init();
}
public void init() {
getRandomBalls(Cfg.INIT_BALL_NUM);
initPos(curPos, Cfg.CUR_POS_COLOR);
initPos(markPos, Cfg.MARK_POS_COLOR);
cx = Cfg.SIZE / 2;
cy = Cfg.SIZE / 2;
mx = -1;
my = -1;
}
void initPos(Image img, int color) {
Graphics g = img.getGraphics();
g.setColor(Cfg.BGCOLOR);
g.fillRect(0, 0, img.getWidth(), img.getHeight());
g.setColor(color);
g.drawLine(0, 0, Cfg.BALL_DIM + 1, 0);
g.drawLine(0, Cfg.BALL_DIM + 1, Cfg.BALL_DIM + 1, Cfg.BALL_DIM + 1);
g.drawLine(0, 0, 0, Cfg.BALL_DIM + 1);
g.drawLine(Cfg.BALL_DIM + 1, 0, Cfg.BALL_DIM + 1, Cfg.BALL_DIM + 1);
}
void getRandomBalls(int num) {
if (nextBallColors != null) {
randomBall(nextBallColors);
} else {
int[] colors = randomColor(num);
randomBall(colors);
}
nextBallColors = randomColor(Cfg.RUNNING_BALL_NUM);
}
int[] randomColor(int num) {
int[] colors = new int[num];
for (int i = 0; i < num; ++i) {
colors[i] = Math.abs(random.nextInt()) % Cfg.BALL_NUM;
}
return colors;
}
void randomBall(int[] colors) {
int num = colors.length;
for (int i = 0; i < num; ++i) {
int pos = Math.abs(random.nextInt()) % (Cfg.SIZE * Cfg.SIZE);
int xPos = pos % Cfg.SIZE;
int yPos = pos / Cfg.SIZE;
if (balls[xPos][yPos] != null) {
--i;
continue;
}
balls[xPos][yPos] = new Ball(colors[i]);
}
}
/**
* expunge the board!
*/
int expunge() {
boolean[][] tobeEx = new boolean[Cfg.SIZE][Cfg.SIZE];
// row
for (int j = 0; j < Cfg.SIZE; ++j) {
expungeXY(0, j, 1, 0, tobeEx);
}
// column
for (int i = 0; i < Cfg.SIZE; ++i) {
expungeXY(i, 0, 0, 1, tobeEx);
}
// slash
for (int j = Cfg.EX_NUM - 1; j < Cfg.SIZE; ++j) {
expungeXY(0, j, 1, -1, tobeEx);
}
for (int i = 1; i <= Cfg.EX_NUM; ++i) {
expungeXY(i, Cfg.SIZE - 1, 1, -1, tobeEx);
}
// back slash
for (int i = Cfg.SIZE - Cfg.EX_NUM; i >= 0; --i) {
expungeXY(i, 0, 1, 1, tobeEx);
}
for (int j = 1; j <= Cfg.SIZE - Cfg.EX_NUM; ++j) {
expungeXY(0, j, 1, 1, tobeEx);
}
int exNum = 0;
for (int i = 0; i < Cfg.SIZE; ++i) {
for (int j = 0; j < Cfg.SIZE; ++j) {
if (tobeEx[i][j]) {
balls[i][j] = null;
++exNum;
}
}
}
return exNum;
}
void expungeXY(int fx, int fy, int dx, int dy, boolean[][] mark) {
int lastId = -1;
int total = 0;
for (int i = fx, j = fy; 0 <= i && i < Cfg.SIZE && 0 <= j
&& j < Cfg.SIZE; i += dx, j += dy) {
if (balls[i][j] == null) {
if (total >= Cfg.EX_NUM) {
int exi = i, exj = j; // 从上一个才是真的!!
for (int c = 0; c < total; ++c) {
exi -= dx;
exj -= dy;
mark[exi][exj] = true;
}
}
total = 0;
lastId = -1;
continue;
}
if (balls[i][j].id == lastId) {
++total;
if (total >= Cfg.EX_NUM
&& (i + dx < 0 || i + dx >= Cfg.SIZE || j + dy < 0 || j
+ dy >= Cfg.SIZE)) {
int exi = i, exj = j;
for (int c = 0; c < total; ++c) {
mark[exi][exj] = true;
exi -= dx;
exj -= dy;
}
}
} else {
if (total >= Cfg.EX_NUM) {
int exi = i, exj = j; // 从上一个才是真的!!
for (int c = 0; c < total; ++c) {
exi -= dx;
exj -= dy;
mark[exi][exj] = true;
}
}
lastId = balls[i][j].id;
total = 1;
}
}
}
public void paint(Graphics g) {
// System.out.println("before paint: " + mx + " " + my + " " + cx + " "
// + cy);
g.setColor(Cfg.BGCOLOR);
g.fillRect(0, 0, getWidth(), getHeight());
drawBoard(g);
drawPos(g);
drawBalls(g);
drawScore(g);
drawNextBalls(g);
}
void drawNextBalls(Graphics g) {
for (int i = 0; i < nextBallColors.length; ++i) {
g.drawString(" Next 3 ", Cfg.TIP_POS_X, Cfg.TIP_POS_Y, 0);
int tipY = Cfg.TIP_POS_Y + Font.getDefaultFont().getHeight() + Cfg.LINE_GAP;
g.drawImage(Ball.imgs[nextBallColors[i]], 3 + Cfg.TIP_POS_X + 4
+ (Cfg.BALL_DIM + 3) * i, tipY, 0);
}
}
void drawScore(Graphics g) {
g.setColor(Cfg.SCORE_COLOR);
g.drawString("Score:", Cfg.SCORE_POS_X, Cfg.SCORE_POS_Y, 0);
int scoreY = Cfg.SCORE_POS_Y + Font.getDefaultFont().getHeight() + Cfg.LINE_GAP;
g.drawString("" + score, Cfg.SCORE_POS_X, scoreY, 0);
//debug
//g.drawRect(0,0,176,208);
}
void drawPos(Graphics g) {
int interval = Cfg.BALL_DIM + 2 * Cfg.EDGE + 1;
g.drawImage(curPos, Cfg.X0 + 1 + cx * interval, Cfg.Y0 + 1 + cy
* interval, 0);
if (!(mx == -1)) {
g.drawImage(markPos, Cfg.X0 + 1 + mx * interval, Cfg.Y0 + 1 + my
* interval, 0);
}
}
void drawBalls(Graphics g) {
for (int i = 0; i < balls.length; ++i) {
for (int j = 0; j < balls.length; ++j) {
if (balls[i][j] != null) {
balls[i][j].draw(g, i, j);
}
}
}
}
void drawBoard(Graphics g) {
g.setColor(Cfg.LINE_COLOR);
int unit = Cfg.BALL_DIM + 2 * Cfg.EDGE + 1;
// horizantal line
for (int i = 0; i <= Cfg.SIZE; ++i) {
int y = Cfg.Y0 + i * unit;
g.drawLine(Cfg.X0, y, Cfg.X0 + Cfg.SIZE * unit, y);
}
// vertical line
for (int i = 0; i <= Cfg.SIZE; ++i) {
int x = Cfg.X0 + i * unit;
g.drawLine(x, Cfg.Y0, x, Cfg.Y0 + Cfg.SIZE * unit);
}
}
boolean isConnect(int x0, int y0, int x1, int y1) {
Maze maze = new Maze(balls, x0, y0, x1, y1);
return maze.isConnect(); // test!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
}
protected void keyPressed(int key) {
int gameKey = getGameAction(key);
switch (gameKey) {
case Canvas.FIRE:
if (!(cx == mx && cy == my)) {
if (balls[cx][cy] != null) {
mx = cx;
my = cy;
} else {
if (mx != -1 && isConnect(mx, my, cx, cy)) {
balls[cx][cy] = balls[mx][my];
balls[mx][my] = null;
mx = -1;
my = -1;
int exNum = expunge();
score += Cfg.SCORE[exNum];
if (exNum == 0) {
getRandomBalls(Cfg.RUNNING_BALL_NUM);
expunge();
}
}
}
}
break;
case Canvas.UP:
cy = (cy - 1 + Cfg.SIZE) % Cfg.SIZE;
break;
case Canvas.DOWN:
cy = (cy + 1) % Cfg.SIZE;
break;
case Canvas.LEFT:
cx = (cx - 1 + Cfg.SIZE) % Cfg.SIZE;
break;
case Canvas.RIGHT:
cx = (cx + 1) % Cfg.SIZE;
break;
}
repaint();
}
}
评论2