forked from menault/TD3_DEV51_Qualite_Algo
		
	
		
			
	
	
		
			51 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
		
		
			
		
	
	
			51 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
|  | import java.util.*;
 | ||
|  | 
 | ||
|  | public class Letter {
 | ||
|  |     public String wordToFind;
 | ||
|  |     public char selectedLetter;
 | ||
|  |     public StringBuilder wordBuilding;
 | ||
|  |     public StringBuilder incorrectLetters = new StringBuilder();
 | ||
|  | 
 | ||
|  |     // Constructeur
 | ||
|  |     public Letter(String mot) {
 | ||
|  |         this.wordToFind = mot;
 | ||
|  |         this.wordBuilding = new StringBuilder();
 | ||
|  |         // Initialise le mot caché avec des underscores
 | ||
|  |         for (int i = 0; i < mot.length(); i++) {
 | ||
|  |             if (mot.charAt(i) == ' ') {
 | ||
|  |                 this.wordBuilding.append(' '); // garde les espaces
 | ||
|  |             } else {
 | ||
|  |                 this.wordBuilding.append('_');
 | ||
|  |             }
 | ||
|  |         }
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     // Setter pour la lettre sélectionnée
 | ||
|  |     public void setLetter(char currentLetter) {
 | ||
|  |         this.selectedLetter = currentLetter;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     // Vérifie si la lettre est dans le mot
 | ||
|  |     public boolean letterInWord() {
 | ||
|  |         boolean found = false;
 | ||
|  | 
 | ||
|  |         for (int i = 0; i < this.wordToFind.length(); i++) {
 | ||
|  |             if (this.selectedLetter == this.wordToFind.charAt(i)) {
 | ||
|  |                 // Remplace l'underscore par la lettre correcte
 | ||
|  |                 this.wordBuilding.setCharAt(i, this.selectedLetter);
 | ||
|  |                 found = true;
 | ||
|  |             }
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         // Si la lettre n'est pas trouvée, on l'ajoute aux incorrectes
 | ||
|  |         if (!found) {
 | ||
|  |             if (this.incorrectLetters.length() > 0) {
 | ||
|  |                 this.incorrectLetters.append(" ");
 | ||
|  |             }
 | ||
|  |             this.incorrectLetters.append(this.selectedLetter);
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         return found;
 | ||
|  |     }
 | ||
|  | }
 |