This repository has been archived on 2024-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
Labo-S1/src/lab6/WordManager.java

114 lines
3.3 KiB
Java
Raw Normal View History

2021-11-19 08:07:14 +01:00
package lab6;
import java.text.Normalizer;
2021-11-20 15:04:48 +01:00
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
2021-11-19 08:07:14 +01:00
public class WordManager {
private String secretWord = "";
public String userWord = "";
void askSecretWord(){
//System.out.print("Enter your secret word: ");
2021-11-20 10:59:16 +01:00
//secretWord = Input.readString();
2021-11-20 17:40:45 +01:00
//secretWord = Dialogs.getHiddenString("Enter your secret word: ");
secretWord = randomWord();
2021-11-19 08:07:14 +01:00
secretWord = stripAccents(secretWord);
secretWord = secretWord.toLowerCase();
userWord = "";
for (int i = 0; i < secretWord.length(); i++) {
userWord += '*';
}
}
boolean checkLetter(char c){
boolean letterPresent = false;
for (int i = 0; i < secretWord.length(); i++) {
if(c == secretWord.charAt(i)){
letterPresent = true;
userWord = userWord.substring(0, i) + c + userWord.substring(i+1);
}
}
return letterPresent;
}
boolean isWordComplete(){
boolean complete = false;
if (secretWord.equals(userWord)) {
complete = true;
//System.out.println("Victory !!");
Dialogs.displayMessage("Victory !!");
}
return complete;
}
public static String stripAccents(String s){
s = Normalizer.normalize(s, Normalizer.Form.NFD);
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
return s;
}
2021-11-20 15:04:48 +01:00
private String randomWord() {
2021-11-20 17:40:45 +01:00
String askLevel = "";
askLevel += "Please choose your level \n";
askLevel += "('e' for easy) \n";
askLevel += "('m' for medium) \n";
askLevel += "('d' for difficult)";
char level = Dialogs.getChar(askLevel);
int nbrLettreMin = 4;
int nbrLettreMax = 12;
2021-11-20 15:04:48 +01:00
String s = "";
2021-11-20 17:40:45 +01:00
String[] word = loadList("C:/Users/remi/OneDrive/Documents/Cours/05-HEVS/S1fb/informatic/labo/vscode/Labo/src/lab6/french_common_words.csv");
switch (level) {
case 'e':
nbrLettreMin = 4;
nbrLettreMax = 6;
break;
case 'm':
nbrLettreMin = 7;
nbrLettreMax = 9;
break;
case 'd':
nbrLettreMin = 10;
nbrLettreMax = 12;
break;
default:
break;
}
int lg;
do {
int nbr = (int)(Math.random()*600);
s = word[nbr];
lg = s.length();
} while (lg < nbrLettreMin || lg > nbrLettreMax);
System.out.println(s);
2021-11-20 15:04:48 +01:00
return s;
}
private String[] loadList(String filePath) {
String[] wordList;
try {
BufferedReader bf = new BufferedReader(new FileReader(filePath));
ArrayList < String > al = new ArrayList < String > ();
while (bf.ready()) {
String[] c = bf.readLine().split(";");
al.add(c[0]);
}
wordList = al.stream().toArray(String[]::new);
System.out.println("[Dictionary loaded with " + wordList.length + " words]");
bf.close();
return wordList;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
2021-11-19 08:07:14 +01:00
}