首先感谢yinnowl提供的文章MIDP1.0-五子棋1.1,我是一个J2ME的初学者,看了yinnowl的这篇文章后,有些自己的想法,所以就冒昧和大家分享,请大家批评指正。
我的思路是把MIDP1.0-五子棋1.1改成在MIDP2.0上实现,同时感觉原文在判定胜败的时候比较慢做了下优化,原文链接请点击这里。
关于具体的解释,原文已经都有,我这也不敢班门弄斧,只给出我自己用到的4个类:
1.MIDlet 主类Gobang.Java
package hero;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Image;
public class Gobang
extends MIDlet
{
startForm startform;
private Display dispaly;
public static Gobang instance;
public Gobang() {
instance = this;
dispaly = Display.getDisplay(this);
}
protected void startApp() {
startform = new startForm();
dispaly.setCurrent(startform);
}
protected void pauseApp() {
}
protected void destroyApp(boolean uncondition) {
}
protected void quitApp(){
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
//读取并添加图片
public static Image createImage(String name) {
Image aImage = null;
try {
aImage = Image.createImage(name);
}
catch (Exception e) {
}
return aImage;
}
}
2.棋子类 Chesses .java
package hero;
public class Chesses {
boolean isPlayer1;
public Chesses(){
}
public Chesses(boolean isPlayer1) {
this.isPlayer1=isPlayer1;
}
}
3.图形加载类 startForm.java (一个过渡界面)
package hero;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
public class startForm extends Form implements CommandListener {
private static Command exitCmd;
private static Command okCmd;
private Image startImage;
protected Gobang gobang;
private GobangCanvas gobangcanvas;
//显示程序的启动画面//
public startForm() {
super("");
okCmd = new Command("进入", Command.OK, 1);
exitCmd = new Command("离开", Command.EXIT, 1);