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

All functions finished ( but not tested and missises just rungame())

parent 312f7d00
Branches
Tags
No related merge requests found
public class Hangman {
/*private String currentWord;
private String guessWord;*/
private static String check(String currentWord, String guessWord, char letter){
StringBuilder newStr = new StringBuilder(currentWord);
for ( int i = 0; i < guessWord.length(); i++){
if (guessWord.charAt(i) == letter){
private String secretWord;
private String currWord;
private int attemp;
private int failedAttempt;
private boolean lastTry;
private Hangman(String word){
this.secretWord = word;
this.currWord = "_".repeat(word.length());
this.attemp = 0;
this.failedAttempt = 0;
this.lastTry = true;
}
public static Hangman withSecretWord(String word){
return new Hangman(word);
}
private String currentWord(){
return this.currWord;
}
public void printCurrentWord(){
System.out.println("Current Word:" + this.currentWord());
}
private int attemps(){
return this.attemp;
}
private String secretWord(){
return this.secretWord;
}
private boolean lastTry(){
return this.lastTry;
}
public int failedAttempts(){
return this.failedAttempt;
}
public void printGallow(){
printHang(this.failedAttempts());
}
public void printGallowIfError(){
if (!this.lastTry()){
printHang(this.failedAttempts());
}
}
private String check(char letter){
StringBuilder newStr = new StringBuilder(this.currentWord());
for ( int i = 0; i < this.secretWord().length(); i++){
if (this.secretWord().charAt(i) == letter){
newStr.setCharAt(i, letter);
}
}
return newStr.toString();
}
public Hangman tryWith(char letter){
Hangman newH = this;
newH.attemp++;
String checked = check(letter);
if (newH.currentWord().equals(checked)){
newH.failedAttempt++;
newH.lastTry = false;
return newH;
}
newH.lastTry = true;
newH.currWord = checked;
return newH;
}
public boolean isFinished(){
if (this.failedAttempts() == 9 || !this.currentWord().contains("_")){
return true;
}
return false;
}
public boolean hasWon(){
if (this.failedAttempts()==9){
return false;
}
return true;
}
private static void printHang(int i){
String i0 = "";
String i1 = "----------\n|/\n|\n|\n|\n|\n|\n|";
......@@ -26,7 +97,10 @@ public class Hangman {
System.out.println(drawHangMan[i]);
}
public static void main(String[] args){
printHang(2);
Hangman h = Hangman.withSecretWord("mystère");
String currentWord = h.currentWord();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment