请教五子棋游戏棋盘类的一些疑问

yezersky 2008-12-18 04:06:06
请教五子棋游戏棋盘类的一些疑问,希望有人帮忙解答一下,先谢过了!
public class WzqChessBoard extends Canvas implements CommandListener {
public WzqChessBoard() {
}

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);
}
}
}

protected void paintSelectBox(Graphics g) {
g.setColor(255, 0, 255);
g.drawRect(nMapX + nSelectX * nGridWidth - nGridWidth / 2,
nMapY + nSelectY * nGridWidth - nGridWidth / 2,
nGridWidth,
nGridWidth);
}

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;
}
}

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.removeCommand(cmdRestart);
this.addCommand(cmdStart);
repaint();
}else if (command == cmdStart){
nState = GAME_RUN;
this.removeCommand(cmdStart);
this.addCommand(cmdRestart);
this.addCommand(cmdPause);
repaint();
}
}

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); //chess.putChess是另一个类里面用来放棋子的方法
if (!chess.isGameOver() && flag) {
this.AIputChess();

if (chess.isGameOver()) {
nState = GAME_OVER;
this.removeCommand(cmdStart);
this.removeCommand(cmdPause);
repaint();
}
break;
}
}
repaint();
}
}
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);
}
}
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);
}
}
}
protected void AIputChess() {
Point ptBest = AI.findBestLocation(this.chess); //AI.findBestLocation是另一个类里用于找电脑最佳下起点的方法
this.chess.putChess(ptBest.getX(), ptBest.getY());
}
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.setColor(255,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString(str,getWidth()/2,getHeight()-20,
Graphics.HCENTER|Graphics.BASELINE);
break;
}
}
}

问题二:paintMap()、paintSelectBox()、paintChesses()这三个方法什么时候调用的???
问题三:jbInit()这个方法有什么用???
...全文
224 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
yezersky 2008-12-19
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 kf156 的回复:]
引用 15 楼 yezersky 的回复:
setCommandListener(this); 为什么也要放到jbInit()方法里面呢?


你看下有没其他地方设置命令监听
如果没有的话,这行代码应该是可以放到这类的构造里。
[/Quote]

其它地方没有哈,那这句也可以放到构造方法里面不?
kf156 2008-12-19
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 yezersky 的回复:]
setCommandListener(this); 为什么也要放到jbInit()方法里面呢?
[/Quote]

你看下有没其他地方设置命令监听
如果没有的话,这行代码应该是可以放到这类的构造里。
yezersky 2008-12-19
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 kf156 的回复:]
case GAME_RUN: //这里就游戏开始,已经调用了
case GAME_OVER:
//…… 这里的代码GAME_RUN也会执行

请注意第一个case后没有break;
那么GAME_OVER下的代码,它也会执行的。


if (bNewGame) { //若是重新游戏
chess = new Chesses(); //我估计chess构造里,肯定有棋子的复位
nSelectX = nSelectY = nMapGrid / 2; //选择光标复位
}


Java codepublicvoidcommandAction(Command command, Displayable displayable…
[/Quote]

多些了,Chesses的构造函数里面确实有复位方法,另外,setCommandListener(this); 为什么也要放到jbInit()方法里面呢?
yezersky 2008-12-19
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 kf156 的回复:]
你去看下paintChesses()方法就知道了
绘制多少棋子是在这里头控制的

游戏中肯定是要绘制棋子的,所以要调用paintChesses()
至于此时绘制了多少棋子是由paintChesses()方法决定
如果这方法里头没绘制任何棋子,哪怕这方法被调用了,界面上也是没绘制棋子的

[/Quote]

明白了,多谢哈
kf156 2008-12-19
  • 打赏
  • 举报
回复
你去看下paintChesses()方法就知道了
绘制多少棋子是在这里头控制的

游戏中肯定是要绘制棋子的,所以要调用paintChesses()
至于此时绘制了多少棋子是由paintChesses()方法决定
如果这方法里头没绘制任何棋子,哪怕这方法被调用了,界面上也是没绘制棋子的
yezersky 2008-12-19
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 kf156 的回复:]

这paintChesses()是绘制所有的棋子,不是一个棋子
[/Quote]


游戏一开始就绘制全部棋子???
kf156 2008-12-19
  • 打赏
  • 举报
回复

这paintChesses()是绘制所有的棋子,不是一个棋子
yezersky 2008-12-19
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 kf156 的回复:]
case GAME_RUN: //这里是游戏开始
case GAME_OVER:


是游戏开始后才执行(即下棋时)
当状态为GMAE_RUN/GAME_OVER时才有绘制

