最近看到了一些五子棋,扫雷的代码讲解的文章,我也就写了个手机的象棋游戏,写的不是太全面,但还是能实现基本功能,共享出来供大家交流交流。
先介绍一下我的大体思路吧,我采用canvas让手机自己画出棋盘和棋子,而不是采用调用图片,虽然麻烦,但能锻炼自己的编程能力还能让算法简单,同时还能节省空间。具体的细节在代码中在说吧。首先要有一个主程序Game,这里比较简单,大家一看就能明白,我就不多说了.
import Javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
public class Game extends MIDlet {
GameCanvas game;//定义游戏界面的Canvas类GameCanvas的对象gobang
public Game() {
super();
game=new GameCanvas(this);//生成GameCanvas类的对象game
}
protected void startApp(){
Display.getDisplay(this).setCurrent(game);
//在屏幕上绘出游戏见面game
}
protected void pauseApp(){
}
protected void destroyApp(boolean arg0){
}
}
然后就是程序的主题部分了--GameCanvas,这里实现了从画棋盘棋子一直到判定和输出.
我的主题思想是把棋盘初始化为一个2维数组,在有棋子的地方初始化为非0数,其他的都初始化为0;
大家可在代码中看到,在图象输出和棋子移动也都是基于这个数组进行的。
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class GameCanvas extends Canvas implements CommandListener
{
protected Game game;
protected int empty;//屏幕右侧留的空间
protected int x;//棋盘输出的坐标
protected int cellWidth;//每个棋格的边长
protected int mapWidth,canvasW;//棋盘的宽度和画布的宽度
protected int a,b,c,d;//这是画炮下面的那几个折线,没什么用
protected int chessR;//棋子的半径
protected int selectedX,selectedY;//选择框在棋盘格局上的x,y位置
protected static int i,j;
protected int m,n,p;//记住开始的selectedX,selectedY和point[selectedX][selectedY]
protected String q;//记住Word[selectedX][selectedY]
protected int guard,guard1,guard2,g,g1;//标记FIRE被按了多少次,g是用来判定走直线时前后的棋子,中间是否有其他棋子的累加器
protected static int g2,isRedWin,isWhiteWin;//g2表示该谁走了,后面那俩顾名思义了
protected Command exitCmd;
protected int point[][]={{1,2,3,4,5,6,7,8,9},//初始化INT数组
{0,0,0,0,0,0,0,0,0},
{0,10,0,0,0,0,0,11,0},
{12,0,13,0,14,0,15,0,16},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{28,0,29,0,30,0,31,0,32},
{0,26,0,0,0,0,0,27,0},
{0,0,0,0,0,0,0,0,0},
{17,18,19,20,21,22,23,24,25}};
protected String[][] word;
public GameCanvas(){};
public GameCanvas(Game game)//构造函数
{
this.game=game;
empty=getWidth()/6;
x=empty*1/3;
canvasW=getWidth()-empty;
mapWidth=canvasW-canvasW%8;
cellWidth=mapWidth/8;
a=cellWidth*2/5;
b=cellWidth/8;
c=cellWidth-a;
d=cellWidth-b;
chessR=cellWidth*2/5;
selectedX=0;
selectedY=0;
guard=0;
guard1=selectedX;guard2=selectedY;
m=guard1;n=guard2;
word=new String[10][9];
g2=1;
for(i=0;i<10;i++)//初始化字符数组
{
for(j=0;j<9;j++)
{
if(i==0)
{
if(j==0){word[i][j]="车";}
if(j==1){word[i][j]="马";}
if(j==2){word[i][j]="相";}
if(j==3){word[i][j]="士";}
if(j==4){word[i][j]="帅";}
if(j==8){word[i][j]="车";}
if(j==7){word[i][j]="马";}
if(j==6){word[i][j]="相";}
if(j==5){word[i][j]="士";}
}
if(i==1){word[i][j]="空";}
if(i==2){
if((j!=1)&(j!=7)){word[i][j]="空";}
if(j==1){word[i][j]="炮";}
if(j==7){word[i][j]="炮";}
}
if(i==3){
if(j%2==0){word[i][j]="卒";}
if(j%2==1){word[i][j]="空";}
}
if(i==4){word[i][j]="空";}
if(i==5){word[i][j]="空";}
if(i==6){
if(j%2==0){word[i][j]="卒";}
if(j%2==1){word[i][j]="空";}
}
if(i==7){
if((j!=1)&(j!=7)){word[i][j]="空";}
if(j==1){word[i][j]="炮";}
if(j==7){word[i][j]="炮";}
}
if(i==8){word[i][j]="空";}
if(i==9)
{
if(j==0){word[i][j]="车";}
if(j==1){word[i][j]="马";}
if(j==2){word[i][j]="相";}
if(j==3){word[i][j]="士";}
if(j==4){word[i][j]="帅";}
if(j==8){word[i][j]="车";}
if(j==7){word[i][j]="马";}
if(j==6){word[i][j]="相";}
if(j==5){word[i][j]="士";}
}
}
}
exitCmd = new Command("退出", Command.EXIT, 0);
addCommand(exitCmd);
setCommandListener(this);
}
protected void paintMapa(Graphics g)//画河的上半部分的棋盘
{
for(int q=0;q<4;q++)
{
for(int w=0;w<8;w++)
{
g.setColor(128,128,128);
g.drawRect(x+w*cellWidth,x+q*cellWidth,cellWidth,cellWidth);
}
}
g.setColor(128,128,128);
g.drawLine(x+3*cellWidth,x,x+5*cellWidth,x+2*cellWidth);
g.drawLine(x+5*cellWidth,x,x+3*cellWidth,x+2*cellWidth);
//画左上方的炮
g.drawLine(x+d,x+cellWidth+c,x+d,x+cellWidth+d);//左上竖
g.drawLine(x+c,x+cellWidth+d,x+d,x+cellWidth+d);//左上横
g.drawLine(x+d+2*b,x+cellWidth+c,x+d+2*b,x+cellWidth+d);//右上竖
g.drawLine(x+cellWidth+b,x+cellWidth+d,x+cellWidth+a,x+cellWidth+d);//右上横
g.drawLine(x+d,x+2*cellWidth+b,x+d,x+2*cellWidth+a);//左下竖
g.drawLine(x+c,x+cellWidth+d+2*b,x+d,x+cellWidth+d+2*b);//左下横
g.drawLine(x+d+2*b,x+2*cellWidth+b,x+d+2*b,x+2*cellWidth+a);//右下竖
g.drawLine(x+cellWidth+b,x+cellWidth+d+2*b,x+cellWidth+a,x+cellWidth+d+2*b);//右下横
//画右上方的炮