文章来源:J2ME开发网
首先感谢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);
Image startImage = Gobang.createImage("/res/me.png");
this.addCommand(okCmd);
this.addCommand(exitCmd);
this.setCommandListener(this);
//Graphics g=getGraphics();
this.append(new ImageItem(null, startImage, ImageItem.LAYOUT_CENTER, null)) ;
// this.append(startImage);
Display.getDisplay(gobang.instance).setCurrent(this);
}
public void commandAction(Command command, Displayable displayable) {
if (command == okCmd)
{
gobangcanvas = new GobangCanvas();
Display.getDisplay(gobang.instance).setCurrent(gobangcanvas);
gobangcanvas.startup();
}
if (command == exitCmd) {
gobang.instance.quitApp();
}
}
}
4.主要实现类
package hero;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
public class GobangCanvas
extends GameCanvas
implements Runnable, CommandListener {
protected Gobang gobang;
private Graphics g;
int empty; //游戏界面到屏幕边缘的留空
int canvasW, canvasH; //画布的长和宽
int chessLength; //棋子的直径
int chessMapLength, chessMapGrid, chessGridLength; //棋盘的边长,棋盘一边格子数,每格宽度
int chessMapX, chessMapY; //棋盘左上角x,y坐标
int selectedX, selectedY; //选择框在棋盘格局上的x,y位置
boolean isPlayer1; //是否是玩家1
Chesses[][] chesses; //棋子数组
boolean newGame; //是否是新的游戏
boolean isplay; //是否游戏进行中
private Command exitCommand;
private Command startCommand;
int player1win, player2win;
public GobangCanvas() {
super(true);
newGame = true;
empty = 10;
canvasW = getWidth() - empty;
canvasH = getHeight() - empty;
chessMapGrid = 15;
chesses = new Chesses[chessMapGrid + 1][chessMapGrid + 1];
if (canvasW > canvasH) {
chessMapLength = canvasH - canvasH % chessMapGrid;
chessMapX = (canvasW - chessMapLength) / 2 + empty / 2;
chessMapY = (canvasH % chessMapGrid) / 2 + empty / 2;
}
else {
chessMapLength = canvasW - canvasW % chessMapGrid;
chessMapX = (canvasW % chessMapGrid) / 2 + empty / 2;
chessMapY = (canvasH - chessMapLength) / 2 + empty / 2;
}
chessGridLength = chessMapLength / chessMapGrid;
chessLength = chessGridLength - 1;
selectedX = selectedY = chessMapGrid / 2;
isPlayer1 = true;
exitCommand = new Command("退出", Command.EXIT, 1);
startCommand = new Command("开始", Command.SCREEN, 1);
addCommand(startCommand);
addCommand(exitCommand);
setCommandListener(this);
}
public void startup() {
isplay = true;
Thread thread = new Thread(this);
thread.start();
}
protected void paintMap(Graphics g) {
for (int i = 0; i < chessMapGrid; i++) {
for (int j = 0; j < chessMapGrid; j++) {
g.setColor(128, 128, 128);
g.drawRect(chessMapX + j * chessGridLength,
chessMapY + i * chessGridLength,
chessGridLength, chessGridLength);
if (chesses[i][j] != null) {
if (chesses[i][j].isPlayer1) {
g.setColor(255, 255, 255);
}
else {
g.setColor(255, 0, 0);
}
g.fillArc(chessMapX + j * chessGridLength - chessLength / 2,
chessMapY + i * chessGridLength - chessLength / 2,
chessLength, chessLength, 0, 360);
}
}
}
}
protected void paintSelected(Graphics g) {
g.setColor(0, 0, 255);
g.drawRect(chessMapX + selectedX * chessGridLength - chessGridLength / 2,
chessMapY + selectedY * chessGridLength - chessGridLength / 2,
chessGridLength, chessGridLength);
}
protected void paintPlayer(Graphics g, boolean isPlayer1) {
if (isPlayer1) {
g.setColor(255, 255, 255);
}
else {
g.setColor(255, 0, 0);
}
g.drawRect(1, 1, getWidth() - 2, getHeight() - 2);
g.drawRect(2, 2, getWidth() - 4, getHeight() - 4);
g.drawRect(3, 3, getWidth() - 6, getHeight() - 6);
}
public void paint(Graphics g) {
g.setColor(0x00000000);
g.fillRect(0, 0, getWidth(), getHeight());
paintPlayer(g, isPlayer1);
paintSelected(g);
paintMap(g);
flushGraphics();
}
private void init() {
if (newGame) {
chesses = new Chesses[chessMapGrid + 1][chessMapGrid + 1];
isPlayer1 = true;
selectedX = selectedY = chessMapGrid / 2;
}
}
public void run() {
g = getGraphics();
while (isplay) {
try {
input(g);
paint(g);
Thread.sleep(500);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public void input(Graphics g) {
int keyStates = getKeyStates();
if ( (keyStates & LEFT_PRESSED) != 0) {
selectedX = (--selectedX + chessMapGrid + 1) % (chessMapGrid + 1);
}
else if ( (keyStates & RIGHT_PRESSED) != 0) {
selectedX = (++selectedX) % (chessMapGrid + 1);
}
else if ( (keyStates & UP_PRESSED) != 0) {
selectedY = (--selectedY + chessMapGrid + 1) % (chessMapGrid + 1);
}
else if ( (keyStates & DOWN_PRESSED) != 0) {
selectedY = (++selectedY) % (chessMapGrid + 1);
}
else if ( (keyStates & FIRE_PRESSED) != 0) {
if (chesses[selectedY][selectedX] == null) {
chesses[selectedY][selectedX] = new Chesses(this.isPlayer1);
if (checkWin()) {
String winner;
if (isPlayer1) {
winner = "白方胜利";
player1win++;
}
else {
winner = "红方胜利";
player2win++;
}
Alert winAlert = new Alert("",
winner + "\n白方 " + player1win + " : " +
player2win + " 红方",
null, AlertType.INFO);
winAlert.setTimeout(Alert.FOREVER);
Display.getDisplay(gobang.instance).setCurrent(winAlert);
init();
flushGraphics();
// repaint();
}
this.isPlayer1 = !this.isPlayer1; //切换下棋方
}
}
flushGraphics();
// repaint();
}
private boolean checkWin() {
int num = 1;
if (num < 5) {
num = 1;
for (int i = 1; i <= 4; i++) {
if (isPlayer1(selectedX - i, selectedY)) {
num++;
}
else if(isPlayer1(selectedX + i, selectedY)) {
num++;
}else{
break;
}
}
}
if (num < 5) {
num = 1;
for (int i = 1; i <= 4; i++) {
if (isPlayer1(selectedX, selectedY - i)) {
num++;
}
else if(isPlayer1(selectedX, selectedY + i)) {
num++;
}
else {
break;
}
}
}
if (num < 5) {
num = 1;
for (int i = 1; i <= 4; i++) {
if (isPlayer1(selectedX - i, selectedY - i)) {
num++;
}
else if (isPlayer1(selectedX + i, selectedY + i)) {
num++;
}
else {
break;
}
}
}
if (num < 5) {
num = 1;
for (int i = 1; i <= 4; i++) {
if (isPlayer1(selectedX + i, selectedY - i)) {
num++;
}
else if(isPlayer1(selectedX - i, selectedY + i)) {
num++;
}
else {
break;
}
}
}
if (num >= 5) {
return true;
}
else {
return false;
}
}
private boolean isPlayer1(int y, int x) {
if (x <= 15 && x >= 0 && y <= 15 && y >= 0 && chesses[x][y] != null) {
if (chesses[x][y].isPlayer1 == this.isPlayer1) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
public void commandAction(Command c, Displayable dpa) {
if (c.equals(startCommand)) {
init();
startup();
}
else if (c.equals(exitCommand)) {
gobang.instance.startApp();
}
}
}
以上代码在wtk2.2下调试通过,最后恳请大家批评指正。