Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
f967ca0
Untrack workspace.xml
JavierGelatti Dec 13, 2015
e2919b5
Untrack .zip files (fitnesse)
JavierGelatti Dec 14, 2015
16078ba
Test ArrowKillWumpus in all directions
JavierGelatti Dec 14, 2015
1089163
Extract scenario to test that the arrow stops when hitting the Wumpus
JavierGelatti Dec 14, 2015
5ccef9d
Promote test arrow stops when hitting Wumpus to test page
JavierGelatti Dec 14, 2015
2a36ff8
Clear the map when given a new map
JavierGelatti Dec 14, 2015
86f2239
Extract scenario for ArrowsAccumulateInFarthestCavern test
JavierGelatti Dec 14, 2015
934a34b
Test ArrowsAccumulateInFarthestCavern in all directions
JavierGelatti Dec 14, 2015
c55e90c
Rename finalCavern argument to farthestCavern
JavierGelatti Dec 14, 2015
673c829
Test that if the Wumpus stops the arrow, the player is not killed by …
JavierGelatti Dec 14, 2015
33863c4
Extract scenario in PlayerCanNotShootWithoutArrows test
JavierGelatti Dec 14, 2015
6d4d87d
Test PlayerCanNotShootWithoutArrows in all directions
JavierGelatti Dec 14, 2015
43757c2
Extract scenario in PlayerKilledByShootingWall test
JavierGelatti Dec 14, 2015
0cf178e
Add more cases in PlayerKilledByShootingWall test
JavierGelatti Dec 14, 2015
87a624e
Generalize scenario in ArrowsAccumulateInFarthestCavern test
JavierGelatti Dec 14, 2015
e3a7205
Extract method connectionsOf(...)
JavierGelatti Dec 19, 2015
00a67b8
Replace imperative style for traversing collections with functional s…
JavierGelatti Dec 19, 2015
9678d82
Implement missing test
JavierGelatti Dec 19, 2015
5448b4b
Extract randomChoice method
JavierGelatti Dec 19, 2015
652c9f7
Replace playerCavern String by Cavern
JavierGelatti Dec 21, 2015
27f2195
Replace wumpusCavern String by Cavern
JavierGelatti Dec 21, 2015
ce98f0b
Replace batCaverns Strings by Caverns
JavierGelatti Dec 21, 2015
04aca0e
Replace pitCaverns Strings by Caverns
JavierGelatti Dec 21, 2015
014406b
Replace arrowsIn Caverns Strings by Caverns
JavierGelatti Dec 21, 2015
0aeeb4f
Replace caverns Caverns Strings by Caverns
JavierGelatti Dec 21, 2015
0b4a3d7
Make connectionsOf receive a Cavern
JavierGelatti Dec 21, 2015
0afed14
Move connectionsOf to Cavern
JavierGelatti Dec 21, 2015
d62b141
Make cavern(...) return the existing cavern (if it already exists)
JavierGelatti Dec 21, 2015
c553765
Move findDestination to Cavern
JavierGelatti Dec 21, 2015
3f9dfe1
Delegate to Cavern to obtain available directions
JavierGelatti Dec 21, 2015
39fd2da
Replace Strings with Caverns in Destination
JavierGelatti Dec 21, 2015
5481162
Replace Strings with Caverns in moveWumpus()
JavierGelatti Dec 21, 2015
bad0c55
Delegate to Cavern in order to find connected caverns
JavierGelatti Dec 21, 2015
6752738
Make ArrowTracker constructor receive a Cavern
JavierGelatti Dec 21, 2015
3d45d2d
Replace Strings in ArrowTracker by Caverns
JavierGelatti Dec 21, 2015
f9584c1
Simplify findDestination(...)
JavierGelatti Dec 21, 2015
3c0247b
Continue replacing Strings with Caverns
JavierGelatti Dec 21, 2015
f7a5c0e
Improve implementation of Cavern.equals(...)
JavierGelatti Dec 21, 2015
aff0347
Move connections logic to Cavern class
JavierGelatti Dec 21, 2015
b288638
Introduce NullCavern
JavierGelatti Dec 21, 2015
931ba74
Clean trackArrow(...)
JavierGelatti Dec 21, 2015
488a223
Move Cavern class to upper level
JavierGelatti Dec 21, 2015
0d74e8e
Move some classes and change the order of the methods
JavierGelatti Dec 21, 2015
13c386d
Remove duplication from moveWumpus and randomlyTransportPlayer
JavierGelatti Dec 21, 2015
72e09ed
Encapsulate Cavern.name
JavierGelatti Dec 21, 2015
e7f8989
Clean HuntTheWumpusGame.cavern(...)
JavierGelatti Dec 21, 2015
fb9c917
Extract RandomChooser class
JavierGelatti Dec 21, 2015
ff9456e
Clean trackArrow(...)
JavierGelatti Dec 21, 2015
567a4eb
Extract some methods to add clarity
JavierGelatti Dec 21, 2015
883aca9
Replace boolean return flag with exception in movePlayer(...)
JavierGelatti Dec 21, 2015
e135b9f
Delete method hasConnectionGoing(...)
JavierGelatti Dec 21, 2015
4a632c8
Make code inspection happy
JavierGelatti Dec 21, 2015
7b1f750
Extract methods to express clarity
JavierGelatti Dec 21, 2015
fd31cdf
Extract HuntTheWumpusMap class
JavierGelatti Dec 21, 2015
ae5af81
Extract game commands to upper classes
JavierGelatti Dec 21, 2015
d24e6c4
Extract method playerIsInWumpusCavern
JavierGelatti Dec 21, 2015
e98f67b
Move responsibility to move Wumpus from game to map
JavierGelatti Dec 21, 2015
57a64d9
Move quiver to Map
JavierGelatti Dec 21, 2015
b5d0713
Remove references to Game from Commands
JavierGelatti Dec 21, 2015
c236c38
Rename HuntTheWumpusGame to HuntTheWumpusFacade, and HuntTheWumpusMap…
JavierGelatti Dec 21, 2015
edd6139
Move methods to Game
JavierGelatti Dec 21, 2015
51ce0d6
Put take method after arrowPath
JavierGelatti Dec 21, 2015
e0ab3cf
Add test to check result of findDestination
JavierGelatti Dec 21, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ HTW/.idea
HTW/.idea/workspace.xml
HTW/.DS_Store

