Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

How to make a text based game in java?

Asked by Lachlan Cameron Oct 10, 2022 1.9K views 2 answers
Share

About this question

 I'm trying to make a text-based game and I think I've got a good start. However, this code seems a little clunky and I'm pretty sure there is a better way of doing most things in the code. I am very new to coding.

package text.based.adventure;

import java.util.*;

public class TextBasedAdventure {


    public static String Name;

    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;

    static Random rand = new Random();

    static Scanner UI = new Scanner(System.in);

    public static int dam = rand.nextInt(6) + 1;

    public static int heal = rand.nextInt(6) + 1;


    public static void room2() {
    }
    public static void StartRoom() throws InterruptedException {
        String input;
        System.out.println("The room is dark and gloomy, it reeks of dead corpses and rotten food,");
        System.out
                .println("You look behind you, the skeleton you recently killed and the damaged map are on the floor");
        System.out.println("the only choice no is to move forward");
        System.out.println("avail commands: forward, heal");
        input = UI.nextLine();
        if (input.equals("heal")) {
            potion = potion - 1;
            HP = maxHP;
            System.out.print("You are now at max HP, HP=" + HP);
            Thread.sleep(2000);
            StartRoom();
        } else if (input.equals("forward")) {
            room2();
        } else {
            System.out.print("Please Choose A Valid Command");
            Thread.sleep(2000);
            StartRoom();
        }
    }
    public static void combatskel() throws InterruptedException {
        int skeldam = rand.nextInt(3) + 1;
        String input;
        int haskell;
        hpskel = 5;
        if (HP <= 0) {
            gameover();
        }
        if (hpskel <= 0) {
            System.out.println("You Defeated A Skeleton");
            atk = atk + 1;
            def = def + 1;
            maxHP = maxHP + 1;
            System.out.format("You Have Levelled Up! atk is now %d, def is now %d, HP is now %d", atk, def, HP);
            System.out.println("");
            return;
        }
        while (hpskel > 0) {
            input = UI.nextLine();
            if (input.equals("attack")) {
                haskell = hpskel - dam - atk;
                HP = HP - skeldam;
                System.out.format("You Have Been Hit, Your HP is %d", HP);
                System.out.println("");
                combatskel();
            } else if (input.equals("heal")) {
                if (potion <= 0) {
                    System.out.println("You Do Not Have Enough Potions");
                    combatskel();
                } else {
                    HP = HP + heal;
                    System.out.println("You Have Been Healed");
                    System.out.format("You Have Been Hit, Your HP is %d", HP);
                    System.out.println("");
                    HP = HP - skeldam;
                    Thread.sleep(1200);
                    System.out.format("You Have Been Hit, Your HP is %d", HP);
                    System.out.println("");
                    combatskel();
                }
            }
        }
    }
    public static void combatzombie() throws InterruptedException {
        System.out.println("You Have Encountered a zombie");
        int zombdam = rand.nextInt(3) + 2;
        String input;
        int hpzomb;
        hpzomb = 7;
        if (HP <= 0) {
            gameover();
        }
        if (hpzomb <= 0) {
            System.out.println("You Defeated A Zombie");
            atk = atk + 1;
            def = def + 1;
            maxHP = maxHP + 1;
            System.out.format("You Have Levelled Up! atk is now %d, def is now %d, HP is now %d", atk, def, HP);
            System.out.println("");
            return;
        }
        while (hpzomb > 0) {
            input = UI.nextLine();
            if (input.equals("attack")) {
                hpzomb = hpzomb - dam - atk;
                HP = HP - zombdam;
                System.out.format("You Have Been Hit, Your HP is %d", HP);
                System.out.println("");
                combatzombie();
            } else if (input.equals("heal")) {
                if (potion <= 0) {
                    System.out.println("You Do Not Have Enough Potions");
                    combatzombie();
                } else {
                    HP = HP + heal;
                    System.out.println("You Have Been Healed");
                    System.out.format("You Have Been Hit, Your HP is %d", HP);
                    System.out.println("");
                    HP = HP - zombdam;
                    Thread.sleep(1200);
                    System.out.format("You Have Been Hit, Your HP is %d", HP);
                    System.out.println("");
                    combatzombie();
                }
            }
        }
    }
    public static void gameover() {
        System.out.println(Name + " Died!");
        System.out.println("GAME OVER!");
        System.exit(0); // terminates if lost
    }
    /**
     * @param args
     *            the command line arguments
     * @throws java.lang.InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        maxHP = 20;
        potion = 3;
        System.out.println("Welcome To Dungeon Maze");
        System.out.println("Please Input Your Name ");
        Name = UI.nextLine();
        System.out.format("%s is a brave adventurer", Name);
        System.out.println("");
        Thread.sleep(3000);
        System.out.println("They were sent on a task to check on one of");
        Thread.sleep(3000);
        System.out.println("the sacred underground shrines of Port Nyanzaru,");
        Thread.sleep(3000);
        System.out.format("but when one of the skeletons knocked off the map %s had,", Name);
        System.out.println("");
        Thread.sleep(3000);
        System.out.println("they had no choice but to try and escape the dungeon");
        Thread.sleep(3000);
        System.out.println("ARE YOU READY BRAVE ADVENTURER");
        HP = maxHP;
        lvl = 1;
        def = 1;
        atk = 1;
        Thread.sleep(5000);
        StartRoom();
    }
}


Your answer

2 Answers

Carolinahowardd Latest answer

Answered on Jul 25, 2025

暗号通貨でスポーツベットを楽しめるRakebit カジノは、本当に革新的なサイトです。インターフェースは直感的で使いやすく、初めての方でも迷うことなく利用できます。特にビットコインでの入出金がスムーズで、スピード感が最高です。サポートも日本語対応しており、安心してプレイできるのが魅力。ライブベッティングのオプションも豊富で、試合を見ながら賭けるスリルは他にない面白さです。ボーナスも頻繁に更新されていて、ユーザーを飽きさせません。とても信頼できるブックメーカーです!

Was this helpful?

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.