-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
66 lines (56 loc) · 2.62 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Room kitchen = new Room("Kitchen", "A dank and dirty room buzzing with flies.");
Room livingroom = new Room("Living Room", "A large room with a large couch.");
Room bathroom = new Room("Bathroom", "A small room with a toilet and a sink.");
Room bedroom = new Room("Bedroom", "A small room with a bed.");
GameMap map = new GameMap();
map.linkRooms(kitchen, livingroom, "north");
map.linkRooms(livingroom, kitchen, "south");
map.linkRooms(livingroom, bathroom, "east");
map.linkRooms(bathroom, livingroom, "west");
map.linkRooms(livingroom, bedroom, "west");
map.linkRooms(bedroom, livingroom, "east");
Player p1 = new Player();
p1.setLocation(kitchen);
Scanner in = new Scanner(System.in);
kitchen.addItem(new Item("key", "A small key.", 1));
kitchen.addItem(new Weapon("sword", "A sharp sword.", 10, 10));
// "game loop"
while (true) {
// print location info
System.out.println("You are in the " + p1.getLocation().getName());
String command = in.next(); // single word
if (command.equals("look")) {
System.out.println(p1.getLocation().getDescription());
System.out.println("Items: " + p1.getLocation().getItemList());
} else if (command.equals("go")) {
String direction = in.next();
// check if direction is valid for this room
if (p1.getLocation().hasLink(direction)) {
p1.setLocation(p1.getLocation().getLinkedRoom(direction));
} else {
System.out.println("Bad direction.");
}
} else if (command.equals("pickup")) {
String itemName = in.next();
if (p1.getLocation().hasItem(itemName)) {
p1.pickUpItem(p1.getLocation().getItem(itemName));
System.out.println("You picked up the " + itemName);
} else {
System.out.println("No such item.");
}
} else if (command.equals("inventory")) {
System.out.println(p1.inventory.keySet());
} else if (command.equals("drop")) {
String itemName = in.next();
p1.dropItem(itemName);
System.out.println("You dropped the " + itemName);
} else if (command.equals("quit")) {
in.close();
break;
}
}
}
}