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.
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|
{{works with|Java|8}}
<
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 javax.swing.SwingUtilities.*;
Line 28 ⟶ 31:
static final Random rand = new Random();
final int
final int playerSize = 16;
Line 34 ⟶ 37:
int currRoom, numArrows, wumpusRoom;
List<String> messages;
public Wumpus() {
setPreferredSize(new Dimension(721,
setBackground(Color.white);
setForeground(Color.lightGray);
Line 44 ⟶ 47:
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (gameOver) {
startNewGame();
Line 73 ⟶ 76:
} else if (isRightMouseButton(e)) {
}▼
}
}
repaint();
}
boolean insideRoom(int ex, int ey, int cx, int cy) {
return ((ex > cx && ex < cx +
&& (ey > cy && ey < cy +
}
});
Line 99 ⟶ 94:
messages = new ArrayList<>();
hazards = new
for (int i = 0; i < rooms.length; i++)
hazards
// hazards can share rooms (unless they are identical)
Line 110 ⟶ 105:
do {
room = rand.nextInt(rooms.length);
} while (tooClose(room) || hazards
if (ord == 0)
wumpusRoom = room;
hazards
}
Line 132 ⟶ 127:
void situation() {
if (set.contains(Hazard.Wumpus)) {
Line 148 ⟶ 143:
do {
currRoom = rand.nextInt(rooms.length);
} while (hazards
// 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
// re-evaluate
Line 165 ⟶ 160:
// look around
for (int link : links[currRoom]) {
for (Hazard hazard : hazards
messages.add(hazard.warning);
}
Line 172 ⟶ 167:
void shoot(int room) {
if (hazards
messages.add("You win! You've killed the Wumpus!");
gameOver = true;
Line 183 ⟶ 178:
} else if (rand.nextInt(4) != 0) { // 75 %
hazards
wumpusRoom = links[wumpusRoom][rand.nextInt(3)];
Line 192 ⟶ 187:
} else {
messages.add("You woke the Wumpus and he moved");
hazards
}
}
Line 199 ⟶ 194:
void drawPlayer(Graphics2D g) {
int x = rooms[currRoom][0] + (
int y = rooms[currRoom][1] + (
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] +
int y1 = rooms[i][1] +
int x2 = rooms[link][0] +
int y2 = rooms[link][1] +
g.drawLine(x1, y1, x2, y2);
}
Line 245 ⟶ 240:
g.setColor(Color.orange);
for (int[] r : rooms)
g.fillOval(r[0], r[1],
if (!gameOver) {
g.setColor(Color.magenta);
for (int link : links[currRoom])
g.fillOval(rooms[link][0], rooms[link][1],
}
g.setColor(Color.darkGray);
for (int[] r : rooms)
g.drawOval(r[0], r[1],
}
Line 264 ⟶ 259:
if (messages != null) {
g.setColor(Color.black);
String msg = messages.stream().distinct().collect(joining(" & "));▼
messages = messages.stream().distinct().collect(toList());
// concat at most three
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}};
}</
|