Files
TD3_DEV51_dick_amary/src/Partie.java

136 lines
3.9 KiB
Java
Raw Normal View History

2025-10-08 17:19:31 +02:00
import java.util.Random;
2025-10-08 10:26:19 +02:00
/**
* La classe <code>Partie</code>
*
2025-10-08 17:19:31 +02:00
* @version 0.2
2025-10-08 16:07:03 +02:00
* @author Aurélien
* Date : 08-10-25
2025-10-08 10:26:19 +02:00
* Licence :
*/
public class Partie {
2025-10-08 16:07:03 +02:00
//Contantes
private static final byte REMAININGTRY = 11 ;
private static final byte CARACTERCODESHIFT = 65 ; //Décalage ASCI > 'A'
2025-10-08 10:26:19 +02:00
//Attributs
2025-10-08 16:07:03 +02:00
private char[] secretword ;
private byte wordsize ;
private boolean[] foundletters ;
private boolean[] entriesletters = new boolean[26] ; //Pseudo Alphabée
private byte remainingtry = REMAININGTRY ;
2025-10-08 10:26:19 +02:00
//Constructeur
public Partie() {
2025-10-08 16:07:03 +02:00
this.secretword = generateSecretWord() ;
this.wordsize = (byte) secretword.length ;
this.foundletters = new boolean[wordsize] ;
2025-10-08 10:26:19 +02:00
}
//Méthodes
2025-10-08 16:07:03 +02:00
public char[] getSecretWord() {
return this.secretword ;
}
public boolean[] getFoundLetters() {
return this.foundletters ;
}
public byte getRemainingTry() {
return this.remainingtry ;
}
/**
* Vérifie l'état de la partie en cours.
*
* @return true si le jeu est fini.
*/
public boolean gameIsEnding() {
if(this.remainingtry <= 0){
return true ;
}else if(wordIsFound()){
return true ;
}else{
return false ;
}
}
/**
* Vérifie si la lettre reçu n'a pas déjà été joué puis, met à jour le tableau "entriesletters" et
* "foundletters" le cas échéant.
*
* @return true si la lettre était déjà présente.
*/
public boolean isAlreadyEntries(char letter) {
short caractercode = (short) letter ; //Récupération du code du caractère
if(this.entriesletters[caractercode-CARACTERCODESHIFT]){
this.remainingtry-- ; //Décrément des essais
return true ;
}else{
2025-10-08 16:14:39 +02:00
boolean isfind = false ;
2025-10-08 16:07:03 +02:00
for(byte i = 0 ; i < this.wordsize ; i++){ //Parcours du "secretword"
if(this.secretword[i] == letter){
this.foundletters[i] = true ;
2025-10-08 16:14:39 +02:00
isfind = true ;
2025-10-08 16:07:03 +02:00
}
}
2025-10-08 16:14:39 +02:00
if(isfind == false){
2025-10-08 16:07:03 +02:00
this.remainingtry-- ; //Décrément des essais
}
this.entriesletters[caractercode-CARACTERCODESHIFT] = true ; //Ajout au tableau des lettres jouées
return false ;
}
}
2025-10-08 17:19:31 +02:00
/**
* Génère un mot à partir d'un grand dictionnaire (enfin en principe).
*
* @return le mot généré.
*/
2025-10-08 16:07:03 +02:00
private char[] generateSecretWord() {
2025-10-08 17:19:31 +02:00
Random random = new Random();
byte grain = (byte) random.nextInt(Mots.dictionarysize);
char[] word = Mots.dictionary[grain].toUpperCase().toCharArray();
2025-10-08 16:07:03 +02:00
return word ;
}
private boolean wordIsFound() {
for(byte i = 0 ; i < this.wordsize ; i++){ //Parcours du "secretword"
if(!this.foundletters[i]){ //Si une lettre n'est pas trouvé
return false ;
}
}
return true ;
}
2025-10-08 10:26:19 +02:00
//Affichage
public String toString() {
return "" ;
}
2025-10-08 16:07:03 +02:00
//Tests
public static void main(String[] args){
2025-10-08 17:19:31 +02:00
char[] test = {'E','O','M','I','E','D','A','Z','N','L','C','R','P','H','T','S'};
2025-10-08 16:14:39 +02:00
byte size = (byte) test.length ;
boolean status ;
2025-10-08 16:07:03 +02:00
2025-10-08 16:14:39 +02:00
Partie game = new Partie();
System.out.println("Trick > " + String.valueOf(game.secretword) + "\n");
for(byte i = 0 ; i < size && !game.gameIsEnding() ; i++){
System.out.println("Essais restants : " + game.getRemainingTry());
status = game.isAlreadyEntries(test[i]);
for(byte l = 0 ; l < game.wordsize ; l++){ //Parcours du "secretword"
if(game.foundletters[l] == true){
System.out.print(game.getSecretWord()[l] + " ");
2025-10-08 16:07:03 +02:00
}else{
System.out.print("_ ");
}
}
System.out.println(""); //Lisibilité
2025-10-08 16:14:39 +02:00
//System.out.println("Lettres : " + game.entriesletters);
2025-10-08 16:07:03 +02:00
}
2025-10-08 17:19:31 +02:00
System.out.println("Essais restants : " + game.getRemainingTry());
2025-10-08 16:07:03 +02:00
}
2025-10-08 10:26:19 +02:00
}