Welcome to Dream.In.Code
Become a Java Expert!

Join 150,389 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,086 people online right now. Registration is fast and FREE... Join Now!




mazeGame SOS!

 
Reply to this topicStart new topic

mazeGame SOS!, Problem Connecting classes and/or Movement in map

th3 mant1s
7 Mar, 2008 - 05:51 PM
Post #1

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 12


My Contributions
Hi Fellow Java Programmers,

I am having some major trouble understanding how to move my "player" within my created map. This project has already been handed in so it will not be re-graded however to complete the next project I will need a good copy. Where I attend college there is a class of 40 kids and only one TA so it is very difficult to get help. I have purchased about 100 dollars worth of java books and got some e-books but nothing seems to be a substitute for a human explanation.

The Project Is a maze game that is supposed to have a map that contains: Rooms, Denizens in rooms, passageway and a Player.

My Game: 5 rooms, one player, 4 denizens, 8 passageways.

I will include the email that I received back from my TA when I asked him questions about my game and why It was not working properly....

----------------------email below--------------------------

Hi Joe,

Without looking at the code, I can give you the following feedback:

> When I do player queries it seems to work fine for instance:
>
> >foo.ralphSnider.name
> "proggy" (proggy = scanner input as a string)
> >foo.ralph.Snider.location
> centralRoom
> >foo.ralphSnider.strength
> 100
> >foo.ralphSnider.tolerance
> 100

These are really field accesses, not methods.

> ... those kinds of queries work fine. Note there is also a toString method in the Room Class which is why it is returning a string for the room. That brings up my first question: Can you and if you can how have two toString methods in a class?

You can't have two toString() methods in the same class unless the
signature is changed, ie different number of method parameters or
different types of parameters. This is called method overloading,
which is discussed in chapter 10, but it's probably not what you are
looking to do. Why not just create another query method? For
instance,

public String roomDescription()

or

public String name()

etc.

> However if I want to query the denizen in my location it will return null. It will also return null if I query the room itself.
>
> example
>
> >foo.ralphSnider.location.denizen
> null

Here denizen is null

> >foo.ralphsnider.location.denizen.name (NOTE: the .name i thought would remedy because i made it a toString in the denizen class- although it didnt)
> NullPointerException:
> at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:36)
> at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:18)
> at java.lang.reflect.Field.get(Field.java:357)
> >foo.centralRoom.denizen
> null
>

Because denizen reference points to null instead of a Denizen object
you get this error. In general, a NullPointerException is generated
in two situations

1. You try to access a field of a null reference, or
2. You try to invoke a method of a null reference.

> that is the first problem i am running into. The second problem that I am encountering is my move commands. When I try and change the players room or use the
>
> //Commands
>
> public void move (Room newRoom){
> newRoom = location;
> }
>
> command it does not work and I dont exactly know why. I have been using the book for reference and I dont seem to understand whats wrong.

The move method defined above will do nothing. This is because Java
is pass by value, even for Reference types. So when you pass the Room
reference to the move method you will really be passing a copy of the
reference. The original reference will still point to the same object
instead of location after the method returns.


>
> example
>
> >foo.ralphSnider.move(eastWing)
> Error: Undefined class 'eastWing'
>

This error is likely because you forgot to declare a variable called
"eastWing". For example,

Room eastWing = new Room("East Wing");
foo.ralphSnider.move(eastWing);

But remember, the east wing will continue to point to the same
location after the move method has returned.

> enclosed is a copy of my code to help explain the problems i am having. Any help would be appreciated!
>

If you have specific comments, we can address them, but it's good for
you to learn how to work through the debug process. Also, try to take
advantage of the discussion folder, a lot of these questions are
useful lessons for other students, as well.

All the best,



----email end----------------------

For clarification that was me discussing my problems while using the Interactions Pane in Dr Java. Yes as a class we are using Dr Java. I did buy a book on using Eclipse for the future.

below I will list all my classes and Main tester's. I am sorry that this post is so long but I really need help! If anyone wants to point out some of my bad programming habbits out to that would help for future technique. Thank you!




CODE


/*
* Author: Joe
*
* Maze Tester Class
*
*/

public class mazeTester{
  
  public static void main(String [] args){
    
    mazeTesterInterface NG = new mazeTesterInterface();
    NG.play();
  }
  
}



CODE


