Java Sample Project - 1 | Text Adventure Game



In a text adventure game, the central character is the Adventurer, which is an object representing the human player. You need this object because you need to keep track of where the player is, what the player is holding, and anything else you need The adventurer plays the game by typing in commands. Commands consist of one or two words (more than two is too much work).


There are several commands that are found in nearly every game: go direction Moves the Adventurer from one Room to another. The direction is usually one of "north", "south", "east", "west", and sometimes "up" and "down." Occasionally other directions are used, such as "northwest" or "sideways". Directions can usually be abbreviated, such as "n" for north, "u" for up, etc. Many games allow the verb "go" to be omitted, so "n" is short for "go north". look Gives a full description of the Room that the Adventurer is in. look thing Gives a full description of the named Thing. For this command to work, the Adventurer must either be holding the named Thing, or at least be in the same Room with it. Looking at a thing sometimes reveals surprising details about it. take thing Picks up a Thing that is in the same room as the Adventurer. The game might contain Things that cannot be picked up, such as a vending machine, or a cave painting. drop thing Puts down a Thing that the player is holding. Usually this means that the Thing is put into the Room that the Adventurer is in, but there may be exceptions (if the Adventurer is on a tightrope, for example), depending on the game.


Here is a command that is occasionally available:

  • use thing

-- This is a very general command, whose meaning depends on the Thing. Using a gun is very different from using a key, or a chocolate. Most often, though, games require more specific verbs: shoot gun, open safe, eat chocolate. Depending on your game and specific items, this may or may not be needed. Every command should get a response. If there is nothing much to say, the response can be simply OK. If the command can't be done, the player should be told, for instance, “I don't understand” or “You can't go that way.” When the player moves to a new location, the response should usually be a full description of the newly- entered Room. Program Details An adventure game consists of:


(1) A TextAdventure class.

This class:

  • Contains the main method used to start the game.

  • Talks to the AdventureModel and to the Adventurer classes, as needed.


(2) An AdventureModel class.

This class: • Creates the Rooms, the Things, and the Adventurer. • Connects the Rooms with "paths" to other Rooms. • Places Things in the Rooms. • Places the Adventurer in some Room. • Accepts commands from the player, and executes them. o As commands are entered, they should be copied to the main text area. o The method that executes commands should return a String to be displayed in the main text area.


(3) An Adventurer (the human player).

An adventurer has:

  • A location (some room).

  • An inventory (the things being carried). When the player executes the “take thing” command, the item should be added to the inventory. An adventurer can:

  • Move from room to room.

  • Carry a number of objects.

  • Pick up, drop, look at, and use various objects.


(4) Some number of Rooms.

A room has:

  • A name.

  • A description, possibly several sentences long.

  • Contents: the things in the room.

  • Exits: paths to other rooms (usually some of north, south, east, and west).

  • Some number of Things.

A thing has:

  • A name § If some of your Things have multi-word names, such as "firebreathing dragon" or "that thing that your aunt gave you that you don't know what it is", then you might want to give your Things both a full name and a one-word name.

  • A description, to be shown when the adventurer "looks" at it.

  • One or more methods for using the Thing. You can have a multi- purpose use verb, or you can make up your own verbs (for instance, drink water or pour water), which determines what the thing does when the adventurer tries to use it.

  • For simplicity, we will say that the adventurer can only use things being carried (in the inventory)

  • Whether "using" a thing does anything or not (or exactly what it does) can depend on what room it is in, what other things are in the room or in the inventory, or any other conditions you can think of. Your assignment is to write an adventure game with your group.


It should have:

  • A general location (battlefield, outer space, etc.)

  • At least 10 rooms

  • An inventory for the player to store items

  • At least 3 examples of Inheritance

  • At least 2 examples of Overridden Methods

  • At least 1 example of Polymorphism (note it in a code comment so I can see you understand it)

  • Be sure only the minimum methods are public; most of your methods and variables should be private

  • README.txt

  • This is where you will describe the available commands and anything else necessary for the user to play your game. The theme of the game, and the goal of the game, is up to you. So that your adventure game is not too difficult to grade, please:

1. Don't let the player "die" or otherwise get into a situation from which there is no possibility of winning the game.

2. Include a walkthrough (in a file named README.txt) to tell me how to play your game to a successful completion



Script to do this:


AdventureModel.java

import java.util.HashMap;
import java.util.Scanner;

public class AdventureModel {
 private Room rooms[];
 private Thing things[];
 private Adventurer adventurer;
 private Wall walls[];
 private Water waters[];
 
