java编程例子源码(狐狸与兔子).doc_第1页
java编程例子源码(狐狸与兔子).doc_第2页
java编程例子源码(狐狸与兔子).doc_第3页
java编程例子源码(狐狸与兔子).doc_第4页
java编程例子源码(狐狸与兔子).doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

- animalpackage animal;import java.util.ArrayList;import field.Location;public abstract class Animal private int ageLimit;private int breedableAge;private int age;private boolean isAlive = true;public Animal(int ageLimit, int breedableAge) this.ageLimit = ageLimit;this.breedableAge = breedableAge;protected int getAge() return age;protected double getAgePercent() return (double)age/ageLimit;public abstract Animal breed();public void grow() age+;if ( age = ageLimit ) die();public void die() isAlive = false;public boolean isAlive() return isAlive; public boolean isBreedable() return age = breedableAge;public Location move(Location freeAdj) Location ret = null;if ( freeAdj.length0 & Math.random() 0.02 ) ret = freeAdj(int)(Math.random()*freeAdj.length);return ret;Overridepublic String toString() return +age+:+(isAlive?live:dead);public Animal feed(ArrayList neighbour) return null;protected void longerLife(int inc) ageLimit += inc;- Cellpackage cell;import java.awt.Graphics; public interface Cell void draw(Graphics g, int x, int y, int size);- Locationpackage field;public class Location private int row;private int col;public Location(int row, int col) this.row = row;this.col = col;public int getRow() return row; public int getCol() return col; - Foxpackage animal;import java.awt.Color;import java.awt.Graphics;import java.util.ArrayList;import cell.Cell;public class Fox extends Animal implements Cell public Fox() super(20,4);Overridepublic void draw(Graphics g, int x, int y, int size) int alpha = (int)(1-getAgePercent()*255);g.setColor(new Color(0, 0, 0, alpha);/(int)(20-getAge()/20.0*255);g.fillRect(x, y, size, size);Overridepublic Animal breed() Animal ret = null;if ( isBreedable() & Math.random() 0.05 ) ret = new Fox();return ret;Overridepublic String toString() return Fox:+super.toString();Overridepublic Animal feed(ArrayList neighbour) Animal ret = null;if ( Math.random() 0.2 ) ret = neighbour.get(int)(Math.random()*neighbour.size();longerLife(2);return ret;- Rabbitpackage animal;import java.awt.Color;import java.awt.Graphics;import cell.Cell;public class Rabbit extends Animal implements Cell public Rabbit() super(10,2);Overridepublic void draw(Graphics g, int x, int y, int size) int alpha = (int)(1-getAgePercent()*255);g.setColor(new Color(255, 0, 0, alpha);g.fillRect(x, y, size, size);Overridepublic Animal breed() Animal ret = null;if ( isBreedable() & Math.random() 0.12 ) ret = new Rabbit();return ret;Overridepublic String toString() return Rabbit:+super.toString();- Viewpackage field;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import javax.swing.JPanel;import cell.Cell;public class View extends JPanel private static final long serialVersionUID = -2417015700213488315L;private static final int GRID_SIZE = 16;private Field theField;public View(Field field) theField = field;Overridepublic void paint(Graphics g) super.paint(g);g.setColor(Color.GRAY);for ( int row = 0; rowtheField.getHeight(); row+ ) g.drawLine(0, row*GRID_SIZE, theField.getWidth()*GRID_SIZE, row*GRID_SIZE);for ( int col = 0; coltheField.getWidth(); col+ ) g.drawLine(col*GRID_SIZE, 0, col*GRID_SIZE, theField.getHeight()*GRID_SIZE);for ( int row = 0; rowtheField.getHeight(); row+ ) for ( int col = 0; coltheField.getWidth(); col+ ) Cell cell = theField.get(row, col);if ( cell != null ) cell.draw(g, col*GRID_SIZE, row*GRID_SIZE, GRID_SIZE);Overridepublic Dimension getPreferredSize() return new Dimension(theField.getWidth()*GRID_SIZE+1, theField.getHeight()*GRID_SIZE+1);- Fieldpackage field;import java.util.ArrayList;import cell.Cell;public class Field private static final Location adjacent = new Location(-1,-1),new Location(-1,0),new Location(-1,1),new Location(0,-1), new Location(0,0), new Location(0,1),new Location(1,-1), new Location(1,0), new Location(1,1);private int width;private int height;private Cell field;public Field(int width, int height) this.width = width;this.height = height;field = new Cellheightwidth;public int getWidth() return width; public int getHeight() return height; public Cell place(int row, int col, Cell o) Cell ret = fieldrowcol;fieldrowcol = o;return ret;public Cell get(int row, int col) return fieldrowcol;public Cell getNeighbour(int row, int col) ArrayList list = new ArrayList();for ( int i=-1; i2; i+ ) for ( int j=-1; j-1 & r-1 & cwidth & !(r= row & c = col) ) list.add(fieldrc);return list.toArray(new Celllist.size();public Location getFreeNeighbour(int row, int col) ArrayList list = new ArrayList();for ( Location loc : adjacent ) int r = row+loc.getRow();int c = col+loc.getCol();if ( r -1 & r-1 & c0 ) int idx = (int)(Math.random()*freeAdj.length);fieldfreeAdjidx.getRow()freeAdjidx.getCol() = cell;ret = true;return ret;public Cell remove(int row, int col) Cell ret = fieldrowcol;fieldrowcol = null;return ret;public void remove(Cell cell) for ( int row = 0; row height; row+ ) for ( int col = 0; col width; col+ ) if ( fieldrowcol = cell ) fieldrowcol = null;break;public void clear() for ( int i=0; iheight; i+ ) for ( int j=0; jwidth; j+ ) fieldij = null;public void move(int row, int col, Location loc) fieldloc.getRow()loc.getCol() = fieldrowcol;remove(row, col);- FoxAndRabbitpackage foxnrabbit;import java.util.ArrayList;import javax.swing.JFrame;import animal.Animal;import animal.Fox;import animal.Rabbit;import cell.Cell;import field.Field;import field.Location;import field.View;public class FoxAndRabbit private Field theField;private View theView;public FoxAndRabbit(int size) theField = new Field(size, size);for ( int row = 0; rowtheField.getHeight(); row+ ) for ( int col = 0; coltheField.getWidth(); col+ ) double probability = Math.random();if ( probability 0.05 ) theField.place(row, col, new Fox(); else if ( probability 0.15 ) theField.place(row, col, new Rabbit(); theView = new View(theField);JFrame frame = new JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setResizable(false);frame.setTitle(Cells);frame.add(theView);frame.pack();frame.setVisible(true);public void start(int steps) for ( int i=0; isteps; i+ ) step();theView.repaint();try Thread.sleep(200); catch (InterruptedException e) e.printStackTrace();public void step() for ( int row = 0; row theField.getHeight(); row+ ) for ( int col = 0; col theField.getWidth(); col+ ) Cell cell = theField.get(row, col);if ( cell != null ) Animal ani

温馨提示

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

最新文档

评论

0/150

提交评论