基于j2me手机五子棋游戏设计程序代码_第1页
基于j2me手机五子棋游戏设计程序代码_第2页
基于j2me手机五子棋游戏设计程序代码_第3页
基于j2me手机五子棋游戏设计程序代码_第4页
基于j2me手机五子棋游戏设计程序代码_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、A.B.C.a) b) ) dA.B.C.a) b) ) d) 开发环境:Netbeans基于J2ME 1StartGame.javaimportimportimportimportimportimport基于J2ME 1StartGame.javaimportimportimportimportimportimportimportimportpublicclassStartGameextendsListimplementsCommandListenerprivateCommandoknewCommand(选择Command.OKprivateImagepublicStartGame()sup

2、er(五子棋游戏Image this.append(开始游戏this.append(游戏说明publicvoidcommandAction(Commandc,Displayabled)this.append(开始游戏this.append(游戏说明publicvoidcommandAction(Commandc,Displayabled) () elseif游戏说明.equals(this.getString(this.getSelectedIndextry image=catch(IOExceptione)Alertalert = newAlert(五子棋游戏说明image,/ elseif

3、(c =ok)2Chesses.java publicclassChessespublicfinalstaticshortNO_CHESS=publicfinalstaticshortBLACK_CHESS=publicfinalstaticshortNO_CHESS=publicfinalstaticshortBLACK_CHESS=publicfinalstaticshortWHITE_CHESS=-publicfinalstaticintBOARD_SIZE16;/ 定义常量BOARD_SIZEprivateshortchess; / privatebooleangameOver=pri

4、vateshortpublicChesses()chess=new chessType=gameOver=publicvoidreset()chessType=gameOver=/ for(inti=0;iBOARD_SIZE;i+)for(intj= 0;j 15)break;/ elseif(chessxy=chessx+iy)elsefor(inti=1; i5;i+)if(x- i=5)returntrue;/ returnprivatebooleanGame_win_2(intx,inty)intcount=returnprivatebooleanGame_win_2(intx,in

5、ty)intcount=forinti1; i5; i+) / if(y+ i 15)break;/ elseif(chessxy=chessxy+i)elseforinti1; i5; i+) / if(y- i=5)returntrue;/ returnprivatebooleanGame_win_3(intif(count=5)returntrue;/ returnprivatebooleanGame_win_3(intx,inty)intcount=/ for(inti=1; i5;i+)/ 4 if(x- i 15)break;/ elseif(chessxy=chessx -iy+

6、 i)elsefor(inti=1; i 15| y - i=5)returntrue;/ returnprivatebooleanGame_win_4(intx,inty)intcount=for(inti=1; i 15| y +i 15)break;/ elseif(chessxy=chessx+iy+i)elsefor(inti=1; i5;i+)if(x- i0| y - i 0)breakif(x- i0| y - i=5)returnreturn3Chessboard.javaimportimportimportimportimportimportimportimportimpo

7、rtpublicclassChessboardextendsCanvasimplementsCommandListenerprivateCommandexitnewpublicclassChessboardextendsCanvasimplementsCommandListenerprivateCommandexitnewCommand(退出Command.EXITprivateCommandpausenewCommand(暂停Command.ITEM,privateCommandundonewCommand(悔棋, Command.ITEM,privateCommandresetnewCom

8、mand(重玩Command.ITEM,privateCommandresumenewCommand(继续Command.BACK,privatePointhumanPoint,privateintchessIndex=-privatebooleanprivateintx0,y0,h0,maxW,maxH,minV,x,privateImageprivateGraphicsprivateChessespublicChessboard(booleanselectBlackChess)this.selectBlackChess=x=y =maxW=maxH=if(maxWx=y =maxW=max

9、H=if(maxWmaxH)minV= elseminV= h0=(minV -16) /x0=(maxW -h0* 15) /y0=(maxH- h0 *15)/ chess=newhumanPoint=newPoint(Chesses.BOARD_SIZE4)/robotPoint=newPoint(Chesses.BOARD_SIZE4)/chessIndex=-if(selectBlackChess=false) gp= publicvoidclear(Graphicsg)g.fillRect(0,0,this.getWidth(),publicvoiddrawChessboard(G

10、raphicspublicvoidclear(Graphicsg)g.fillRect(0,0,this.getWidth(),publicvoiddrawChessboard(Graphicsg)g.setColor(0,0,for(inti=0;i16;i+)g.drawLine(x0,y0+h0* i,x0+15*h0,y0+h0* g.drawLine(x0+h0* i,y0,x0+h0* i,y0 +h0 *shortch=for(inti=0;iChesses.BOARD_SIZE;i+)for(intj= 0;j Chesses.BOARD_SIZE;j+)if(chij=Che

11、sses.BLACK_CHESS)g.setColor(0,0,g.fillRoundRect(x0+i*h0 - h0/2,y0+j *h0 -h0/h0,h0,h0,elseif(chij=Chesses.WHITE_CHESS)g.setColor(255,255,g.fillRoundRect(x0+i*h0 - h0/2,y0+j *h0 -h0/h0,h0,h0,g.setColor(255,0,g.drawRect(x*h0+x0- h0 /h0,h0,h0,g.setColor(255,0,g.drawRect(x*h0+x0- h0 /2,y* h0+y0 - h0/ 2,h

12、0,protectedvoidpaint(Graphicsg)/ / protectedvoidkeyPressed(intkeyCode)intgameKey=if(gameKey=Canvas.UP)if(y= 15)y=elseif(gameKey=Canvas.LEFT)if(x= 0)x=elseif(gameKeyif(x= 15)x=elseif(gameKey=Canvas.FIRE)booleanisHas=chess.hasChess(x,if(isHas)Imageimage=try image=catch(IOExceptione)/ 创建Alert Alertalne

13、w Alert(提示窗口该处已有棋子,请另选点落子image, elsechess.putChess(x,humanPointchessIndex=newPoint(x,if(chess.isGameOver()Imageimage=try imagehumanPointchessIndex=newPoint(x,if(chess.isGameOver()Imageimage=try image=catch(IOExceptione)Alertalnew Alert(本局结果本次人获胜 elsePointp= / if(chess.isGameOver()Imageimage=try imag

14、e=catch(IOExceptione)Alertalnew Alert(本局结果本次机器获胜catch(IOExceptione)Alertalnew Alert(本局结果本次机器获胜 drawChessboard(gp);/ publicvoidcommandAction(Commandc,Displayabled)/ if(c=exit)elseif(c=pause)Imageimage=try image=catch(IOExceptione)Alertalnew Alert(暂停暂停中,欢迎回来! elseifAlertalnew Alert(暂停暂停中,欢迎回来! elseif(

15、c=resume)elseif(c=undo)if(chessIndex0)AlertalnewAlert(不能悔棋该步不可以悔棋 elseif(c=reset)chessIndex=-x=y =if(selectBlackChess=false)4 x=y =if(selectBlackChess=false)4 publicclassAIpublicstaticPointfindBestLocation(Chesseschess)intpower=new for(short i=0;iChesses.BOARD_SIZE;i+) for(shortj= 0;jChesses.BOARD_S

16、IZE;j+)/ 当已有子时将该点的权值设为-if(chess.getChess()ij!=0)powerij=-/ elsepowerij=(short)calcPower(i,j,chess.getCurrentType(),PointbestLoc=power=return intmaxpower=return intmax=shortx=0,y=for(short i=0;iChesses.BOARD_SIZE;i+) for(shortj= 0;jmax)x=y =max=returnnewPoint(x,privatestaticintcalcPower(shortm,shortn

17、,shortcurrentType,Chesseschess)intp=intpower= 1000,85,60,45,20,15,10,5shortarrLinks=newchess.setChessFeeler(m,n,arrLinks=calcLinkCount(m,n,/ 如果m,n 是中心点, if(m= Chesses.BOARD_SIZE/2& n=Chesses.BOARD_SIZE/2)pif(m= Chesses.BOARD_SIZE/2& n=Chesses.BOARD_SIZE/2)p+=for(short i=0;i4; i+)if(Math.abs(arrLinks

18、i)=5)p+=elseif(arrLinksi= 4)p+= elseif(arrLinksi= 3)p+= elseif(arrLinksi= 2)p+= if(currentType=Chesses.BLACK_CHESS) else arrLinks=calcLinkCount(m,n,/ for(short i=0;i4; i+)if(Math.abs(arrLinksi)=5)p+=elseif(arrLinksi= 4)p+=elseif(arrLinksi= 3)p+=elseifelseif(arrLinksi= 3)p+=elseif(arrLinksi= 2)p+=che

19、ss.setChessFeeler(m,n,(short)arrLinks=returnprivatestaticshortcalcLinkCount(shortm,shortn,shortchessmap)shortcounts=newshortflag= shortnum= / shortx=(short)(m+ inty=n+ for(;x= 0;)if(chessmapxn=chessmapmn)elseif(x= -1)else/ if(chessmapxn!=0)if(flag=2) counts0=(short)-elseif(flagif(flag=2) counts0=(sh

20、ort)-elseif(flag=1 &num =3) / 3 counts0=(short)-elsecounts0=flag=num=x=(short)(n+for(;x= 0;)/ x=(short)(n- for(;x= 0;)if(chessmapmx=chessmapmn)elseifx = -1 / elseifchessmapmx0) / flag+; if(flag=2) counts1=(short)-elseif(flag=1 &num =3) / 3 counts1=(short)-elsecounts1=flag=0;num= xshort)m1);/ forxChesses.BOARD_SIZEflag=0;num= xshort)m1);/ forxChesses.BOARD_SIZE&yChesses.BOARD_SIZE;) / if(chessmapxy=chessmapmn)else/ if(x=Chesses.BOARD_SIZE|y=Ch

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论