This site requires JavaScript, please enable it in your browser!
Greenfoot back
blankaex
blankaex wrote ...

2014/4/24

The Travelling Salesman Problem

blankaex blankaex

2014/4/24

#
Hey guys, I'm trying to develop a program that can solve the Travelling Salesman Problem using the Nearest Neighbour Algorithm with Greenfoot. I'm doing it on a smaller scale, using Akhabara (a city in Japan) as the entire workspace, and individual shops in that city as the places to visit. This is my code so far:
import greenfoot.*;

public class Akihabara extends World
{
    /**
     * Constructor for objects of class Akihabara.
     * 
     */
    public Akihabara()
    {    
        super(1000, 1000, 1);
        prepare();
    }

    private void prepare()
    {
        Shop station = new Shop();
        addObject(station, 800, 600);
        Shop tarou = new Shop();
        addObject(tarou, 800, 800);
        Shop donquihote = new Shop();
        addObject(donquihote, 600, 200);
        Shop kotobukiya = new Shop();
        addObject(kotobukiya, 200, 600);
        Shop tsukumo = new Shop();
        addObject(tsukumo, 400, 500);
        Shop akibacultures = new Shop();
        addObject(akibacultures, 200, 400);
        Shop treasurehunter = new Shop();
        addObject(treasurehunter, 200, 200);
        Shop misterdonuts = new Shop();
        addObject(misterdonuts, 400, 300);
        Shop fujisoba = new Shop();
        addObject(fujisoba, 400, 700);
        Shop animate = new Shop();
        addObject(animate, 600, 400);
    }
}
import greenfoot.*;

public class Shop extends Actor
{
    public Shop()
    {
        
    }
    
    public void act() 
    {
       
    }    
}
I just started so it's not that intricate yet, but I was wondering if it was possible to set a different image to each individual instance variable of the Shop class that I generated, so that they are distinguishable when they appear on the world.
danpost danpost

2014/4/24

#
Why not? You could do something like this:
private void prepare()
{
    Shop station = new Shop();
    station.setImage("station.png");
    addObject(station, 800, 600);
    Shop tarou = new Shop();
    tarou.setImage("tarou.png");
    addObject(tarou, 800, 800);
    // etc.
blankaex blankaex

2014/4/24

#
Oh, awesome. Thanks for the quick reply, I'll test it out.
You need to login to post a reply.