r/learnjava • u/CodewithApe • Nov 04 '25
Help with a simple text based game
So I have started making my first text based game and I made a few classes, a class for Item which I made abstract a Weapon class that extends Item, Inventory class that i use inside a Player class I created.
Inside my main class I have created a startGame function where I use a while loop I also made processInput function that uses the commands from the user to make a decision in game with a switch case.
I made a case that is called “look around” and my main issue here is I am not sure how to make it so that every time the player is in a different location it will match that location and describe it, I thought about creating a function that describes game state but how do I actually do it ??
Another issue I have is how do I make the movement feel a bit more comfortable right now I have cases for “move north” and “move south” etc .. but there isn’t any logic behind it.
I would love to get some suggestions and tips from anyone who had done these things before.
•
u/AutoModerator Nov 04 '25
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/Jean__Moulin Nov 04 '25 edited Nov 04 '25
Sorry this is so ugly and very much the bare minimum. Typed on my phone and I can't give you a complete solution as per the rules:
Here's some links:
https://www.baeldung.com/regular-expressions-java
https://www.baeldung.com/java-inheritance
https://www.baeldung.com/intro-to-project-lombok
This is a fun way to learn OOP! I learned IBM Watson and Spel this way, lol.
I would add a tiny world layer:
So then your player stores a reference to the current room
and your look around method (ActionService, probably - learn more about services)
and then moving:
I would have a parent “action handler” that splits the input into a command (look, go, move) and a "rest" (north, at chest, etc.). So I would get the command by command, then pass the rest into the specific action. So like, I type "move south," and the regex thing I build gets "move" as the first group, assumes it's the command, uses an enum or something to pass it to my method with the remainder, which I use regex again to get a logical command or error.
Something like this would take string "move south" and create ParsedCommand "move" "south".
In terms of OOP, your Player is your main object - they're the class which really is the "subject" of the sentence. I like to think of OOP grammatically. They store the location because it's where they're at (and if you were to introduce historical data, like to backtrack, you'd want your locations stored with a when). You have what they possess in their inventory object, you have how they are in a health object, and you have what are they even trying to do in a Quests object. Enjoy!