From a4a6a39bf9ebeb8dd73ad45ce198df58f9311e0a Mon Sep 17 00:00:00 2001 From: Paul Sideleau Date: Mon, 13 Apr 2015 11:56:58 -0400 Subject: [PATCH] paul sideleau first iteration --- .../java/com/hangman/players/YourPlayer.java | 51 +++++++++++++++++-- .../com/hangman/players/YourPlayerTest.java | 50 ++++++++++++++---- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/hangman/players/YourPlayer.java b/src/main/java/com/hangman/players/YourPlayer.java index ddcfbd3..c4583db 100644 --- a/src/main/java/com/hangman/players/YourPlayer.java +++ b/src/main/java/com/hangman/players/YourPlayer.java @@ -1,13 +1,56 @@ package com.hangman.players; import com.hangman.Player; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; +import java.util.*; public class YourPlayer implements Player { + private final Random random = new Random(); + private final Set usedLetters = new HashSet<>(); + private final List letters ; + + public YourPlayer() { + List tempLetters = new ArrayList<>(); + for (char i = 'a'; i <= 'z'; i++) { + tempLetters.add((char) i); + } + + letters = Collections.unmodifiableList(tempLetters); + } + + void addLetter(Character c) { + usedLetters.add(c); + } + @Override public char GetGuess(List currentClue) { - return 'a'; + if (currentClue == null || currentClue.isEmpty()) { + return ' '; + } + + if (currentClue.stream().noneMatch((c) -> c == '_')) { + return ' '; + } + + if (usedLetters.size() == 26) { + usedLetters.clear(); + } + + Character newCharacter; + + do { + newCharacter = getRandomLetter(); + } + while (usedLetters.contains(newCharacter)); + + usedLetters.add(newCharacter); + + return newCharacter; } + + private char getRandomLetter() { + int randomIndex = random.nextInt(letters.size()); + + return letters.get(randomIndex); + } + } diff --git a/src/test/java/com/hangman/players/YourPlayerTest.java b/src/test/java/com/hangman/players/YourPlayerTest.java index d613f6b..78b1ed7 100644 --- a/src/test/java/com/hangman/players/YourPlayerTest.java +++ b/src/test/java/com/hangman/players/YourPlayerTest.java @@ -2,33 +2,65 @@ import org.junit.Test; import java.util.Arrays; +import java.util.Collections; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static java.lang.String.valueOf; public class YourPlayerTest { + @Test - public void GuessesAWhenThereAreNoSuccessfulCharactersGuessedYet() { + public void alwaysGuessesACharacter() { YourPlayer player = new YourPlayer(); - char guess = player.GetGuess(Arrays.asList('_', '_', '_')); + for (int i = 0; i < 100; i++) { + char guess = player.GetGuess(Arrays.asList('_', '_', '_')); + + assertTrue(Character.isAlphabetic(guess)); + assertFalse(Character.isDigit(guess)); + } + } + + @Test + public void shouldNotGuessTheSameCharacterUntilAllOtherCharacterTried() { + YourPlayer player = new YourPlayer(); + player.addLetter('a'); - assertEquals('a', guess); + for (int i = 0; i < 25; i++) { + char guess = player.GetGuess(Arrays.asList('_', '_', '_')); + assertNotEquals(valueOf(guess), valueOf('a')); + } } @Test - public void GuessesAWhenThereAreSuccessfulCharactersGuessedThatAreNotA() { + public void DoesNotGuessWhenWordIsSolved() { YourPlayer player = new YourPlayer(); - char guess = player.GetGuess(Arrays.asList('m', '_', 'n')); + char guess = player.GetGuess(Arrays.asList('m', 'a', 'n')); - assertEquals('a', guess); + assertEquals(" ", valueOf(guess)); } @Test - public void GuessesAWhenAIsThereAreAsInTheClueAsWell() { + public void ShouldReturnIfPassedNullList() { YourPlayer player = new YourPlayer(); - char guess = player.GetGuess(Arrays.asList('_', 'a', '_')); + char guess = player.GetGuess(null); - assertEquals('a', guess); + assertEquals(" ", valueOf(guess)); } + + @Test + public void ShouldReturnIfPassedEmptyList() { + YourPlayer player = new YourPlayer(); + + char guess = player.GetGuess(Collections.emptyList()); + + assertEquals(" ", valueOf(guess)); + } + + }