Skip to content
Snippets Groups Projects
Commit e30233e7 authored by fefe's avatar fefe
Browse files

Forgot to push the last version of hangman

parent a31926da
Branches
Tags
No related merge requests found
No preview for this file type
import java.util.Scanner;
public class Hangman {
private String secretWord;
private String currWord;
......@@ -26,7 +27,7 @@ public class Hangman {
private int attemps(){
return this.attemp;
}
private String secretWord(){
public String secretWord(){
return this.secretWord;
}
private boolean lastTry(){
......@@ -39,6 +40,7 @@ public class Hangman {
printHang(this.failedAttempts());
}
public void printGallowIfError(){
System.out.println("Error(s): " + this.failedAttempts());
if (!this.lastTry()){
printHang(this.failedAttempts());
}
......@@ -82,6 +84,18 @@ public class Hangman {
return true;
}
private static char askLetter(){
System.out.println("Enter a letter :");
Scanner myObj = new Scanner(System.in);
String str = myObj.nextLine();
if (str.length() != 1){
System.out.println(" Just one letter please : ");
str = myObj.nextLine();
}
return str.charAt(0);
}
private static void printHang(int i){
String i0 = "";
String i1 = "----------\n|/\n|\n|\n|\n|\n|\n|";
......@@ -96,11 +110,26 @@ public class Hangman {
String [] drawHangMan = {i0, i1, i2, i3, i4, i5, i6, i7, i8, i9};
System.out.println(drawHangMan[i]);
}
public static void main(String[] args){
Hangman h = Hangman.withSecretWord("mystère");
String currentWord = h.currentWord();
public static void runGame(String secretWord){
Hangman h = Hangman.withSecretWord(secretWord);
while(!h.isFinished()){
System.out.println("-----------------");
char askedChar = Hangman.askLetter();
h = h.tryWith(askedChar);
h.printCurrentWord();
h.printGallowIfError();
}
if(h.hasWon()){
System.out.println("Goal Achieved !");
}
else{
System.out.println("That's an horrible fail, you had to find: " + h.secretWord() );
}
}
public static void main(String[] args){
Hangman.runGame("helloworld");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment