Hunt The Wumpus/Java: Difference between revisions

m
fix formatting
(→‎Code: don't try to place identical hazards in the same room)
m (fix formatting)
 
(5 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|300px200px|thumb|right]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Path2D;
import java.util.*;
import java.util.List;
import static java.util.stream.Collectors.joining;
import javax.swing.*;
import static java.util.stream.Collectors.joining*;
import static javax.swing.SwingUtilities.*;
 
Line 28 ⟶ 31:
static final Random rand = new Random();
 
final int caveSizeroomSize = 45;
final int playerSize = 16;
 
Line 34 ⟶ 37:
int currRoom, numArrows, wumpusRoom;
List<String> messages;
Map<Integer, EnumSetSet<Hazard>>[] hazards;
 
public Wumpus() {
setPreferredSize(new Dimension(721, 677687));
setBackground(Color.white);
setForeground(Color.lightGray);
Line 44 ⟶ 47:
 
addMouseListener(new MouseAdapter() {
boolean firedArrow = false;
 
@Override
public void mousePressed(MouseEvent e) {
 
if (gameOver) {
startNewGame();
Line 73 ⟶ 76:
 
} else if (isRightMouseButton(e)) {
if shoot(!firedArrowselectedRoom) {;
firedArrow = true;
shoot(selectedRoom);
}
}
}
repaint();
}
 
@Override
public void mouseReleased(MouseEvent e) {
firedArrow = false;
}
 
boolean insideRoom(int ex, int ey, int cx, int cy) {
return ((ex > cx && ex < cx + caveSizeroomSize)
&& (ey > cy && ey < cy + caveSizeroomSize));
}
});
Line 99 ⟶ 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 110 ⟶ 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 132 ⟶ 127:
 
void situation() {
EnumSetSet<Hazard> set = hazards.get([currRoom)];
 
if (set.contains(Hazard.Wumpus)) {
Line 148 ⟶ 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
set.remove(Hazard.Bat);
int newRoom;
do {
newRoom = rand.nextInt(rooms.length);
} while (newRoom == currRoom || hazards[newRoom].contains(Hazard.Bat));
hazards.get([newRoom)].add(Hazard.Bat);
 
// re-evaluate
Line 165 ⟶ 160:
// look around
for (int link : links[currRoom]) {
for (Hazard hazard : hazards.get([link)])
messages.add(hazard.warning);
}
Line 172 ⟶ 167:
 
void shoot(int room) {
if (hazards.get([room)].contains(Hazard.Wumpus)) {
messages.add("You win! You've killed the Wumpus!");
gameOver = true;
Line 183 ⟶ 178:
 
} else if (rand.nextInt(4) != 0) { // 75 %
hazards.get([wumpusRoom)].remove(Hazard.Wumpus);
wumpusRoom = links[wumpusRoom][rand.nextInt(3)];
 
Line 192 ⟶ 187:
} else {
messages.add("You woke the Wumpus and he moved");
hazards.get([wumpusRoom)].add(Hazard.Wumpus);
}
}
Line 199 ⟶ 194:
 
void drawPlayer(Graphics2D g) {
int x = rooms[currRoom][0] + (caveSizeroomSize - playerSize) / 2;
int y = rooms[currRoom][1] + (caveSizeroomSize - playerSize) - 2;
 
Path2D player = new Path2D.Double();
Line 235 ⟶ 230:
for (int i = 0; i < links.length; i++) {
for (int link : links[i]) {
int x1 = rooms[i][0] + caveSizeroomSize / 2;
int y1 = rooms[i][1] + caveSizeroomSize / 2;
int x2 = rooms[link][0] + caveSizeroomSize / 2;
int y2 = rooms[link][1] + caveSizeroomSize / 2;
g.drawLine(x1, y1, x2, y2);
}
Line 245 ⟶ 240:
g.setColor(Color.orange);
for (int[] r : rooms)
g.fillOval(r[0], r[1], caveSizeroomSize, caveSizeroomSize);
 
if (!gameOver) {
g.setColor(Color.magenta);
for (int link : links[currRoom])
g.fillOval(rooms[link][0], rooms[link][1], caveSizeroomSize, caveSizeroomSize);
}
 
g.setColor(Color.darkGray);
for (int[] r : rooms)
g.drawOval(r[0], r[1], caveSizeroomSize, caveSizeroomSize);
}
 
Line 264 ⟶ 259:
if (messages != null) {
g.setColor(Color.black);
 
String msg = messages.stream().distinct().collect(joining(" & "));
g.drawString(msg,// 20,collapse getHeight()identical - 30);messages
messages = messages.stream().distinct().collect(toList());
 
// concat at most three
String msg = messages.stream().distinctlimit(3).collect(joining(" & "));
g.drawString(msg, 20, getHeight() - 40);
 
// if there's more, print underneath
if (messages.size() > 3) {
g.drawString("& " + messages.get(3), 20, getHeight() - 17);
}
 
messages.clear();
}
Line 308 ⟶ 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