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.
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"); } } }
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; } }