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

initial commit : Dominic & Paul #58

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
91 changes: 89 additions & 2 deletions src/main/java/com/hangman/players/YourPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,98 @@

import com.hangman.Player;

import java.util.List;
import java.util.*;

public class YourPlayer implements Player {
private List<Character> previousGuesses = new ArrayList<>();

private char[] alpha = "abcdefghijklmnopqrstuvwxyz".toCharArray();
private char[] vowels = "aeiouy".toCharArray();
private char[] charByFrequency = "etaoinshrdlcumwfgypbvkjxqz".toCharArray();

private Map<Integer, char[]> clueLengthToCharacters = new HashMap<>();

@Override
public char getGuess(List<Character> currentClue) {
return 'a';
setClueCharacter();
char guess;

do{
guess = getGuessExecute(currentClue);
}while(previousGuesses.contains(guess));

previousGuesses.add(guess);

return guess;
}

private char getGuessExecute(List<Character> currentClue){
char guess;

if(currentClue.contains('q') && !previousGuesses.contains('u')) {
guess = 'u';
}else if(currentClue.contains('t') && !previousGuesses.contains('h')){
guess = 'h';
}else {
guess = getCleanCharByFrequency(currentClue).get(0);
}

return guess;
}

private List<Character> getCleanCharByFrequency(List<Character> currentClue){
List<Character> cleanArray = new ArrayList<>();
char[] characterList = getCharacterList(currentClue);
for(char letter : characterList){
if(!previousGuesses.contains(letter)){
cleanArray.add(letter);
}
}

return cleanArray;
}

private char[] getCharacterList(List<Character> currentClue){
if(currentClue.size() > 20){
return charByFrequency;
}else{
char[] charList = clueLengthToCharacters.get(currentClue.size());
if(previousGuesses.size() >= charList.length){
return charByFrequency;
}else{
return charList;
}
}
}

private void setClueCharacter(){
clueLengthToCharacters.put(1 ,"ai".toCharArray());
clueLengthToCharacters.put(2 ,"aoumbh".toCharArray());
clueLengthToCharacters.put(3 ,"aeouyhbck".toCharArray());
clueLengthToCharacters.put(4 ,"aeouysbf".toCharArray());
clueLengthToCharacters.put(5 ,"seaouyh".toCharArray());
clueLengthToCharacters.put(6 ,"eiusY".toCharArray());
clueLengthToCharacters.put(7 ,"iaus".toCharArray());
clueLengthToCharacters.put(8 ,"iaou".toCharArray());
clueLengthToCharacters.put(9 ,"iaou".toCharArray());
clueLengthToCharacters.put(10 ,"ioau".toCharArray());
clueLengthToCharacters.put(11 ,"ioad".toCharArray());
clueLengthToCharacters.put(12 ,"ioaf".toCharArray());
clueLengthToCharacters.put(13 ,"ieoa".toCharArray());
clueLengthToCharacters.put(14 ,"ieo".toCharArray());
clueLengthToCharacters.put(15 ,"iea".toCharArray());
clueLengthToCharacters.put(16 ,"ieh".toCharArray());
clueLengthToCharacters.put(17 ,"ier".toCharArray());
clueLengthToCharacters.put(18 ,"iea".toCharArray());
clueLengthToCharacters.put(19 ,"iea".toCharArray());
clueLengthToCharacters.put(20 ,"ie".toCharArray());
}

public void setPreviousGuesses(Character... guesses){
previousGuesses.addAll(Arrays.asList(guesses));
}

public int getGuessCount(){
return previousGuesses.size();
}
}
95 changes: 76 additions & 19 deletions src/test/java/com/hangman/players/YourPlayerTest.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,91 @@
package com.hangman.players;

import org.junit.Test;
import java.util.Arrays;

import java.util.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

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

char guess = player.getGuess(Arrays.asList(null, null, null));
@Test
public void should_increment_guess_count(){
YourPlayer player = new YourPlayer();
player.getGuess(Arrays.asList(null, null, null));

assertEquals(1, player.getGuessCount());
}

@Test
public void should_after_first_two_guesses_guess_a(){
YourPlayer player = new YourPlayer();
player.setPreviousGuesses('e','t');
char guess = player.getGuess(Arrays.asList(null, null, null));

assertEquals('a', guess);
}

@Test
public void should_not_guess_the_same_thing_twice(){
YourPlayer player = new YourPlayer();
Set<Character> guesses = new HashSet<>();

for(int i=1;i<27;i++){
guesses.add(player.getGuess(Arrays.asList(null, null, null)));
}

assertEquals(26, guesses.size());
}

@Test
public void should_guess_u_if_clue_has_q(){
YourPlayer player = new YourPlayer();
char guess = player.getGuess(Arrays.asList('q', null, null));

assertEquals('u', guess);
}

@Test
public void should_not_guess_u_if_clue_has_q_but_already_guess_u(){
YourPlayer player = new YourPlayer();
player.setPreviousGuesses('u');
char guess = player.getGuess(Arrays.asList('q', null, null));

assertNotEquals('u', guess);
}

@Test
public void should_guess_h_if_clue_has_t(){
YourPlayer player = new YourPlayer();
char guess = player.getGuess(Arrays.asList('t', null, null));

assertEquals('a', guess);
}
assertEquals('h', guess);
}

@Test
public void guessesAWhenThereAreSuccessfulCharactersGuessedThatAreNotA() {
YourPlayer player = new YourPlayer();
@Test
public void should_not_guess_what_has_already_been_guessed(){
YourPlayer player = new YourPlayer();
player.setPreviousGuesses('t');
char guess = player.getGuess(Arrays.asList('e', null, null));

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

assertEquals('a', guess);
}
@Test
public void if_clue_length_is_2_guess_a(){
YourPlayer player = new YourPlayer();
char guess = player.getGuess(Arrays.asList(null, null));

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

char guess = player.getGuess(Arrays.asList(null, 'a', null));
@Test
public void if_clue_length_is_7_guess_i(){
YourPlayer player = new YourPlayer();
char guess = player.getGuess(Arrays.asList(null, null, null, null, null, null, null));

assertEquals('a', guess);
}
assertEquals('i', guess);
}
}