Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMuttalip İpek
918 PointsI need finished Hangman game
Could anyone add a finished Hangman game ? so I can check and comprehend better.
2 Answers
jacobproffer
24,604 PointsHey Muttalip,
You should be able to download a complete version via the download section of the course.
But here is mine:
Hangman class:
public class Hangman {
public static void main(String[] args) {
if(args.length == 0) {
System.out.println("Please enter a word");
System.exit(0);
}
Game game = new Game(args[0]);
Prompter prompter = new Prompter(game);
prompter.play();
}
}
Game class:
public class Game {
public static final int MAX_MISSES = 7;
private String mAnswer;
private String mHits;
private String mMisses;
public Game(String answer) {
mAnswer = answer;
mHits = "";
mMisses = "";
}
private char validateGuess(char letter) {
if(! Character.isLetter(letter)) {
throw new IllegalArgumentException("A letter is required");
}
letter = Character.toLowerCase(letter);
if(mMisses.indexOf(letter) >= 0 || mHits.indexOf(letter) >= 0) {
throw new IllegalArgumentException(letter + " has already been guessed");
}
return letter;
}
public boolean applyGuess(String letters) {
if(letters.length() == 0) {
throw new IllegalArgumentException("No letter found");
}
return applyGuess(letters.charAt(0));
}
public boolean applyGuess(char letter) {
letter = validateGuess(letter);
boolean isHit = mAnswer.indexOf(letter) >= 0;
if(isHit) {
mHits += letter;
} else {
mMisses += letter;
}
return isHit;
}
public String getCurrentProgress() {
String progress = "";
for(char letter : mAnswer.toCharArray()) {
char display = '-';
if(mHits.indexOf(letter) >= 0) {
display = letter;
}
progress += display;
}
return progress;
}
public int getRemainingTries() {
return MAX_MISSES - mMisses.length();
}
public String getAnswer() {
return mAnswer;
}
public boolean isSolved() {
return getCurrentProgress().indexOf('-') == -1;
}
}
Prompter class:
import java.io.Console;
public class Prompter {
private Game mGame;
public Prompter(Game game) {
mGame = game;
}
public void play() {
while(mGame.getRemainingTries() > 0 && !mGame.isSolved()) {
displayProgress();
promptForGuess();
}
if(mGame.isSolved()) {
System.out.printf("Congratulations, you won with %d tries remaining.\n",
mGame.getRemainingTries());
} else {
System.out.printf("Bummer the word was %s. :(\n",
mGame.getAnswer());
}
}
public boolean promptForGuess() {
Console console = System.console();
boolean isHit = false;
boolean isValidGuess = false;
while(! isValidGuess) {
String guessAsString = console.readLine("Enter a letter: ");
try {
isHit = mGame.applyGuess(guessAsString);
isValidGuess = true;
} catch (IllegalArgumentException iae) {
console.printf("%s. Please try again.\n", iae.getMessage());
}
}
return isHit;
}
public void displayProgress() {
System.out.printf("You have %d tries left to solve: %s\n",
mGame.getRemainingTries(),
mGame.getCurrentProgress());
}
}
Hope this helps.
Best,
Jacob
Craig Dennis
Treehouse TeacherYou can launch a new Workspace on the video, and then choose File > Download Workspace from the editor window.
Hope that helps!
Muttalip İpek
918 PointsMuttalip İpek
918 PointsHi Jacob, Firstly, I'm very thankful for your help. and how can I download a complete version of courses ?
jacobproffer
24,604 Pointsjacobproffer
24,604 PointsHey Muttalip,
For some reason, I'm not seeing a download link to the project files for the Hangman game (Neither in the Creating or Delivering section).
I'm glancing through the Splitting Strings video of the Java Data Structures course at the moment. Within the downloads section, I am able to download project files, the particular video that I'm on at the moment, and a transcript. Typically this is offered for most courses on Treehouse. I'm not sure why this is not available for the Hangman game.
I'd suggest submitting a request to have this course updated with the project files available for download.
However, for the time being, the code I provided should be close to, if not spot on, of what was covered throughout the MVP sections.
Let me know how it goes.
Best,
Jacob
Muttalip İpek
918 PointsMuttalip İpek
918 PointsThank you so much again for your information and the code. it is same as in the courses. I think it will help for finding my errors.