Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

paul sideleau first iteration #37

Open
wants to merge 1 commit into
base: gradle
Choose a base branch
from
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
51 changes: 47 additions & 4 deletions src/main/java/com/hangman/players/YourPlayer.java
Original file line number Diff line number Diff line change
@@ -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<Character> usedLetters = new HashSet<>();
private final List<Character> letters ;

public YourPlayer() {
List<Character> 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<Character> 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);
}

}
50 changes: 41 additions & 9 deletions src/test/java/com/hangman/players/YourPlayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


}