下载

1下载券

加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 Java小游戏——FiveInLine

Java小游戏——FiveInLine

Java小游戏——FiveInLine

atopos_lee
2012-07-30 0人阅读 举报 0 0 暂无简介

简介:本文档为《Java小游戏——FiveInLinedoc》,可适用于IT/计算机领域

FileName:FiveInLinejavaDescription:Ina*grid,Youcanmoveandmakefivesamecolorpiecesinoneline(‘|’)thenyougetscorefollowtheruleFutureImprovements:wecanaddakindofpolychromepiecethatcanmatchsomeexistingcolor(likeabluegreenpieces)wecanmakeourprograminterfacebeautifulthanbefore(maketheDcolorpieceetc)wecancreatearankingforthe‘top’whogetHighscoresimportjavaawt*importjavaxswing*importjavaawtevent*importjavautilArrayListimportjavaxswingborderLineBorderpublicclassFiveInLineextendsJFrame{staticintscores=recordscoresstaticintmultiple=recordhowmanytimesfreelinecontinuouslystaticintpreX=,preY=recordthePanelactivestaticStringnextColor=newStringrecordthenextturnthreepiecescolor(String)staticintchangeColor=newintrecordthenextturnthreepiecescolor(String)staticintsaveColor=newintrecordeverycelldrawColorstaticCellcells=newCellrecordallpanelstatuspublicbooleanGameOver(){checkthegameisoverornotintcounter=booleangameOver=falsefor(inti=i<=i){checkhowmanycellsbefilledfor(intj=j<=j){if(cellsijfilled==true)counter}}if(counter>=)gameOver=trueifnexttimecan'taddnewthreepieceselsegameOver=falsecounter=returngameOver}publicvoidNewPieces(){addnewthreepiecesfor(inti=i<=i){changeColori=(int)(Mathrandom()*)switch(changeColori){case:nextColori="red"continuecase:nextColori="blue"continuecase:nextColori="black"continuecase:nextColori="gray"continuecase:nextColori="orange"continuecase:nextColori="yellow"continuecase:nextColori="green"continue}}}publicvoidReStart(){restartthegamefor(inti=i<i=i){resetthewallofthe'maze'for(intj=j<j){cellsijcanReach=falsesaveColorij=}}for(intj=j<j=j){resetthewallofthe'maze'for(inti=i<i){cellsijcanReach=falsesaveColorij=}}for(inti=i<=i)resetthepanelsfor(intj=j<=j){cellsijfilled=falsecellsijselected=falsecellsijcanReach=truesaveColorij=}for(inti=i<=i){addpiecesforinitialintrandomX=(int)(Mathrandom()*)intrandomY=(int)(Mathrandom()*)while(cellsrandomXrandomYfilled){randomX=(int)(Mathrandom()*)randomY=(int)(Mathrandom()*)}cellsrandomXrandomYfilled=truecellsrandomXrandomYcanReach=falseintrandomColor=(int)(Mathrandom()*)saveColorrandomXrandomY=randomColorcellsrandomXrandomYsetDrawColor(randomColor)repaint()}NewPieces()scores=}publicclassCellextendsJPanel{thecellstorealldataofstatusintdirect=thepiece'smovedirectinthe'maze'booleanfilled=falsethecellbefilledbyOvalornotbooleanselected=falsetheplayselectthecellornotbooleancanReach=trueifthecellbefilled,itcan'treachColordrawColor=ColorblackpublicvoidpaintComponent(Graphicsg){drawtheOvalorLineinthepanelgsetColor(Colorblack)if(selected){gdrawLine(,getHeight(),getWidth(),getHeight())gdrawLine(getWidth(),,getWidth(),getHeight())}if(filled){gsetColor(drawColor)gfillOval(getWidth(),getHeight(),getWidth()*,getHeight()*)}}publicvoidsetDrawColor(intx){setthedrawcolorswitch(x){case:drawColor=Colorredbreakcase:drawColor=Colorbluebreakcase:drawColor=Colorblackbreakcase:drawColor=Colorgraybreakcase:drawColor=Colororangebreakcase:drawColor=Coloryellowbreakcase:drawColor=Colorgreenbreak}}}publicbooleanMazePath(intpreX,intpreY,intx,inty){theproblemof'MazePath'thatwehavelearnedinthecourseofdatastructureArrayList<Cell>passby=newArrayList<Cell>()recordonecellpassedornotinthemazefor(inti=i<=i)resetthedataaboutMazePathfor(intj=j<=j){cellsijdirect=}cellspreXpreYcanReach=trueintmoveX=preXintmoveY=preYbooleancanMove=falseStackpath=newStack()createanewstacktorecordthepathpasseddo{if(cellsmoveXmoveYcanReach!passbycontains(cellsmoveXmoveY)){ifthecellisn'tpassedpassbyadd(cellsmoveXmoveY)pathPush(moveX,moveY)if(moveX==xmoveY==y){ifwehaveapathtothecellwewanttoarrivecanMove=truebreak}else{movenorthmoveY}}else{ifthecellispassedif(!pathisEmpty()){moveX=pathTopXpathtopmoveY=pathTopYpathtoppathPop()while(cellsmoveXmoveYdirect==!pathisEmpty()){ifacell'salldirectarepassedmoveX=pathTopXpathtopmoveY=pathTopYpathtoppassbyadd(cellsmoveXmoveY)pathPop()}if(cellsmoveXmoveYdirect<){checkwhichdirectarenotpassedcellsmoveXmoveYdirectpathPush(moveX,moveY)if(cellsmoveXmoveYdirect==)moveXmoveeastelseif(cellsmoveXmoveYdirect==)moveYmovesouthelseif(cellsmoveXmoveYdirect==)moveXmovewest}}}}while(!pathisEmpty())cellspreXpreYcanReach=falsereturncanMove}publicclassStack{thisclasscanrecordthepathwepassedintTopX=newintintTopY=newintinttop=publicvoidPush(intx,inty){topTopXtop=xTopYtop=y}publicvoidPop(){top}publicbooleanisEmpty(){if(top==)returntrueelsereturnfalse}}publicbooleanCalculateScores(intcurX,intcurY){calculatescorefollowtheruleintpreScores=scoresintonceCompleted=howmanylinescompletedconeintpieces={,,,,}recordhowmanypiecesforeachlineintpartScores={,,,,}recordthescoresforeachlineintcurColor=saveColorcurXcurYbooleanswitchOfmultiple=falsefor(inti=curX,j=curYj>=j){thelineof''if(cellsijfilled(saveColorij==curColor))pieceselsebreak}for(inti=curX,j=curYj<=j){if(cellsijfilled(saveColorij==curColor))pieceselsebreak}for(inti=curX,j=curYi>=i){thelineof'|'if(cellsijfilled(saveColorij==curColor))pieceselsebreak}for(inti=curX,j=curYi<=i){if(cellsijfilled(saveColorij==curColor))pieceselsebreak}for(inti=curX,j=curYi>=j>=i,j){thelineof''if(cellsijfilled(saveColorij==curColor))pieceselsebreak}for(inti=curX,j=curYi<=j<=i,j){if(cellsijfilled(saveColorij==curColor))pieceselsebreak}for(inti=curX,j=curYi>j<=i,j){thelineof''if(cellsijfilled(saveColorij==curColor))pieceselsebreak}for(inti=curX,j=curYi<=j>=i,j){if(cellsijfilled(saveColorij==curColor))pieceselsebreak}while(true){intmaxPieces=intmaxNumber=for(inti=i<=i)findoutthemaxpiecesofthefourlineif(piecesi>maxPieces){maxPieces=piecesimaxNumber=i}if(maxPieces<)ifthemaxpiecesislessthanfivebreakif(maxPieces>=!switchOfmultiple){ifthemaxpiecesismorethanfivemultipleswitchOfmultiple=truecellscurXcurYfilled=falsecellscurXcurYcanReach=truesaveColorcurXcurY=}if(maxNumber==){erasethepiecesofline''andcalculatescoresfor(inti=curX,j=curYj>j){if(cellsijfilled(saveColorij==curColor)){cellsijfilled=falsecellsijcanReach=truesaveColorij=}elsebreak}for(inti=curX,j=curYj<j){if(cellsijfilled(saveColorij==curColor)){cellsijfilled=falsecellsijcanReach=truesaveColorij=}elsebreak}onceCompletedpartScores=(pieces)*onceCompleted*multiple}if(maxNumber==){erasethepiecesofline'|'andcalculatescoresfor(inti=curX,j=curYi>i){if(cellsijfilled(saveColorij==curColor)){cellsijfilled=falsecellsijcanReach=truesaveColorij=}elsebreak}for(inti=curX,j=curYi<i){if(cellsijfilled(saveColorij==curColor)){cellsijfilled=falsecellsijcanReach=truesaveColorij=}elsebreak}onceCompletedpartScores=(pieces)*onceCompleted*multiple}if(maxNumber==){erasethepiecesofline''andcalculatescoresfor(inti=curX,j=curYi>j>i,j){if(cellsijfilled(saveColorij==curColor)){cellsijfilled=falsecellsijcanReach=truesaveColorij=}elsebreak}for(inti=curX,j=curYi<j<i,j){if(cellsijfilled(saveColorij==curColor)){cellsijfilled=falsecellsijcanReach=truesaveColorij=}elsebreak}onceCompletedpartScores=(pieces)*onceCompleted*multiple}if(maxNumber==){erasethepiecesofline''andcalculatescoresfor(inti=curX,j=curYi>j<i,j){if(cellsijfilled(saveColorij==curColor)){cellsijfilled=falsecellsijcanReach=truesaveColorij=}elsebreak}for(inti=curX,j=curYi<j>i,j){if(cellsijfilled(saveColorij==curColor)){cellsijfilled=falsecellsijcanReach=truesaveColorij=}elsebreak}onceCompletedpartScores=(pieces)*onceCompleted*multiple}piecesmaxNumber=}onceCompleted=scores=partScorespartScorespartScorespartScoresif(scores==preScores)returnfalsecheckthescoreschangedornotelsereturntrue}publicFiveInLine(){JPanelp=newJPanel(newFlowLayout())psetBorder(BorderFactorycreateRaisedBevelBorder())JPanelp=newJPanel(newGridLayout(,))psetBorder(newLineBorder(Colorblack,))JPanelp=newJPanel(newGridLayout(,))psetBorder(BorderFactorycreateRaisedBevelBorder())finalJLabelinfo=newJLabel("Score:"""scores)padd(info)NewPieces()finalJLabelinfo=newJLabel("Next:"nextColor","nextColor","nextColor)padd(info)for(inti=i<i=i){initializethewallofmazefor(intj=j<j){cellsij=newCell()}}for(intj=j<j=j){for(inti=i<i){cellsij=newCell()}}for(inti=i<=i){initializethecellfor(intj=j<=j){finalintx=ifinalinty=jcellsij=newCell()cellsijsetBorder(BorderFactorycreateLoweredBevelBorder())padd(cellsij)cellsijaddMouseListener(newMouseAdapter(){whenweclickthecellpublicvoidmouseClicked(MouseEvente){if(cellsxyfilled(preX==preY==)){ifnocellbeselectedcellsxyselected=truepreX=xpreY=yrepaint()}elseif(cellsxyfilledpreX!=){ifonecellhasbeenselectedcellspreXpreYselected=falsecellsxyselected=truepreX=xpreY=yrepaint()}elseif(!cellsxyfilledpreX!=){ifwewanttomoveapiecesif(MazePath(preX,preY,x,y)){checkwecanmoveitornotsaveColorxy=saveColorpreXpreYsaveColorpreXpreY=cellsxysetDrawColor(saveColorxy)cellspreXpreYselected=falsecellspreXpreYfilled=falsecellspreXpreYcanReach=truecellsxyfilled=truecellsxycanReach=falsepreX=preY=if(!CalculateScores(x,y)){checkwegetscoresornotif(!GameOver()){for(inti=i<=i){addthreepiecesintrandomX=(int)(Mathrandom()*)intrandomY=(int)(Mathrandom()*)while(cellsrandomXrandomYfilled){randomX=(int)(Mathrandom()*)randomY=(int)(Mathrandom()*)}cellsrandomXrandomYfilled=truecellsrandomXrandomYcanReach=falsesaveColorrandomXrandomY=changeColoricellsrandomXrandomYsetDrawColor(changeColori)CalculateScores(randomX,randomY)repaint()}multiple=if(!GameOver()){showthenextthreepiecescolorNewPieces()infosetText("Next:"nextColor","nextColor","nextColor)}else{JOptionPaneshowMessageDialog(,"Gameover!n""YourScoresis"scores)infosetText("Gameover")ReStart()}repaint()}else{JOptionPaneshowMessageDialog(,"Gameover!n""YourScoresis"scores)infosetText("Gameover")ReStart()}}repaint()infosetText("Score:"""scores)}}}})}}JButtonreStart=newJButton("RestartGame")reStartaddActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){ReStart()NewPieces()infosetText("Next:"nextColor","nextColor","nextColor)}})padd(reStart)add(p,BorderLayoutNORTH)add(p,BorderLayoutCENTER)add(p,BorderLayoutSOUTH)ReStart()}publicstaticvoidmain(Stringargs){FiveInLineframe=newFiveInLine()framesetTitle("FiveinALine")framesetLocationRelativeTo()framesetDefaultCloseOperation(JFrameEXITONCLOSE)framesetSize(,)framesetVisible(true)}}

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/12

Java小游戏——FiveInLine

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利