forked from menault/TD3_DEV51_Qualite_Algo
		
	Compare commits
	
		
			6 Commits
		
	
	
		
			Dificulty_
			...
			c87527973e
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| c87527973e | |||
| 5336077fc4 | |||
| 90cd0b42a0 | |||
| 7bc1bb498f | |||
| 843043740c | |||
| 2f3a6df740 | 
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 166 KiB After Width: | Height: | Size: 122 KiB | 
| @@ -1,22 +1,38 @@ | ||||
| package fr.iutfbleau.TD3_DEV51_Qualite_Algo.Controllers; | ||||
|  | ||||
| import fr.iutfbleau.TD3_DEV51_Qualite_Algo.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 { | ||||
|     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; | ||||
|  | ||||
|     /* Hangmam Game */ | ||||
|     public Game(Word word ,List<Word> allWord) { | ||||
|     // Constructor | ||||
|     public Game(Word word ,List<Word> allWord,hangedManView hangedManView) { | ||||
|         this.word = word; | ||||
|         this.hangedManView = hangedManView; | ||||
|         this.errors = 0; | ||||
|         | ||||
|         this.wrongLetters = ""; | ||||
|         this.startTime = System.currentTimeMillis(); | ||||
|         this.endTime = 0; | ||||
|  | ||||
|         switch (difficulty) { | ||||
|             case EASY: | ||||
|                 this.word = selectWord(allWord, 0 , 7 ); | ||||
| @@ -43,7 +59,7 @@ public class Game { | ||||
|  | ||||
|     /* Selected word level Difficult */ | ||||
|     private List<Word> selectTwoWords(List<Word> list) { | ||||
|         return list.subList(0, Math.min(2, list.size()));  | ||||
|         return list.subList(0, Math.min(2, list.size())); | ||||
|     } | ||||
|  | ||||
|     public boolean playLetter(char caractere) { | ||||
| @@ -59,7 +75,7 @@ public class Game { | ||||
|         return correct; | ||||
|     } | ||||
|  | ||||
|     /* Verify error number  */ | ||||
|     // Getters - Errors | ||||
|     public int getErrors() { | ||||
|         return this.errors; | ||||
|     } | ||||
| @@ -67,14 +83,94 @@ public class Game { | ||||
|     public int getMaxErrors() { | ||||
|         return this.maxErrors; | ||||
|     } | ||||
|  | ||||
|     /* All letter is completed : Won */  | ||||
|     public boolean isWon() { | ||||
|         return word.IsComplete(); | ||||
|     // Getters - Wrong Letters | ||||
|     public String getWrongLetters() { | ||||
|         return this.wrongLetters; | ||||
|     } | ||||
|  | ||||
|     /* Lost if maxErrors is greater than or egal to errors */  | ||||
|     public boolean isLost() { | ||||
|     // 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) { | ||||
|         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(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return correct; | ||||
|     } | ||||
|  | ||||
|     // Finish the game | ||||
|     private boolean finishedGame() { | ||||
|         this.endTime = System.currentTimeMillis(); | ||||
|  | ||||
|         if (isLost()) { | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|         boolean isWon = isWon(); | ||||
|         return isWon; | ||||
|     } | ||||
|  | ||||
|     // 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); | ||||
|     } | ||||
|  | ||||
|     // 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,31 @@ | ||||
| package fr.iutfbleau.TD3_DEV51_Qualite_Algo.Controllers.Handler; | ||||
|  | ||||
| import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Controllers.Game; | ||||
|  | ||||
| import javax.swing.*; | ||||
| import java.awt.event.ActionEvent; | ||||
| import java.awt.event.ActionListener; | ||||
|  | ||||
| public class SendLetterHandler implements ActionListener { | ||||
|  | ||||
|     private JFrame frame; | ||||
|     private Game gameControler; | ||||
|     private JTextField letterField; | ||||
|  | ||||
|     public SendLetterHandler(JFrame frame, Game gameControler, JTextField letterField) { | ||||
|         this.frame = frame; | ||||
|         this.gameControler = gameControler; | ||||
|         this.letterField = letterField; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void actionPerformed(ActionEvent e) { | ||||
|         // on récupère la lettre du champs de saisie | ||||
|         if(this.letterField.getText().length() > 1) { | ||||
|             JOptionPane.showMessageDialog(this.frame, "Veuillez n'envoyer qu'une seule lettre. Merci (⓿_⓿).","(⓿_⓿)", JOptionPane.WARNING_MESSAGE); | ||||
|             return; | ||||
|         } | ||||
|         char letter = letterField.getText().charAt(0); | ||||
|         this.gameControler.playLetter(letter); | ||||
|     } | ||||
| } | ||||
| @@ -1,7 +1,55 @@ | ||||
| package fr.iutfbleau.TD3_DEV51_Qualite_Algo; | ||||
|  | ||||
| import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Controllers.Game; | ||||
| import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Controllers.Handler.SendLetterHandler; | ||||
| import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models.Word; | ||||
| import fr.iutfbleau.TD3_DEV51_Qualite_Algo.View.hangedManView; | ||||
|  | ||||
| import javax.swing.*; | ||||
| import java.awt.*; | ||||
| import java.io.IOException; | ||||
|  | ||||
| public class Main { | ||||
|     public static void main(String[] args) { | ||||
|         | ||||
|     public static void main(String[] args) throws IOException { | ||||
|         // TODO | ||||
|         Game gameControler = new Game(new Word("")); | ||||
|         // TODO | ||||
|  | ||||
|         JFrame frame = new JFrame(); | ||||
|         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||||
|         frame.setSize(800, 600); | ||||
|         frame.setResizable(false); | ||||
|  | ||||
|         frame.setLayout(new GridBagLayout()); | ||||
|         GridBagConstraints GBC = new GridBagConstraints(); | ||||
|  | ||||
|         GBC.fill = GridBagConstraints.BOTH; | ||||
|         GBC.gridx = 0; | ||||
|         GBC.gridy = 0; | ||||
|         GBC.gridwidth = 1; | ||||
|         GBC.gridheight = 1; | ||||
|         GBC.weightx = 1; | ||||
|         GBC.weighty = 1; | ||||
|  | ||||
|         hangedManView drawingView = new hangedManView(); | ||||
|         frame.add(drawingView, GBC); | ||||
|  | ||||
|         GBC.gridy = 1; | ||||
|         GBC.weighty = 0; | ||||
|  | ||||
|         JPanel panel = new JPanel(); | ||||
|         panel.setLayout(new FlowLayout(FlowLayout.CENTER)); | ||||
|  | ||||
|         JTextField TextField = new JTextField(); | ||||
|         TextField.setColumns(10); | ||||
|         panel.add(TextField,GBC); | ||||
|  | ||||
|         JButton button = new JButton("send"); | ||||
|         button.addActionListener(new SendLetterHandler(frame,gameControler,TextField)); | ||||
|         panel.add(button); | ||||
|  | ||||
|         frame.add(panel,GBC); | ||||
|  | ||||
|         frame.setVisible(true); | ||||
|     } | ||||
| } | ||||
| @@ -4,10 +4,10 @@ import java.util.*; | ||||
|  | ||||
| public class Word {      | ||||
|     private final String word; | ||||
|     private Letter[] tabLetter; | ||||
|                                                 private Letter[] tabLetter; | ||||
|     private String character; | ||||
|  | ||||
|     private Word(String word){ | ||||
|     public Word(String word){ | ||||
|         this.word = word; | ||||
|         for(int i = 0; i< this.word.length();i++){ | ||||
|             this.tabLetter[i] = new Letter(this.word.charAt(i)); | ||||
| @@ -39,5 +39,5 @@ public class Word { | ||||
|     public String getWord() { | ||||
|        return word; | ||||
|     } | ||||
|      | ||||
|  | ||||
| } | ||||
| @@ -1,75 +1,110 @@ | ||||
| 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; | ||||
|     private String word; | ||||
|     private String wrongLetters; | ||||
|  | ||||
|     public hangedManView() throws IOException { | ||||
|         super(); | ||||
|         this.state = 0; | ||||
|         this.state = 8; | ||||
|         this.word = ""; | ||||
|         this.wrongLetters = ""; | ||||
|     } | ||||
|  | ||||
|     public hangedManView(String word) throws IOException { | ||||
|         super(); | ||||
|         this.state = 8; | ||||
|         this.word = word; | ||||
|         this.wrongLetters = ""; | ||||
|     } | ||||
|  | ||||
|     protected void paintComponent(Graphics g) { | ||||
|         super.paintComponent(g); | ||||
|         Graphics2D g2d = (Graphics2D) g; | ||||
|  | ||||
|         int x = 150; | ||||
|         int y = 200; | ||||
|  | ||||
|         int x = this.getWidth()/2; | ||||
|         int y = this.getHeight()/2; | ||||
|  | ||||
|         if(this.state > 8) { | ||||
|             // paint une image | ||||
|             g2d.drawImage(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("resources/HangedGirl.jpg")).getImage(), x, y, this); | ||||
|             double ratio = 0.5; | ||||
|             g2d.drawImage(new ImageIcon("resources/HangedGirl.jpg").getImage(), (this.getWidth()-(int) (567*ratio))/2, 0, (int) (567*ratio), (int) (1080*ratio), this); | ||||
|         }else{ | ||||
|             this.drawStand(g2d, x, y); | ||||
|             // 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; | ||||
|             this.drawBudy(g2d, x, y); | ||||
|         } | ||||
|  | ||||
|             // 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); | ||||
|             } | ||||
|         // on dessine le mot en dessous du pendu et au centre de la fenêtre. | ||||
|         g2d.setFont(new Font("Roboto", Font.BOLD, 20)); | ||||
|         g2d.drawString(this.word, (this.getWidth()-g2d.getFontMetrics().stringWidth(this.word))/2, this.getHeight()-50); | ||||
|  | ||||
|         // on dessine les mauvaises lettres en rouge | ||||
|         g2d.setColor(Color.RED); | ||||
|         g2d.drawString(this.wrongLetters, (this.getWidth()-g2d.getFontMetrics().stringWidth(this.wrongLetters))/2, this.getHeight()-10); | ||||
|     } | ||||
|  | ||||
|     private void drawStand(Graphics2D g2d, int x, int y) { | ||||
|         // 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); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void drawBudy(Graphics g2d,int x, int y) { | ||||
|  | ||||
|         // 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; | ||||
|         this.repaint(); | ||||
|     } | ||||
|  | ||||
|     public void setWord(String word) { | ||||
|         this.word = word; | ||||
|         this.repaint(); | ||||
|     } | ||||
|  | ||||
|     public void setWrongLetters(String letters) { | ||||
|         this.wrongLetters = letters; | ||||
|         this.repaint(); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user