//从这里看出,是用户点了游戏开始命令后,状态才切换到GAME_RUN的
(command == cmdStart){ //从这里看出一开始是有添加cmdStart命令的
nState = GAME_RUN; //点击后,游戏开始

[/Quote]

多谢了哈,我的意思是不是该放下棋子时才调用paintChesses()方法吗
kf156 2008-12-19
  • 打赏
  • 举报
回复
case GAME_RUN: //这里是游戏开始
case GAME_OVER:


是游戏开始后才执行(即下棋时)
当状态为GMAE_RUN/GAME_OVER时才有绘制

//从这里看出,是用户点了游戏开始命令后,状态才切换到GAME_RUN的
(command == cmdStart){ //从这里看出一开始是有添加cmdStart命令的
nState = GAME_RUN; //点击后,游戏开始
yezersky 2008-12-19
  • 打赏
  • 举报
回复
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());
}



还想问一下:paintChesses()这段代码游戏一开始就执行吗,不是应该下棋的时候哦才执行吗?
kf156 2008-12-19
  • 打赏
  • 举报
回复
那你试着把下边这句放构造里看看,目前来看应该是没问题
setCommandListener(this);
kf156 2008-12-18
  • 打赏
  • 举报
回复
case GAME_RUN: //这里就游戏开始,已经调用了
case GAME_OVER:
//…… 这里的代码GAME_RUN也会执行

请注意第一个case后没有break;
那么GAME_OVER下的代码,它也会执行的。


if (bNewGame) { //若是重新游戏
chess = new Chesses(); //我估计chess构造里,肯定有棋子的复位
nSelectX = nSelectY = nMapGrid / 2; //选择光标复位
}



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.removeCommand(cmdRestart);
this.addCommand(cmdStart);
repaint();
}else if (command == cmdStart){ //从这里看出一开始是有添加cmdStart命令的
nState = GAME_RUN; //点击后,游戏开始
this.removeCommand(cmdStart); //cmdStart命令才被移除
this.addCommand(cmdRestart);
this.addCommand(cmdPause);
repaint();
}
}
biaozai06 2008-12-18
  • 打赏
  • 举报
回复
lz把代码贴全先吧,现在只贴了WzqChessBoard类,管中窥豹。。
yezersky 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 kf156 的回复:]
问题二:paintMap()和paintSelectBox()为什么游戏开始的时候没有调用呢?

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());
}



问题三:jbInit()方法里的工作为什么不直接放到…
[/Quote]

但是那个判断之后没有任何代码都嘛,杂调用的安?

另外,jbInit()方法里面这些为什么需要重新调用?
setCommandListener(this);
if (bNewGame) {
chess = new Chesses();
nSelectX = nSelectY = nMapGrid / 2;

之前的Chesses对象没有删除好吗?

很菜,别见笑……
wap21 2008-12-18
  • 打赏
  • 举报
回复
帮顶
yezersky 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 biaozai06 的回复:]
引用 4 楼 yezersky 的回复:
第一个问题是游戏里面用到command:

cmdStart = new Command("?aê?ó??·",Command.OK,1);

cmdPause = new Command("?Yí£",Command.OK,1);

cmdExit = new Command("í?3?ó??·", Command.EXIT, 0);

cmdRestart = new Command("??D??aê?ó??·", Command.SCREEN, 0);

addCommand(cmdRestart);

addCommand(cmdExit);

addCommand(cmdPause);//问题一:这里为什么不加上addCommand(cmdS…
[/Quote]

明白,多谢了哈
biaozai06 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yezersky 的回复:]
第一个问题是游戏里面用到command:

cmdStart = new Command("?aê?ó??·",Command.OK,1);

cmdPause = new Command("?Yí£",Command.OK,1);

cmdExit = new Command("í?3?ó??·", Command.EXIT, 0);

cmdRestart = new Command("??D??aê?ó??·", Command.SCREEN, 0);

addCommand(cmdRestart);

addCommand(cmdExit);

addCommand(cmdPause);//问题一:这里为什么不加上addCommand(cmdStart)???
[/Quote]

这里可以不加上addCommand(cmdStart),因为程序一运行游戏就已经自动开始了,只有restart才有意义。
kf156 2008-12-18
  • 打赏
  • 举报
回复
问题二:paintMap()和paintSelectBox()为什么游戏开始的时候没有调用呢?


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());
}

问题三:jbInit()方法里的工作为什么不直接放到构造方法里面呢?
当用户要重新游戏时,还得调用jbInit()
kf156 2008-12-18
  • 打赏
  • 举报
回复
你那是哪看到的没加?
我觉得应该是有加上cmdStart
yezersky 2008-12-18
  • 打赏
  • 举报
回复
问题三:jbInit()方法里的工作为什么不直接放到构造方法里面呢?



多谢各位了,自己最近学习,看一个五子棋的代码,不大懂,多谢各位指教。
加载更多回复(5)

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