From 0c1b5c684c3b7b870fda0eddbd27189a8ddba551 Mon Sep 17 00:00:00 2001 From: Erik Paulsen Date: Mon, 13 Apr 2015 11:57:54 -0400 Subject: [PATCH] Crummy start --- .../java/com/hangman/players/YourPlayer.java | 42 ++++++++++++++++--- .../com/hangman/players/YourPlayerTest.java | 40 ++++++++++++------ 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/hangman/players/YourPlayer.java b/src/main/java/com/hangman/players/YourPlayer.java index ddcfbd3..a96475e 100644 --- a/src/main/java/com/hangman/players/YourPlayer.java +++ b/src/main/java/com/hangman/players/YourPlayer.java @@ -2,12 +2,44 @@ import com.hangman.Player; import java.util.Arrays; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; public class YourPlayer implements Player { - @Override - public char GetGuess(List currentClue) { - return 'a'; - } + List vowels = Arrays.asList('a','e','i','o','u'); + List cons = Arrays.asList('s','m'); + List guessed = new ArrayList(); + + @Override + public char GetGuess(List currentClue) { + if (countMatches(currentClue) >= 2) { + for (Character ch : cons) { + if (!guessed.contains(ch)) { + return remember(ch); + } + } + } + else { + for (Character ch : vowels) { + if (!guessed.contains(ch)) { + return remember(ch); + } + } + } + return remember('z'); + } + + private int countMatches(List currentClue) { + int matches = 0; + for (Character ch : currentClue) { + if (!ch.equals('_')) { + matches++; + } + } + return matches; + } + private char remember(char ch) { + guessed.add(ch); + return ch; + } } diff --git a/src/test/java/com/hangman/players/YourPlayerTest.java b/src/test/java/com/hangman/players/YourPlayerTest.java index d613f6b..e6d7a08 100644 --- a/src/test/java/com/hangman/players/YourPlayerTest.java +++ b/src/test/java/com/hangman/players/YourPlayerTest.java @@ -1,34 +1,48 @@ package com.hangman.players; -import org.junit.Test; +import org.junit.*; import java.util.Arrays; +import java.util.List; import static org.junit.Assert.assertEquals; +import com.hangman.Player; public class YourPlayerTest { + Player player; + + @Before + public void setup() { + player = new YourPlayer(); + } @Test public void GuessesAWhenThereAreNoSuccessfulCharactersGuessedYet() { - YourPlayer player = new YourPlayer(); char guess = player.GetGuess(Arrays.asList('_', '_', '_')); assertEquals('a', guess); } - @Test - public void GuessesAWhenThereAreSuccessfulCharactersGuessedThatAreNotA() { - YourPlayer player = new YourPlayer(); +// @Test +// public void GuessesAWhenThereAreSuccessfulCharactersGuessedThatAreNotA() { +// +// char guess = player.GetGuess(Arrays.asList('m', '_', 'n')); +// +// assertEquals('a', guess); +// } - char guess = player.GetGuess(Arrays.asList('m', '_', 'n')); - assertEquals('a', guess); + @Test + public void ShouldTryAllVowelsWhenNoMatch() { + List noMatch = Arrays.asList('_', '_', '_'); + assertEquals('a', player.GetGuess(noMatch)); + assertEquals('e', player.GetGuess(noMatch)); + assertEquals('i', player.GetGuess(noMatch)); + assertEquals('o', player.GetGuess(noMatch)); + assertEquals('u', player.GetGuess(noMatch)); } @Test - public void GuessesAWhenAIsThereAreAsInTheClueAsWell() { - YourPlayer player = new YourPlayer(); - - char guess = player.GetGuess(Arrays.asList('_', 'a', '_')); - - assertEquals('a', guess); + public void ShouldTryConsonentsWhenTwoMatches() { + List noMatch = Arrays.asList('_', 'a', '_', 'o', '_'); + assertEquals('s', player.GetGuess(noMatch)); } }