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

smarter hangman player #32

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
2 changes: 1 addition & 1 deletion src/main/java/com/hangman/HangmanRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void main(String [] args) throws Exception {

HangmanRunner runner = new HangmanRunner(game,
display,
new YourPlayer(),
new YourPlayer(WordList.words),
new SleepTicker());
runner.run();
}
Expand Down
62 changes: 60 additions & 2 deletions src/main/java/com/hangman/players/YourPlayer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,71 @@
package com.hangman.players;
import com.hangman.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;


public class YourPlayer implements Player {

private final List<Character> charsNotGuessedYet = new ArrayList<>();
private final List<Character> charsAlreadyGuessed = new ArrayList<>();

private final List<String> wordsList = new ArrayList<>();


public YourPlayer(String[] wordsList) {
// initialize non-guessed characters
for (char c = 'a'; c <= 'z'; ++c) {
this.charsNotGuessedYet.add(c);
}

// copy the word list
this.wordsList.addAll(Arrays.asList(wordsList));
}

@Override
public char GetGuess(List<Character> currentClue) {
return 'a';
Character guess = null;

StringBuilder pattern = new StringBuilder(currentClue.size());
int nextYetUnknownCharacterIndex = 0;
int i = 0;
for (Character c : currentClue) {
if (c.equals('_')) {
nextYetUnknownCharacterIndex = i;
pattern.append(".");
} else {
// known character, act like if it was already guessed
this.charsNotGuessedYet.remove(c);
this.charsAlreadyGuessed.add(c);
pattern.append(c);
}
++i;
}
Pattern wordPattern = Pattern.compile(pattern.toString());

for (String word : this.wordsList) {
if ((word.length() == currentClue.size()) && wordPattern.matcher(word).matches()) {
guess = word.charAt(nextYetUnknownCharacterIndex);
if (!this.charsAlreadyGuessed.contains(guess)) {
break;
}
}
}

if (guess == null) {
// no word matching, just get the next not-yet-guessed character
guess = this.charsNotGuessedYet.isEmpty() ?
'a' :
this.charsNotGuessedYet.get(0);
}

// store char as already guessed
this.charsNotGuessedYet.remove(guess);
this.charsAlreadyGuessed.add(guess);
return guess;
}

}
45 changes: 34 additions & 11 deletions src/test/java/com/hangman/players/YourPlayerTest.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
package com.hangman.players;

import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class YourPlayerTest {

private static final String[] wordsForTest = {
"abc",
"acc",
"something_longer",
"somethingshorter"
};


@Test
public void GuessesAWhenThereAreNoSuccessfulCharactersGuessedYet() {
YourPlayer player = new YourPlayer();
public void GuessesWhenThereAreNoSuccessfulCharactersGuessedYet() {
YourPlayer player = new YourPlayer(wordsForTest);

char guess = player.GetGuess(Arrays.asList('_', '_', '_'));
// last character of the first 3 chars long word
char shortGuess = player.GetGuess(Arrays.asList('_', '_', '_'));
assertEquals('c', shortGuess);

assertEquals('a', guess);
// last character of the first 16 chars long word
char longGuess = player.GetGuess(Arrays.asList('_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'));
assertEquals('r', longGuess);
}

@Test
public void GuessesAWhenThereAreSuccessfulCharactersGuessedThatAreNotA() {
YourPlayer player = new YourPlayer();
public void GuessesWhenThereAreSuccessfulCharactersGuessedAndThereIsNoMatchingKnownWord() {
YourPlayer player = new YourPlayer(wordsForTest);

char guess = player.GetGuess(Arrays.asList('m', '_', 'n'));
char guess1 = player.GetGuess(Arrays.asList('m', '_', 'n'));
assertEquals('a', guess1);
}

assertEquals('a', guess);
@Test
public void GuessesWhenThereAreSuccessfulCharactersGuessedAndThereIsAMatchingKnownWord() {
YourPlayer player = new YourPlayer(wordsForTest);

char guess2 = player.GetGuess(Arrays.asList('a', '_', 'b'));
assertEquals('c', guess2);
}

@Test
public void GuessesAWhenAIsThereAreAsInTheClueAsWell() {
YourPlayer player = new YourPlayer();
public void GuessesFirstCharacterOfACompleteClue() {
YourPlayer player = new YourPlayer(wordsForTest);

char guess = player.GetGuess(Arrays.asList('_', 'a', '_'));
char guess = player.GetGuess(Arrays.asList('a', 'b', 'c'));

assertEquals('a', guess);
}

}