dimanche 27 novembre 2016

Party simulation and a random generator [duplicate]

This question already has an answer here:

I'm trying to create a simulation for a party. There will be 4 classes: host, engineer, artist and scientist. Each one of those will have to be placed on a 50x50 field. I have to create a random generator in order to do this and I'm really stuck on this part for many hours now since I've never done random generators before so I hope that I can get some help. (I'm a beginner, yes.) I'm currently receiving this error: "Exception in thread "main" java.lang.NullPointerException at Simulator.populate(Simulator.java:88) at Simulator.partyStatus(Simulator.java:101) at Simulator.(Simulator.java:56) at Simulator.main(Simulator.java:41) BUILD SUCCESSFUL (total time: 3 seconds)"

If there is a different, better way of doing it then I'd be more than happy to see it and if you need any more details regarding this then just ask.

Thanks in advance!

public class Simulator {

    private static double ARTIST_CREATION_PROBABILITY = 0.2;
    private static double HOST_CREATION_PROBABILITY = 0.2;
    private static double ENGINEER_CREATION_PROBABILITY = 0.2;
    private static double SCIENTIST_CREATION_PROBABILITY = 0.2;

    private static int seed;

    private ArrayList<Artist> artists;
    private ArrayList<Scientist> scientists;
    private ArrayList<Engineer> engineers;
    private ArrayList<Host> hosts;
    private ArrayList<People> people;

    private int depth;
    private int width;
    private Field field;
//    private static int step;
    private SimulatorView simView;

    Random rand = RandomGenerator.getRandom();


    public static void main(String [] args) {
//     SimulatorView simView = new SimulatorView(50,50); 
     Simulator simulator = new Simulator(50,50);


    }

    /**
     *
     * @param depth
     * @param width
     */
    public Simulator(int depth, int width) {
        this.simView = new SimulatorView(depth, width);
        this.field = new Field(depth, width);
        this.depth = depth;
        this.width = width;
        this.partyStatus(simView, field);

        RandomGenerator.initialiseWithSeed(seed);
    }   

    public void populate(Field field){
//        People host = new Host();
//        People artist = new Artist();
//        People scientist = new Scientist();
//        People engineer = new Engineer(); 
//        field.place(artist, 10, 10);
//        field.place(scientist, 20, 20);
//        field.place(engineer, 30, 30);
//        field.place(host,40,40);

               Random rand = RandomGenerator.getRandom();
        field.clear();

         for(int row = 0; row<field.getDepth(); row++){
            for(int col = 0; col<field.getWidth(); col++){
               if(rand.nextDouble() < ARTIST_CREATION_PROBABILITY){
                 Artist artist = new Artist();
                 artists.add(artist);
                 field.place(artist, row, col);
               }
               else if(rand.nextDouble() < SCIENTIST_CREATION_PROBABILITY){
                 Scientist scientist = new Scientist();
                 scientists.add(scientist);
                 field.place(scientist, row, col);
               }
               else if(rand.nextDouble() < ENGINEER_CREATION_PROBABILITY){
                 Engineer engineer = new Engineer(); 
                 engineers.add(engineer);
                 field.place(engineer, row, col);
               }
               else if(rand.nextDouble() < HOST_CREATION_PROBABILITY){
                 Host host = new Host();
                 hosts.add(host);
                 field.place(host, row, col);
               } 
            }
         }        
    }

    public void partyStatus(SimulatorView simView, Field field){
        this.populate(field);
        simView.setColor(Artist.class, Color.ORANGE);
        simView.setColor(Scientist.class, Color.BLUE);
        simView.setColor(Engineer.class, Color.MAGENTA);
        simView.setColor(Host.class, Color.GREEN);
        simView.showStatus(1, field);
    }  
    }




Aucun commentaire:

Enregistrer un commentaire