-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRoom.java
More file actions
135 lines (118 loc) · 3.38 KB
/
Room.java
File metadata and controls
135 lines (118 loc) · 3.38 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/***
* A room in the game. A room contains a 2d int array to represent the things in
* the room. See the Game class for int constants that represent different game
* things. For example: Game.WUMPUS = 3; These are the values in the room int
* array.
*
* @author David
*/
public class Room {
private int[][] room; // 2d grid for the room
private static String[] displaySymbols = { ".", "*", "X", "W" };
private int width, height;
private String longDescription;
private String shortDescription;
public Room(int w, int h) {
width = w;
height = h;
room = new int[h][w];
addBorder();
}
// Adds a border of wall objects around the edges of the room
private void addBorder() {
for (int i = 0; i < room.length; i++) {
room[i][0] = room[i][room[0].length - 1] = 2;
}
for (int j = 0; j < room[0].length; j++) {
room[0][j] = room[room.length - 1][j] = 2;
}
}
/**
* Returns the value of (row, col) in the room. Possible values include
* Game.WUMPUS, Game.WALL, Game.PLAYER, Game.EMPTY, Game.INVALID
*
* @param row the row in the room grid
* @param col the column in the room grid
* @return what is at that location in the room.
*/
public int get(int row, int col) {
if (isInRoom(row, col)) {
return room[row][col];
} else {
return Game.INVALID;
}
}
/**
* Put a new value into the room grid at (row, col). Possible values include
* Game.WUMPUS, Game.WALL, Game.PLAYER, Game.EMPTY
*
* @param row the row to place the new value
* @param col the column to place the new value
* @param value the value to be placed at (row, col)
*/
public void put(int row, int col, int value) {
if (isInRoom(row, col))
room[row][col] = value;
}
/**
* Return a string representation for the room. This is what actually
* gets displayed by the GUI class and what the user sees.
*/
public String toString() {
StringBuilder b = new StringBuilder();
for (int r = 0; r < room.length; r++) {
for (int c = 0; c < room[0].length; c++) {
b.append(displaySymbols[room[r][c]]);
}
b.append("\n");
}
return b.toString();
}
int getWidth() {
return room[0].length;
}
int getHeight() {
return room.length;
}
// return true if (newrow, newcol) is Game.EMPTY
boolean isEmpty(int newrow, int newcol) {
return get(newrow, newcol) == 0;
}
// return true if Location loc is Game.EMPTY
boolean isEmpty(Location loc) {
return isEmpty(loc.row, loc.col);
}
// Move the element at Location loc in the direction
// This places the element at the new location and sets
// the previous location to Game.EMPTY
void moveElementAt(Location loc, int direction) {
if (!isInRoom(loc))
return;
Location moveTo = Location.locationInDirection(loc, direction);
if (!isInRoom(moveTo))
return;
room[moveTo.row][moveTo.col] = room[loc.row][loc.col]; // move thing
room[loc.row][loc.col] = Game.EMPTY; // old square empty
}
// return true if (row, col) is a valid location in the room
public boolean isInRoom(int row, int col) {
if (row < 0 || col < 0) {
return false;
}
if (row >= room.length) {
return false;
}
if (col >= room[0].length) {
return false;
}
return true;
}
public boolean isInRoom(Location loc) {
return isInRoom(loc.row, loc.col);
}
// return a random location in the room
public Location getRandomLocation() {
return new Location((int) (Math.random() * height),
(int) (Math.random() * width));
}
}