forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
13 Commits
Controller
...
5336077fc4
Author | SHA1 | Date | |
---|---|---|---|
5336077fc4 | |||
c70c2b73ad | |||
90cd0b42a0 | |||
d4d8249c8b | |||
7bc1bb498f | |||
94e7394494 | |||
111dcad559 | |||
dbeed240d1 | |||
d07d4a7adc | |||
bc4aa06445 | |||
e5f9fd7fa8 | |||
0dbd7751e1 | |||
ccb68428a8 |
13
TD3_DEV51_Qualite_Algo/.idea/compiler.xml
generated
Normal file
13
TD3_DEV51_Qualite_Algo/.idea/compiler.xml
generated
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<module name="TD3_DEV51_Qualite_Algo" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
|
</component>
|
||||||
|
</project>
|
7
TD3_DEV51_Qualite_Algo/.idea/encodings.xml
generated
Normal file
7
TD3_DEV51_Qualite_Algo/.idea/encodings.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
20
TD3_DEV51_Qualite_Algo/.idea/jarRepositories.xml
generated
Normal file
20
TD3_DEV51_Qualite_Algo/.idea/jarRepositories.xml
generated
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="RemoteRepositoriesConfiguration">
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Central Repository" />
|
||||||
|
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Maven Central repository" />
|
||||||
|
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="jboss.community" />
|
||||||
|
<option name="name" value="JBoss Community repository" />
|
||||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||||
|
</remote-repository>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
TD3_DEV51_Qualite_Algo/.idea/vcs.xml
generated
Normal file
6
TD3_DEV51_Qualite_Algo/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
BIN
TD3_DEV51_Qualite_Algo/resources/HangedGirl.jpg
Normal file
BIN
TD3_DEV51_Qualite_Algo/resources/HangedGirl.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 166 KiB |
@@ -1,18 +1,78 @@
|
|||||||
package Controllers;
|
package fr.iutfbleau.TD3_DEV51_Qualite_Algo.Controllers;
|
||||||
|
|
||||||
import Models.Word;
|
import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models.Difficulty;
|
||||||
|
import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models.Word;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models.Word;
|
||||||
|
import fr.iutfbleau.TD3_DEV51_Qualite_Algo.View.hangedManView;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
private Word word;
|
private Word word;
|
||||||
private int errors;
|
private hangedManView hangedManView;
|
||||||
private final int maxErrors = 6;
|
|
||||||
|
|
||||||
public Game(Word word) {
|
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;
|
||||||
|
|
||||||
|
/* Hangmam Game */
|
||||||
|
// Constructor
|
||||||
|
public Game(Word word ,List<Word> allWord,hangedManView hangedManView) {
|
||||||
this.word = word;
|
this.word = word;
|
||||||
|
this.hangedManView = hangedManView;
|
||||||
this.errors = 0;
|
this.errors = 0;
|
||||||
|
this.wrongLetters = "";
|
||||||
|
this.startTime = System.currentTimeMillis();
|
||||||
|
this.endTime = 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Verify error number */
|
/* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters - Errors
|
||||||
public int getErrors() {
|
public int getErrors() {
|
||||||
return this.errors;
|
return this.errors;
|
||||||
}
|
}
|
||||||
@@ -20,25 +80,94 @@ public class Game {
|
|||||||
public int getMaxErrors() {
|
public int getMaxErrors() {
|
||||||
return this.maxErrors;
|
return this.maxErrors;
|
||||||
}
|
}
|
||||||
|
// Getters - Wrong Letters
|
||||||
|
public String getWrongLetters() {
|
||||||
|
return this.wrongLetters;
|
||||||
|
}
|
||||||
|
|
||||||
/* Vue call method VerifyLetter, check status letter false/True */
|
// 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;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// From view, verify if the letter is correct & finish the game if necessary (lost or won)
|
||||||
public boolean playLetter(char c) {
|
public boolean playLetter(char c) {
|
||||||
boolean correct = word.VerifyLetter(c);
|
boolean correct = word.VerifyLetter(c);
|
||||||
|
|
||||||
if (!correct) {
|
if (!correct) {
|
||||||
errors++;
|
errors++;
|
||||||
|
hangedManView.setState(errors);
|
||||||
|
|
||||||
|
wrongLetters += c + " ";
|
||||||
|
hangedManView.setWrongLetters(wrongLetters);
|
||||||
|
|
||||||
|
if (isLost()) {
|
||||||
|
finishedGame();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
hangedManView.setCorrectLetters(word.getHiddenWord());
|
||||||
|
|
||||||
|
if (word.IsComplete()) {
|
||||||
|
finishedGame();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return correct;
|
return correct;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* All letter is completed : Won */
|
// Finish the game
|
||||||
public boolean isWon() {
|
private boolean finishedGame() {
|
||||||
return word.IsComplete();
|
this.endTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
if (isLost()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isWon = isWon();
|
||||||
|
return isWon;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Lost if maxErrors is greater than or egal to errors */
|
// For view, return the remaining time
|
||||||
public boolean isLost() {
|
public long getRemainingTime() {
|
||||||
return errors >= maxErrors;
|
long elapsed = (System.currentTimeMillis() - startTime) / 1000;
|
||||||
|
long remaining = (maxTime / 1000) - elapsed;
|
||||||
|
|
||||||
|
if (remaining < 0) {
|
||||||
|
remaining = 0;
|
||||||
|
finishedGame();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.max(remaining, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -0,0 +1,5 @@
|
|||||||
|
package fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models;
|
||||||
|
|
||||||
|
public enum Difficulty {
|
||||||
|
EASY, MEDIUM, HARD
|
||||||
|
}
|
@@ -1,3 +1,5 @@
|
|||||||
|
package fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models;
|
||||||
|
|
||||||
public class Letter{
|
public class Letter{
|
||||||
private char letter;
|
private char letter;
|
||||||
private boolean status = false;
|
private boolean status = false;
|
||||||
@@ -6,11 +8,11 @@ public class Letter{
|
|||||||
this.letter = letter;
|
this.letter = letter;
|
||||||
}
|
}
|
||||||
/* Show status */
|
/* Show status */
|
||||||
private boolean getStatus(){
|
public boolean getStatus(){
|
||||||
return status
|
return status;
|
||||||
}
|
}
|
||||||
/* Verify status true, false */
|
/* Verify status true, false */
|
||||||
private boolean isGood(char c){
|
public boolean isGood(char c){
|
||||||
if( this.letter == c){
|
if( this.letter == c){
|
||||||
this.status = true;
|
this.status = true;
|
||||||
return true;
|
return true;
|
@@ -1,22 +1,24 @@
|
|||||||
|
package fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class Word {
|
public class Word {
|
||||||
private 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;
|
||||||
for(int i = 0; i< this.word.length();i++){
|
for(int i = 0; i< this.word.length();i++){
|
||||||
this.tabLetter[i] = Letter(this.word.charAt(i))
|
this.tabLetter[i] = new Letter(this.word.charAt(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Lettre dans le mot */
|
/* Lettre dans le mot */
|
||||||
private boolean VerifyLetter(String c){
|
public boolean VerifyLetter(char character){
|
||||||
boolean return_bool = false;
|
boolean return_bool = false;
|
||||||
for(Letter letter in 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,8 +27,8 @@ public class Word {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Le mot a été deviné */
|
/* Le mot a été deviné */
|
||||||
private boolean IsComplet(){
|
public boolean IsComplete(){
|
||||||
if(Letter letter : this.tabLetter){
|
for(Letter letter : this.tabLetter){
|
||||||
if(!letter.getStatus()){
|
if(!letter.getStatus()){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -34,16 +36,8 @@ public class Word {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
public String getWord() {
|
||||||
|
return word;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -0,0 +1,75 @@
|
|||||||
|
package fr.iutfbleau.TD3_DEV51_Qualite_Algo.View;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
|
public class hangedManView extends JComponent {
|
||||||
|
|
||||||
|
private int state;
|
||||||
|
|
||||||
|
public hangedManView() throws IOException {
|
||||||
|
super();
|
||||||
|
this.state = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
|
||||||
|
int x = 150;
|
||||||
|
int y = 200;
|
||||||
|
|
||||||
|
|
||||||
|
if(this.state > 8) {
|
||||||
|
// paint une image
|
||||||
|
g2d.drawImage(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("resources/HangedGirl.jpg")).getImage(), x, y, this);
|
||||||
|
}else{
|
||||||
|
// on paint un homme batton
|
||||||
|
// Base
|
||||||
|
if (this.state > 0) {
|
||||||
|
g2d.drawLine(x - 100, y + 100, x + 100, y + 100);
|
||||||
|
}
|
||||||
|
// Poteau vertical
|
||||||
|
if (this.state > 1) {
|
||||||
|
g2d.drawLine(x - 50, y + 100, x - 50, y - 100);
|
||||||
|
}
|
||||||
|
// Traverse horizontale
|
||||||
|
if (this.state > 2) {
|
||||||
|
g2d.drawLine(x - 50, y - 100, x + 50, y - 100);
|
||||||
|
}
|
||||||
|
// Petite corde verticale
|
||||||
|
if (this.state > 3) {
|
||||||
|
g2d.drawLine(x + 50, y - 100, x + 50, y - 70);
|
||||||
|
}
|
||||||
|
|
||||||
|
x += 50;
|
||||||
|
y -= 40;
|
||||||
|
|
||||||
|
// Tête
|
||||||
|
if (this.state > 4) {
|
||||||
|
g2d.drawOval(x - 15, y - 30, 30, 30);
|
||||||
|
}
|
||||||
|
// Corps
|
||||||
|
if (this.state > 5) {
|
||||||
|
g2d.drawLine(x, y, x, y + 40);
|
||||||
|
}
|
||||||
|
// Bras
|
||||||
|
if (this.state > 6) {
|
||||||
|
g2d.drawLine(x, y + 10, x - 20, y + 20);
|
||||||
|
g2d.drawLine(x, y + 10, x + 20, y + 20);
|
||||||
|
}
|
||||||
|
// Jambes
|
||||||
|
if (this.state > 7) {
|
||||||
|
g2d.drawLine(x, y + 40, x - 20, y + 70);
|
||||||
|
g2d.drawLine(x, y + 40, x + 20, y + 70);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setState(int state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user