Skip to content

Commit 5448b4b

Browse files
committed
Extract randomChoice method
1 parent 9678d82 commit 5448b4b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

HTW/src/htw/game/HuntTheWumpusGame.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ private List<Connection> connectionsOf(String cavern) {
5858
.collect(Collectors.toList());
5959
}
6060

61-
6261
public void addBatCavern(String cavern) {
6362
batCaverns.add(cavern);
6463
}
@@ -85,19 +84,23 @@ protected void moveWumpus() {
8584
wumpusChoices.add(wumpusCavern);
8685

8786
int nChoices = wumpusChoices.size();
88-
int choice = (int) (Math.random() * nChoices);
87+
int choice = randomChoice(nChoices);
8988
wumpusCavern = wumpusChoices.get(choice);
9089
}
9190

9291
private void randomlyTransportPlayer() {
9392
Set<String> transportChoices = new HashSet<>(caverns);
9493
transportChoices.remove(playerCavern);
9594
int nChoices = transportChoices.size();
96-
int choice = (int) (Math.random() * nChoices);
95+
int choice = randomChoice(nChoices);
9796
String[] choices = new String[nChoices];
9897
playerCavern = transportChoices.toArray(choices)[choice];
9998
}
10099

100+
private int randomChoice(int numberOfPossibleChoices) {
101+
return (int) (Math.random() * numberOfPossibleChoices);
102+
}
103+
101104
public void setQuiver(int arrows) {
102105
this.quiver = arrows;
103106
}

0 commit comments

Comments
 (0)