📄 wzqchessboard.java~107~
字号:
package wzq;
import java.util.Vector;
import javax.microedition.lcdui.*;
import java.io.IOException;
/**
* <p>Title:五子棋棋盘 </p>
*
* <p>Description:在这里实现了五子棋棋盘的绘制工作 </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: Star Group</p>
*
* @author: wangyaobsz
* @version 1.0
*/
public class WzqChessBoard extends Canvas implements CommandListener {
int nWhiteWidth; //空白区域的宽度
int nCanvasW, nCanvasH; //画布的宽度和高度
int nChessWidth; //棋子的宽度
int nChessMapLength, nMapGrid, nGridWidth; //棋盘的宽度,网格的数目、宽度
int nMapX, nMapY; //棋盘的原点坐标
int nSelectX, nSelectY; //选择框的坐标
Chesses chess; //棋盘上的棋子信息
boolean bNewGame; //是否开始新游戏
private Graphics gx; //绘图设备的属性
private final int GAME_RUN = 1;
private final int GAME_PAUSE = 2;
private final int GAME_OVER = 3;
private int nState; //游戏状态
Command cmdStart, cmdPause, cmdExit, cmdRestart;
public WzqChessBoard() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
cmdStart = new Command("开始游戏",Command.OK,1);
cmdPause = new Command("暂停",Command.OK,1);
cmdExit = new Command("退出游戏", Command.EXIT, 0);
cmdRestart = new Command("重新开始游戏", Command.SCREEN, 0);
addCommand(cmdRestart);
addCommand(cmdExit);
addCommand(cmdPause);
//开始初始化变量
bNewGame = true;
nWhiteWidth = 10; //预留的空白位置的宽度
nCanvasW = getWidth() - nWhiteWidth;
nCanvasH = getHeight() - nWhiteWidth;
nMapGrid = Chesses.BOARD_SIZE;
chess = new Chesses();
nState = GAME_RUN;
//根据屏幕的大小,自动调整棋盘的大小
if (nCanvasW > nCanvasH) {
nChessMapLength = nCanvasH - nCanvasH % nMapGrid;
nMapX = (nCanvasW - nChessMapLength) / 2 + nWhiteWidth / 2;
nMapY = (nCanvasH % nMapGrid) / 2 + nWhiteWidth / 2;
} else {
nChessMapLength = nCanvasW - nCanvasW % nMapGrid;
nMapX = (nCanvasW % nMapGrid) / 2 + nWhiteWidth / 2;
nMapY = (nCanvasH - nChessMapLength) / 2 + nWhiteWidth / 2;
}
nGridWidth = nChessMapLength / nMapGrid;
nChessWidth = nGridWidth - 1;
nSelectX = nSelectY = nMapGrid / 2;
}
/**
* 绘制棋盘上的网格<p>
* @param g
*/
protected void paintMap(Graphics g) {
g.setColor(128, 128, 128);
for (int i = 0; i < nMapGrid; i++) {
for (int j = 0; j < nMapGrid; j++) {
g.drawRect(nMapX + j * nGridWidth,
nMapY + i * nGridWidth,
nGridWidth,
nGridWidth);
}
}
}
/**
* 在当前位置上绘制一个提示框<p>
* @param g
*/
protected void paintSelectBox(Graphics g) {
g.setColor(255, 0, 255);
g.drawRect(nMapX + nSelectX * nGridWidth - nGridWidth / 2,
nMapY + nSelectY * nGridWidth - nGridWidth / 2,
nGridWidth,
nGridWidth);
}
/**
* 绘制棋子<p>
* @param g
*/
protected void paintChesses(Graphics g) {
for (int i = 0; i <= nMapGrid; i++) {
for (int j = 0; j <= nMapGrid; j++) {
if (chess.getChess()[i][j] != Chesses.NO_CHESS) {
if (chess.getChess()[i][j] == Chesses.WHITE_CHESS) {
g.setColor(255, 255, 255);
} else {
g.setColor(0, 0, 0);
}
g.fillArc(nMapX + j * nGridWidth - nChessWidth / 2,
nMapY + i * nGridWidth - nChessWidth / 2,
nChessWidth, nChessWidth, 0, 360);
}
}
}
}
private void jbInit() throws Exception {
setCommandListener(this);
if (bNewGame) {
chess = new Chesses();
nSelectX = nSelectY = nMapGrid / 2;
}
}
/**
* 响应命令消息<p>
* @param command
* @param displayable
*/
public void commandAction(Command command, Displayable displayable) {
if (command == cmdExit) {
WzqMIDlet.quitApp();
} else if (command == cmdRestart) {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}else if (command == cmdPause){
nState = GAME_PAUSE;
this.removeCommand(cmdPause);
this.addCommand(cmdStart);
repaint();
}else if (command == cmdStart){
nState = GAME_RUN;
this.removeCommand(cmdStart);
this.addCommand(cmdPause);
repaint();
}
}
/**
* 响应键盘消息<p>
* @param keyCode
*/
protected synchronized void keyPressed(int keyCode) {
if (nState == GAME_RUN) { //如果游戏已经结束,则不再响应键盘消息
int action = getGameAction(keyCode);
switch(action){
case Canvas.LEFT://向左
nSelectX = (--nSelectX + nMapGrid + 1) % (nMapGrid + 1);
break;
case Canvas.RIGHT://向右
nSelectX = (++nSelectX) % (nMapGrid + 1);
break;
case Canvas.UP://向上
nSelectY = (--nSelectY + nMapGrid + 1) % (nMapGrid + 1);
break;
case Canvas.DOWN://向下
nSelectY = (++nSelectY) % (nMapGrid + 1);
break;
case Canvas.FIRE://选择确认
boolean flag = chess.putChess(nSelectY, nSelectX); //放置棋子
if (!chess.isGameOver() && flag) { //判断上一步放置是否成功且游戏是否结束
this.AIputChess(); //机器下棋
if (chess.isGameOver()) {
nState = GAME_OVER;
this.removeCommand(cmdStart);
this.removeCommand(cmdPause);
repaint();
}
break;
}
}
repaint();
}
}
/**
* 绘制消息<p>
* @param msg
*/
protected void drawMessage(Vector msg) {
int rowW = 0;
int w = 0;
for (int i = 0; i < msg.size(); i++) {
w = gx.getFont().stringWidth("" + msg.elementAt(i));
if (w > rowW) {
rowW = w;
}
}
int rowH = gx.getFont().getHeight();
int gap = 5;
int boxX = (getWidth() - rowW - gap * 2) / 2;
int boxH = rowH * msg.size() + gap * 2;
int boxY = (getHeight() - boxH) / 2;
int boxW = rowW + gap * 2;
gx.setColor(128, 128, 128);
drawMask(boxX, boxY, boxW, boxH, true);
gx.setColor(0, 255, 0);
gx.drawRect(boxX, boxY, boxW, boxH);
gx.setColor(255, 0, 0);
for (int i = 0; i < msg.size(); i++) {
int msgW = gx.getFont().stringWidth("" + msg.elementAt(i));
gx.drawString("" + msg.elementAt(i), boxX + gap,
boxY + gap + i * rowH, 0);
}
}
/**
* 绘制消息的底纹<p>
* @param mx
* @param my
* @param mw
* @param mh
* @param dark
*/
private void drawMask(int mx, int my, int mw, int mh, boolean dark) {
for (int i = 0; i < mh; i += 2) {
gx.drawLine(mx, my + i, mx + mw, my + i);
}
if (dark) {
for (int i = 0; i < mw; i += 2) {
gx.drawLine(mx + i, my, mx + i, my + mh);
}
}
}
/**
* 机器下棋<p>
*/
protected void AIputChess() {
Point ptBest = AI.findBestLocation(this.chess);
this.chess.putChess(ptBest.getX(), ptBest.getY());
}
/**
* 绘制所有的屏幕内容<p>
* @param g
*/
protected void paint(Graphics g) {
switch(nState){
case GAME_RUN:
case GAME_OVER:
gx = g;
g.setColor(0, 0, 191);
g.fillRect(0, 0, getWidth(), getHeight());
paintMap(g);
paintSelectBox(g);
paintChesses(g);
if (this.chess.getMessages().size() > 0) {
drawMessage(this.chess.getMessages());
}
break;
case GAME_PAUSE:
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
Image img = null;
try {
img = Image.createImage("/res/pause.png");
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(img, getWidth() / 2, getHeight() / 2,
Graphics.HCENTER | Graphics.VCENTER);
String str = new String("暂停中...");
g.drawString(str,getWidth()/2,getHeight()-20,Graphics.VCENTER|Graphics.BASELINE);
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -