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;
}
}