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

Player AI Implementation #47

Open
wants to merge 4 commits 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
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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<Character> 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<Character> 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!
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<targetTests>
<param>com.hangman.players.*</param>
</targetTests>
<timestampedReports>false</timestampedReports>
</configuration>
</plugin>
</plugins>
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/com/hangman/players/YourPlayer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
package com.hangman.players;
import com.hangman.Player;
import com.sun.org.apache.xerces.internal.impl.xs.util.LSInputListImpl;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class YourPlayer implements Player {
@Override

private List<Character> vowels = new LinkedList<Character>(Arrays.asList('e', 'a', 'i', 'o', 'u'));
private List<Character> consonants = new LinkedList<Character>(Arrays.asList('r', 's', 't', 'l', 'n', 'd', 'g', 'b', 'c', 'm', 'p', 'f', 'h', 'v', 'w', 'y', 'j', 'k', 'q', 'x', 'z'));

public char GetGuess(List<Character> clue) {
return 'a';
return MakeEducatedGuess(clue);
}

private int EmptySpaces(List<Character> clue) {
int spaces = 0;

for(char i : clue) {
if (i == '_') {
spaces++;
}
}
return spaces;
}

private char MakeEducatedGuess(List<Character> clue) {
Random randomizer = new Random();
int spaces = EmptySpaces(clue);

if (spaces == clue.size()) {
return vowels.remove(0);
} else if (spaces == clue.size() - 1) {
return vowels.remove(0);
} else {
return consonants.remove(randomizer.nextInt(consonants.size() -1));
}
}
}
40 changes: 37 additions & 3 deletions src/test/java/com/hangman/players/YourPlayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,49 @@

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

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

public class YourPlayerTest {
List<Character> vowels = new LinkedList<Character>(Arrays.asList('a', 'e', 'i', 'o', 'u'));

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

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

assertNotEquals('a', guess);
}

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

char firstGuess = player.GetGuess(Arrays.asList('_', '_', '_'));
char secondGuess = player.GetGuess(Arrays.asList('_', '_', firstGuess));

assertNotEquals(firstGuess, secondGuess);
}

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

char firstGuess = player.GetGuess(Arrays.asList('_', '_', '_', '_', '_'));

assertEquals(true, vowels.contains(firstGuess));
}

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

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

assertEquals('a', guess);
assertEquals(false, vowels.contains(nextGuess));
}
}