 public AdventureModel() {
    }
 public void initGame(){
 things = new Thing[8];
 walls = new Wall[4];
 waters = new Water[10];
 rooms = new Room[16];
 
 for(int i =0 ;i< 4 ;i++) 
 walls[i] = new Wall();
 
 for(int i =0 ;i< 10 ;i++) 
 waters[i] = new Water();
 
 
 things[0] = new Thing("Gold", "A gold is hidden in this room");
 things[1] = new Thing("Dragon", "A dragon is taking care of room");
 things[2] = new Thing("Banner", "Banner is pasted outside the wall");
 things[3] = new Thing("Food",   "Food for whole people in the room");
 things[4] = new Thing("Army", "An army to protect room");
 things[5] = new Thing("Boat", "A boat to sail over water");
 things[6] = new Thing("Dragon Glass", "An Sword to shoot Enemy");
 
 
 rooms[0] = new Room("Iron Bank","Iron Bank contains 2 Golds and pick up items command");
 rooms[0].addObject(things[0]);
 rooms[0].addObject(things[0]);
 
 
 
 rooms[1] = new Room("Vaas Dothrak","Vaas Dothrak needs items");
 rooms[2] = new Room("Slaver's Bay","Slaver's Bay pick up items");
 rooms[3] = new Room("Pentos","Pentos pick up items and start position");
 
 rooms[4] = new Room("Meereen","Meereen contains pick up items and Army");
 rooms[4].addObject(things[4]);
 
 rooms[5] = new Room("Qohor","Qohor contains pick up items and Dragon");
 rooms[5].addObject(things[1]);
 
 rooms[6] = new Room("Astapor","Astapor contains pick up items, Reqruired command,Gold and Boat");
 rooms[6].addObject(things[0]);
 rooms[6].addObject(things[5]);
 
 rooms[7] = new Room("Dragon Stone","Dragon Stone contains pick up items, Reqruired command,Dragon and Banner");
 rooms[7].addObject(things[1]);
 rooms[7].addObject(things[2]);
 
 rooms[8] = new Room("The Vale","The Vale contains pick up items, Reqruired command,Food and Banner");
 rooms[8].addObject(things[3]);
 rooms[8].addObject(things[2]);
 
 rooms[9] = new Room("Castle Black","Castle Black contains pick up items, Reqruired command,Dragon Glass and 2 Banner");
 rooms[9].addObject(things[6]);
 rooms[9].addObject(things[2]);
 rooms[9].addObject(things[2]);
 
 rooms[10] = new Room("High Garten","High Garten contains pick up items and Food");
 rooms[10].addObject(things[3]);
 
 rooms[11] = new Room("Dorne","Dorne contains pick up items,sticker and Food");
 rooms[11].addObject(things[2]);
 rooms[11].addObject(things[3]);
 
 rooms[12] = new Room("Iron Island","Iron Island contains pick up items,sticker and Food");
 rooms[12].addObject(things[0]);
 rooms[12].addObject(things[2]);
 
 rooms[13] = new Room("WinterFell","WinterFell contains pick up items,sticker and Food");
 rooms[13].addObject(things[2]);
 
 
 rooms[14] = new Room("Casterly Rock","Casterly Rock contains pick up items,Items required,sticker and Food");
 rooms[14].addObject(things[2]);
 rooms[14].addObject(things[3]);
 
 rooms[15] = new Room("King's Landing(End)","King's Landing(End) and items required Dragon,Sticker, and Army");
 rooms[15].addObject(things[1]);
 rooms[15].addObject(things[2]);
 rooms[15].addObject(things[4]);
 
 
 //maping wall direction
 
 walls[0].addDirection("east", walls[1]);
 walls[0].addDirection("south", rooms[0]);
 
 walls[1].addDirection("east", walls[2]);
 walls[1].addDirection("south", rooms[1]);
 walls[1].addDirection("west", walls[0]);
 
 walls[2].addDirection("east", walls[3]);
 walls[2].addDirection("south", waters[2]);
 walls[2].addDirection("west", walls[1]);
 
 walls[3].addDirection("east", waters[4]);
 walls[3].addDirection("south", waters[3]);
 walls[3].addDirection("west", walls[2]);
 
 
 //maping waters direction
 waters[0].addDirection("east", waters[1]);
 waters[0].addDirection("south", waters[4]);
 waters[0].addDirection("west", walls[3]);
 
 waters[1].addDirection("south", rooms[2]);
 waters[1].addDirection("west", waters[0]);
 
 waters[2].addDirection("east", waters[3]);
 waters[2].addDirection("west", rooms[1]);
 waters[2].addDirection("north", walls[2]);
 waters[2].addDirection("south", rooms[5]);
 
 waters[3].addDirection("east", waters[4]);
 waters[3].addDirection("west", waters[2]);
 waters[3].addDirection("north", walls[3]);
 waters[3].addDirection("south", waters[5]);
 
 waters[4].addDirection("east", rooms[2]);
 waters[4].addDirection("west", waters[3]);
 waters[4].addDirection("north", waters[0]);
 waters[4].addDirection("south", rooms[6]);
 
 waters[5].addDirection("east", rooms[6]);
 waters[5].addDirection("west", rooms[5]);
 waters[5].addDirection("north", waters[3]);
 waters[5].addDirection("south", rooms[10]);
 
 waters[6].addDirection("east", rooms[10]);
 waters[6].addDirection("west", rooms[9]);
 waters[6].addDirection("north", rooms[5]);
 waters[6].addDirection("south", waters[7]);
 
 waters[7].addDirection("east", rooms[15]);
 waters[7].addDirection("west", rooms[14]);
 waters[7].addDirection("north", waters[6]);
 
 waters[8].addDirection("east", waters[9]);
 waters[8].addDirection("west", rooms[15]);
 waters[8].addDirection("north", rooms[11]);
 
 waters[9].addDirection("west", waters[8]);
 waters[9].addDirection("north", rooms[12]);

 
 //maping rooms direction
 rooms[0].addDirection("east", rooms[1]);
 rooms[0].addDirection("north",walls[0]);
 rooms[0].addDirection("south",rooms[3]);
 
 
 rooms[1].addDirection("east",waters[2]);
 rooms[1].addDirection("west",rooms[0]);
 rooms[1].addDirection("north",walls[1]);
 rooms[1].addDirection("south",rooms[4]);
 
 rooms[2].addDirection("west",waters[4]);
 rooms[2].addDirection("north",waters[1]);
 rooms[2].addDirection("south",rooms[7]);
 
 rooms[3].addDirection("east",rooms[4]);
 rooms[3].addDirection("north",rooms[0]);
 rooms[3].addDirection("south",rooms[8]);
 
 rooms[4].addDirection("east",rooms[5]);
 rooms[4].addDirection("west",rooms[3]);
 rooms[4].addDirection("north",rooms[1]);
 rooms[4].addDirection("south",rooms[9]);
 
 rooms[5].addDirection("east",waters[5]);
 rooms[5].addDirection("west",rooms[4]);
 rooms[5].addDirection("north",waters[2]);
 rooms[5].addDirection("south",waters[6]);
 
 rooms[6].addDirection("east",rooms[7]);
 rooms[6].addDirection("west",waters[5]);
 rooms[6].addDirection("north",waters[4]);
 rooms[6].addDirection("south",rooms[11]);
 
 rooms[7].addDirection("west",rooms[6]);
 rooms[7].addDirection("north",rooms[2]);
 rooms[7].addDirection("south",rooms[12]);
 
 rooms[8].addDirection("east",rooms[9]);
 rooms[8].addDirection("north",rooms[3]);
 rooms[8].addDirection("south",rooms[13]);
 
 rooms[9].addDirection("east",waters[6]);
 rooms[9].addDirection("west",rooms[8]);
 rooms[9].addDirection("north",rooms[4]);
 rooms[9].addDirection("south",rooms[14]);

 rooms[10].addDirection("east",rooms[11]);
 rooms[10].addDirection("west",waters[5]);
 rooms[10].addDirection("north",waters[6]);
 rooms[10].addDirection("south",rooms[15]);
 
 rooms[11].addDirection("east",rooms[12]);
 rooms[11].addDirection("west",rooms[6]);
 rooms[11].addDirection("north",rooms[10]);
 rooms[11].addDirection("south",waters[8]);
 
 rooms[12].addDirection("west",rooms[11]);
 rooms[12].addDirection("north",rooms[7]);
 rooms[12].addDirection("south",waters[9]);
 
 rooms[13].addDirection("east",rooms[14]);
 rooms[13].addDirection("north",rooms[8]);
 
 rooms[14].addDirection("east",waters[7]);
 rooms[14].addDirection("west",rooms[13]);
 rooms[14].addDirection("north",rooms[9]);
 
 
 rooms[15].addDirection("east",waters[8]);
 rooms[15].addDirection("west",waters[7]);
 rooms[15].addDirection("north",rooms[10]);      
 
 adventurer = new Adventurer(rooms[6],null,0);
    }
 public void startGame(){
 initGame();
 Scanner input = new Scanner(System.in);
 String cmd;
 Location currentLoc ,newLoc= null;
 int takeThing;
 
 boolean b = false;
 do{
 currentLoc = adventurer.getLocation();
 showHelpMenu();
 cmd = input.next();
 if(cmd.equalsIgnoreCase("Go")){
 do {
 System.out.println("Current Location : "+ currentLoc);
 System.out.println("Choose direction:");
 System.out.println("one of north, south, east, west");
 String direc = input.next();
 
 b = checkPathExist(direc,currentLoc);
 if(b) {
 newLoc = adventurerNextLocation(direc,currentLoc);
 adventurer.setLocation((Room) newLoc);
                }
 
 else {
 System.out.println("Here is Water or Wall");
 System.out.println("You can't Move to this direction");
                     }
                }while(b==false);       
            }
 else if(cmd.equalsIgnoreCase("Look")){
 if(currentLoc instanceof Room)
                    ((Room) currentLoc).viewRoomContent();
 adventurer.viewAdventurerInventory();
            }
 else if(cmd.equalsIgnoreCase("Take")){
 if(currentLoc instanceof Room) {
                ((Room) currentLoc).viewRoomContent();
 System.out.println("Take the thing 0/1/2/3 .. from the room : ");
 takeThing = Integer.parseInt(input.next());
 Thing t = ((Room) currentLoc).getRoomItem(takeThing);
 adventurer.take(t);
 adventurer.viewAdventurerInventory();
                }
 
            }
 else if(cmd.equalsIgnoreCase("Drop")){
 adventurer.viewAdventurerInventory();
 System.out.println("Drop the thing 0/1/2/3 .. from the Inventory to room : ");
 takeThing = Integer.parseInt(input.next());
 if(currentLoc instanceof Room) {
 Thing t =adventurer.getInventoryItem(takeThing);
                    ((Room) currentLoc).addObject(t);
                    ((Room) currentLoc).viewRoomContent();
                }
 
            }
 else if(cmd.equalsIgnoreCase("Use")){
 adventurer.viewAdventurerInventory();
 System.out.println("Use the thing 0/1/2/3 .. from the Inventory : ");
 takeThing = Integer.parseInt(input.next());
 Thing t =adventurer.getInventoryItem(takeThing);
 adventurer.drop(t);
            }
 else if(cmd.equalsIgnoreCase("Exit")){
 System.exit(0);
            }
 else{
 System.out.println("Invalid command");
            }
 
        }while(true);
 
    }
 public void showHelpMenu() {
 System.out.println("Choose command: ");
 System.out.println("Go");
 System.out.println("Look");
 System.out.println("Take");
 System.out.println("Drop");
 System.out.println("Use");
 System.out.println("Exit ");
 System.out.println();
    }
 
 public boolean checkPathExist(String path , Location loc) {
 
 HashMap<String ,Location> hm = loc.getRoomDir();
 if(hm.containsKey(path)) {
 if(hm.get(path) instanceof Room)
 return true;
        }   
 return false;
        }
 
 public Location adventurerNextLocation(String path , Location loc) {
 
 HashMap<String ,Location> hm = loc.getRoomDir();
 if(hm.containsKey(path)) {
 if(hm.get(path) instanceof Room)
 return hm.get(path);
        }
 return loc;
 
    }
}


Adventurer.java

import java.util.ArrayList;

public class Adventurer {
 private Room location;
 private Room moveFrom;
 private ArrayList<Thing> inventory;
 private int numberOfObjects;
 
 public Adventurer() {
 location = null;
 moveFrom = null;
 inventory = new ArrayList<Thing>();
 numberOfObjects = 0;
    }
 public Adventurer(Room location, Room moveFrom,int numberOfObjects) {
 super();
 this.location = location;
 this.moveFrom = moveFrom;
 this.inventory = new ArrayList<Thing>();
 this.numberOfObjects = numberOfObjects;
    }
 public void take(Thing item){
 inventory.add(item);
    }
 public void drop(Thing item){
 inventory.remove(item);
    }
 public Room getLocation() {
 return location;
    }
 public void setLocation(Room location) {
 this.location = location;
    }
 public Room getMoveFrom() {
 return moveFrom;
    }
 public void setMoveFrom(Room moveFrom) {
 this.moveFrom = moveFrom;
    }
 public ArrayList<Thing> getInventory() {
 return inventory;
    }
 public void setInventory(ArrayList<Thing> inventory) {
 this.inventory = inventory;
    }
 public int getNumberOfObjects() {
 return numberOfObjects;
    }
 public void setNumberOfObjects(int numberOfObjects) {
 this.numberOfObjects = numberOfObjects;
    }
 
