Computer Science 15-100, Summer 2009
Class Notes: Getting Started with Text Adventures (Practice Writing
Classes)
Getting Started with Text Adventures
Here is AdventureDemo.java, containing the AdventureDemo class and the Adventure, Dir, Thing, and Room classes as we wrote them in today's lecture.
import java.util.*;
public class AdventureDemo {
public static void main(String[] args) {
Adventure game = new Adventure();
game.play();
}
}
class Adventure {
// instance variables (aka "fields")
private Room currentRoom;
private Scanner scanner = new Scanner(System.in);
private boolean gameOver;
private ArrayList<Thing> inventory;
public Adventure() {
Room rm5419a = new Room("5419a lab");
Room rm5419b = new Room("5419b lab");
Room hall5400 = new Room("5400 hallway");
rm5419b.setExit(Dir.SOUTH, hall5400);
rm5419b.setExit(Dir.WEST, rm5419a);
hall5400.setExit(Dir.NORTH, rm5419b);
rm5419a.setExit(Dir.EAST, rm5419b);
Thing johnTran = new Thing("JohnTran");
rm5419b.addThing(johnTran);
Thing projector = new Thing("Projector");
rm5419b.addThing(projector);
currentRoom = rm5419b;
inventory = new ArrayList<Thing>();
gameOver = false;
}
private void printInventory() {
System.out.print("You are carrying: ");
if (this.inventory.size() == 0)
System.out.println("Nothing!");
else
System.out.println(this.inventory);
}
private void printObjectsInRoom() {
System.out.println("You can see: " +
currentRoom.getThings());
}
private void printLegalExits() {
System.out.print("You can go: ");
boolean hasExits = false;
for (int dir=0; dir<Dir.DIRS; dir++) {
Room exitRoom = currentRoom.getExit(dir);
if (exitRoom != null) {
if (hasExits == true)
System.out.print(", ");
System.out.print(Dir.getDirName(dir));
hasExits = true;
}
}
if (hasExits == false)
System.out.print("no directions!");
System.out.println();
}
private void doGet(String thingName) {
Thing thing = currentRoom.findThing(thingName);
if (thing == null) {
System.out.println("I don't see that here!");
return;
}
currentRoom.removeThing(thing);
inventory.add(thing);
}
private void doGo(String noun) {
int dir = Dir.getDir(noun);
if (dir < 0) {
System.out.println("I do not know that direction!");
return;
}
Room newRoom = currentRoom.getExit(dir);
if (newRoom == null) {
System.out.println("I cannot go in that direction!");
return;
}
currentRoom = newRoom;
}
private void doQuit() {
gameOver = true;
System.out.println("Goodbye!");
}
private void doCommand() {
System.out.print("Your command --> ");
String cmd = scanner.nextLine().toLowerCase();
String[] cmdWords = cmd.split(" ");
String verb = cmdWords[0];
String noun = (cmdWords.length < 2 ? "" : cmdWords[1]);
if (verb.equals("go")) doGo(noun);
else if (verb.equals("get")) doGet(noun);
else if (verb.equals("n")) doGo("north");
else if (verb.equals("quit")) doQuit();
else if (verb.equals("q")) doQuit();
else System.out.println("I don't know how to " + verb);
}
public void play() {
while (gameOver == false) {
System.out.println("\nYou are in " + currentRoom);
printObjectsInRoom();
printInventory();
printLegalExits();
doCommand();
}
System.out.println("You win!");
}
}
class Dir {
public static final int NORTH = 0;
public static final int SOUTH = 1;
public static final int EAST = 2;
public static final int WEST = 3;
public static final int UP = 4;
public static final int DOWN = 5;
public static final int DIRS = 6;
private static final String[] dirNames = {
"North", "South", "East", "West", "Up", "Down"
};
public static String getDirName(int dir) {
return dirNames[dir];
}
public static int getDir(String dirName) {
for (int i=0; i<dirNames.length; i++)
if (dirNames[i].toLowerCase().equals(dirName))
return i;
return -1;
}
}
class Thing {
// fields (aka "instance variables")
private String name;
// constructor
public Thing(String name) {
this.name = name;
}
// accessor
public String getName() {
return this.name;
}
// toString method
public String toString() {
return this.name;
}
}
class Room {
// fields (aka "instance variables")
private String name;
private Room[] exits;
private ArrayList<Thing> things; // like Thing[]
// constructor
public Room(String name) {
this.name = name;
this.exits = new Room[Dir.DIRS];
this.things = new ArrayList<Thing>();
}
// accessor for name
public String getName() {
return this.name;
}
// accessor and mutator for this.things
public ArrayList<Thing> getThings() {
return this.things;
}
public Thing findThing(String thingName) {
for (int i=0; i<this.things.size(); i++) {
Thing thing = this.things.get(i);
if (thing.getName().toLowerCase().equals(thingName))
return thing;
}
return null;
}
public void addThing(Thing thing) {
this.things.add(thing);
}
public void removeThing(Thing thing) {
this.things.remove(thing);
}
// accessor and mutator for this.exits
public Room getExit(int dir) {
return this.exits[dir];
}
public void setExit(int dir, Room room) {
this.exits[dir] = room;
}
// toString method
public String toString() {
return this.name;
}
}
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem