Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8178665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:49:09+00:00 2026-06-06T23:49:09+00:00

Problem: When a skeleton enters a place, I want all the Players screens updated

  • 0

Problem: When a skeleton enters a place, I want all the Players screens updated using this method in my World object:

/**
 * Say `text' in the Place `place'. The text will become visible at the
 * bottom of the text window of any Players currently watching `place'.
 * 
 * @param place
 *            The place where the string will be displayed.
 * @param text
 *            The string to be diplayed.
 */
public void sayAtPlace(Place place, String text) {
    synchronized (players) {
        Iterator<Player> ls = players.iterator();
        while (ls.hasNext()) {
            Player p = ls.next();
            if (p.currentPlace() == place) {
                p.say(text);
            }
        }
    }
}

I’ve got two classes, Person and Player and I want a Person to write to the textarea when the method goTo is called but I can’t make the Person object have a proper reference to a Player that has the textarea:

package adventure;

import java.awt.*;
import java.util.*;

/**
 * ADT for persons which is completed which subclasses to creating actors with
 * specific properties
 */
public class Person {
    public Player player = null;

    /**
     * Name of the Person.
     */
    public String name;

    /**
     * The World which this Person is associated with.
     */
    public World world;

    /**
     * Tells where this Person is at the moment.
     */
    public Place where;    


    /**
     * Create Person named `name' in world `world'.
     * 
     * @param world
     *            The world where the Person is created.
     * @param name
     *            Name of the Person.
     * @param app
     *            An image used to display the Person.
     */
    public Person(World world, String name, Image app) {
        this.world = world;
        this.name = name;
        this.appearance = app;
        // this.player = player;
        where = world.defaultPlace();
        where.enter(this);

        inventory = Collections.synchronizedCollection(new LinkedList<Thing>());
    }

    /**
     * Go directly, and quietly, to 'place'. That is to say, without posting a
     * message that you're doing so.
     * 
     * @param place
     *            A string referring to the Place to go to.
     * @see #goTo(Place)
     * @see #go
     */
    public void goTo(String place) {
        goTo(world.getPlace(place), null);
    }

    /**
     * Go directly, and quietly, to `whereTo'. That is to say, without posting a
     * message that you're doing so.
     * 
     * @param whereTo
     *            The Place to go to. Can be null in which case nothing happens.
     * @see #goTo(String)
     * @see #go
     */
    public void goTo(Place whereTo, Player player) {
        if (whereTo != null) {
            where.exit(this);
            whereTo.enter(this);
            // Update any Player's which are viewing place `where'.
            world.update(where);
            // Record our new position.
            where = whereTo;
            // Also update Player's here.
            world.update(where);
        }

        System.out.println("player:"+player);
        if(player != null){
            player.say("A terrifying skeleton warrior appears!");
        }
        // send a msg which doors are available
        Object[] doorNames = whereTo.exits.keySet().toArray();
        String s = "";
        int i = 1;
        for (Object obj : doorNames) {
            if (i < doorNames.length) {
                s = s + obj.toString().toLowerCase();

                if(i<doorNames.length){

                    s = s+ ",";
                }

            } if (i == doorNames.length && i > 1) {
                s = s + " and " + obj.toString().toLowerCase();
            }
            if (i == doorNames.length && i == 1) {
                s = obj.toString().toLowerCase();
            }
            ++i;
        }
        if (player != null) {
            player.say("There are doors " + s);
        }
    }



package adventure;

import java.awt.*;

/**
 * A Player object enables commands to control a certain person: »you». This
 * object is displayed graphically as a control panel where the user both can
 * control and follow the course of events
 */
public class Player extends Panel {
    private Person me;
    private PlaceView placeview;
    private Commands commands;
    private TextArea textarea;
    private static final long serialVersionUID = 100L;

    /**
     * Creates a new instance of Player, in World w, reflecting Person p.
     * 
     * @param w
     *            The world in which the Player will play in.
     * @param p
     *            The Person associated by Player.
     */
    Player(World w, Person p) {
        setLayout(new BorderLayout());
        setSize(650, 540);

        me = p;
        p.player = this;
        placeview = new PlaceView(me);
        commands = new Commands(me);
        textarea = new TextArea("", 10, 60, TextArea.SCROLLBARS_VERTICAL_ONLY);
        textarea.append("You are in a dungeon. The horrible shrieks of the undead chill your bones.\n");
        textarea.setEditable(false);
        add("West", placeview);
        add("East", commands);
        add("South", textarea);
        w.addPlayer(this);
    }


    /**
     * Display a string in the players graphical interface.
     * 
     * @param text
     *            A string to display.
     */
    void say(String text) {
        textarea.append(text + '\n');
        textarea.repaint();
    }

    /**
     * Returns the Place of the Person associated with the Player.
     * 
     * @return The Place of the Person associated with the Player.
     */
    public Place currentPlace() {
        return me.where;
    }
}

Do you understand what I’m trying to do? The code I want to work is

    System.out.println("player:"+player);
    if(player != null && this.name.equals("Skeleton")){
        player.say("A terrifying skeleton warrior appears!");
    }

But the player reference of the Person that is a zombie is not instanciated. The only Person who has a Player object instanciated is the Player that is also a Person.

Can you help me? I also posted the full versions of the Person, Player and World classes here

http://pastebin.com/RJCcr2ph (Person)
http://pastebin.com/eYSh8L9Q (Player)
http://pastebin.com/DKvRvEY8 (World)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:49:10+00:00Added an answer on June 6, 2026 at 11:49 pm

    If I understand your question well, you want to have 2 classes in which each class has a reference to the other. What you need is to create one of them (let it be Foo), create the other class (let it be Boo) and pass a reference of Foo to it via the constructor, and then set a reference of Boo inside Foo class via setter method.

    For example:

    public static void main(String[] args)
    {
        Foo f = new Foo();
        Boo b = new Boo(f);
        f.setBoo(b);
    }
    
    class Foo
    {
        private Boo b;
    
        public Foo()
        {
            // ...
        }
    
        public void setBoo(Boo b)
        {
            this.b = b;
        }
    }
    
    class Boo
    {
        private Foo f;
    
        public Boo(Foo f)
        {
            this.f = f;
        }
    }
    

    Now, Foo has a reference of Boo, and Boo has a reference of Foo 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a homework problem that I am having difficulty with. The skeleton for
Problem: I am trying to build a recursive tree using a function and data
Problem: I'm streaming my video from a php file with stream_get_contents(); using Flowplayer as
Problem Using Director 11.5 and Windows 7, with MouseWheel Xtra (wheelmouse.zip), I have the
This is for uni homework, but I reckon it is more a generic problem
My problem is I want to keep rotating the scene if the checkbox is
I am using zeromq to solve a problem which involves several hundred (potentially thousands)
Hello, I get a NullReferenceException when running this: void newSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) {
I have a strange Problem in Zend Framework 2. I've used the Zend Skeleton
I got a problem where I want to use template including in Django. Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.