Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Threads Issue

  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Threads Issue

    Hello,

    I'm trying to code a simple artificial life simulation, and I'm having some issues giving organisms unique ids. When I run it, they initialize their ids correctly, but when they begin their run() methods, both their ids become -2. Here's the code:

    Organism super-class:

    Code:
    public class Organism extends Nature{
    	public int x;
    	public int y;
    	public static int id;
    	public int visible;
    	public int stomach;
    	public int metabolism;
    	public int speed;
    	public int size;
    	public static int age;
    	public void move(int x, int y) {
    	}
    	public int[] hunt(int dist) {
    	return null;
    	}
    	public void eat() {
    	}
    	public int getX() {
    		return x;
    	}
    	public int getY() {
    		return y;
    	}
    	public int getStomach() {
    		return stomach;
    	}
    	public int getMeta() {
    		return metabolism;
    	}
    	public int getAge() {
    		return age;
    	}
    }
    Species 1 sub-class:

    Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class Species1 extends Organism implements Runnable {
    	public Species1(int myx, int myy, int mystomach, int mymetabolism, int myspeed, int myvisible, int mysize, int myid) {
    		try {
    		Thread.currentThread().sleep((int)(Math.random() * 1000));
    		}
    		catch(InterruptedException e) {
    		}
    		id = myid;
    		age = 0;
    		Timer incage = new Timer(1000, new AgeListener());
    		incage.start();
    		x = myx;
    		y = myy;
    		size = mysize;
    		visible = myvisible;
    		Nature.coords.add(id);
    		Nature.coords.add(new Integer(x));
    		Nature.coords.add(new Integer(y));
    		Nature.coords.add(new Integer(visible));
    		Nature.coords.add(new Integer(size));
    		Nature.coords.add(new Integer(age));
    		speed = myspeed;
    		stomach = mystomach;
    		metabolism = mymetabolism;
    		id = myid;
    		System.out.println(id);
    		System.out.println(id);
    	}
    	public void run() {
    		while(5<10) {
    			move(1,1);
    			try {
    			Thread.currentThread().sleep(1000);
    			}
    			catch(InterruptedException e) {
    			}
    			System.out.println(Nature.coords);
    			System.out.println(id);
    		}
    	}
    	public void move(int xoff, int yoff) {
    		if(xoff != 1000 && yoff != 1000) {
    			Nature.coords.set(Nature.coords.indexOf(new Integer(id)) + 3, 1);
    			if(x > 0) {
    				for(int i = 0; i < xoff; i++) {
    					x++;
    					Nature.coords.set(Nature.coords.indexOf(new Integer(id)) + 1, new Integer(x));
    					try {
    						Thread.currentThread().sleep(100-speed);
    					}
    					catch(InterruptedException ie) {
    					}
    				}
    			}
    			else if(x < 0) {
    				for(int i = 0; i > xoff; i--) {
    					x--;
    					Nature.coords.set(Nature.coords.indexOf(new Integer(id)) + 1, new Integer(x));
    					try {
    						Thread.currentThread().sleep(100-speed);
    					}
    					catch(InterruptedException ie) {
    					}
    				}
    			}
    			if(y > 0) {
    				for(int i = 0; i < yoff; i++) {
    					y++;
    					Nature.coords.set(Nature.coords.indexOf(new Integer(id)) + 2, new Integer(y));
    					try {
    						Thread.currentThread().sleep(100-speed);
    					}
    					catch(InterruptedException ie) {
    					}
    				}
    			}
    			else if(y < 0) {
    				for(int i = 0; i > yoff; i--) {
    					y--;
    					Nature.coords.set(Nature.coords.indexOf(new Integer(id)) + 2, new Integer(y));
    					try {
    						Thread.currentThread().sleep(100-speed);
    					}
    					catch(InterruptedException ie) {
    					}
    				}
    			}
    		}
    		Nature.coords.set(Nature.coords.indexOf(new Integer(id)) + 3, 1);
    
    	}
    	public int[] hunt(int dist) {
    		double shortest = dist;
    		int preyx = 1000;
    		int preyy = 1000;
    		for(int i = 0; i < Nature.orgs; i++) {
    			if(distance(getX(),getY(),Nature.coords.get(6*i + 1),Nature.coords.get(6*i + 2)) < shortest && 6*i != Nature.coords.indexOf(id) && Nature.coords.get(6*i + 3) == 1) {
    				preyx = Nature.coords.get(6*i + 1);
    				preyy = Nature.coords.get(6*i + 2);
    				shortest = distance(getX(),getY(),Nature.coords.get(6*i + 1),Nature.coords.get(6*i + 2));
    			}
    		}
    		System.out.println(preyx);
    		System.out.println(preyy);
    		return new int[]{preyx,preyy};
    	}
    	public void eat() {
    		for(int i = 0; i < Nature.orgs; i++) {
    			if(Nature.coords.get(6*i + 1) == getX() && Nature.coords.get(6*i + 2) == getY() && Nature.coords.get(6*i) != id) {
    				Nature.coords.set(6*i,new Integer(-4514));
    				stomach += Nature.coords.get(6*i + 4);
    				if(stomach > 100) {
    					stomach = 100;
    				}
    				Nature.coords.set(Nature.coords.indexOf(id) + 4,stomach);
    				return;
    			}
    		}
    	}
    	public static void die() {
    		Nature.coords.remove(Nature.coords.indexOf(new Integer(id)));	
    		Nature.coords.remove(Nature.coords.indexOf(new Integer(id)) + 1);			
    		Nature.coords.remove(Nature.coords.indexOf(new Integer(id)) + 2);			
    		Nature.coords.remove(Nature.coords.indexOf(new Integer(id)) + 3);		
    		Nature.coords.remove(Nature.coords.indexOf(new Integer(id)) + 4);			
    		Nature.coords.remove(Nature.coords.indexOf(new Integer(id)) + 5);	
    	}		
    			
    	public double distance(int x1,int y1,int x2,int y2) {
    		int sqrdist = (int)((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    		return Math.sqrt(sqrdist);
    	}
    	private class AgeListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			Species1.age++;
    		}
    	}
    	private class DieListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			if(Nature.coords.get(Nature.coords.indexOf(Species1.id)) == -4514) {
    				Species1.die();
    			}
    		}
    	}
    }
    Driver Class:

    Code:
    public class Driver {
    	public static void main(String args[]) {
    		Nature world = new Nature();
    		Species1 org1 = new Species1(450,450,50,50,99,0,70,-1);
    		Species1 org2 = new Species1(400,400,50,50,99,0,50,-2);
    		Thread t1 = new Thread(org1);
    		Thread t2 = new Thread(org2);
    		t1.start();
    		t2.start();
    	}
    }
    Nature Class:

    Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class Nature {
    	public static ArrayList<Integer> coords = new ArrayList<Integer>();
    	public static int orgs = 2;
    	public Nature() {
    	}
    }
    Thanks!

  • #2
    New Coder
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    69
    Thanks
    0
    Thanked 13 Times in 13 Posts
    hey mate, first of all please include comments in your code, it would be much easier to understand it as you provide us with no comment in your post it's really hard to get going

    You make a lot of beginner mistakes. It would be best if you follow a good book or tutorial to learn programming in general.

    From what I understand the code it seems you just need to change line 4 of Organism.java.
    id there is static so it is not unique in each instance, instead it is overwritten by the latest instance.
    Of course when you change it you will happen to have badly written methods such as "public static void die()"
    which in real, properly working application should take id as an argument since it is static !!
    if it would be instance method it could do what you just did


    I just checked and it seems to work if you do what I just wrote, however logic of your program is overall very bad and need fix from the very foundation of your little project.
    (to make it work:
    - change id to instance variable
    - remove/change die method
    - in action listener remove/change call to die method )

    Have fun

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks a lot...I made id static so it would work with the die() method, and never really thought about it. I'll remember to comment my code in the future.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •