📄 gametable.java~232~
字号:
package genius.game.mobile.bachelor;
import javax.microedition.lcdui.*;
/**
* <p>Title: GENIUS bachelor</p>
* <p>Description: GENIUS Mobile Game - Bachelor</p>
* <p>Copyright: Copyright (c) 2002 GENIUS Tech.</p>
* <p>Company: GENIUS Technology Software Development Union.</p>
* @author Rodger hu
* @version 1.01
*/
public class gameTable extends Canvas implements CommandListener {
private Command cmdUndo = new Command("悔棋", Command.OK, 1);
private Command cmdRestart = new Command("重来", Command.OK, 2);
private Command cmdExit = new Command("退出", Command.EXIT, 3);
private Command cmdReadme = new Command("说明", Command.HELP, 4);
private Command cmdAbout = new Command("关于", Command.HELP, 5);
private static Image imgChessman, imgCopyright, imgText;
private static int RADIUS_WIDTH = 12;
private static int BORDER_WIDTH = 10;
private static int SPACE_WIDTH = 33;
private static int NONE_STONE = 0;
private static int HAVE_STONE = 1;
private static int WH = 7;
private int[][] tableMap;
private boolean selected;
private int selectedX;
private int selectedY;
private int oldSelectedX;
private int oldSelectedY;
private boolean fWin, fLose;
private int OldX, OldY, HideX, HideY, CurX, CurY;
/**Construct the displayable*/
public gameTable() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
tableMap = new int [WH][WH];
_newGame();
}
/**Component initialization*/
private void jbInit() throws Exception {
// set up this Displayable to listen to command events
setCommandListener(this);
// add the Exit command
addCommand(cmdUndo);
addCommand(cmdRestart);
addCommand(cmdExit);
addCommand(cmdReadme);
addCommand(cmdAbout);
try
{
imgText = Image.createImage("/bachelor_text.png");
imgCopyright = Image.createImage("/copyright.png");
imgChessman = Image.createImage("/qz.png");
} catch(java.io.IOException e)
{
e.printStackTrace();
}
}
/**Handle command events*/
public void commandAction(Command command, Displayable displayable) {
if (command == cmdExit)
{
// stop the MIDlet
mainGame.quitApp();
} else if (command == cmdRestart)
{
_newGame();
} else if (command == cmdUndo)
{
if (HideX != -1 && HideY != -1) {
tableMap[CurX][CurY] = NONE_STONE;
tableMap[HideX][HideY] = HAVE_STONE;
tableMap[OldX][OldY] = HAVE_STONE;
repaint();
}
} else if (command == cmdReadme)
{
gameReadme r = new gameReadme();
r.setParent(this);
Display.getDisplay(mainGame.instance).setCurrent(r);
} else if (command == cmdAbout)
{
about a = new about();
a.setParent(this);
Display.getDisplay(mainGame.instance).setCurrent(a);
}
}
/** Required paint implementation */
protected void paint(Graphics g) {
int x = 0;
int y = 0;
int w = this.getWidth();
int h = this.getHeight();
g.setColor(0xffffff);
g.fillRect(x, y, w, h);
g.setColor(0, 0, 255);
_drawTable(g);
for (int i = 0; i < WH; i++)
{
for (int j = 0; j < WH; j++)
{
if (tableMap[i][j] == HAVE_STONE)
{
g.drawImage(imgChessman,
i * SPACE_WIDTH + BORDER_WIDTH,
j * SPACE_WIDTH + BORDER_WIDTH,
Graphics.LEFT | Graphics.TOP);
}
}
}
if (selected)
{
if (oldSelectedX != -1 && oldSelectedY != -1)
{
if (tableMap[oldSelectedX][oldSelectedY] == HAVE_STONE)
{
}
else
{
}
}
g.setColor(10, 10, 10);
g.drawRect(
selectedX * SPACE_WIDTH + BORDER_WIDTH - 2,
selectedY * SPACE_WIDTH + BORDER_WIDTH - 2,
25 + 2,
25 + 2);
}
if (fWin) {
int n = g.getFont().stringWidth("恭喜恭喜!您成功了!!!");
g.drawString("恭喜恭喜!您成功了!!!", (w - n)/2, 60, Graphics.LEFT | Graphics.TOP);
} else if (fLose) {
int n = g.getFont().stringWidth("重新来一次吧 :(");
g.drawString("重新来一次吧 :(", (w - n)/2, 60, Graphics.LEFT | Graphics.TOP);
}
}
protected void pointerPressed(int x, int y) {
super.pointerPressed(x, y);
if (fWin == false || fLose == false) {
int nX = _getIndex(x);
int nY = _getIndex(y);
if (selected) {
if (tableMap[nX][nY] == HAVE_STONE) {
oldSelectedX = selectedX;
oldSelectedY = selectedY;
selectedX = nX;
selectedY = nY;
selected = true;
} else if (_canGo(selectedX, selectedY, nX, nY)) {
oldSelectedX = selectedX;
oldSelectedY = selectedY;
if (selectedX == nX) {
HideX = nX;
HideY = min(selectedY, nY) + 1;
} else if (selectedY == nY) {
HideX = min(selectedX, nX) + 1;
HideY = nY;
}
selectedX = nX;
selectedY = nY;
tableMap[HideX][HideY] = NONE_STONE;
tableMap[oldSelectedX][oldSelectedY] = NONE_STONE;
tableMap[selectedX][selectedY] = HAVE_STONE;
OldX = oldSelectedX;
OldY = oldSelectedY;
CurX = selectedX;
CurY = selectedY;
selected = false;
if (_canWin()) {
fWin = true;
} else if (_canLose()) {
fLose = true;
}
}
repaint();
}
else {
if (tableMap[nX][nY] == HAVE_STONE) {
selectedX = nX;
selectedY = nY;
oldSelectedX = selectedX;
oldSelectedY = selectedY;
selected = true;
}
repaint();
}
}
}
private void _newGame() {
//Clear chess table.
for (int i = 0; i < WH; i++)
{
for (int j = 0; j < WH; j++)
{
tableMap[i][j] = HAVE_STONE;
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
tableMap[i][j] = NONE_STONE;
tableMap[i+5][j+5] = NONE_STONE;
tableMap[i+5][j] = NONE_STONE;
tableMap[i][j+5] = NONE_STONE;
}
}
tableMap[3][3] = NONE_STONE;
selectedX = -1;
selectedY = -1;
oldSelectedX = -1;
oldSelectedY = -1;
HideX = -1;
HideY = -1;
selected = false;
fWin = false;
fLose = false;
repaint();
}
private void _drawTable(Graphics g) {
int nOldColor = g.getColor();
for (int i = 0; i < 3; i++)
{
g.drawLine(20, 87 + i * SPACE_WIDTH, 220, 87 + i * SPACE_WIDTH);
g.drawLine(87 + i * SPACE_WIDTH, 20, 87 + i * SPACE_WIDTH, 220);
}
for (int i = 0; i < 2; i++)
{
g.drawLine(20 + i * SPACE_WIDTH, 87, 20 + i * SPACE_WIDTH, 87 + 2 * SPACE_WIDTH);
g.drawLine(20 + (5 + i) * SPACE_WIDTH + 2, 87, 20 + (5 + i) * SPACE_WIDTH + 2, 87 + 2 * SPACE_WIDTH);
g.drawLine(87, 20 + i * SPACE_WIDTH, 87 + 2 * SPACE_WIDTH, 20 + i * SPACE_WIDTH);
g.drawLine(87, 20 + (5 + i) * SPACE_WIDTH + 2, 87 + 2 * SPACE_WIDTH, 20 + (5 + i) * SPACE_WIDTH + 2);
}
g.drawImage(imgText, 180, 20, Graphics.LEFT | Graphics.TOP);
g.drawImage(imgCopyright, 10, 180, Graphics.LEFT | Graphics.TOP);
g.setColor(nOldColor);
}
private int _getIndex(int n) {
int nResult;
if (n < BORDER_WIDTH || n > 7 * SPACE_WIDTH + BORDER_WIDTH)
{
nResult = 0;
}
else
{
nResult = (n - BORDER_WIDTH) / (SPACE_WIDTH);
}
return nResult;
}
//Move chessman from x, y to cx, cy
private boolean _canGo(int x, int y, int cx, int cy) {
if ((cx < 2 || cx > 4) && (cy < 2 || cy > 4))
return false;
if (tableMap[cx][cy] == NONE_STONE) {
if (x == cx) {
if ((y - cy) == 2 || (cy - y) == 2) {
if (tableMap[x][max(y, cy) - 1] != NONE_STONE) {
return true;
}
}
} else if (y == cy) {
if ((x - cx) == 2 || (cx - x) == 2) {
if (tableMap[max(x, cx) - 1][y] != NONE_STONE) {
return true;
}
}
}
}
return false;
}
private boolean _canWin() {
if (tableMap[3][3] == HAVE_STONE) {
for (int i = 0; i < WH; i++) {
for (int j = 0; j < WH; j++) {
if (tableMap[i][j] == HAVE_STONE)
return false;
}
}
return true;
}
return false;
}
private boolean _canLose() {
return false;
}
private int min(int x, int y) {
int n = (x > y) ? y : x;
return n;
}
private int max(int x, int y) {
int n = (x > y) ? x : y;
return n;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -