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

Saad and Jacob's first commit #53

Open
wants to merge 1 commit into
base: master
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
17 changes: 16 additions & 1 deletion src/main/java/com/hangman/players/YourPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@

import com.hangman.Player;

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

public class YourPlayer implements Player {
private String chars = "etaoinshrdlcubmwfgypvkjxqz";
private int nextIndex = 0;

@Override
public char getGuess(List<Character> currentClue) {
return 'a';
List<Character> toExclude = currentClue.stream()
.filter(c -> c != null)
.collect(toList());

return getNextChar(toExclude);
}

public char getNextChar(List<Character> excludes) {
char c = chars.charAt(nextIndex++);
return excludes.contains(c) ? getNextChar(excludes) : c;
}
}
28 changes: 12 additions & 16 deletions src/test/java/com/hangman/players/YourPlayerTest.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
package com.hangman.players;

import org.junit.Test;

import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import java.util.List;

import static org.junit.Assert.assertTrue;

public class YourPlayerTest {
@Test
public void guessesAWhenThereAreNoSuccessfulCharactersGuessedYet() {
public void makesSuccessfulGuess() {
YourPlayer player = new YourPlayer();

char guess = player.getGuess(Arrays.asList(null, null, null));

assertEquals('a', guess);
assertTrue(Character.valueOf(guess) != null);
}

@Test
public void guessesAWhenThereAreSuccessfulCharactersGuessedThatAreNotA() {
public void guessesRandomLetterWhenAIsThereAreAsInTheClueAsWell() {
YourPlayer player = new YourPlayer();
List<Character> clues = Arrays.asList('x', 'b', 'g', 'f', 'i');

char guess = player.getGuess(Arrays.asList('m', null, 'n'));

assertEquals('a', guess);
}

@Test
public void guessesAWhenAIsThereAreAsInTheClueAsWell() {
YourPlayer player = new YourPlayer();

char guess = player.getGuess(Arrays.asList(null, 'a', null));

assertEquals('a', guess);
clues.stream().forEach(c -> {
char guess = player.getGuess(Arrays.asList(null, null, c));
assertTrue(guess != c);
});
}
}