 public void viewAdventurerInventory() {
 System.out.println("Adventurer Inventory");
 System.out.println();
 if(inventory.size()==0)
 System.out.println("No Item in Inventory");
 else {
 for(int i = 0 ; i < inventory.size(); i++)
 System.out.println(i + "  " + inventory.get(i));
        }
 System.out.println();
    }
 
 public Thing getInventoryItem(int takeThing) {
 Thing t = inventory.get(takeThing);
 drop(t);
 return t;
    }
 
    @Override
 public String toString() {
 return "Adventurer [location=" + location + ", moveFrom=" + moveFrom + ", inventory=" + inventory
                + ", numberOfObjects=" + numberOfObjects + "]";
    }
 
 
 
}

Location.java

import java.util.HashMap;
import java.util.Map;

//general location (battlefield, outer space, etc.)
public class Location {
 private String type;
 private HashMap<String ,Location> roomDir;
 

 public Location() {
 roomDir = new HashMap<String,Location>();
    }
 
 public String getType() {
 return type;
 
    }

 public void setType(String type) {
 this.type = type;
    }
 public void addDirection(String dir,Location location){
 roomDir.put(dir, location);  
      }

 public HashMap<String, Location> getRoomDir() {
 return roomDir;
    }

 public void setRoomDir(HashMap<String, Location> roomDir) {
 this.roomDir = roomDir;
    }

