您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > java编程例子源码(狐狸与兔子)
--------------------------------------------animalpackageanimal;importjava.util.ArrayList;importfield.Location;publicabstractclassAnimal{privateintageLimit;privateintbreedableAge;privateintage;privatebooleanisAlive=true;publicAnimal(intageLimit,intbreedableAge){this.ageLimit=ageLimit;this.breedableAge=breedableAge;}protectedintgetAge(){returnage;}protecteddoublegetAgePercent(){return(double)age/ageLimit;}publicabstractAnimalbreed();publicvoidgrow(){age++;if(age=ageLimit){die();}}publicvoiddie(){isAlive=false;}publicbooleanisAlive(){returnisAlive;}publicbooleanisBreedable(){returnage=breedableAge;}publicLocationmove(Location[]freeAdj){Locationret=null;if(freeAdj.length0&&Math.random()0.02){ret=freeAdj[(int)(Math.random()*freeAdj.length)];}returnret;}@OverridepublicStringtoString(){return+age+:+(isAlive?live:dead);}publicAnimalfeed(ArrayListAnimalneighbour){returnnull;}protectedvoidlongerLife(intinc){ageLimit+=inc;}}-----------------------------------------------Cellpackagecell;importjava.awt.Graphics;publicinterfaceCell{voiddraw(Graphicsg,intx,inty,intsize);}-----------------------------------------------Locationpackagefield;publicclassLocation{privateintrow;privateintcol;publicLocation(introw,intcol){this.row=row;this.col=col;}publicintgetRow(){returnrow;}publicintgetCol(){returncol;}}-----------------------------------------------Foxpackageanimal;importjava.awt.Color;importjava.awt.Graphics;importjava.util.ArrayList;importcell.Cell;publicclassFoxextendsAnimalimplementsCell{publicFox(){super(20,4);}@Overridepublicvoiddraw(Graphicsg,intx,inty,intsize){intalpha=(int)((1-getAgePercent())*255);g.setColor(newColor(0,0,0,alpha));//(int)((20-getAge())/20.0*255)));g.fillRect(x,y,size,size);}@OverridepublicAnimalbreed(){Animalret=null;if(isBreedable()&&Math.random()0.05){ret=newFox();}returnret;}@OverridepublicStringtoString(){returnFox:+super.toString();}@OverridepublicAnimalfeed(ArrayListAnimalneighbour){Animalret=null;if(Math.random()0.2){ret=neighbour.get((int)(Math.random()*neighbour.size()));longerLife(2);}returnret;}}----------------------------------------------Rabbitpackageanimal;importjava.awt.Color;importjava.awt.Graphics;importcell.Cell;publicclassRabbitextendsAnimalimplementsCell{publicRabbit(){super(10,2);}@Overridepublicvoiddraw(Graphicsg,intx,inty,intsize){intalpha=(int)((1-getAgePercent())*255);g.setColor(newColor(255,0,0,alpha));g.fillRect(x,y,size,size);}@OverridepublicAnimalbreed(){Animalret=null;if(isBreedable()&&Math.random()0.12){ret=newRabbit();}returnret;}@OverridepublicStringtoString(){returnRabbit:+super.toString();}}-----------------------------------------Viewpackagefield;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Graphics;importjavax.swing.JPanel;importcell.Cell;publicclassViewextendsJPanel{privatestaticfinallongserialVersionUID=-2417015700213488315L;privatestaticfinalintGRID_SIZE=16;privateFieldtheField;publicView(Fieldfield){theField=field;}@Overridepublicvoidpaint(Graphicsg){super.paint(g);g.setColor(Color.GRAY);for(introw=0;rowtheField.getHeight();row++){g.drawLine(0,row*GRID_SIZE,theField.getWidth()*GRID_SIZE,row*GRID_SIZE);}for(intcol=0;coltheField.getWidth();col++){g.drawLine(col*GRID_SIZE,0,col*GRID_SIZE,theField.getHeight()*GRID_SIZE);}for(introw=0;rowtheField.getHeight();row++){for(intcol=0;coltheField.getWidth();col++){Cellcell=theField.get(row,col);if(cell!=null){cell.draw(g,col*GRID_SIZE,row*GRID_SIZE,GRID_SIZE);}}}}@OverridepublicDimensiongetPreferredSize(){returnnewDimension(theField.getWidth()*GRID_SIZE+1,theField.getHeight()*GRID_SIZE+1);}}-----------------------------------------------Fieldpackagefield;importjava.util.ArrayList;importcell.Cell;publicclassField{privatestaticfinalLocation[]adjacent={newLocation(-1,-1),newLocation(-1,0),newLocation(-1,1),newLocation(0,-1),newLocation(0,0),newLocation(0,1),newLocation(1,-1),newLocation(1,0),newLocation(1,1)};privateintwidth;privateintheight;privateCell[][]field;publicField(intwidth,intheight){this.width=width;this.height=height;field=newCell[height][width];}publicintgetWidth(){returnwidth;}publicintgetHeight(){returnheight;}publicCellplace(introw,intcol,Cello){Cellret=field[row][col];field[row][col]=o;returnret;}publicCellget(introw,intcol){returnfield[row][col];}publicCell[]getNeighbour(introw,intcol){ArrayListCelllist=newArrayListCell();for(inti=-1;i2;i++){for(intj=-1;j2;j++){intr=row+i;intc=col+j;if(r-1&&rheight&&c-1&&cwidth&&!(r==row&&c==col)){list.add(field[r][c]);}}}returnlist.toArray(newCell[list.size()]);}publicLocation[]getFreeNeighbour(introw,intcol){ArrayListLocationlist=newArrayListLocation();for(Locationloc:adjacent){intr=row+loc.getRow();intc=col+loc.getCol();if(r-1&&rheight&&c-1&&cwidth&&field[r][c]==null){list.add(newLocation(r,c));}}returnlist.toArray(newLocation[list.size()]);}publicbooleanplaceRandomAdj(introw,intcol,Cellcell){booleanret=false;Location[]freeAdj=getFreeNeighbour(row,col);if(freeAdj.length0){intidx=(int)(Math.random()*freeAdj.length);fiel
本文标题:java编程例子源码(狐狸与兔子)
链接地址:https://www.777doc.com/doc-4219142 .html