目前類的結構如下(部分):
啓動J2ME的主方法 需要啓動的MIDlet(必須繼承MIDlet類)
FiveLove.java
/*
* Created on 2005-5-7
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.leqi.midlet;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.leqi.common.Constants;
/**
* @author leqi
* create 2005-5-7
*
* msn: [email protected]
*
* qq: 26826880
*
* mail: [email protected]
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Fivelove extends MIDlet {
/**
*
*/
private Display display;
private Chessboard chessboard;
private MainMenu mainMenu;
public Fivelove() {
super();
// TODO Auto-generated constructor stub
display = Display.getDisplay(this);
mainMenu = new MainMenu(this, Constants.MAIN_MENU_TITLE, Constants.LIST_IMPLICIT);
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(mainMenu);
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp() {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
public Display getDisplay(){
return this.display;
}
public void exitMIDlet(){
try {
destroyApp(true);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
}
}
其中構造函數,是在啓動程序的時候,只運行一次的,所以將首界面的初始化放在次過程進行
public Fivelove() {
super();
// TODO Auto-generated constructor stub
display = Display.getDisplay(this);
mainMenu = new MainMenu(this, Constants.MAIN_MENU_TITLE, Constants.LIST_IMPLICIT);
}
避免了每一次都初始化操作
在startApp()就是每一次進入的時候,都調用的方法。會重複調用
至於另外幾個方法,就都是基本的功能,如命名!
----------------------------------------------------------------------
主菜單所用到的界面
MainMenu.java
需要繼承list來實現頁面的表現,以及實現CommandListener來對按鍵進
行監聽
/*
* Created on 2006-3-14
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.leqi.midlet;
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextBox;
import com.leqi.common.Constants;
/**
* @author leqi
* create 2006-3-14
*
* msn: [email protected]
*
* qq: 26826880
*
* mail: [email protected]
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class MainMenu extends List implements CommandListener {
private Command exit;
private Command menu;
private Command help;
private Command start;
private Command back;
private TextBox helpMesg;
private Form formHelp;
private StringItem content;
private Image image;
private ImageItem imageItem;
private Fivelove gobang;
private Chessboard chessboard;
public MainMenu(Fivelove gobang, String str, int i){
super(str, i);
this.gobang = gobang;
exit = new Command("Exit", Command.EXIT, 1);
start = new Command("Start", Command.OK, 1);
back = new Command("Back", Command.BACK, 2);
help = new Command("Info", Command.HELP, 3);
formHelp = new Form("Info");
content = new StringItem("", Constants.MAIN_HELP);
try {
image = Image.createImage(Constants.MAIN_IMAGE);
imageItem = new ImageItem(null, image,
ImageItem.LAYOUT_BOTTOM |
ImageItem.LAYOUT_CENTER , "My Image");
formHelp.append(content);
formHelp.append(imageItem);
formHelp.addCommand(back);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.append(Constants.FIVE_LOVE, null);
this.append(Constants.FIGHT_LORD, null);
this.addCommand(help);
this.addCommand(start);
this.addCommand(exit);
formHelp.setCommandListener(this);
setCommandListener(this);
}
/**
* @param arg0
* @param arg1
*/
public MainMenu(String str, int i) {
super(str, i);
// TODO Auto-generated constructor stub
}
/**
* @param arg0
* @param arg1
* @param arg2
* @param arg3
*/
public MainMenu(String arg0, int arg1, String[] arg2, Image[] arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command command, Displayable displayable) {
// TODO Auto-generated method stub
if (command == start) {
if(this.getSelectedIndex() == 0){
chessboard = new Chessboard(gobang);
Display.getDisplay(gobang).setCurrent(chessboard);
}
}else if (command == exit) {
gobang.exitMIDlet();
}else if (command == help){
Display.getDisplay(gobang).setCurrent(formHelp);
}else if (command == back){
Display.getDisplay(gobang).setCurrent(this);
}
}
}
------------------------------------------------------------------------------
五子棋遊戲主界面
Chessboard.java
該類繼承了Cavans類,對頁面才用低層的畫步處理,同時實現了
CommandListener接口,對頁面上的操作進行了監聽
/*
* Created on 2005-5-7
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.leqi.midlet;
//import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDletStateChangeException;
//import javax.microedition.rms.RecordStore;
//import javax.microedition.rms.RecordStoreException;
//import javax.microedition.rms.RecordStoreNotFoundException;
import com.leqi.chess.Chessman;
import com.leqi.chess.JudgeOver;
import com.leqi.chess.RecordStoreChessman;
import com.leqi.common.Constants;
/**
* @author leqi
* create 2005-5-7
*
* msn: [email protected]
*
* qq: 26826880
*
* mail: [email protected]
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Chessboard extends Canvas implements CommandListener {
/**
*
*/
private Command exit;
private Command start;
private Command stop;
private Command menu;
private Command cancle;
private Command back;
private Command help;
private TextBox helpMesg;
private Fivelove gobang;
private Alert alert;
/*
* 棋盤上的座標點
*/
private int height = getHeight();
private int width = getWidth();
private int A = height / 13;
private int B = A * 2;
private int C = A * 3;
private int D = A * 4;
private int E = A * 5;
private int F = A * 6;
private int G = A * 7;
private int H = A * 8;
private int I = A * 9;
private int J = A * 10;
private int K = A * 11;
private int L = A * 12;
private int a = width / 13;
private int b = a * 2;
private int c = a * 3;
private int d = a * 4;
private int e = a * 5;
private int f = a * 6;
private int g = a * 7;
private int h = a * 8;
private int i = a * 9;
private int j = a * 10;
private int k = a * 11;
private int l = a * 12;
/*
* 棋盤上的橫向,縱向,單元格的半徑和直徑 a 爲橫向的直徑 A爲縱向的直徑
*/
private int rw = a / 2;
private int rh = A / 2;
private int initX = g;
private int initY = F;
private boolean fire = false;//表示按鈕是否被激活
private boolean once = true;//表示是否進行過操作
/*
* false 代表下黑
* true 代表下白
*/
private boolean color = false;
// private int index_X;
// private int index_Y;
public Chessboard(Fivelove gobang) {
super();
this.gobang = gobang;
exit = new Command("Exit", Command.EXIT, 1);
stop = new Command("Stop", Command.STOP, 2);
back = new Command("Back", Command.BACK, 2);
cancle = new Command("Cancle", Command.CANCEL, 2);
help = new Command("Help", Command.HELP, 3);
helpMesg = new TextBox("Online Help", Constants.CHESSMAN_HELP, 81, 0);
helpMesg.addCommand(back);
RecordStoreChessman.deleteChessman();
this.addCommand(stop);
this.addCommand(cancle);
this.addCommand(help);
this.addCommand(exit);
setCommandListener(this);
helpMesg.setCommandListener(this);
// TODO Auto-generated constructor stub
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
*/
protected void paint(Graphics graphics) {
// TODO Auto-generated method stub
graphics.setColor(213, 197, 170);
graphics.fillRect(0, 0, width, height);
graphics.setColor(0, 0, 0);
graphics.drawLine(a, A, l, A);
graphics.drawLine(a, B, l, B);
graphics.drawLine(a, C, l, C);
graphics.drawLine(a, D, l, D);
graphics.drawLine(a, E, l, E);
graphics.drawLine(a, F, l, F);
graphics.drawLine(a, G, l, G);
graphics.drawLine(a, H, l, H);
graphics.drawLine(a, I, l, I);
graphics.drawLine(a, J, l, J);
graphics.drawLine(a, K, l, K);
graphics.drawLine(a, L, l, L);
graphics.drawLine(a, A, a, L);
graphics.drawLine(b, A, b, L);
graphics.drawLine(c, A, c, L);
graphics.drawLine(d, A, d, L);
graphics.drawLine(e, A, e, L);
graphics.drawLine(f, A, f, L);
graphics.drawLine(g, A, g, L);
graphics.drawLine(h, A, h, L);
graphics.drawLine(i, A, i, L);
graphics.drawLine(j, A, j, L);
graphics.drawLine(k, A, k, L);
graphics.drawLine(l, A, l, L);
graphics.setColor(223, 37, 17);
graphics.drawRect(initX - rw, initY-rh, a, A);
Chessman[] chessmans = RecordStoreChessman.read();
if(chessmans != null){
for(int x = 0; x < chessmans.length; x++){
if(chessmans[x].isColor()){
graphics.setColor(255, 255, 255);
graphics.fillArc(chessmans[x].getX() - rw,
chessmans[x].getY() - rh,
a, A, 0, 360);
}else{
graphics.setColor(0, 0, 0);
graphics.fillArc(chessmans[x].getX() - rw,
chessmans[x].getY() - rh,
a, A, 0, 360);
}
}
}else{
RecordStoreChessman.closeChessman();
}
if(fire && once){
boolean find = false;
if(chessmans != null){
//判斷此位置是否已經存在下了的棋子,如果有find = true
for(int x = 0; x < chessmans.length; x++){
if(chessmans[x].getX() == initX && chessmans[x].getY() == initY){
find = true;
break;
}
}
}
if(!find){
Chessman chessman = new Chessman();
chessman.setX(initX);
chessman.setY(initY);
chessman.setHaveChess(true);
chessman.setColor(color);
RecordStoreChessman.storeChessman(chessman);
if(color){
graphics.setColor(255, 255, 255);
graphics.fillArc(initX-rw, initY-rh, a, A, 0, 360);
}else{
graphics.setColor(0, 0, 0);
graphics.fillArc(initX-rw, initY-rh, a, A, 0, 360);
}
//add
if(chessmans != null){
JudgeOver judge = new JudgeOver();
byte ju;
ju = judge.judgeMent(chessmans, chessman, a, A);
if(ju == 1){
alert = new Alert("Game over", "黑棋獲勝", null,
AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
alert.addCommand(exit);
gobang.getDisplay().setCurrent(alert);
}
if(ju == 2){
alert = new Alert("Game over", "白棋獲勝", null,
AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
alert.addCommand(exit);
gobang.getDisplay().setCurrent(alert);
}
}
//add to over
color = !color;
fire = false;
once = false;
}
}
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
* javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command command, Displayable displayable) {
// TODO Auto-generated method stub
if (command == exit) {
try {
RecordStoreChessman.deleteChessman();
gobang.startApp();
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if (command == help){
Display.getDisplay(gobang).setCurrent(helpMesg);
}else if (command == back){
Display.getDisplay(gobang).setCurrent(this);
}else if (command == stop){
gobang.pauseApp();
}
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.Canvas#keyPressed(int)
*/
protected void keyPressed(int key) {
switch (getGameAction(key)) {
case Canvas.UP:
initY = initY - A;
if(initY<A){
initY = A;
}
fire = false;
once = true;
break;
case Canvas.DOWN:
initY = initY + A;
if(initY > L){
initY = L;
}
fire = false;
once = true;
break;
case Canvas.LEFT:
initX = initX - a;
if(initX < a){
initX = a;
}
fire = false;
once = true;
break;
case Canvas.RIGHT:
initX = initX + a;
if(initX >l){
initX = l;
}
fire = false;
once = true;
break;
case Canvas.FIRE:
fire = true;
break;
}
repaint();
}
}
-------------------------------------------------
用來描述棋盤每一個點信息的類
Chessman.java
涉及到四個屬性,座標,所下棋子顏色,已經是否存在棋子
/*
* Created on 2005-5-9
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.leqi.chess;
/**
* @author leqi
* create 2005-5-9
*
* msn: [email protected]
*
* qq: 26826880
*
* mail: [email protected]
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Chessman {
private int x;
private int y;
private boolean haveChess;
/*
* true 爲白色
* false 爲黑色
*/
private boolean color;
/**
* @return Returns the color.
*/
public boolean isColor() {
return color;
}
/**
* @param color The color to set.
*/
public void setColor(boolean color) {
this.color = color;
}
/**
* @return Returns the haveChess.
*/
public boolean isHaveChess() {
return haveChess;
}
/**
* @param haveChess The haveChess to set.
*/
public void setHaveChess(boolean haveChess) {
this.haveChess = haveChess;
}
/**
* @return Returns the x.
*/
public int getX() {
return x;
}
/**
* @param x The x to set.
*/
public void setX(int x) {
this.x = x;
}
/**
* @return Returns the y.
*/
public int getY() {
return y;
}
/**
* @param y The y to set.
*/
public void setY(int y) {
this.y = y;
}
}
--------------------------------------
RecordStroeChessman.java
此類調用了RecordStore類,作爲在遊戲過程中的存儲介質,其中
提供的查看,存儲,關閉,刪除。
/*
* Created on 2005-5-9
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.leqi.chess;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
import com.leqi.common.Constants;
/**
* @author leqi create 2005-5-9
*
* msn: [email protected]
*
* qq: 26826880
*
* mail: [email protected]
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class RecordStoreChessman {
private static RecordStore recordstore = null;
/**
* @param chessman
*/
public static void storeChessman(Chessman chessman) {
try {
recordstore = RecordStore.openRecordStore(Constants.CHESSMAN_STORE,
true);
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] outputRecord;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new DataOutputStream(outputStream);
try {
outputDataStream.writeInt(chessman.getX());
outputDataStream.writeBoolean(chessman.isHaveChess());
outputDataStream.writeInt(chessman.getY());
outputDataStream.writeBoolean(chessman.isColor());
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.addRecord(outputRecord, 0, outputRecord.length);
outputStream.reset();
outputStream.close();
outputDataStream.close();
recordstore.closeRecordStore();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void closeChessman() {
try {
recordstore.closeRecordStore();
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void deleteChessman() {
if (RecordStore.listRecordStores() != null) {
try {
RecordStore.deleteRecordStore(Constants.CHESSMAN_STORE);
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static Chessman[] read() {
try {
recordstore = RecordStore.openRecordStore(Constants.CHESSMAN_STORE,
true);
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] byteInputData = new byte[10];
ByteArrayInputStream inputStream = new ByteArrayInputStream(
byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
try {
Chessman[] chessmans = null;
int num = recordstore.getNumRecords();
if (num < 1) {
return chessmans;
} else {
chessmans = new Chessman[num];
int tmpX;
int tmpY;
boolean tmpHaveChess;
boolean tmpColor;
for (int x = 1; x <= num; x++) {
Chessman chessman = new Chessman();
recordstore.getRecord(x, byteInputData, 0);
tmpX = inputDataStream.readInt();
tmpHaveChess = inputDataStream.readBoolean();
tmpY = inputDataStream.readInt();
tmpColor = inputDataStream.readBoolean();
inputStream.reset();
chessman.setX(tmpX);
chessman.setHaveChess(tmpHaveChess);
chessman.setY(tmpY);
chessman.setColor(tmpColor);
chessmans[x - 1] = chessman;
}
inputStream.close();
inputDataStream.close();
recordstore.closeRecordStore();
return chessmans;
}
} catch (RecordStoreNotOpenException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvalidRecordIDException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
-----------------------------------
JudgeOver.java
此類,用來判斷當前情況下,五子是否已經滿足獲勝的條件,其中需要從八個方向分別進行判斷
/*
* Created on 2005-5-10
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.leqi.chess;
/**
* @author leqi
* create 2005-5-10
*
* msn: [email protected]
*
* qq: 26826880
*
* mail: [email protected]
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class JudgeOver {
/**
* 判斷上下方是否已經形成五子
*
* @param chessmans
* @param chessman
* @param a
* @param A
* @return
*/
private byte judgeUp(Chessman[] chessmans, int x, int y, boolean color,
int a, int A, byte num) {
for (int z = 0; z < chessmans.length; z++) {
if (chessmans[z].isColor() == color && chessmans[z].getX() == x
&& chessmans[z].getY() == y - A) {
num++;
if (num == 5) {
return 5;
}
num = judgeUp(chessmans, x, y - A, color, a, A, num);
return num;
}
}
return num;
}
/**
* 判斷下方是否已經形成五子
*
* @param chessmans
* @param chessman
* @param a
* @param A
* @return
*/
private byte judgeDown(Chessman[] chessmans, int x, int y, boolean color,
int a, int A, byte num) {
for (int z = 0; z < chessmans.length; z++) {
if (chessmans[z].isColor() == color && chessmans[z].getX() == x
&& chessmans[z].getY() == y + A) {
num++;
if (num == 5) {
return 5;
}
num = judgeDown(chessmans, x, y + A, color, a, A, num);
return num;
}
}
return num;
}
/**
* 判斷左方是否已經形成五子
*
* @param chessmans
* @param chessman
* @param a
* @param A
* @return
*/
private byte judgeLeft(Chessman[] chessmans, int x, int y, boolean color,
int a, int A, byte num) {
for (int z = 0; z < chessmans.length; z++) {
if (chessmans[z].isColor() == color && chessmans[z].getX() == x - a
&& chessmans[z].getY() == y) {
num++;
if (num == 5) {
return 5;
}
num = judgeLeft(chessmans, x - a, y, color, a, A, num);
return num;
}
}
return num;
}
/**
* 判斷右方是否已經形成五子
*
* @param chessmans
* @param chessman
* @param a
* @param A
* @return
*/
private byte judgeRight(Chessman[] chessmans, int x, int y, boolean color,
int a, int A, byte num) {
for (int z = 0; z < chessmans.length; z++) {
if (chessmans[z].isColor() == color && chessmans[z].getX() == x + a
&& chessmans[z].getY() == y) {
num++;
if (num == 5) {
return 5;
}
num = judgeRight(chessmans, x + a, y, color, a, A, num);
return num;
}
}
return num;
}
/**
* 判斷左上方是否已經形成五子
*
* @param chessmans
* @param chessman
* @param a
* @param A
* @return
*/
private byte judgeUpLeft(Chessman[] chessmans, int x, int y, boolean color,
int a, int A, byte num) {
for (int z = 0; z < chessmans.length; z++) {
if (chessmans[z].isColor() == color && chessmans[z].getX() == x - a
&& chessmans[z].getY() == y - A) {
num++;
if (num == 5) {
return 5;
}
num = judgeUpLeft(chessmans, x - a, y - A, color, a, A, num);
return num;
}
}
return num;
}
/**
* 判斷右下方是否已經形成五子
*
* @param chessmans
* @param chessman
* @param a
* @param A
* @return
*/
private byte judgeDownRight(Chessman[] chessmans, int x, int y,
boolean color, int a, int A, byte num) {
for (int z = 0; z < chessmans.length; z++) {
if (chessmans[z].isColor() == color && chessmans[z].getX() == x + a
&& chessmans[z].getY() == y + A) {
num++;
if (num == 5) {
return 5;
}
num = judgeDownRight(chessmans, x + a, y + A, color, a, A, num);
return num;
}
}
return num;
}
/**
* 判斷右上方是否已經形成五子
*
* @param chessmans
* @param chessman
* @param a
* @param A
* @return
*/
private byte judgeUpRight(Chessman[] chessmans, int x, int y,
boolean color, int a, int A, byte num) {
for (int z = 0; z < chessmans.length; z++) {
if (chessmans[z].isColor() == color && chessmans[z].getX() == x + a
&& chessmans[z].getY() == y - A) {
num++;
if (num == 5) {
return 5;
}
num = judgeUpRight(chessmans, x + a, y - A, color, a, A, num);
return num;
}
}
return num;
}
/**
* 判斷左下方是否已經形成五子
*
* @param chessmans
* @param chessman
* @param a
* @param A
* @return
*/
private byte judgeDownLeft(Chessman[] chessmans, int x, int y,
boolean color, int a, int A, byte num) {
for (int z = 0; z < chessmans.length; z++) {
if (chessmans[z].isColor() == color && chessmans[z].getX() == x - a
&& chessmans[z].getY() == y + A) {
num++;
if (num == 5) {
return 5;
}
num = judgeDownLeft(chessmans, x - a, y + A, color, a, A, num);
return num;
}
}
return num;
}
/**
*
* @param chessmans
* @param chessman
* @param a
* @param A
* @return 0 代表沒有分出勝負 1 代表黑方勝 2 代表白方勝
*/
public byte judgeMent(Chessman[] chessmans, Chessman chessman, int a, int A) {
int x = chessman.getX();
int y = chessman.getY();
boolean color = chessman.isColor();
byte num = 1;
byte totleUpDown = 1;
byte totleLeRi = 1;
byte totleLeUpRiDo = 1;
byte totleRiUpLeDo = 1;
if ((totleUpDown = judgeUp(chessmans, x, y, color, a, A, totleUpDown)) == 5
|| judgeDown(chessmans, x, y, color, a, A, totleUpDown) == 5
|| (totleLeRi = judgeLeft(chessmans, x, y, color, a, A,
totleLeRi)) == 5
|| judgeRight(chessmans, x, y, color, a, A, totleLeRi) == 5
|| (totleLeUpRiDo = judgeUpLeft(chessmans, x, y, color, a, A,
totleLeUpRiDo)) == 5
|| judgeDownRight(chessmans, x, y, color, a, A, totleLeUpRiDo) == 5
|| (totleRiUpLeDo = judgeUpRight(chessmans, x, y, color, a, A,
totleRiUpLeDo)) == 5
|| judgeDownLeft(chessmans, x, y, color, a, A, totleRiUpLeDo) == 5) {
if (color) {
return 2;
} else {
return 1;
}
} else {
return 0;
}
}
}
-----------------------------------
GameClock.java
此類,用來做遊戲的時間控制。目前還沒有調用此方法,在以後會進行調用。
需要使用java.util.TimerTask類進行控制
/*
* Created on 2006-3-16
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.leqi.common;
import java.util.TimerTask;
/**
* @author leqi
* create 2006-3-16
*
* msn: [email protected]
*
* qq: 26826880
*
* mail: [email protected]
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class GameClock extends TimerTask {
private int timeLeft = 60;//默認時間
private boolean pause = false;
/**
*
*/
public GameClock() {
super();
// TODO Auto-generated constructor stub
}
public GameClock(int timeLeft){
this.timeLeft = timeLeft;
}
/**
* @return Returns the timeLeft.
*/
public int getTimeLeft() {
return timeLeft;
}
/**
* @param timeLeft The timeLeft to set.
*/
public void setTimeLeft(int timeLeft) {
this.timeLeft = timeLeft;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
// TODO Auto-generated method stub
if(!pause){
timeLeft--;
}
}
public void pause(){
pause = true;
}
public void resume(){
pause = false;
}
}