    @Override
 public String toString() {
 return "Location [type=" + type + ", roomDir=" + roomDir + "]";
    }

 
 
 
 
 
}

Room.java

import java.util.ArrayList;

public class Room extends Location {
 
 private String name;
 private String desc;
 private ArrayList<Thing> contents;
 private int numberOfThings;
 public Room() {
 contents = new ArrayList<>();
 setType("Room");
    }
 
 public Room(String name, String desc) {
 this.name = name;
 this.desc = desc;
 contents = new ArrayList<>();
 setType("Room");
    }

 public ArrayList<Thing>  getContents() {
 return contents;
    }
 
 public void addObject(Thing item){
 contents.add(item);
    }
 public void dropObject(Thing item){
 contents.remove(item);
    }
 

 public void setContents(ArrayList<Thing> contents) {
 this.contents = contents;
    }

 public int getNumberOfThings() {
 numberOfThings = contents.size();
 return numberOfThings;
    }

 public String getName() {
 return name;
    }
 public void setName(String name) {
 this.name = name;
    }
 public String getDesc() {
 return desc;
    }
 public void setDesc(String desc) {
 this.desc = desc;
    }
 public String setLocationType() {
 return "Room";
 
    }
 
 public void viewRoomContent() {
 System.out.println("Room Contents");
 System.out.println();
 if(contents.size()==0)
 System.out.println("No Item in Room");
 else {
 for(int i = 0 ; i < contents.size(); i++)
 System.out.println(i + "  " + contents.get(i));
        }
 System.out.println();
    }

    @Override
 public String toString() {
 return "Room [name=" + name + ", desc=" + desc + ", contents=" + contents
                + ", numberOfThings=" + getNumberOfThings() + "]";
    }

 public Thing getRoomItem(int takeThing) {
 Thing t = contents.get(takeThing);
 dropObject(t);
 return t;
 
    }
}


TextAdventure.java

public class TextAdventure {

 public static void main(String[] args) {
 AdventureModel adv = new AdventureModel();
 adv.startGame();
    }

}

Thing.java

public class Thing {
 private String name;
 private String desc;
 
 public Thing() {
 
    }
 public Thing(String name, String desc) {
 this.name = name;
 this.desc = desc;
    }
 public String getName() {
 return name;
    }
 public void setName(String name) {
 this.name = name;
    }
 public String getDesc() {
 return desc;
    }
 public void setDesc(String desc) {
 this.desc = desc;
    }
 public String look() {
 return getDesc();
    }
    @Override
 public String toString() {
 return "Thing name=" + name + ", desc=" + desc ;
    }
 
 
 
}

Wall.java

public class Wall extends Location{
 
 public Wall() {
 setType("Wall");
 
    }
 
   }

Water.java

public class Water extends Location {
 
 public Water() {
 setType("Water");
    }

}


Contact Us to get Java Project Help, Java Assignment Help, Java Homework Help with an affordable prices at cotact@codersarts.com


Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

Terms and Conditions        Privacy Policy    Pricing Policy       Refund Policy         COPYRIGHT © 2021

Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram