首先感谢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下调试通过,最后恳请大家批评指正。
闽公网安备 35060202000074号