Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion HTW/src/htw/HtwMessageReceiver.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package htw;

import htw.HuntTheWumpus.Direction;

public interface HtwMessageReceiver {
void noPassage();
void hearBats();
void hearPit();
void smellWumpus();
void passage(HuntTheWumpus.Direction direction);
void passage(Direction direction);
void noArrows();
void arrowShot();
void playerShootsSelfInBack();
Expand All @@ -16,4 +18,7 @@ public interface HtwMessageReceiver {
void playerMovesToWumpus();
void wumpusMovesToPlayer();
void batsTransport();
void addPitCoverToAdjacentCavern(Direction direction);
void cavernNotAdjacentForPitCover();
void noPitCover();
}
1 change: 1 addition & 0 deletions HTW/src/htw/HuntTheWumpus.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public Direction opposite() {
HuntTheWumpusGame.Command makeRestCommand();
HuntTheWumpusGame.Command makeShootCommand(Direction direction);
HuntTheWumpusGame.Command makeMoveCommand(Direction direction);
HuntTheWumpusGame.Command makeAddPitCoverCommand(Direction direction);

public interface Command {
void execute();
Expand Down
23 changes: 23 additions & 0 deletions HTW/src/htw/console/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ else if (command.equalsIgnoreCase("sn"))
c = game.makeShootCommand(NORTH);
else if (command.equalsIgnoreCase("ss"))
c = game.makeShootCommand(SOUTH);
else if (command.equalsIgnoreCase("pce"))
c = game.makeAddPitCoverCommand(EAST);
else if (command.equalsIgnoreCase("pcw"))
c = game.makeAddPitCoverCommand(WEST);
else if (command.equalsIgnoreCase("pcn"))
c = game.makeAddPitCoverCommand(NORTH);
else if (command.equalsIgnoreCase("pcs"))
c = game.makeAddPitCoverCommand(SOUTH);
else if (command.equalsIgnoreCase("q"))
return;

Expand Down Expand Up @@ -236,6 +244,21 @@ public void batsTransport() {
System.out.println("Some bats carried you away.");
}

@Override
public void addPitCoverToAdjacentCavern(Direction direction) {
System.out.println("Pit cover added to cavern to the " + direction);
}

@Override
public void cavernNotAdjacentForPitCover() {
System.out.println("You cannot add a pit cover in that direction");
}

@Override
public void noPitCover() {
System.out.println("You have no more pit covers");
}

private void hit(int points) {
hitPoints -= points;
if (hitPoints <= 0) {
Expand Down
143 changes: 113 additions & 30 deletions HTW/src/htw/game/HuntTheWumpusGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class HuntTheWumpusGame implements HuntTheWumpus {
private List<Connection> connections = new ArrayList<>();
Expand All @@ -17,11 +18,16 @@ public class HuntTheWumpusGame implements HuntTheWumpus {
private String wumpusCavern = "NONE";
private int quiver = 0;
private Map<String, Integer> arrowsIn = new HashMap<>();
private String cavernWithPitCover = "NONE";

public HuntTheWumpusGame(HtwMessageReceiver receiver) {
this.messageReceiver = receiver;
}

public void initializeArrowsIn() {
arrowsIn = new HashMap<>();
}

public void setPlayerCavern(String playerCavern) {
this.playerCavern = playerCavern;
}
Expand All @@ -41,17 +47,21 @@ private void reportStatus() {
}

private boolean reportNearby(Predicate<Connection> nearTest) {
for (Connection c : connections)
if (playerCavern.equals(c.from) && nearTest.test(c))
return true;
return false;
return connections.stream()
.filter(c -> playerCavern.equals(c.from) && nearTest.test(c))
.findAny()
.isPresent();
}

private void reportAvailableDirections() {
for (Connection c : connections) {
if (playerCavern.equals(c.from))
messageReceiver.passage(c.direction);
}
connections.stream()
.filter(isFromPlayerCavern())
.map(c -> c.direction)
.forEach(messageReceiver::passage);
}

private Predicate<Connection> isFromPlayerCavern() {
return c -> playerCavern.equals(c.from);
}

public void addBatCavern(String cavern) {
Expand All @@ -71,24 +81,40 @@ public String getWumpusCavern() {
}

protected void moveWumpus() {
List<String> wumpusChoices = new ArrayList<>();
for (Connection c : connections)
if (wumpusCavern.equals(c.from))
wumpusChoices.add(c.to);
List<String> wumpusChoices = connections.stream()
.filter(isFromWumpusCavern())
.map(c -> c.to)
.collect(Collectors.toList());
wumpusChoices.add(wumpusCavern);

int nChoices = wumpusChoices.size();
int choice = (int) (Math.random() * nChoices);
wumpusCavern = wumpusChoices.get(choice);
wumpusCavern = wumpusChoices.stream()
.skip(randomCavernIndex(wumpusChoices.size()))
.findFirst()
.get();
}

private Predicate<Connection> isFromWumpusCavern() {
return c -> wumpusCavern.equals(c.from);
}

private int randomCavernIndex() {
return randomCavernIndex(caverns.size() - 1);
}

private int randomCavernIndex(int size) {
return new Random().nextInt(size);
}

private void randomlyTransportPlayer() {
Set<String> transportChoices = new HashSet<>(caverns);
transportChoices.remove(playerCavern);
int nChoices = transportChoices.size();
int choice = (int) (Math.random() * nChoices);
String[] choices = new String[nChoices];
playerCavern = transportChoices.toArray(choices)[choice];
playerCavern = caverns.stream()
.filter(excludePlayerCavern())
.skip(randomCavernIndex())
.findFirst()
.get();
}

private Predicate<String> excludePlayerCavern() {
return c -> !playerCavern.equals(c);
}

public void setQuiver(int arrows) {
Expand All @@ -110,6 +136,26 @@ private int zeroIfNull(Integer integer) {
return integer.intValue();
}

public String farthestCavern(String startingCavern, Direction direction) {
String cavern = startingCavern;
String furthestCavern = null;
for (int count = 0; count < 100 && (cavern = nextCavern(cavern, direction)) != null; count++)
furthestCavern = cavern;
return furthestCavern;
}

private String nextCavern(String cavern, Direction direction) {
Connection connection = connections.stream()
.filter(isFromDirection(cavern, direction))
.findFirst()
.orElse(null);
return connection != null ? connection.to : null;
}

private Predicate<Connection> isFromDirection(String cavern, Direction direction) {
return c -> cavern.equals(c.from) && direction.equals(c.direction);
}

private class Connection {
String from;
String to;
Expand All @@ -129,10 +175,15 @@ public void connectCavern(String from, String to, Direction direction) {
}

public String findDestination(String cavern, Direction direction) {
for (Connection c : connections)
if (c.from.equals(cavern) && c.direction == direction)
return c.to;
return null;
Connection destination = connections.stream()
.filter(isDestination(cavern, direction))
.findFirst()
.orElse(null);
return destination != null ? destination.to : null;
}

private Predicate<Connection> isDestination(String cavern, Direction direction) {
return c -> c.from.equals(cavern) && c.direction == direction;
}

public Command makeRestCommand() {
Expand All @@ -147,6 +198,10 @@ public Command makeMoveCommand(Direction direction) {
return new MoveCommand(direction);
}

public Command makeAddPitCoverCommand(Direction direction) {
return new AddPitCoverCommand(direction);
}

public abstract class GameCommand implements Command {
public void execute() {
processCommand();
Expand Down Expand Up @@ -242,10 +297,15 @@ private boolean shotSelfInBack() {
}

private String nextCavern(String cavern, Direction direction) {
for (Connection c : connections)
if (cavern.equals(c.from) && direction.equals(c.direction))
return c.to;
return null;
Connection connection = connections.stream()
.filter(isFromDirection(cavern, direction))
.findFirst()
.orElse(null);
return connection != null ? connection.to : null;
}

private Predicate<Connection> isFromDirection(String cavern, Direction direction) {
return c -> cavern.equals(c.from) && direction.equals(c.direction);
}
}
}
Expand Down Expand Up @@ -289,7 +349,7 @@ public boolean movePlayer(Direction direction) {
}

private void checkForPit() {
if (pitCaverns.contains(playerCavern))
if (pitCaverns.contains(playerCavern) && !cavernWithPitCover.equals(playerCavern))
messageReceiver.fellInPit();
}

Expand All @@ -301,4 +361,27 @@ private void checkForArrows() {
arrowsIn.put(playerCavern, 0);
}
}

private class AddPitCoverCommand implements Command {
private Direction direction;

public AddPitCoverCommand(Direction direction) {
this.direction = direction;
}

public void execute() {
if (cavernWithPitCover.equals("NONE")) {
String destination = findDestination(playerCavern, direction);
if (destination != null) {
cavernWithPitCover = destination;
messageReceiver.addPitCoverToAdjacentCavern(direction);
} else {
messageReceiver.cavernNotAdjacentForPitCover();
}
}
else {
messageReceiver.noPitCover();
}
}
}
}
Loading