How to make a text based adventure game in java

Welcome to the web site Best Blog Hồng, At present will introduce you to the article How to make a text based adventure game in java
, Let’s be taught extra about it with us. The best way to make a textual content based mostly journey sport in java
article beneath

Τhanks for sharing your code!

From taking a look at your present code, it seems to be like it would shortly change into tough to keep up. This sport seems to be like a major candidate to refactor into completely different courses.

Studying: The best way to make a textual content based mostly journey sport in java

You mentioned that you just had been new to programming, so it’s possible you’ll not have a lot (if any) expertise with creating your personal courses.

Lessons could be considered a blueprint for an object. You need to create courses to signify the issues in your sport. Some potential courses that leap out to me are perhaps a Recreation class to signify the sport itself, a Participant class to signify the participant of the sport, a Monster or ‘Enemy’ class to signify an enemy in your sport. You at present have a Skeleton and a Zombie so each of these might be an occasion of Monster as an alternative of a group of values ​​that it’s worthwhile to handle your self!

You would settle with simply these, or you might go even additional and have a Room class, an Merchandise class (a potion might be an merchandise) and so forth.

Your TextBasedAdventure class is doing the whole lot in the intervening time; it would shortly change into tough to handle. Separating it out into completely different courses will go a great distance in the direction of rising readability and likewise maintainability.

Let’s begin with creating the Participant class and see why it will be preferable over your present manner of dealing with the participant (that being)

public static String Title; public static int HP; public static int maxHP; public static int xp; public static int atk; public static int def; public static int lvl; public static int potion;

You at present have only a bunch of various values ​​which can be all free in your program. Let’s transfer these right into a Participant class to encapsulate them.

public class Participant { personal int HP; personal int maxHP; personal int xp; personal int atk; personal int def; personal int lvl; personal int numPotions; public Participant(){ HP = maxHP; lvl = 1; // … another preliminary values ​​you need } /* Getters & Setters */ public int getHp(){ return HP; } public void setHP(int hp){ HP = hp; } // .. another ones you want }

So we now have a Participant class now that holds all of the values ​​which can be associated to the Participant of your sport.

You may as well present some helper strategies in your Participant that would appear to be this.

public boolean isAlive(){ return HP > 0; }

Learn extra: The best way to make tom riddle’s diary

It is much more readable to see one thing like this

if(participant.isAlive()){ // do stuff }

over this

if(HP > 0){ // do stuff }

And the way about one other one

public void heal(){ if(numPotions > 0){ numPotions-; HP = maxHP; } }

now we are able to use;

as an alternative of implementing it instantly in the primary methodology. This additionally has the benefit of permitting us to name this methodology any time it must be used, and likewise altering the implementation in a single place if it wants to vary, as an alternative of discovering and altering the logic all all through your code.

A giant drawback I can see is your strategies combatskel() and combatzombie(). These strategies are virtually similar, the one factor that modifications is the textual content that’s printed out and a few numbers. This might be refactored to take an occasion of a Monster as an argument.

First we have to make a Monster class. these 2 strategies, let’s take out all of the variables {that a} Monster ought to have.

It seems to be like hp, injury, atk, def. I could also be lacking some however let’s go together with this for now.

We would find yourself with one thing like this

public class Monster { personal int hp; personal int injury; personal int atk; personal int def; public Monster(int hp, int injury, int atk, int def) { = hp; bestchoicekitchen.comge = injury; = atk; = def; } public int getHp() { return hp; } public int getDamage() { return injury; } public int getAtk() { return atk; } public int getDef() { return def; } }

Now we now have plenty of cross over between the Participant class and the Monster class, so we might derive them each from a Creature class or one thing related. However let’s maintain issues easy for now and simply have some code duplication.

Learn extra: The best way to make mustard seed oil

Now that we now have a Monster class, we might perhaps make a technique that appears like

public void fight(Monster monster){ // do the struggle logic }

Now this monster might be a zombie, a skeleton, a ghost, orc, elf or nonetheless many new monsters you need to add to your sport (you might put their info in a file and cargo in any variety of them!)

Let’s attempt to begin writing the strategy out, copying the logic out of your present implementation.

public void fight(Monster monster){ bestchoicekitchen.comtln(“You Have Encountered a ” + bestchoicekitchen.comame()); int injury = bestchoicekitchen.comamage(); if (!participant.isAlive()) { gameover(); } if(!monster.isAlive()){ bestchoicekitchen.comtln(“You Defeated A ” + bestchoicekitchen.comame()); bestchoicekitchen.comlUp(); // does not exist – you might implement it your self by inserting the extent up logic there. } // and the remaining… }

That is clearly solely the beginning, I will not utterly refactor your entire code, however I hope this provides a good suggestion of ​​how you need to use objects and strategies to scale back code duplication. Now you might have 1 methodology to deal with fight with a monster as an alternative of 1 x the variety of monsters in your sport!

Quite a lot of your strategies throw an InterruptedException. That is implementation element leaking out of your strategies. There isn’t any purpose the calling code ought to should deal with these exceptions simply since you need to use bestchoicekitchen.comp() in your sport. I’d advocate one thing like this

public void wait(int numSeconds){ strive { bestchoicekitchen.comp(numSeconds * 1000); } catch (InterruptedException e) { // don’t be concerned a lot about this } }

Usually it’s best to by no means do a strive catch with an empty block, however on this state of affairs, it does not actually matter if one thing messes up when sleeping, which is able to most likely by no means occur. Now the remainder of your code does not should take care of the exceptions. You would use it like this


will anticipate 3 seconds.

Do not use public variables. You utilize plenty of public variables in your predominant code, all variables ought to be personal in 99% of circumstances. Some good examples of fine use circumstances for public are bestchoicekitchen.com_VALUE and These are good examples as a result of these are values ​​that may by no means change. If you wish to present entry to variables in different courses, create personal fields after which create public getter strategies.

Your variable names and methodology names ought to use camelCase. You adhere to this accurately in plenty of locations, however variables like Title and StartGame violate this conference.

See the docs on naming conventions right here

Hopefully this assessment was useful for you!

Learn extra: The best way to make an aries man jealous

Related Posts

Leave a Reply

Your email address will not be published.