Java进阶学习笔记(十)

2019-07-25  本文已影响0人  理以周

1、数据与表现分离--细胞自动机:

package cellmachine;
import java.lang.reflect.Field;

import javax.swing.JFrame;

public class Cellmachine {

    public static void main(String[] args) {
        Field field = new Field(30,30);//30x30的网格
        for(int row=0;row<field.getHeight();row++) {
            for(int col=0;col<field.getWidth();col++) {
                field.place(row,col,new Cell());//准备数据
            }
        }
        for (int row =0;row<field.getHeight();row++) {
            for(int col=0;col<field.getWidth();col++) {
                Cell cell = field.get(row,col);
                if(Math.random()<0.2) {//0-0.1之间的数
                    cell.reborn();
                }
            }
        }
        View view = new View(field);
        JFrame frame = new JFrame;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭操作:x结束操作
        frame.setResizable(false);
        frame.setTitle("cells");
        frame.add(view);
        frame.pack();
        frame.setVisible(true);//
        
        for(int i=0;i<1000;i++) {
            for(int row=0;row<field.getHeight();row++) {
                for(int col=0;col<field.getWidth();col++) {
                    Cell cell = field.get(row,col);
                    Cell[] neighbour = field.getNeighbour(row,col);
                    int numOfLive = 0;
                    for(Cell c:neighbour) {
                        if(c.isAlive()) {
                            numOfLice++;
                        }
                    }
                    System.out.print("["+row+"]["+col+"]:");
                    System.out.print(cell.print(cell.isAlive()?"live":"dead"));
                    System.out.print(":"+numOfLive+"-->");
                    if(cell.isAlive()) {
                        if(numOfLive<2 || numOfLive>3) {
                            cell.die();
                            System.out.print("die");
                        }
                    }else if(numOfLive==3) {
                        cell.reborn();
                        System.out.print("reborn");
                    }
                    System.out.println();
                }
            }
            System.out.println("UPDATE");
            frame.repaint();
            try {
                Thread.sleep(200);
            }catch(InterruptedException e) {
                e.printStackTrace();*
            }
        }
    }
}

1.1 数据与表现分离:

1.2 View和Field的关系:

package field;
import java.awt.Dimension;
import java.lang.reflect.Field;

public class View extends JPanel {//图形库画面
    private static final long serialVersionUID = -52589956212330595L;
    private static final int GRID_SIZE =16;
    private Field theField;
    
    public View(Field field) {
        theField = field;
    }
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        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) {
                    cell.draw(g,col*GRID_SIZE,row*GRID_SIZE,GRID_SIZE);
                }
            }
        }
    }
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(theField.getWidth()*GRID_SIZE+1.theField,getHeight()GRID_SIZE+1);
    }

}

1.3 责任驱动的设计:

1.4 网格化:

1.5 问题0:

package cells;

import java.awt.Graphics;

public class Cell {
    private boolean alive = false;
    
    public void die() {alive = false;}
    public void reborn() {alive = false;}
    public boolean isAlive() {return alive;}
    
    public void draw(Graphics g,int x,int y,int size) {
        if(alive) {
            g.fillRect(x,y,size,size);
        }
    }
}

1.6 问题1:

public Cell[]getNeighbour(int row,int col){
        ArrayList<Cell>list = new ArrayList<Cell>();
        for(int i=-1;i<2.i++) {
            for(int j=1;j<2;j++) {
                int r=row+i;
                int c=col+j;
                if(r>-1&&r<height&& c>-1&&c<width&&!(r==row&&c==col)) {
                    list.add(field[r][c]);
                }
            }
        }
         return list.toArray(new Cell[list.size()]);
    }

1.7 问题2:

package cells;

import java.awt.Graphics;

public class Cell {
    private boolean alive = false;
    
    public void die() {alive = false;}
    public void reborn() {alive = false;}
    public boolean isAlive() {return alive;}
    
    public void draw(Graphics g,int x,int y,int size) {
        if(alive) {
            g.fillRect(x,y,size,size);
        }
    }

}

2、接口--狐狸与兔子:

2.1 狐狸与兔子:

package foxnrabbit;
import java.util.ArrayList;
import javax.swing.JFrame;

public class FoxnrAndabbit {
    private Field theField;
    private View theView;
    
    public FoxnrAndrabbit(int size) {
        for(int row=0;row<theField.getHeight();row++ ) {
            for(int col=0;col<theField.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);
    }
}

2.2 Cell类的地位很尴尬:

2.3 接口(表达概念、规范):

package field;

import java.util.ArrayList;

public class Field {
    private static final Location[] adjacent = {
            new Locaton(-1-1),new Location(-1,0),new Location(-1,-1),
            new Locaton(0,-1),new Location(0,0),new Location(0,1),
            new Locaton(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 Cell[height][width];
    }
    
    public int getWidth() {return width;}
    public int gerHeight() {return height;}
    
    public Cell place(int row,int col,Cell o) {//o表示任何实现了Cell接口的对象
        return field[row][col];
    }

2.4 主程序:

package foxnrabbit;

import java.util.ArrayList;

import javax.swing.JFrame;

import field.Field;

public class FoxnrAndabbit {
    private Field theField;
    private View theView;
    
    public FoxnrAndrabbit(int size) {
        for(int row=0;row<theField.getHeight();row++ ) {
            for(int col=0;col<theField.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;i<steps;i++) {
            step();
            theView.repaint();
            try {
                Thread.sleep(200);
            }catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    publci 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 animal = (Animal)cell;//造型
                    animal.grow();
                    if(animal.isAlive()) {
                        //move
                        Location loc = animal.move(theField.getFreeNeighbour(row,col));
                        if(loc!=null) {
                            theField.move(row,col,loc);
                        }
                        //eat
                        Cell[]neighbour = theField.getNeighbour(row,col);
                        ArrayList(Cell an:neighbour){
                            if(an instanceof Rabbit) {
                                listRabbit.add(Rabbit)an);
                            }
                        }
                        if(!listRabbit.isEmpty()) {
                            Animal fed = animal.feed(listRabbit);
                            if(fed!=null) {
                                theField.remove((Cell)fed);
                            }
                        }
                        //breed
                        Animal baby = animal breed();
                        if(bay!=null) {
                            theField.placeRandomAdj(row, col, (Cell)baby);
                        }
                    }else {
                        theField.remove(row,col);
                    }
                }
            }
        }
    }

}

2.5 接口设计模式:

package cell;

import java.awt.Graphics;

public interface Cell {
    void draw(Graphics g,int x,int y,int size);

}

实现接口:

面向接口的编程方式:

2.6 Cell和Field的关系:

                        animal.eat(theField);
                        Cell[]neighbour = theField.getNeighbour(row,col);
                        ArrayList(Cell an:neighbour){
                            if(an instanceof Rabbit) {
                                listRabbit.add(Rabbit)an);
                            }
                        }

2.7 讨论:

讨论0:

讨论1:

上一篇 下一篇

猜你喜欢

热点阅读