Jump to content
  • Advertisement
Sign in to follow this  
kevin_mchugh

Java Text Adventure. First Attempt.

This topic is 3406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello All, As I am sure I mentioned earlier I have been working on a text adventure in Java. This is to help me brush up on my programming skills and because I thought it might be fun. At the moment it is simply a rudimentary program which allows you to move through rooms and there is a simple monster which inhabits my dungeon as well. The code is as follows:

import java.io.*;
import java.util.Scanner;
import java.util.Random;

public class DD4 {

    public static void main(String args[]) {

        int location = 1;
        int newLocation = 1;
        int sqLocation = 11;
        boolean exit = false;
        boolean treasure = false;
        boolean dead = false;

        intro();

        location = room1(sqLocation);
        if (location == 0) {
            exit = true;
        }


        while (exit == false) {

            sqLocation = updatesqLocation(sqLocation);
            if (location == sqLocation) {
                dead = true;
                exit = true;
            }
            if (dead == true) {
                System.out.println("Suddenly there is a high pitched scream and a the shadows come alive. Then all is dark. The Spider Queen has eaten you.");
                System.out.println("Game Over.");
                System.exit(1);
            }

            switch (location) {
                case 1:
                    newLocation = room1(sqLocation);
                    break;
                case 2:
                    newLocation = room2(sqLocation);
                    break;
                case 3:
                    newLocation = room3(sqLocation);
                    break;
                case 4:
                    newLocation = room4(sqLocation);
                    break;
                case 5:
                    newLocation = room5(sqLocation);
                    break;
                case 6:
                    newLocation = room6(sqLocation);
                    break;
                case 7:
                    newLocation = room7(sqLocation);
                    break;
                case 8:
                    newLocation = room8(sqLocation);
                    break;
                case 9:
                    newLocation = room9(sqLocation);
                    break;
                case 10:
                    newLocation = room10(sqLocation);
                    break;
                case 11:
                    newLocation = room11(sqLocation);
                    break;
                default: newLocation = location;
            }

            location = newLocation;
            if (location == 0) {
                exit = true;
            }
            if (location == 12) {
                treasure = true;
                location = 8;
            }

        }

        if (dead != true && (treasure == false)) {
            System.out.println("You flee into the night, your tail between your legs and the Crown of Cytorac remains for another brave soul to claim\n");
        }
        if (dead != true && (treasure == true)) {
            System.out.println("Congratulations, you have survived the Temple of the Spider Queen and have claimed the Crown of Cytorac.\n");
            System.out.println("News of your victory here will soon spread throughout the land and beyond.\n");
            System.out.println("Tomorrow brings new adventures, new enemies and new dangers but for now enjoy your victory");
        }

    }

    public static void intro() {
        System.out.println("Welcome to the Temple of the SpiderQueen \n");
        System.out.println("We hope you survive.\n");
        System.out.println("You must navigate your way through her underground layer, retrieve the lost crown of Cytorac and escape.\n");
        System.out.println("That is if the Queen doesn't find you first.....\n");
        System.out.println("Navigate through the dungeon by typing 'north', 'south', 'east' and 'west'. Use 'take crown' to collect the treasure once you find it.\n");

    }

    public static int room1(int sqLocation) {
        int newLocation = 0;
        System.out.println("As the mist settles, you stand before the gaping maw of an ancient cave. The smell of decay creeps out of the darkness.");
        System.out.println("On either side of you are ancient pillars which once served as a warning to the unwary but now lay shattered and worn by the elements.");
        System.out.println("You can go north into the cave or south to freedom.\n");
        if (sqLocation == 0 || (sqLocation == 2)) {
            sqProx();
        }
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("north")) {
            newLocation = 2;
        }
        if ((userInput.equals("south"))) {
            newLocation = 0;
        }
        return newLocation;
    }

    public static int room2(int sqLocation) {
        int newLocation = 0;
        System.out.println("You light your lamp and venture forth into the cave. You walk for some time before coming across a small pile of bones.");
        System.out.println("Upon closer inspection you see that the bones still have some flesh upon them; They are also riddled with teeth marks.");
        if (sqLocation == 1 || (sqLocation == 4)) {
            sqProx();
        }
        System.out.println("You can go north further into the cave or south into the light of dawn\n");
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("north")) {
            newLocation = 4;
        }
        if (userInput.equals("south")) {
            newLocation = 1;
        }
        return newLocation;
    }

    public static int room3(int sqLocation) {
        int newLocation = 0;
        System.out.println("Turning around a sharp bend you arrive at a dead end. The roof has collapsed here and the blockage would take a team of men to clear.\n");
        if (sqLocation == 4) {
            sqProx();
        }
        System.out.println("The only way out is back west.\n");
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("west")) {
            newLocation = 4;
        }
        return newLocation;

    }

    public static int room4(int sqLocation) {
        int newLocation = 0;
        System.out.println("After a couple of minutes what where once little dusty cobwebs begin to change into sticky nets which hang down from the ceiling.");
        System.out.println("The webs are big enough to hold several men and they stick to your skin, burning you flesh as you pull yourself free.");
        System.out.println("You notice that in some places the webs are thick with blood or have been torn as if struck by a bladed weapon.");
        if (sqLocation == 3 || (sqLocation == 2) || (sqLocation == 5)) {
            sqProx();
        }
        System.out.println("There are exits to the south, the east and the west.\n");
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("east")) {
            newLocation = 3;
        }
        if (userInput.equals("west")) {
            newLocation = 5;
        }
        if (userInput.equals("south")) {
            newLocation = 2;
        }
        return newLocation;
    }

    public static int room5(int sqLocation) {
        int newLocation = 0;
        System.out.println("The passage opens out into a massive cavern. The ceiling disappears into darkness. The floor feels soft under your feet.");
        System.out.println("To the east you can hear running water and there is a small passage to the north. The walls of this passage glow faintly in the darkness.");
        if (sqLocation == 4 || (sqLocation == 6) || (sqLocation == 8)) {
            sqProx();
        }
        System.out.println("There are exits to the north, the east and the west.\n");
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("north")) {
            newLocation = 8;
        }
        if (userInput.equals("east")) {
            newLocation = 4;
        }
        if (userInput.equals("west")) {
            newLocation = 6;
        }
        return newLocation;
    }

    public static int room6(int sqLocation) {
        int newLocation = 0;
        System.out.println("You arrive at what appears to be the edge of a small river. The water is little more than thick black sludge.");
        System.out.println("The dark water bubbles and ebbs its way around the corpse of a horse. The undrinkable goop trickling through its eye sockets.");
        if (sqLocation == 5 || (sqLocation == 7)) {
            sqProx();
        }
        System.out.println("There is an exit to the east or a small space in the northern wall you could call through.\n");
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("north")) {
            newLocation = 7;
        }
        if (userInput.equals("east")) {
            newLocation = 5;
        }
        return newLocation;
    }

    public static int room7(int sqLocation) {
        int newLocation = 0;
        System.out.println("You drag and pull your way through the confined space. Insects crawl over your flesh and into your ears!");
        if (sqLocation == 6 || (sqLocation == 8)) {
            sqProx();
        }
        System.out.println("You can crawl north or crawl south.\n");
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("south")) {
            newLocation = 6;
        }
        if (userInput.equals("north")) {
            newLocation = 8;
        }
        return newLocation;
    }

    public static int room8(int sqLocation) {
        int newLocation = 0;
        System.out.println("The smell burns through your lungs and stings your eyes. The large cavern is slick with a luminous green slime ");
        System.out.println("which gives the room the appearance that it is breathing. Hanging from the ceiling are large larvae sacks, full of unborn spiders.");
        System.out.println("The creatures pull and tear from within the fleshy prisons. Slime drips down and splashes upon the floor with a sinister hiss.");
        if (sqLocation == 7 || (sqLocation == 5) || (sqLocation == 9) || (sqLocation == 11)) {
            sqProx();
        }
        System.out.println("If you tread carefully there are exits in all directions.\n");
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("south")) {
            newLocation = 5;
        }
        if (userInput.equals("east")) {
            newLocation = 9;
        }
        if (userInput.equals("west")) {
            newLocation = 7;
        }
        if (userInput.equals("north")) {
            newLocation = 11;
        }
        return newLocation;
    }

    public static int room9(int sqLocation) {
        int newLocation = 0;
        System.out.println("As you walk forward you arrive at the edge of a large pit. Half eaten corpses lay atop a mountain of bones.");
        System.out.println("The smell of rotting flesh and decay almost makes you sick. Rats and insects scurry across the pile of carrion.");
        System.out.println("There is a small ledge that runs around the edge of the pit");
        if (sqLocation == 8 || (sqLocation == 11)) {
            sqProx();
        }
        System.out.println("There are exits to the north and the west.\n");
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("north")) {
            newLocation = 10;
        }
        if (userInput.equals("west")) {
            newLocation = 8;
        }
        return newLocation;
    }

    public static int room10(int sqLocation) {
        int newLocation = 0;
        System.out.println("You have reached a dead end. The ground simply gives way to a gaping black chasm.");
        System.out.println("The light from your lamp struggles against the suffocating darkness.");
        if (sqLocation == 9) {
            sqProx();
        }
        System.out.println("The only exit is to the south.\n");
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("south")) {
            newLocation = 9;
        }
        return newLocation;
    }

    public static int room11(int sqLocation) {
        int newLocation = 0;
        System.out.println("At the end of a winding corridor is was black chasm. Running from the ledge upon which you stand and beyond into the darkness ");
        System.out.println("is a thick web. Thick with blood and covered in the skeletal remains of dozens of fallen warriors you see the a brief glimmer ");
        System.out.println("as the light from your torch shines upon the lost crown of Cytorac.");
        if (sqLocation == 8) {
            sqProx();
        }
        System.out.println("The only exit is to the south. To crawl out and take the crown type 'take crown'.\n");
        Scanner scan = new Scanner(System.in);
        String userInput = scan.nextLine();
        if (userInput.equals("south")) {
            newLocation = 8;
        }
        if (userInput.equals("take crown")) {
            newLocation = 12;
            System.out.println("You crawl out across the fragile web, trying not to look down into the darkness.");
            System.out.println("With the crown in hand you return to the ledge and head south again.");
        }
        return newLocation;
    }

    public static int updatesqLocation(int sqLocation) {

        while (sqLocation <= 1 || (sqLocation >= 11)) {
            int test = 1 + (int) (Math.random() * 100);
            if (test % 2 == 0) {
                sqLocation++;
            } else {
                sqLocation--;
            }

        }
        return sqLocation;
    }

    public static void sqProx() {
        int warning = 1 + (int) (Math.random() * 5);
        switch (warning) {
            case 1:
                System.out.println("You can hear a scratching noise in the next room.");
                break;
            case 2:
                System.out.println("You can hear a faint scream in the darkness.");
                break;
            case 3:
                System.out.println("You can hear something breathing.");
                break;
            case 4:
                System.out.println("Something is crawling towards the room you are in.");
                break;
            case 5:
                System.out.println("Something is watching you in the darkness.");
                break;
        }

    }
}





I know that the program has a couple of bugs straight off the bat, such as the game crumbles when the user inputs an unexpected command and that law of averages states my monster will not move particularly far....but I am quite proud of my first project. I plan upon making improvements in the future (moving towards OO Code for the rooms, a more comprehensive player and actual enemies to fight). I guess I am just worried that it might not be the "best" way to do this sort of thing and that it works mainly through luck and not skill. Anyway, thanks for all the help and any/all feedback is welcome. Later Kev P.S If anyone wants to steal/copy from my design please feel free. IF you have any questions, again please ask. [Edited by - kevin_mchugh on April 18, 2009 12:31:05 PM]

Share this post


Link to post
Share on other sites
Advertisement
Please use [source][/source] tags instead of [code][/code] tags for that much code.

Quote:
I plan upon making improvements in the future (moving towards OO Code for the rooms, a more comprehensive player and actual enemies to fight).

I guess I am just worried that it might not be the "best" way to do this sort of thing and that it works mainly through luck and not skill.


Moving towards improvements is always the right way to do things. ;) I don't really understand what you mean about working through luck rather than skill. Are you talking about the process of writing the program? Programs never work through luck. When they work despite a lack of skill, they either (a) work through way too much effort on the programmer's part, or (b) don't really work (i.e. have bugs, often far beyond what are recognized).

In your case you fall mostly into the (a) category. ;) But that's OK. Although we can't reclaim the time it took you to get this far, we can clean things up in a way that will make it easier to continue working on it. And you will hone your skills at the same time, because you'll see how code is supposed to be organized.

Before we get into OO, let's just try a bit of organization. The most important idea in code cleanup is removal of duplication. Having more than one bit of code that "does the same thing" is bad in that (a) you already had to write it more than once, meaning that you're wasting time on that; (b) if you have to do the same thing again somewhere else in the code, you'll have to write it again; (c) if you discover a bug in that code, you have to fix all the copies; (d) there's a very high chance of missing a copy when you do that, and eventually ending up with a huge mess.

So, first things first. We have code all over the place that asks for input from the user, and filters it down to one of "north", "south", "east" or "west". Let's make a function for that step, and call it from the room functions. Translate the direction into a numeric code (you'll see why later), and use a loop in the function to reject bad input until the user types something acceptable. We'll need to tell the function which directions are acceptable for the current room. Can you think of a good way to do that? (Hint: how is information normally fed into a function?)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!