开发者

Termite colony simulator using java

I have to design a simulator that will maintain an environment, which consists of a collection of patches arranged in a rectangular grid of arbitrary size. Each patch contains zero or more wood chips.

A patch may be occupied by one or more termites or predators, which are mobile entities that live within the world and behave according to simple rules.

A TERMITE can pick up a wood chip from the patch that it is currently on, or drop a wood chip that it is carrying. Termites travel around the grid by moving randomly from their current patch to a neighboring patch, in one of four possible directions. New termites may hatch from eggs, and this is simulated by the appearance of a new termite at a random patch within the environment.

A PREDATOR moves in a similar way to termites, and if a predator moves onto a patch that is occupied by a termite, then the predator eats the termite.

At initialization, the termites, predators, and wood chips are distributed randomly in the environment. Simulation then proceeds in a loop, and the new state of the environment is obtained at each iteration.

I have designed the arena using JPanel but im not able to randomly place wood,termite and predator in that arena. can any one help me out?

My code for the arena is as following:

01 import java.awt.*;
02 import javax.swing.*;
03  
04 public class Arena extends JPanel
05 {
06         private static final int Rows = 8;
07         private static final int Cols = 8;
08         public void paint(Graphics g)
09             {
10                 Dimension d = this.getSize();

11                 // don't draw both sets of squares, when you can draw one
12                 
                        // fill in the entire thing with one color
13                 
                        g.setColor(Color.WHITE);
14                 
                        // make the background
15                 
                        g.fillRect(0,0,d.width,d.height);
16                 
                        // draw only black
17                 
                        g.setColor(Color.BLACK);
18                 
                        // pick a square size based on the smallest dimension
19                 
                        int sqsize = ((d.width<d.height) ? d.width/Cols : d.height/Rows);
20                 
                         // loop for rows
21                 
                         for (int row=0; row<Rows; row++)
22                  
                           {
23                         int y = row*sqsize;  // y stays same for entire row, set  here
24                         int x = (row%2)*sqsize; // x starts at 0 or one square in

25                         for (int i=0; i<Cols/2; i++)

26                             {

27                             // you will only be drawing half the squares per row

28                             // draw square

29                             g.fillRect(x,y,sqsize,sqsize);

30                             // move two square sizes over

31                             x += sqsize*2;

32                             }

33                     }

34  

35         }

36  

37  
38  
39         public void update(Graphics g) { paint(g); }
40  
41  
42  
43         public static void main (String[] args)
44         {
45  
46                 JFrame frame = new JFrame("Arena");
47                 frame.se开发者_运维知识库tSize(600,400);
48                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
49                 frame.setContentPane(new Arena());
50                 frame.setVisible(true);
51         }
52  
53 }


A word of advice: forget about the UI and Swing until you have the underlying model running perfectly. If you can make your model work with a text-only driven interface you'll be sure to have good MVC separation. As it is, your difficulties with the model are confounded with your Java/Swing troubles.

Java's an object-oriented language: where are the Termite, Predator, Wood, Egg, Board, etc. classes? Would you not be better off encapsulating the behavior you need inside objects? Wouldn't you like to be able to simulate the behavior of your rule-driven world before worrying about the display issues?

You're going about this all wrong.


Try to avoid using your panel to represent the data, I can recommend using a two dimensional array to represent it, and make a funcion which gets 2 arguments, the Graphics object from your Panel and the 2 dimensional array.

to get a 2 dimensional array do something like int[][] board = new int[rows][cols]


It looks like you already have a grid, and if I understand you correctly, you want to know how to produce the random coordinates. You would probably go about it using something like:

int xcoord =Math.floor(Math.random()*Rows);

int ycoord = Math.floor(Math.random()*Cols);

This will get your random coordinates; you'll have to repeat this until you land on an empty space.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