分享
 
 
 

MIDP1.0-五子棋1.1读后的修改

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

文章来源: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下调试通过,最后恳请大家批评指正。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有