Hunt The Wumpus/Java: Difference between revisions
m
fix formatting
(→Code: fix bug with bat relocation) |
m (fix formatting) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1:
{{collection|Hunt_The_Wumpus}}
In this version, arrows only fire a distance of one room.
about the wumpus room being unreachable. There are also 3 bats instead of 2, and you get 3 arrows instead of 5.▼
Since bats relocate, we don't have to worry about the wumpus room being unreachable. <br>
▲
<br>
==Code==
[[File:wumpus_java.png|200px|thumb|right]]
{{works with|Java|8}}
<
import java.awt.event.*;
import java.awt.geom.Path2D;
Line 34 ⟶ 37:
int currRoom, numArrows, wumpusRoom;
List<String> messages;
public Wumpus() {
Line 44 ⟶ 47:
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (gameOver) {
Line 83 ⟶ 80:
}
repaint();
}
Line 102 ⟶ 94:
messages = new ArrayList<>();
hazards = new
for (int i = 0; i < rooms.length; i++)
hazards
// hazards can share rooms (unless they are identical)
Line 113 ⟶ 105:
do {
room = rand.nextInt(rooms.length);
} while (tooClose(room) || hazards
if (ord == 0)
wumpusRoom = room;
hazards
}
Line 135 ⟶ 127:
void situation() {
if (set.contains(Hazard.Wumpus)) {
Line 151 ⟶ 143:
do {
currRoom = rand.nextInt(rooms.length);
} while (hazards
// relocate the bat, but not to the player room or a room with a bat
Line 158 ⟶ 150:
do {
newRoom = rand.nextInt(rooms.length);
} while (newRoom == currRoom || hazards
hazards
// re-evaluate
Line 168 ⟶ 160:
// look around
for (int link : links[currRoom]) {
for (Hazard hazard : hazards
messages.add(hazard.warning);
}
Line 175 ⟶ 167:
void shoot(int room) {
if (hazards
messages.add("You win! You've killed the Wumpus!");
gameOver = true;
Line 186 ⟶ 178:
} else if (rand.nextInt(4) != 0) { // 75 %
hazards
wumpusRoom = links[wumpusRoom][rand.nextInt(3)];
Line 195 ⟶ 187:
} else {
messages.add("You woke the Wumpus and he moved");
hazards
}
}
Line 322 ⟶ 314:
{10, 2, 12}, {13, 19, 11}, {14, 3, 12}, {5, 15, 13}, {14, 16, 19},
{6, 17, 15}, {16, 8, 18}, {19, 10, 17}, {15, 12, 18}};
}</
|