diff --git a/README.md b/README.md new file mode 100644 index 0000000..31f30f3 --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +# Hangman in Java - for code sparring. + +This program tries to solve a game of hangman. It doesn't do a very good job - it's your job to fork it and create a new solution for it. + +## How To + +Fork this repo then clone it. To run the tests: + +```bash +mvn test +``` + +Tough stuff. To run the program: + +```bash +mvn package +java -jar target/Hangman-1.jar +``` + +When you run the program you'll see something like this: + +```bash +Erics-MacBook-Pro:HangmanJava paytonrules$ java -jar target/Hangman-1.jar +Current Clue Is _ _ _ _ _ +Current Clue Is _ _ _ _ _ +Current Clue Is _ _ _ _ _ +Current Clue Is _ _ _ _ _ +Current Clue Is _ _ _ _ _ +Current Clue Is _ _ _ _ _ +Current Clue Is _ _ _ _ _ +Current Clue Is _ _ _ _ _ +Current Clue Is _ _ _ _ _ +com.hangman.Game Over +``` + +What this means is is that the clue length was five characters, and your player successfully guessed....nothing. To see why let's take a look at YourPlayer.java. + +```java +public class YourPlayer implements Player { + @Override + public char GetGuess(List clue) { + return 'a'; + } +} +``` + +That's right `YourPlayer` - regardless of the clue - will guess the letter `a`. If it got `a` right, it guessed `a`. Wrong? `a`. It's a pretty stupid player. + +So to complete this exercise you will need to delete return 'a' - and instead return better guesses. How? Well look the parameter: + +`List clue`. The clue is exactly what you're seeing on screen. It is an array of characters where the '_' character means that you haven't gotten that location correct yet. + +This stumps people so let's go through an example. Let's assume the secret word is "aaron". The first time GetGuess is called it will get: + +`['_', '_', '_', '_', '_']` + +`YourPlayer` will guess 'a'. The next time GuetGuess is called it will look like: + +`['a', 'a', '_', '_', '_']` + +Naturally `YourPlayer` will guess 'a' again, because it is stupid. To complete this exercise you need to make `YourPlayer` good at hangman. + +## Tips + +* Maybe keep track of what you've guessed. +* Vowels are good. +* Your testing WILL BE GRADED! diff --git a/pom.xml b/pom.xml index 117652a..25d43c1 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,7 @@ com.hangman.players.* + false diff --git a/src/main/java/com/hangman/players/YourPlayer.java b/src/main/java/com/hangman/players/YourPlayer.java index f537169..9875883 100644 --- a/src/main/java/com/hangman/players/YourPlayer.java +++ b/src/main/java/com/hangman/players/YourPlayer.java @@ -6,8 +6,42 @@ import java.util.List; public class YourPlayer implements Player { + private char[] ALL = { + 'e', + 't', + 'a', + 'o', + 'i', + 'n', + 's', + 'r', + 'h', + 'd', + 'l', + 'u', + 'c', + 'm', + 'f', + 'y', + 'w', + 'g', + 'p', + 'b', + 'v', + 'k', + 'x', + 'q', + 'j', + 'z' + }; + @Override public char GetGuess(List clue) { + for(char c : ALL){ + if(!clue.contains(c)){ + return c; + } + } return 'a'; } } diff --git a/src/test/java/com/hangman/players/YourPlayerTest.java b/src/test/java/com/hangman/players/YourPlayerTest.java index bc92a3b..52712c9 100644 --- a/src/test/java/com/hangman/players/YourPlayerTest.java +++ b/src/test/java/com/hangman/players/YourPlayerTest.java @@ -1,16 +1,47 @@ package com.hangman.players; import org.junit.Test; +import java.util.ArrayList; import java.util.Arrays; -import static org.junit.Assert.assertEquals; +import java.util.List; +import static org.junit.Assert.*; public class YourPlayerTest { + YourPlayer player = new YourPlayer(); + + @Test + public void DoesNotGuessAIfExists() { + char guess = player.GetGuess(Arrays.asList('_', 'a')); + + assertNotEquals('a', guess); + } + @Test - public void AlwaysGuessA() { - YourPlayer player = new YourPlayer(); + public void DoesNotGuessCharactersThatExist() { + for(char c = 'a'; c <= 'z'; c++){ + List clue = allLetters(); + clue.remove((Character) c); + clue.add('_'); + + char guess = player.GetGuess(clue); - char guess = player.GetGuess(Arrays.asList('a', 'b', 'c')); + assertEquals("Expected: " + c + " got: " + guess, c, guess); + } + } + + @Test + public void guessesTWhenEExists(){ + char guess = player.GetGuess(Arrays.asList('_', 'e')); + + assertEquals('t', guess); + } - assertEquals('a', guess); + //case + private static List allLetters(){ + List result = new ArrayList(); + for(char c = 'a'; c <= 'z'; c++){ + result.add(c); + } + return result; } }