Android实现中国象棋附源码下载
象棋,很多人多接触过,学者写了一个,大神可以指点一下~直接上代码:
贴出主要代码,想要Demo的点击下载:中国象棋Demo
packagewyf.ytl;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.media.MediaPlayer;
importandroid.view.MotionEvent;
importandroid.view.SurfaceHolder;
importandroid.view.SurfaceView;
/**
*该类是整个程序最主要的类,是主游戏的界面
*该界面继承自SurfaceView并实现了SurfaceHolder.Callback接口
*其中包含了一个刷帧的线程类
*
*/
publicclassGameViewextendsSurfaceViewimplementsSurfaceHolder.Callback{
privateTutorialThreadthread;//刷帧的线程
TimeThreadtimeThread;
ChessActivityactivity;//声明Activity的引用
BitmapqiPan;//棋盘
Bitmapqizibackground;//棋子的背景图片
Bitmapwin;//胜利的图片
Bitmaplost;//失败的图片
Bitmapok;//确定按钮
Bitmapvs;//黑方红方VS的图片
Bitmapright;//向右的指针
Bitmapleft;//向左的指针
Bitmapcurrent;//“当前”文字
Bitmapexit2;//退出按钮图片
Bitmapsound2;//声音按钮图片
Bitmapsound3;//当前是否播放了声音
Bitmaptime;//冒号
Bitmapredtime;//红色冒号
Bitmapbackground;//背景图片
MediaPlayergo;//下棋声音
Paintpaint;//画笔
booleancaiPan=true;//是否为玩家走棋
booleanfocus=false;//当前是否有选中的棋子
intselectqizi=0;//当然选中的棋子
intstartI,startJ;//记录当前棋子的开始位置
intendI,endJ;//记录当前棋子的目标位置
Bitmap[]heiZi=newBitmap[7];//黑子的图片数组
Bitmap[]hongZi=newBitmap[7];//红子的图片数组
Bitmap[]number=newBitmap[10];//数字的图片数组,用于显示时间
Bitmap[]redNumber=newBitmap[10];//红色数字的图片,用于显示时间
GuiZeguiZe;//规则类
intstatus=0;//游戏状态。0游戏中,1胜利,2失败
intheiTime=0;//黑方总共思考时间
inthongTime=0;//红方总共思考时间
int[][]qizi=newint[][]{//棋盘
{2,3,6,5,1,5,6,3,2},
{0,0,0,0,0,0,0,0,0},
{0,4,0,0,0,0,0,4,0},
{7,0,7,0,7,0,7,0,7},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{14,0,14,0,14,0,14,0,14},
{0,11,0,0,0,0,0,11,0},
{0,0,0,0,0,0,0,0,0},
{9,10,13,12,8,12,13,10,9},
};
publicGameView(Contextcontext,ChessActivityactivity){//构造器
super(context);
this.activity=activity;//得到Activity的引用
getHolder().addCallback(this);
go=MediaPlayer.create(this.getContext(),R.raw.go);//加载下棋的声音
this.thread=newTutorialThread(getHolder(),this);//初始化刷帧线程
this.timeThread=newTimeThread(this);//初始化思考时间的线程
init();//初始化所需资源
guiZe=newGuiZe();//初始化规则类
}
publicvoidinit(){//初始化方法
paint=newPaint();//初始化画笔
qiPan=BitmapFactory.decodeResource(getResources(),R.drawable.qipan);//棋盘图片
qizibackground=BitmapFactory.decodeResource(getResources(),R.drawable.qizi);//棋子的背景
win=BitmapFactory.decodeResource(getResources(),R.drawable.win);//胜利的图片
lost=BitmapFactory.decodeResource(getResources(),R.drawable.lost);//失败的图片
ok=BitmapFactory.decodeResource(getResources(),R.drawable.ok);//确定按钮图片
vs=BitmapFactory.decodeResource(getResources(),R.drawable.vs);//vs字样的图片
right=BitmapFactory.decodeResource(getResources(),R.drawable.right);//向右的指针
left=BitmapFactory.decodeResource(getResources(),R.drawable.left);//向左的指针
current=BitmapFactory.decodeResource(getResources(),R.drawable.current);//文字“当前”
exit2=BitmapFactory.decodeResource(getResources(),R.drawable.exit2);//退出按钮图片
sound2=BitmapFactory.decodeResource(getResources(),R.drawable.sound2);//声音按钮图片
time=BitmapFactory.decodeResource(getResources(),R.drawable.time);//黑色冒号
redtime=BitmapFactory.decodeResource(getResources(),R.drawable.redtime);//红色冒号
sound3=BitmapFactory.decodeResource(getResources(),R.drawable.sound3);
heiZi[0]=BitmapFactory.decodeResource(getResources(),R.drawable.heishuai);//黑帅
heiZi[1]=BitmapFactory.decodeResource(getResources(),R.drawable.heiju);//黑车
heiZi[2]=BitmapFactory.decodeResource(getResources(),R.drawable.heima);//黑马
heiZi[3]=BitmapFactory.decodeResource(getResources(),R.drawable.heipao);//黑炮
heiZi[4]=BitmapFactory.decodeResource(getResources(),R.drawable.heishi);//黑士
heiZi[5]=BitmapFactory.decodeResource(getResources(),R.drawable.heixiang);//黑象
heiZi[6]=BitmapFactory.decodeResource(getResources(),R.drawable.heibing);//黑兵
hongZi[0]=BitmapFactory.decodeResource(getResources(),R.drawable.hongjiang);//红将
hongZi[1]=BitmapFactory.decodeResource(getResources(),R.drawable.hongju);//红车
hongZi[2]=BitmapFactory.decodeResource(getResources(),R.drawable.hongma);//红马
hongZi[3]=BitmapFactory.decodeResource(getResources(),R.drawable.hongpao);//红砲
hongZi[4]=BitmapFactory.decodeResource(getResources(),R.drawable.hongshi);//红仕
hongZi[5]=BitmapFactory.decodeResource(getResources(),R.drawable.hongxiang);//红相
hongZi[6]=BitmapFactory.decodeResource(getResources(),R.drawable.hongzu);//红卒
number[0]=BitmapFactory.decodeResource(getResources(),R.drawable.number0);//黑色数字0
number[1]=BitmapFactory.decodeResource(getResources(),R.drawable.number1);//黑色数字1
number[2]=BitmapFactory.decodeResource(getResources(),R.drawable.number2);//黑色数字2
number[3]=BitmapFactory.decodeResource(getResources(),R.drawable.number3);//黑色数字3
number[4]=BitmapFactory.decodeResource(getResources(),R.drawable.number4);//黑色数字4
number[5]=BitmapFactory.decodeResource(getResources(),R.drawable.number5);//黑色数字5
number[6]=BitmapFactory.decodeResource(getResources(),R.drawable.number6);//黑色数字6
number[7]=BitmapFactory.decodeResource(getResources(),R.drawable.number7);//黑色数字7
number[8]=BitmapFactory.decodeResource(getResources(),R.drawable.number8);//黑色数字8
number[9]=BitmapFactory.decodeResource(getResources(),R.drawable.number9);//黑色数字9
redNumber[0]=BitmapFactory.decodeResource(getResources(),R.drawable.rednumber0);//红色数字0
redNumber[1]=BitmapFactory.decodeResource(getResources(),R.drawable.rednumber1);//红色数字1
redNumber[2]=BitmapFactory.decodeResource(getResources(),R.drawable.rednumber2);//红色数字2
redNumber[3]=BitmapFactory.decodeResource(getResources(),R.drawable.rednumber3);//红色数字3
redNumber[4]=BitmapFactory.decodeResource(getResources(),R.drawable.rednumber4);//红色数字4
redNumber[5]=BitmapFactory.decodeResource(getResources(),R.drawable.rednumber5);//红色数字5
redNumber[6]=BitmapFactory.decodeResource(getResources(),R.drawable.rednumber6);//红色数字6
redNumber[7]=BitmapFactory.decodeResource(getResources(),R.drawable.rednumber7);//红色数字7
redNumber[8]=BitmapFactory.decodeResource(getResources(),R.drawable.rednumber8);//红色数字8
redNumber[9]=BitmapFactory.decodeResource(getResources(),R.drawable.rednumber9);//红色数字9
background=BitmapFactory.decodeResource(getResources(),R.drawable.bacnground);
}
/**
*该方法是自己定义的并非重写的
*该方法是死的,只根据数据绘制屏幕
*/
publicvoidonDraw(Canvascanvas){//自己写的绘制方法
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(background,0,0,null);//清背景
canvas.drawBitmap(qiPan,10,10,null);//绘制棋盘
for(inti=0;i<qizi.length;i++){
for(intj=0;j<qizi[i].length;j++){//绘制棋子
if(qizi[i][j]!=0){
canvas.drawBitmap(qizibackground,9+j*34,10+i*35,null);//绘制棋子的背景
if(qizi[i][j]==1){//为黑帅时
canvas.drawBitmap(heiZi[0],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==2){//为黑车时
canvas.drawBitmap(heiZi[1],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==3){//为黑马时
canvas.drawBitmap(heiZi[2],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==4){//为黑炮时
canvas.drawBitmap(heiZi[3],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==5){//为黑士时
canvas.drawBitmap(heiZi[4],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==6){//为黑象时
canvas.drawBitmap(heiZi[5],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==7){//为黑兵时
canvas.drawBitmap(heiZi[6],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==8){//为红将时
canvas.drawBitmap(hongZi[0],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==9){//为红车时
canvas.drawBitmap(hongZi[1],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==10){//为红马时
canvas.drawBitmap(hongZi[2],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==11){//为红砲时
canvas.drawBitmap(hongZi[3],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==12){//为红仕时
canvas.drawBitmap(hongZi[4],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==13){//为红相时
canvas.drawBitmap(hongZi[5],12+j*34,13+i*35,paint);
}
elseif(qizi[i][j]==14){//为红卒时
canvas.drawBitmap(hongZi[6],12+j*34,13+i*35,paint);
}
}
}
}
canvas.drawBitmap(vs,10,360,paint);//绘制VS背景图
//绘制黑方的时间
canvas.drawBitmap(time,81,411,paint);//绘制冒号
inttemp=this.heiTime/60;//换算时间
StringtimeStr=temp+"";//转换成字符串
if(timeStr.length()<2){//当不足两位时前面填0
timeStr="0"+timeStr;
}
for(inti=0;i<2;i++){//循环绘制时间
inttempScore=timeStr.charAt(i)-'0';
canvas.drawBitmap(number[tempScore],65+i*7,412,paint);
}
//画分钟
temp=this.heiTime%60;
timeStr=temp+"";//转换成字符串
if(timeStr.length()<2){
timeStr="0"+timeStr;//当长度小于2时在前面添加一个0
}
for(inti=0;i<2;i++){//循环
inttempScore=timeStr.charAt(i)-'0';
canvas.drawBitmap(number[tempScore],85+i*7,412,paint);//绘制
}
//开始绘制红方时间
canvas.drawBitmap(this.redtime,262,410,paint);//红方的冒号
inttemp2=this.hongTime/60;//换算时间
StringtimeStr2=temp2+"";//转换成字符串
if(timeStr2.length()<2){//当不足两位时前面填0
timeStr2="0"+timeStr2;
}
for(inti=0;i<2;i++){//循环绘制时间
inttempScore=timeStr2.charAt(i)-'0';
canvas.drawBitmap(redNumber[tempScore],247+i*7,411,paint);//绘制
}
//画分钟
temp2=this.hongTime%60;//求出当前的秒数
timeStr2=temp2+"";//转换成字符串
if(timeStr2.length()<2){//不足两位时前面用0补
timeStr2="0"+timeStr2;
}
for(inti=0;i<2;i++){//循环绘制
inttempScore=timeStr2.charAt(i)-'0';
canvas.drawBitmap(redNumber[tempScore],267+i*7,411,paint);//绘制时间数字
}
if(caiPan==true){//当该玩家走棋时,即红方走棋
canvas.drawBitmap(right,155,420,paint);//绘制向右的指针
}
else{//黑方走棋,即电脑走棋时
canvas.drawBitmap(left,120,420,paint);//绘制向左的指针
}
canvas.drawBitmap(current,138,445,paint);//绘制当前文字
canvas.drawBitmap(sound2,10,440,paint);//绘制声音
if(activity.isSound){//如果正在播放声音
canvas.drawBitmap(sound3,80,452,paint);//绘制
}
canvas.drawBitmap(exit2,250,440,paint);//绘制退出按钮
if(status==1){//当胜利时
canvas.drawBitmap(win,85,150,paint);//绘制胜利图片
canvas.drawBitmap(ok,113,240,paint);
}
if(status==2){//失败后
canvas.drawBitmap(lost,85,150,paint);//绘制失败界面
canvas.drawBitmap(ok,113,236,paint);
}
}
/**
*该方法是游戏主要逻辑接口
*接受玩家输入
*根据点击的位置和当前的游戏状态做出相应的处理
*而当需要切换View时,通过给Activity发送Handler消息来处理
*注意的是只取屏幕被按下的事件
*/
@Override
publicbooleanonTouchEvent(MotionEventevent){//重写的屏幕监听
if(event.getAction()==MotionEvent.ACTION_DOWN){//只取鼠标按下的事件
if(event.getX()>10&&event.getX()<10+sound2.getWidth()
&&event.getY()>440&&event.getY()<440+sound2.getHeight()){//按下了声音按钮
activity.isSound=!activity.isSound;//声音取反
if(activity.isSound){//当需要放声音时
if(activity.gamesound!=null){//gamesound不为空时
if(!activity.gamesound.isPlaying()){//当前没有音乐时
activity.gamesound.start();//播放音乐
}
}
}
else{
if(activity.gamesound!=null){//gamesound不为空时
if(activity.gamesound.isPlaying()){//当前有音乐时
activity.gamesound.pause();//停止音乐
}
}
}
}//end按下了声音按钮
if(event.getX()>250&&event.getX()<250+exit2.getWidth()
&&event.getY()>440&&event.getY()<440+exit2.getHeight()){//按下了退出按钮
activity.myHandler.sendEmptyMessage(1);//发送消息,切换到MenuView
}
if(status==1){//胜利后
if(event.getX()>135&&event.getX()<190
&&event.getY()>249&&event.getY()<269){//点击了确定按钮
activity.myHandler.sendEmptyMessage(1);//发送消息,切换到MenuView
}
}
elseif(status==2){//失败后
if(event.getX()>135&&event.getX()<190
&&event.getY()>245&&event.getY()<265){//点击了确定按钮
activity.myHandler.sendEmptyMessage(1);//发送消息,切换到MenuView
}
}
/**
*游戏过程中的逻辑处理
*当点击棋盘时,先判断当前是否为玩家走棋,
*然后再判断当然玩家是否已经有选中的棋子,如果没有则选中
*如果之前有选中的棋子,再判断点击的位置是空地、对方棋子还是自己的棋子
*是空地判断是否可走
*是对方棋子同样判断是否可以走,能走自然吃子
*是自己的棋子则选中该棋子
*/
elseif(status==0){//游戏中时
if(event.getX()>10&&event.getX()<310
&&event.getY()>10&&event.getY()<360){//点击的位置在棋盘内时
if(caiPan==true){//如果是该玩家走棋
inti=-1,j=-1;
int[]pos=getPos(event);//根据坐标换算成所在的行和列
i=pos[0];
j=pos[1];
if(focus==false){//之前没有选中的棋子
if(qizi[i][j]!=0){//点击的位置有棋子
if(qizi[i][j]>7){//点击的是自己的棋子。即下面的黑色棋子
selectqizi=qizi[i][j];//将该棋子设为选中的棋子
focus=true;//标记当前有选中的棋子
startI=i;
startJ=j;
}
}
}
else{//之前选中过棋子
if(qizi[i][j]!=0){//点击的位置有棋子
if(qizi[i][j]>7){//如果是自己的棋子.
selectqizi=qizi[i][j];//将该棋子设为选中的棋子
startI=i;
startJ=j;
}
else{//如果是对方的棋子
endI=i;
endJ=j;//保存该点
booleancanMove=guiZe.canMove(qizi,startI,startJ,endI,endJ);
if(canMove){//如果可以移动过去
caiPan=false;//不让玩家走了
if(qizi[endI][endJ]==1||qizi[endI][endJ]==8){//如果是“帅”或“将”
this.success();//胜利了
}
else{
if(activity.isSound){
go.start();//播放下棋声音
}
qizi[endI][endJ]=qizi[startI][startJ];//移动棋子
qizi[startI][startJ]=0;//将原来处设空
startI=-1;
startJ=-1;
endI=-1;
endJ=-1;//还原保存点
focus=false;//标记当前没有选中棋子
ChessMovecm=guiZe.searchAGoodMove(qizi);//根据当前局势查询一个最好的走法
if(activity.isSound){
go.start();//播放下棋声音
}
qizi[cm.toX][cm.toY]=qizi[cm.fromX][cm.fromY];//移动棋子
qizi[cm.fromX][cm.fromY]=0;
caiPan=true;//恢复玩家响应
}
}
}
}//end点击的位置有棋子
else{//如果点击的位置没有棋子
endI=i;
endJ=j;
booleancanMove=guiZe.canMove(qizi,startI,startJ,endI,endJ);//查看是否可走
if(canMove){//如果可以移动
caiPan=false;//不让玩家走了
if(activity.isSound){
go.start();//播放下棋声音
}
qizi[endI][endJ]=qizi[startI][startJ];//移动棋子
qizi[startI][startJ]=0;//将原来处置空
startI=-1;
startJ=-1;
endI=-1;
endJ=-1;//还原保存点
focus=false;//标志位设false
ChessMovecm=guiZe.searchAGoodMove(qizi);//得到一步走法
if(qizi[cm.toX][cm.toY]==8){//电脑吃了您的将
status=2;//切换游戏状态为失败
}
if(activity.isSound){//需要播放声音时
go.start();//播放下棋声音
}
qizi[cm.toX][cm.toY]=qizi[cm.fromX][cm.fromY];//移动棋子
qizi[cm.fromX][cm.fromY]=0;
caiPan=true;//恢复玩家响应
}
}
}//end之前选中过棋子
}
}//end点击的位置在棋盘内时
}//end游戏中时
}
returnsuper.onTouchEvent(event);
}
publicint[]getPos(MotionEvente){//将坐标换算成数组的维数
int[]pos=newint[2];
doublex=e.getX();//得到点击位置的x坐标
doubley=e.getY();//得到点击位置的y坐标
if(x>10&&y>10&&x<10+qiPan.getWidth()&&y<10+qiPan.getHeight()){//点击的是棋盘时
pos[0]=Math.round((float)((y-21)/36));//取得所在的行
pos[1]=Math.round((float)((x-21)/35));//取得所在的列
}
else{//点击的位置不是棋盘时
pos[0]=-1;//将位置设为不可用
pos[1]=-1;
}
returnpos;//将坐标数组返回
}
publicvoidsuccess(){//胜利了
status=1;//切换到胜利状态
}
publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,
intheight){
}
publicvoidsurfaceCreated(SurfaceHolderholder){//重写的
this.thread.setFlag(true);
this.thread.start();//启动刷帧线程
timeThread.setFlag(true);
timeThread.start();//启动思考时间的线程
}
publicvoidsurfaceDestroyed(SurfaceHolderholder){//view被释放时调用的
booleanretry=true;
thread.setFlag(false);//停止刷帧线程
timeThread.setFlag(false);//停止思考时间线程
while(retry){
try{
thread.join();
timeThread.join();//等待线程结束
retry=false;//设置循环标志位为false
}
catch(InterruptedExceptione){//不断地循环,直到等待的线程结束
}
}
}
classTutorialThreadextendsThread{//刷帧线程
privateintspan=300;//睡眠的毫秒数
privateSurfaceHoldersurfaceHolder;//SurfaceHolder的引用
privateGameViewgameView;//gameView的引用
privatebooleanflag=false;//循环标志位
publicTutorialThread(SurfaceHoldersurfaceHolder,GameViewgameView){//构造器
this.surfaceHolder=surfaceHolder;//得到SurfaceHolder引用
this.gameView=gameView;//得到GameView的引用
}
publicvoidsetFlag(booleanflag){//设置循环标记
this.flag=flag;
}
publicvoidrun(){//重写的方法
Canvasc;//画布
while(this.flag){//循环绘制
c=null;
try{
c=this.surfaceHolder.lockCanvas(null);
synchronized(this.surfaceHolder){
gameView.onDraw(c);//调用绘制方法
}
}finally{//用finally保证下面代码一定被执行
if(c!=null){
//更新屏幕显示内容
this.surfaceHolder.unlockCanvasAndPost(c);
}
}
try{
Thread.sleep(span);//睡眠span毫秒
}catch(Exceptione){//不会异常信息
e.printStackTrace();//打印异常堆栈信息
}
}
}
}
}
以上就是本文的全部内容,好好玩吧,希望对大家的学习也有帮助。