add: Difficulty Mode

This commit is contained in:
2025-10-08 16:11:40 +02:00
parent 111dcad559
commit 94e7394494
4 changed files with 69 additions and 21 deletions

View File

@@ -1,15 +1,62 @@
package Controllers; package Controllers;
import java.util.*;
import Models.Word; import Models.Word;
import Models.Difficulty;
public class Game { public class Game {
private Word word; private Word word;
private int errors; private int errors;
private List<Word> allwords;
private Difficulty difficulty;
private final int maxErrors = 6; private final int maxErrors = 6;
public Game(Word word) { /* Hangmam Game */
public Game(Word word ,List<Word> allWord) {
this.word = word; this.word = word;
this.errors = 0; this.errors = 0;
switch (difficulty) {
case EASY:
this.word = selectWord(allWord, 0 , 7 );
break;
case MEDIUM:
this.word = selectWord(allWord, 0, Integer.MAX_VALUE);
break;
case HARD:
this.allwords = selectTwoWords(allWord);
break;
}
}
/* Selected Word with conditions */
private Word selectWord(List<Word> list, int min, int max) {
for (Word word : list) {
int length = word.getWord().length();
if (length >= min && length <= max) {
return word;
}
}
return list.get(0);
}
/* Selected word level Difficult */
private List<Word> selectTwoWords(List<Word> list) {
return list.subList(0, Math.min(2, list.size()));
}
public boolean playLetter(char caractere) {
Boolean correct;
if(difficulty == Difficulty.HARD){
correct = allwords.get(0).VerifyLetter(caractere) || word.get(1).VerifyLetter(caractere);
} else{
correct = word.VerifyLetter(caractere);
}
if (!correct) errors++;
return correct;
} }
/* Verify error number */ /* Verify error number */
@@ -21,24 +68,13 @@ public class Game {
return this.maxErrors; return this.maxErrors;
} }
/* Vue call method VerifyLetter, check status letter false/True */
public boolean playLetter(char c) {
boolean correct = word.VerifyLetter(c);
if (!correct) {
errors++;
}
return correct;
}
/* All letter is completed : Won */ /* All letter is completed : Won */
public boolean isWon() { public boolean isWon() {
return word.IsComplete(); return word.IsComplet();
} }
/* Lost if maxErrors is greater than or egal to errors */ /* Lost if maxErrors is greater than or egal to errors */
public boolean isLost() { public boolean isLost() {
return errors >= maxErrors; return errors >= maxErrors;
} }
} }

View File

@@ -0,0 +1,5 @@
package Models;
public enum Difficulty {
EASY, MEDIUM, HARD
}

View File

@@ -1,4 +1,4 @@
package fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models; package Models;
public class Letter{ public class Letter{
private char letter; private char letter;

View File

@@ -1,11 +1,11 @@
package fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models; package Models;
import java.util.*; import java.util.*;
public class Word { public class Word {
private final String word; private final String word;
private Letter[] tabLetter; private Letter[] tabLetter;
private String c; private String character;
private Word(String word){ private Word(String word){
this.word = word; this.word = word;
@@ -14,11 +14,11 @@ public class Word {
} }
} }
/* Lettre dans le mot */ /* Lettre dans le mot */
private boolean VerifyLetter(char c){ public boolean VerifyLetter(char character){
boolean return_bool = false; boolean return_bool = false;
for(Letter letter : this.tabLetter){ for(Letter letter : this.tabLetter){
if(!letter.getStatus()){ if(!letter.getStatus()){
if(letter.isGood(c)){ if(letter.isGood(character)){
return_bool = true; return_bool = true;
} }
} }
@@ -27,7 +27,7 @@ public class Word {
} }
/* Le mot a été deviné */ /* Le mot a été deviné */
private boolean IsComplet(){ public boolean IsComplet(){
for(Letter letter : this.tabLetter){ for(Letter letter : this.tabLetter){
if(!letter.getStatus()){ if(!letter.getStatus()){
return false; return false;
@@ -36,5 +36,12 @@ public class Word {
} }
return true; return true;
} }
public String getWord() {
return word;
}
public Word get(int i) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'get'");
}
} }