/*
* Author: Joe
*
* Creates a new maze
*/
import java.util.*;
public class mazeTesterInterface{
  /*
   * this will create new players,
   * rooms, Denizens and passage ways for
   * our game.
   */
  
Scanner input = new Scanner(System.in);
  
//Rooms
  public Room centralWing;
  public Room eastWing;
  public Room southWing;
  public Room northWing;
  public Room westWing;
  //Denizens
  public Denizen rice;
  public Denizen cheney;
  public Denizen libby;
  public Denizen bush;
//Passageway
  public Passageway one;
  public Passageway two;
  public Passageway three;
  public Passageway four;
  public Passageway five;
  public Passageway six;
  public Passageway seven;
  public Passageway eight;
  //Player
  public Player ralphSnider;
  
  
  public mazeTesterInterface(){
    
    //rooms
    centralWing = new Room("Central Wing", rice, "This room is located in the middle of the map: holds the fierce Condoleezza Rice",
                                one, three, two, four);
    eastWing = new Room("East Wing", null, "This room is located to the east and holds no denizen", eight, seven, null, two);
    southWing = new Room("South Wing", cheney, "This room is located to the south and holds AHHH!... Dick Cheney", three, null, seven, six);
    northWing = new Room("North Wing", libby, "this room is located to the north and holds SCOOTER LIBBY :(", null, one, eight, five);
    westWing = new Room("West Wing", bush, "this room is located to the west and is home to George W. Bush...Annoy him!", five, six, four,
                             null);
    //denizens
     rice = new Denizen("Condoleezza Rice", centralWing, 25, 100);
     cheney = new Denizen("Dick Cheney", southWing, 75, 75);
     libby = new Denizen("Scooter Libby", northWing, 25,25);
     bush = new Denizen("George W. Bush", westWing, 100, 100);
    //passageway
       one = new Passageway("Passageway one", centralWing, northWing);
       two = new Passageway("Passageway two", centralWing, eastWing);
       three = new Passageway("Passageway three", centralWing, southWing);
       four = new Passageway("Passageway four", centralWing, westWing);
       five = new Passageway("Passageway five", westWing, northWing);
       six = new Passageway("Passageway six", westWing, southWing);
       seven = new Passageway("Passageway seven", eastWing, southWing);
       eight = new Passageway("Passageway eight", eastWing, northWing);
       //player
      ralphSnider = new Player( (input.next()), centralWing, 100, 100);
  
    
  }
    public void play(){
      System.out.println ("welcome to the game, you are: " +ralphSnider.name);
      System.out.println ("you are located in: " + ralphSnider.location.name);
      System.out.println ("you have a strength of: "+ ralphSnider.strength +" and a tolerance of: " +ralphSnider.tolerance);
  
      
      
    
  }
  }

  
                          


CODE


/*
* Author: Joe
*
* Player class - this class will
* hold information about the player
* such as: players name, current room,
* Strength, tolerance
*/
import java.util.*;
public class Player{
  
  /*
   * Will hold instance
   * variables
   */
  
  //instance variables
  
  public String name;
  public Room location;
  public int strength;
  public int tolerance;
  public int minusTolerance;
  public Room newRoom;
  
  /*
   * create a new player
   */
  
  //Constructor
  
  public Player(String name , Room location, int strength, int tolerance){
  
    this.name = name;
    this.location = location;
    this.strength = strength;
    this.tolerance = tolerance;
    }
  
  
    
  
  /*
   * display information about the player
   *
   */
  
  //Queries
  
// public String toString(){
   // return location.name;
  //}
  
  
   public String name(){
    return name;
  }
  
  public Room location(){
    return location;
  }
  
  public int strength(){
    return strength;
  }
  
  public int tolerance(){
    return tolerance;
  }
  
  //Commands
  
  public void move (Room newRoom){
    newRoom = location;
  }
  
  public void takeThat (int hitStrength){
    tolerance = tolerance -hitStrength;
  }
  
  public void poke (Denizen opponent){
    opponent.takeThat(strength);
  }

}


CODE

/*
* Author: Joe
*
* Passageway Class
*
* Contains all the conencted passageways
* for the rooms
*
*/

public class Passageway{

  /*
   * This will hold the instances of Passageway
   */
  
  public String name;
  public Room room1;
  public Room room2;
  
  /*
   * Create a new passageway
   */
  
  //Constructor
  
  
  public Passageway (String name, Room room1, Room room2){
  
    this.name = name;
    this.room1 = room1;
    this.room2 = room2;
    
  }
  
  /*
   * call information about the passageways
   */

  //Queries
  
  public String name(){
    return name;
  }
  
  public Room room1(){
    return room1;
  }
  
  public Room room2(){
    return room2;
  }
  
}
  


CODE


/*
* Author: Joe
*
* Denzien class:
* -This class will contain information about the Denziens in each room!
*/

//Denzien Class instance

public class Denizen{
  
  public String name;
  public int strength;
  public int tolerance;
  public Room location;

// Constructor
  
  /*
   * create a new Denzien
   * with a name, location
   * strength and tolerance
   */
  
  public Denizen (String name, Room location, int strength, int tolerance){
  
    this.name = name;
    this.location = location;
    this.strength = strength;
    this.tolerance = tolerance;
    
  }
  
// Queries
    
  /*
   * display the: Name,
   * location,  strength
   * and tolerance of
   * a Denzien
   */
  
  public String toString(){
    return name;
  }
  
public String name(){
  return name;
}

public Room location(){
  return location;
}

public int strength(){
  return strength;
}

public int tolerance(){
  return tolerance;
}

//Commands

/*
  * Change the value
  * of a Denziens
  * Strength and
  * Tolerance
  */

  public void takeThat(int hitStrength){
    tolerance = tolerance - hitStrength;
  }
  
  public void poke (Denizen opponent){
    opponent.takeThat(strength);
  }
  

}
  


CODE

/*
* Author: Joe
*
* ROOM Class- will hold the:
* name of the room, any denizen
* a description of the room
* and the passageways it's connected to
*/

public class Room{
  
  /*
   * generated variables
   */
  
  //instance variables
  
  public String name;
  public String description;
  public Denizen denizen;
  public Passageway north;
  public Passageway south;
  public Passageway east;
  public Passageway west;
  
  /*
   * create an instance of a room
   */
  
  //constructor
  
  public Room (String name, Denizen denizen, String description, Passageway north, Passageway south,
                 Passageway east, Passageway west){
  
    this.name = name;
    this.denizen = denizen;
    this.description = description;
    this.north = north;
    this.south = south;
    this.east = east;
    this.west = west;
  }
  
  /*
   * display each of the described at the top
   */
  
  //Queries
  
  
  public String toString(){
   return name;
  }
  
  public String name(){
    return name;
  }
  
  public Denizen denizen(){
    return denizen;
  }
  
  public String description(){
    return description;
  }
  
  public Passageway north(){
    return north;
  }
  
  public Passageway south(){
    return south;
  }
  
  public Passageway east(){
    return east;
  }
  
  public Passageway west(){
    return west;
  }
}







Attached File(s)
Attached File  DREAMCODE.zip ( 6.3k ) Number of downloads: 30
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: MazeGame SOS!
8 Mar, 2008 - 02:27 AM
Post #2

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 229



Thanked: 5 times
My Contributions
first off THANK you for adding a zip link

also.. u think dr java is bad our teacher has us using bjay sad.gif i use eclipse at home tongue.gif makes shit so much eaiser


im going out to a concert now bt ill look at ur code asap and see we shall see wt happens

if u could, can u add a quick overview of how the program actualy works. ie game starts map is loaded from such in such class blah blah. it would make all this much easier
User is offlineProfile CardPM
+Quote Post

th3 mant1s
RE: MazeGame SOS!
8 Mar, 2008 - 08:33 AM
Post #3

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 12


My Contributions
QUOTE(gl3thr0 @ 8 Mar, 2008 - 03:27 AM) *

first off THANK you for adding a zip link

also.. u think dr java is bad our teacher has us using bjay sad.gif i use eclipse at home tongue.gif makes shit so much eaiser


im going out to a concert now bt ill look at ur code asap and see we shall see wt happens

if u could, can u add a quick overview of how the program actualy works. ie game starts map is loaded from such in such class blah blah. it would make all this much easier



gl3thr0 THANK YOU for the reply!

so this is how my game works...

First, compile the code. Then by running the mazeTester class it should then point to the mazeTesterInterface class. The mazeTesterInterface class's responsibility is to hold information about the map. The map rooms are trivial with names such as northWing, southWing, centralWing, etc (eastWing does not have a denizen). Within each room is a denizen and connecting each room is a passageway. Keep in mind this is all initialized when a new mazeTesterInterface is created. Basically the mazeTesterInterface class does all of the work (should do all the work) creating the : Player, Denizens, Rooms and Passageways.

Obviously this is not great programming practice but for my purposes as a beginner I am just trying to get a working game.

The player within the map should be able to: Move to other rooms, annoy denizens in the room by poking, and also receive a hit from the denizen.

again thank you for your time!

Mant1s
User is offlineProfile CardPM
+Quote Post

th3 mant1s
RE: MazeGame SOS!
9 Mar, 2008 - 07:39 PM
Post #4

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 12


My Contributions
gl3thr0 (or anyone),

What do you think Is the problem with the Move commands? Drawing a huge blank.

Everything seems to be fine once a new mazeTesterInterface is created however commands are bumming out.

I have included all java files in a ZIP file for your convenience. icon_up.gif

Any help would be appreciated!

Mant1s
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: MazeGame SOS!
10 Mar, 2008 - 04:27 AM
Post #5

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 229



Thanked: 5 times
My Contributions
lol ur going to hate me for this tongue.gif
ur code looked like this (int the Player class)
java

public void move (Room newRoom){
newRoom=location;
}
/// should look like this
public void move (Room newRoom){
location=newRoom;
}



now the move command works


**edit**
i have some sample code from a game called Zork.. u may have heard of it yes no?

its alot like urs, a map of rooms with a player that can move around them
however the one thing tht it has tht might be reallly useful to u is a class called parser that takes userinput and changes it into a command word
ie "go" or "poke"
ill attach the whole thing take a look at how it works i think it will help you


**edit2**
you will need a program that can use .rar
i recommend winRar tongue.gif

This post has been edited by gl3thr0: 10 Mar, 2008 - 04:39 AM
User is offlineProfile CardPM
+Quote Post

th3 mant1s
RE: MazeGame SOS!
10 Mar, 2008 - 11:56 AM
Post #6

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 12


My Contributions
gl3thr0, I do hate you! haha kidding.

I switched that around and compiled it but when i went to do this:

CODE


> foo.ralphSnider.move(eastWing)
Error: Undefined class 'eastWing'
> foo.ralphSnider.location.move(eastWing)
Error: Undefined class 'eastWing'



I got a freaky error. However I don't know why i get an "undefined class error". The second input i just put it in like that to play around and a no go.

Sorry that I am a beginner at this and I really appreciate you time!

I would really be interested in seeing how 'Zork' works. I have never heard of it, well maybe I have I just don't remember, but yes I can receive RAR files.

What compiler are you using?

Thank You again! cool.gif
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: MazeGame SOS!
10 Mar, 2008 - 11:59 AM
Post #7

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 229



Thanked: 5 times
My Contributions
wts this whole foo.ralphSnider thing??

i jus used ralhSnider.move(eastWing)


edit
im using eclipse..
bt it shouldnt matter im jus adding code to the play method.
all i did was add this

ralphSnider.move(eastWing);
System.out.println ("you are located in: " + ralphSnider.location.name);

after
System.out.println ("welcome to the game, you are: " +ralphSnider.name);
System.out.println ("you are located in: " + ralphSnider.location.name);



seriously wtf is foo??? its nt in any of the classes is this some weird dr java shit??
System.out.println ("you have a strength of: "+ ralphSnider.strength +" and a tolerance of: " +ralphSnider.tolerance);

im nt getting any errors ...



if u add me on msn nowish i can help u through this
ylanday2000@hotmail.com

This post has been edited by gl3thr0: 10 Mar, 2008 - 12:14 PM
User is offlineProfile CardPM
+Quote Post

th3 mant1s
RE: MazeGame SOS!
10 Mar, 2008 - 01:32 PM
Post #8

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 12


My Contributions
gl3thr0,

Sorry for the confusion.

When I went to create a new mazeTesterInterface it went like this..

CODE


mazeTesterInterface foo = new mazeTesterInterface()



that was just the new MTI I created. Also foo is just a variable that they use all the time up here in my major. I found it weird at first but it's just kind of stuck crazy.gif

i added you on my msn mine is jbfasulo@yahoo.com

User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: MazeGame SOS!
10 Mar, 2008 - 06:30 PM
Post #9

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 229



Thanked: 5 times
My Contributions

foo.ralphSnider.move(foo.southWing);
System.out.println(foo.ralphSnider.location);

(rooms where created in the foo object tongue.gif
so u have to access them through foo.room tongue.gif
User is offlineProfile CardPM
+Quote Post

th3 mant1s
RE: MazeGame SOS!
10 Mar, 2008 - 07:16 PM
Post #10

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 12


My Contributions
gl3thr0,

I see your online now! MSN?
User is offlineProfile CardPM
+Quote Post

th3 mant1s
RE: MazeGame SOS!
15 Mar, 2008 - 11:12 AM
Post #11

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 12


My Contributions
Hey gl3thr0,

I now got all my commands to work! Thank you


Mant1s
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month