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. Since bats relocate, we don't have to worry
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>
about the wumpus room being unreachable. There are also 3 bats instead of 2, and you get 3 arrows instead of 5.
<br>
 
==Code==
[[File:wumpus_java.png|200px|thumb|right]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Path2D;
Line 34 ⟶ 37:
int currRoom, numArrows, wumpusRoom;
List<String> messages;
Map<Integer, EnumSetSet<Hazard>>[] hazards;
 
public Wumpus() {
Line 44 ⟶ 47:
 
addMouseListener(new MouseAdapter() {
boolean mouseReleased = true;
 
@Override
public void mousePressed(MouseEvent e) {
/* mousePressed feels more responsive than mouseClicked
but we must prevent multiple events going through */
if (!mouseReleased)
return;
mouseReleased = false;
 
if (gameOver) {
Line 83 ⟶ 80:
}
repaint();
}
 
@Override
public void mouseReleased(MouseEvent e) {
mouseReleased = true;
}
 
Line 102 ⟶ 94:
messages = new ArrayList<>();
 
hazards = new HashMap<>()Set[rooms.length];
for (int i = 0; i < rooms.length; i++)
hazards.put([i,] = EnumSet.noneOf(Hazard.class));
 
// hazards can share rooms (unless they are identical)
Line 113 ⟶ 105:
do {
room = rand.nextInt(rooms.length);
} while (tooClose(room) || hazards.get([room)].contains(values[ord]));
 
if (ord == 0)
wumpusRoom = room;
 
hazards.get([room)].add(values[ord]);
}
 
Line 135 ⟶ 127:
 
void situation() {
EnumSetSet<Hazard> set = hazards.get([currRoom)];
 
if (set.contains(Hazard.Wumpus)) {
Line 151 ⟶ 143:
do {
currRoom = rand.nextInt(rooms.length);
} while (hazards.get([currRoom)].contains(Hazard.Bat));
 
// 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.get([newRoom)].contains(Hazard.Bat));
hazards.get([newRoom)].add(Hazard.Bat);
 
// re-evaluate
Line 168 ⟶ 160:
// look around
for (int link : links[currRoom]) {
for (Hazard hazard : hazards.get([link)])
messages.add(hazard.warning);
}
Line 175 ⟶ 167:
 
void shoot(int room) {
if (hazards.get([room)].contains(Hazard.Wumpus)) {
messages.add("You win! You've killed the Wumpus!");
gameOver = true;
Line 186 ⟶ 178:
 
} else if (rand.nextInt(4) != 0) { // 75 %
hazards.get([wumpusRoom)].remove(Hazard.Wumpus);
wumpusRoom = links[wumpusRoom][rand.nextInt(3)];
 
Line 195 ⟶ 187:
} else {
messages.add("You woke the Wumpus and he moved");
hazards.get([wumpusRoom)].add(Hazard.Wumpus);
}
}
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}};
}</langsyntaxhighlight>
236

edits