-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGame.java
More file actions
79 lines (68 loc) · 2 KB
/
Game.java
File metadata and controls
79 lines (68 loc) · 2 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
import javax.swing.JTextArea;
/***
* Represents the entire game.
*
* @author David
*/
public class Game {
public enum KeyAction {
RIGHT, LEFT, UP, DOWN
};
// possible values for locations in a Room object
static final int INVALID = -1;
static final int EMPTY = 0;
static final int PLAYER = 1;
static final int WALL = 2;
static final int WUMPUS = 3;
private Room currentRoom;
private Player player;
private Wumpus enemy;
private JTextArea display;
public Game(JTextArea display) {
this.display = display;
currentRoom = new Room(60, 20);
player = new Player(currentRoom);
enemy = new Wumpus(currentRoom);
displayWelcome();
}
/**
* Performs the appropriate action in response to a keyboard press action.
*
* @param the key that was pressed which we want to perform an action for.
*/
public void handleEvent(KeyAction e) {
if (e == KeyAction.RIGHT) {
player.move(Location.EAST);
}
if (e == KeyAction.LEFT) {
player.move(Location.WEST);
}
if (e == KeyAction.UP) {
player.move(Location.NORTH);
}
if (e == KeyAction.DOWN) {
player.move(Location.SOUTH);
}
}
/**
* Performs an action for a command typed at the game console.
*
* @param cmd the string the user typed at the game console.
*/
public void handleCommand(String cmd) {
if (cmd.contains("look")) display("DON'T TOUCH THE WUMPUS!!");
else
display("I don't know what you mean...");
}
private void displayWelcome() {
display.append("Welcome to our AMAZING TEXT ADVENTURE!\n");
display.append("Use the arrow keys to navigate\n\n");
display.append("Don't touch the wumpus");
}
public void display(String msg) {
display.append(msg + "\n");
}
public Room getCurrentRoom() {
return currentRoom;
}
}