fitnesse/FitNesseRoot/**/*.zip

1,804 changes: 0 additions & 1,804 deletions HTW/.idea/workspace.xml

This file was deleted.

12 changes: 5 additions & 7 deletions HTW/src/htw/HuntTheWumpus.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package htw;

import htw.game.HuntTheWumpusGame;

public interface HuntTheWumpus {
public enum Direction {
enum Direction {
NORTH {
public Direction opposite() {
return SOUTH;
Expand Down Expand Up @@ -39,11 +37,11 @@ public Direction opposite() {
Integer getArrowsInCavern(String cavern);
void connectCavern(String from, String to, Direction direction);
String findDestination(String cavern, Direction direction);
HuntTheWumpusGame.Command makeRestCommand();
HuntTheWumpusGame.Command makeShootCommand(Direction direction);
HuntTheWumpusGame.Command makeMoveCommand(Direction direction);
Command makeRestCommand();
Command makeShootCommand(Direction direction);
Command makeMoveCommand(Direction direction);

public interface Command {
interface Command {
void execute();
}
}
8 changes: 4 additions & 4 deletions HTW/src/htw/console/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class Main implements HtwMessageReceiver {
};

public static void main(String[] args) throws IOException {
game = HtwFactory.makeGame("htw.game.HuntTheWumpusGame", new Main());
game = HtwFactory.makeGame("htw.game.HuntTheWumpusFacade", new Main());
createMap();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
game.makeRestCommand().execute();
Expand Down Expand Up @@ -107,8 +107,8 @@ else if (command.equalsIgnoreCase("q"))
}

private static void createMap() {
int ncaverns = (int) (Math.random() * 30.0 + 10.0);
while (ncaverns-- > 0)
int nCaverns = (int) (Math.random() * 30.0 + 10.0);
while (nCaverns-- > 0)
caverns.add(makeName());

for (String cavern : caverns) {
Expand Down Expand Up @@ -153,7 +153,7 @@ private static void maybeConnectCavern(String cavern, Direction direction) {
}

private static void connectIfAvailable(String from, Direction direction, String to) {
if (game.findDestination(from, direction) == null) {
if (game.findDestination(from, direction).isEmpty()) {
game.connectCavern(from, to, direction);
}
}
Expand Down
76 changes: 76 additions & 0 deletions HTW/src/htw/game/Cavern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package htw.game;

import htw.HuntTheWumpus;

import java.util.*;

import static htw.HuntTheWumpus.*;

class Cavern {
public static final Cavern NULL = new NullCavern();

private String name;
private Map<HuntTheWumpus.Direction, Cavern> connections = new HashMap<>();

public Cavern(String name) {
this.name = name;
}

public String getName() {
return name;
}

public boolean isNamed(String name) {
return this.name.equals(name);
}

public Cavern findDestination(Direction direction) {
Cavern destination = connections.get(direction);
return (destination != null) ? destination : new NullCavern();
}

public Set<HuntTheWumpus.Direction> availableDirections() {
return connections.keySet();
}

public List<Cavern> connectedCaverns() {
return new ArrayList<>(connections.values());
}

public void addConnection(Cavern to, Direction direction) {
connections.put(direction, to);
}

public List<Cavern> getCavernsGoing(Direction direction) {
List<Cavern> caverns = new ArrayList<>();
return findDestination(direction).accumulateCavernsGoing(direction, caverns, this);
}

protected List<Cavern> accumulateCavernsGoing(Direction direction, List<Cavern> caverns, Cavern initialCavern) {
caverns.add(this);
if (initialCavern.equals(this))
return caverns;

return findDestination(direction).accumulateCavernsGoing(direction, caverns, initialCavern);
}

public boolean isNull() {
return false;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Cavern) {
Cavern c = (Cavern) obj;
return getName().equals(c.getName());
}

return false;
}

@Override
public int hashCode() {
return getName().hashCode();
}
}

161 changes: 161 additions & 0 deletions HTW/src/htw/game/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package htw.game;

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

import static htw.HuntTheWumpus.Direction;

public class Game {
private Set<Cavern> caverns = new HashSet<>();
private Set<Cavern> pitCaverns = new HashSet<>();
private Set<Cavern> batCaverns = new HashSet<>();

private Cavern playerCavern = Cavern.NULL;
private Cavern wumpusCavern = Cavern.NULL;
private int quiver = 0;

private Map<Cavern, Integer> arrowsIn = new HashMap<>();
private RandomChooser randomChooser = new RandomChooser();

public Cavern getCavernNamed(String cavernName) {
return caverns.stream()
.filter(c -> c.isNamed(cavernName))
.findAny()
.orElse(new Cavern(cavernName));
}

public void connectCavern(Cavern from, Cavern to, Direction direction) {
from.addConnection(to, direction);
caverns.add(from);
caverns.add(to);
}

public Set<Direction> availableDirections() {
return playerCavern.availableDirections();
}

public void addBatCavern(Cavern cavern) {
batCaverns.add(cavern);
}

public void addPitCavern(Cavern cavern) {
pitCaverns.add(cavern);
}

public Cavern getWumpusCavern() {
return wumpusCavern;
}

public void setWumpusCavern(Cavern cavern) {
wumpusCavern = cavern;
}

public Cavern getPlayerCavern() {
return playerCavern;
}

public void setPlayerCavern(Cavern cavern) {
playerCavern = cavern;
}

public boolean playerIsInPitCavern() {
return pitCaverns.contains(playerCavern);
}

public boolean playerIsInBatsCavern() {
return batCaverns.contains(playerCavern);
}

public boolean playerIsInWumpusCavern() {
return playerCavern.equals(wumpusCavern);
}

public boolean wumpusCavernIsNear() {
return reportNearby(wumpusCavern::equals);
}

public boolean pitCavernIsNear() {
return reportNearby(pitCaverns::contains);
}

public boolean batCavernIsNear() {
return reportNearby(batCaverns::contains);
}

private boolean reportNearby(Predicate<Cavern> nearTest) {
return playerCavern.connectedCaverns().stream()
.anyMatch(nearTest::test);
}

public void randomlyTransportPlayer() {
List<Cavern> transportChoices = new ArrayList<>(caverns);
transportChoices.remove(playerCavern);

playerCavern = randomChooser.chooseFrom(transportChoices);
}

public void moveWumpus() {
List<Cavern> wumpusChoices = wumpusCavern.connectedCaverns();
wumpusChoices.add(wumpusCavern);

wumpusCavern = randomChooser.chooseFrom(wumpusChoices);
}

public Cavern getPlayerDestinationGoing(Direction direction) {
return playerCavern.findDestination(direction);
}

public void incrementArrowsIn(Cavern arrowCavern, int numberOfArrows) {
int arrows = getArrowsInCavern(arrowCavern);
arrowsIn.put(arrowCavern, arrows + numberOfArrows);
}

public void clearArrowsInPlayerCavern() {
arrowsIn.put(playerCavern, 0);
}

public int getArrowsInPlayerCavern() {
return getArrowsInCavern(playerCavern);
}

public Integer getArrowsInCavern(Cavern cavern) {
return zeroIfNull(arrowsIn.get(cavern));
}

private int zeroIfNull(Integer integer) {
if (integer == null)
return 0;
else
return integer;
}

public void clearMap() {
playerCavern = Cavern.NULL;
wumpusCavern = Cavern.NULL;

batCaverns.clear();
pitCaverns.clear();
arrowsIn.clear();
caverns.clear();
}

public int getQuiver() {
return quiver;
}

void setQuiver(int arrows) {
this.quiver = arrows;
}

public void decrementQuiverBy(int arrowNumber) {
quiver -= arrowNumber;
}

public void incrementQuiverBy(int arrowNumber) {
quiver += arrowNumber;
}

public boolean quiverEmpty() {
return quiver == 0;
}
}
45 changes: 45 additions & 0 deletions HTW/src/htw/game/GameCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package htw.game;

import htw.HtwMessageReceiver;
import htw.HuntTheWumpus;

abstract class GameCommand implements HuntTheWumpus.Command {
protected Game game;
protected HtwMessageReceiver messageReceiver;

public GameCommand(Game game, HtwMessageReceiver messageReceiver) {
this.game = game;
this.messageReceiver = messageReceiver;
}

public void execute() {
processCommand();

game.moveWumpus();

reportStatus();
}

protected abstract void processCommand();

private void reportStatus() {
reportAvailableDirections();
reportSpecialLocations();
}

private void reportAvailableDirections() {
game.availableDirections().forEach(messageReceiver::passage);
}

private void reportSpecialLocations() {
if (game.playerIsInWumpusCavern())
messageReceiver.wumpusMovesToPlayer();
if (game.batCavernIsNear())
messageReceiver.hearBats();
if (game.pitCavernIsNear())
messageReceiver.hearPit();
if (game.wumpusCavernIsNear())
messageReceiver.smellWumpus();
}

}
Loading