Guest User

Untitled

a guest
Nov 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1.  
  2. public class HangmanLogic {
  3.  
  4.     private String word;
  5.     private String guessedLetters;
  6.     private int numberOfFaults;
  7.  
  8.     public HangmanLogic(String word) {
  9.         this.word = word.toUpperCase();
  10.         this.guessedLetters = "";
  11.         this.numberOfFaults = 0;
  12.     }
  13.  
  14.     public int numberOfFaults() {
  15.         return this.numberOfFaults;
  16.     }
  17.  
  18.     public String guessedLetters() {
  19.         return this.guessedLetters;
  20.     }
  21.  
  22.     public int losingFaultAmount() {
  23.         return 12;
  24.     }
  25.  
  26.     public void guessLetter(String letter) {
  27.         if (this.word.contains(letter)){
  28.            
  29.         } else if (this.guessedLetters.contains(letter)){
  30.            
  31.         } else{
  32.             this.guessedLetters += letter;
  33.             this.numberOfFaults ++;
  34.         }
  35.     }
  36.  
  37.     public String hiddenWord() {
  38.         String hiddenWord = this.word;
  39.         String currentChar = "";
  40.        
  41.         for(int k = 0; k < this.word.length() ; k++ ){
  42.             hiddenWord = hiddenWord.replace(hiddenWord.charAt(k), '_');
  43.            
  44.            
  45.         }
  46.         for (int i = 0; i < this.word.length() ; i++){
  47.             currentChar = currentChar.replace(currentChar.charAt(i), this.guessedLetters.charAt(i));
  48.             // ligne du haut place la première lettre de la liste devinée dans currentchar
  49.             if (this.word.contains(currentChar)){
  50.                for (int j = 0; j < this.word.length() ; j++){
  51.                    if (this.word.charAt(j) == this.guessedLetters.charAt(i)){
  52.                        hiddenWord = hiddenWord.replace(hiddenWord.charAt(j), this.guessedLetters.charAt(i));
  53.                    }
  54.                }
  55.             }
  56.  
  57.            
  58.         }
  59.        
  60.        
  61.         return hiddenWord;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment