forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
3 Commits
NewControl
...
Dificulty_
Author | SHA1 | Date | |
---|---|---|---|
c70c2b73ad | |||
d4d8249c8b | |||
94e7394494 |
@@ -1,122 +1,80 @@
|
||||
package fr.iutfbleau.TD3_DEV51_Qualite_Algo.Controllers;
|
||||
|
||||
import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models.Difficulty;
|
||||
import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models.Word;
|
||||
import fr.iutfbleau.TD3_DEV51_Qualite_Algo.View.hangedManView;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Game {
|
||||
private Word word;
|
||||
private hangedManView hangedManView;
|
||||
|
||||
private int errors;
|
||||
private int maxErrors = 9;
|
||||
private String wrongLetters;
|
||||
private long startTime;
|
||||
private long endTime;
|
||||
private final int maxTime = 120000; // 2 minutes
|
||||
private List<Word> allwords;
|
||||
private Difficulty difficulty;
|
||||
private final int maxErrors = 6;
|
||||
|
||||
// Constructor
|
||||
public Game(Word word) {
|
||||
/* Hangmam Game */
|
||||
public Game(Word word ,List<Word> allWord) {
|
||||
this.word = word;
|
||||
this.hangedManView = hangedManView;
|
||||
this.errors = 0;
|
||||
this.wrongLetters = "";
|
||||
this.startTime = System.currentTimeMillis();
|
||||
this.endTime = 0;
|
||||
}
|
||||
|
||||
// Getters - Errors
|
||||
public int getErrors() {
|
||||
return this.errors;
|
||||
}
|
||||
|
||||
// Getters - Wrong Letters
|
||||
public String getWrongLetters() {
|
||||
return this.wrongLetters;
|
||||
}
|
||||
|
||||
// Determine if the game is lost
|
||||
private boolean isLost() {
|
||||
return errors >= maxErrors;
|
||||
}
|
||||
|
||||
// Determine if the game is won
|
||||
public boolean isWon() {
|
||||
int score = calculateScore();
|
||||
if (word.IsComplete() && score > 0) {
|
||||
return true;
|
||||
|
||||
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;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// From view, verify if the letter is correct & finish the game if necessary (lost or won)
|
||||
public boolean playLetter(char c) {
|
||||
boolean correct = word.VerifyLetter(c);
|
||||
|
||||
if (!correct) {
|
||||
errors++;
|
||||
hangedManView.setState(errors);
|
||||
|
||||
wrongLetters += c + " ";
|
||||
hangedManView.setWrongLetters(wrongLetters);
|
||||
|
||||
if (isLost()) {
|
||||
finishedGame();
|
||||
}
|
||||
} else {
|
||||
hangedManView.setCorrectLetters(word.getHiddenWord());
|
||||
|
||||
if (word.IsComplete()) {
|
||||
finishedGame();
|
||||
/* 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.getFirst();
|
||||
}
|
||||
|
||||
/* 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 = this.allwords.get(0).VerifyLetter(caractere) || this.allwords.get(1).VerifyLetter(caractere);
|
||||
} else{
|
||||
correct = word.VerifyLetter(caractere);
|
||||
}
|
||||
|
||||
if (!correct) errors++;
|
||||
return correct;
|
||||
}
|
||||
|
||||
// Finish the game
|
||||
private boolean finishedGame() {
|
||||
this.endTime = System.currentTimeMillis();
|
||||
|
||||
if (isLost()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean isWon = isWon();
|
||||
return isWon;
|
||||
/* Verify error number */
|
||||
public int getErrors() {
|
||||
return this.errors;
|
||||
}
|
||||
/* Put error max */
|
||||
public int getMaxErrors() {
|
||||
return this.maxErrors;
|
||||
}
|
||||
|
||||
// For view, return the remaining time
|
||||
public long getRemainingTime() {
|
||||
long elapsed = (System.currentTimeMillis() - startTime) / 1000;
|
||||
long remaining = (maxTime / 1000) - elapsed;
|
||||
|
||||
if (remaining < 0) {
|
||||
remaining = 0;
|
||||
finishedGame();
|
||||
}
|
||||
|
||||
return Math.max(remaining, 0);
|
||||
/* All letter is completed : Won */
|
||||
public boolean isWon() {
|
||||
return word.IsComplete();
|
||||
}
|
||||
|
||||
// Get elapsed time for calculateScore
|
||||
private long calculateTime() {
|
||||
long elapsedTime = (endTime - startTime) / 1000;
|
||||
return elapsedTime;
|
||||
}
|
||||
|
||||
// Calculate Score
|
||||
public int calculateScore() {
|
||||
long time = calculateTime();
|
||||
int baseScore = 1000;
|
||||
|
||||
int errorPenalty = errors * 50;
|
||||
int timePenalty = (int) time * 2;
|
||||
|
||||
int score = baseScore - errorPenalty - timePenalty;
|
||||
if (score < 0) {
|
||||
score = 0;
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
}
|
||||
/* Lost if maxErrors is greater than or egal to errors */
|
||||
public boolean isLost() {
|
||||
return errors >= maxErrors;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,5 @@
|
||||
package fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models;
|
||||
|
||||
public enum Difficulty {
|
||||
EASY, MEDIUM, HARD
|
||||
}
|
@@ -4,8 +4,8 @@ import java.util.*;
|
||||
|
||||
public class Word {
|
||||
private final String word;
|
||||
private Letter[] tabLetter;
|
||||
private String c;
|
||||
private Letter[] tabLetter;
|
||||
private String character;
|
||||
|
||||
private Word(String word){
|
||||
this.word = word;
|
||||
@@ -14,11 +14,11 @@ public class Word {
|
||||
}
|
||||
}
|
||||
/* Lettre dans le mot */
|
||||
public boolean VerifyLetter(char c){
|
||||
public boolean VerifyLetter(char character){
|
||||
boolean return_bool = false;
|
||||
for(Letter letter : this.tabLetter){
|
||||
if(!letter.getStatus()){
|
||||
if(letter.isGood(c)){
|
||||
if(letter.isGood(character)){
|
||||
return_bool = true;
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public class Word {
|
||||
}
|
||||
|
||||
/* Le mot a été deviné */
|
||||
private boolean IsComplet(){
|
||||
public boolean IsComplete(){
|
||||
for(Letter letter : this.tabLetter){
|
||||
if(!letter.getStatus()){
|
||||
return false;
|
||||
@@ -36,5 +36,8 @@ public class Word {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public String getWord() {
|
||||
return word;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user