GameWorld.java

import java.util.*; // for Set, Random


/**
*/
public class GameWorld implements Model
{
    protected Set listeners;
    protected Map players;
    protected Random rand;
    int xmax,ymax;


    public GameWorld()
    {
        this(40,20);
    }

    public GameWorld(int xmax,int ymax)
    {
        this.xmax = xmax;
        this.ymax = ymax;

        listeners = new HashSet();
        players = new HashMap();
        rand = new Random();
    }


    /**
    Add a player to the board at a random location.
    */
    public void addPlayer(Player player)
    {
        players.put(player.name,player);
    }


    public void removePlayer(Player player)
    {
        players.remove(player.name);
    }


    public void addChangeListener(ModelChangeListener listener)
    {
        listeners.add(listener);
    }


    public void removeChangeListener(ModelChangeListener listener)
    {
        listeners.remove(listener);
    }


    public void notifyListeners()
    {
        Iterator iter = listeners.iterator();
        while (iter.hasNext()) {
            ModelChangeListener mcl =
                (ModelChangeListener)(iter.next());
            mcl.modelChanged(new ModelChangeEvent(this));
            }
    }


    public String toString()
    {
        return "GameWorld[" +
            "xmax=" + xmax + "," +
            "ymax=" + ymax + "," +
            "]";
    }
}