A Question About Merging Many Methods into One
Hello all, I've bumped into an interesting coding conundrum recently and seek the wisdom of much more experienced programmers.
So I've been working on a text adventure (which was formerly written in C++) and I wanted to put together a method that displays the player's stats, location and inventory. In the spirit of compartmentalization I wrote three simple methods spread across two classes so I could create a separate method invoking all three. Now that I've done that I'm having some trouble putting them all together. I can't invoke the methods of an object until it's been created in the main method, but I can't write the combined method unless I can invoke the others.
I'm not entirely sure what to do and thus I'm looking for advice. Is there a way I can combine these without breaking up the code compartmentalization?
The two classes are shown below for reference. The three methods I want to combine are the methods displayRoom, displayHealth and displayRace.
I would be extremely grateful for any assistance.
Code :
package wounded.chapter.pkg1;
/**
*
* @author Geoffrey
*/
public class room{
//feilds
public int number;
public int wind;
public int hardKnock;
public int blueBlood;
public int ura;
// constructor
// room 1 = clinic
// room 2 = training room
// room 3 = lab
// room 4 = Kitchen
public room(){
number = 1;
wind = 1;
hardKnock = 2;
blueBlood = 2;
ura = 2;
}
public void displayRoom(){
//image for the training room
if(number == 2){
System.out.println("TRAINING ROOM\n"
// this is complicated, but it's the best way to do it as far as I can see.
+ "0000000000000000000000000000000000000000000000\n"
+ "0 () () () () 0\n"
+ "0 KICKING BAGS 0\n"
+ "0 0\n"
+ "0------- ______________________ -------0\n"
+ "0 | | | | 0\n"
+ "0 WEAPON| | | | 0\n"
+ "0 LOCKER| | EXERCISE MAT | | COT 0\n"
+ "0 | | | | 0\n");
if(hardKnock == 2){System.out.println("0 | | (HARD KNOCK) | | 0\n");}
else {System.out.println("0 | | | | 0\n");}
System.out.println("0 | | | -------0\n"
+ "0------- |______________________| |\n"
+ "0 DOOR|\n");
if (ura == 2) {System.out.println("0 (URA) |\n");}
else {System.out.println("0 |\n");}
if (wind == 2) {System.out.println("0 (WIND) 0\n");}
else {System.out.println("0 0\n");}
if (blueBlood == 2) {System.out.println("0 DOOR (BLUEBLOOD) 0\n");}
else {System.out.println("0 DOOR 0\n");}
System.out.println("00000000_____000000000000000000000000000000000\n\n");
}
//image for the Clinic
if(number == 1) {
System.out.println("CLINIC"
+ "00000000_____000000000000000000000000000000000"
+ "0 DOOR U()U | 0"
+ "0 WASH BASIN | 0"
+ "0 |CABINET 0");
if (ura == 1){System.out.println("0 (URA) | 0");}
else {System.out.println("0 | 0");}
System.out.println("0 ----------0"
+ "0 0"
+ "0 0");
if (blueBlood == 1) {System.out.println("0 (BLUEBLOOD) 0");}
else {System.out.println("0 0");}
System.out.println("0 0");
if (hardKnock == 1) {System.out.println("0------- -------- (HARD KNOCK) 0");}
else {System.out.println("0------- -------- 0");}
System.out.println("0 | | | |");
if (wind == 1) {System.out.println("0 YOUR | (WIND) | | DOOR|");}
else {System.out.println("0 YOUR | | | DOOR|");}
System.out.println("0 COT | | COT | |"
+ "0 | | | 0"
+ "0 | | | 0"
+ "0000000000000000000000000000000000000000000000");
}
}
}
Code :
package wounded.chapter.pkg1;
/**
*
* @author Geoffrey
*/
public class player {
//has one health feild, one race feild and a feild for each type of item they pick up.
//I will add items as the code requires starting with the healing potion.
//and half healing potion.
public String name;
public int health;
public int playerLocation;
//race= 1 unicorn
//race= 2 pegasus
//race= 3 earth pony
public int race;
public int healingPotion;
public int halfHealingPotion;
public player(int startingHealth, int noHealingPotion, int noHalfHealingPotion, int startingRace){
name = "startingName";
startingHealth = 1;
health = startingHealth;
race = startingRace;
healingPotion = noHealingPotion;
halfHealingPotion = noHalfHealingPotion;
}
public void displayRace(){
switch (race){
case 1:
System.out.println("UNICORN\n"
+".................~()~().../.....\n"
+"..................(~0000/ /............\n"
+"..............(~0/ _ ............\n"
+".............(~0/ Q __.....\n"
+"............(~0/ _D........\n"
+"...........(~0/ ___/.........\n"
+"..........(~0/ __/...................\n"
+"____________/ /.........................\n"
+" |..........................\n"
+" |...........................\n"
+" |..........................\n"
+" |..........................\n"
+" |.........................\n"
+" .......................\n"
+" .......................\n"
+"________| |_____ .......................\n"
+"........| |...... ......................\n"
+"........| |....... .........................\n\n");
break;
case 2:
System.out.println("PEGASUS\n"
+"..................~()~()......\n"
+"..................(~000000............\n"
+"..............(~0/ _ ............\n"
+".............(~0/ Q __.....\n"
+"............(~0/ _D........\n"
+"...........(~0/ ___/.........\n"
+"..........(~0/ __/...................\n"
+"____________/ /.........................\n"
+"|||||||||>> |..........................\n"
+"|||||||||>> |...........................\n"
+"|||||||||> |..........................\n"
+"||||||||| |..........................\n"
+"||||||||| |.........................\n"
+"||||||||| .......................\n"
+"||||||||| .......................\n"
+"||||||||| |_____ .......................\n"
+"--------| |...... ......................\n"
+"........| |....... .........................\n\n");
break;
case 3:
System.out.println("EARTH PONY\n"
+"..................~()~()......\n"
+"..................(~000000............\n"
+"..............(~0/ _ ............\n"
+".............(~0/ Q __.....\n"
+"............(~0/ _D........\n"
+"...........(~0/ ___/.........\n"
+"..........(~0/ __/...................\n"
+"____________/ /.........................\n"
+" |..........................\n"
+" |...........................\n"
+" |..........................\n"
+" |..........................\n"
+" |.........................\n"
+" .......................\n"
+" .......................\n"
+" | |_____ .......................\n"
+"--------| |...... ......................\n"
+"........| |....... .........................\n\n");
}
}
public void displayHealth(){
System.out.println("HEALTH: " + health + "\n");
}
public void setName(String newName){
name = newName;
}
public void setRace(int raceChoice){
race = raceChoice;
}
public void heal(int healing){
health += healing;
}
public void harm(int damage){
health -= damage;
}
public void getHealingPotion(int pickUp) {
healingPotion += pickUp;
}
public void getHalfHealingPotion(int pickUp){
halfHealingPotion += pickUp;
}
public void dropHealingPotion(int putDown) {
healingPotion -= putDown;
}
public void dropHalfHealingPotion(int putDown){
halfHealingPotion -= putDown;
}
public void move (int newLocation){
playerLocation = newLocation;
}
}
Re: A Question About Merging Many Methods into One
A method could just call the three in the methods in the order you wanted to display them.
Code java:
public void displayAllThree(Player myPlayer) {
myPlayer.displayRace();
myPlayer.displayHealth();
myPlayer.getRoom().displayRoom();
}
...Where getRoom() would return the room the player is currently in, or by using what ever system you have in place to determine the location...
(For convention I have used uppercase Player in place of the class name player)
Edit:
just for the record, this method name is to suggest the idea, rather than actually be used
Re: A Question About Merging Many Methods into One
Quote:
Originally Posted by
jps
A method could just call the three in the methods in the order you wanted to display them.
Code java:
public void displayAllThree(Player myPlayer) {
myPlayer.displayRace();
myPlayer.displayHealth();
myPlayer.getRoom().displayRoom();
}
...Where getRoom() would return the room the player is currently in, or by using what ever system you have in place to determine the location...
(For convention I have used uppercase Player in place of the class name player)
Edit:
just for the record, this method name is to suggest the idea, rather than actually be used
I'm a bit confused by this answer.
I'm new to java, but it appears to me from what you've coded that you're creating a new object every time you run that method. That implies that I would have to rig the constructor somehow to give all of the variables in the player class (and the room class where the "public int number" variable is defined) definitions which had been modified by the user over the course of play so that the player and NPCs could move from room to room. Could you give me an example on how you think something like that could be accomplished?
Re: A Question About Merging Many Methods into One
Quote:
you're creating a new object every time you run that method
Not exactly. The Player object is passed to the method as a parameter. It must be an existing object before calling this method. Besides this was not designed to be a copy-paste-into-code example. Rather just a self-contained example of how to go about combining all three methods. Basically just call the three methods where ever you want all three called. If it is convenient to make a method for doing so, then by all means make one.
Quote:
Could you give me an example on how you think something like that could be accomplished?
I am not sure I know what it is you are trying to accomplish. I just showed an example of what I thought you asked how to do.
If you still need help, please explain more about what you are having problems doing.