EssayGhost Assignment代写,Essay代写,网课代修,Quiz代考

EssayGhost-Essay代写,作业代写,网课代修代上,cs代写代考

一站式网课代修,论文代写

高质量的Assignment代写、Paper代写、Report代写服务

EG1hao
网课代修代上,cs代写代考
Java代写
您的位置: 主页 > 编程案例 > Java代写 >
代写Java:swing Game Hangman Game吊人游戏GUI Graphical - Java代写
发布时间:2021-07-25 20:40:49浏览次数:
String[] options = {"Easy", "Medium", "Hard"}; int level = JOptionPane.showOptionDialog(null, "Select difficulty level of words(easy 4-6 letters, Medium 7-10 letters, Hard 10+ letters)", "Difficulty Level", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]); System.out.println("level: " + options[level]); hiddenWord = hangmanLexicon.randomSelectWord(level).toUpperCase(); System.out.println(hiddenWord + ", length:" + hiddenWord.length()); // set displayedChars displayedLetters = new char[hiddenWord.length() * 2 - 1]; correctLetters = new char[hiddenWord.length()]; for (int i = 0; i   displayedLetters.length; i++) { if (i % 2 == 0)  displayedLetters[i] = '_'; else  displayedLetters[i] = ' '; displayedCharsTextField.setText(String.copyValueOf(displayedLetters));  * when enter key is pressed, check letter private class inputKeyListener extends KeyAdapter { @Override        public void keyReleased(KeyEvent e) {            if(e.getKeyCode() == KeyEvent.VK_ENTER){                if (inputTextField.getText().length()   1) {                 JOptionPane.showMessageDialog(new JFrame(), "Please guess one letter at a time!",                 "Warninig", JOptionPane.WARNING_MESSAGE);                } else if (inputTextField.getText().length() == 1) {                 char letter = inputTextField.getText().toUpperCase().charAt(0);                 if (!(letter  = 'A'   letter  = 'Z'))                 JOptionPane.showMessageDialog(new JFrame(), "Please input a letter!",                     "Warninig", JOptionPane.WARNING_MESSAGE);                 else                  checkLetter(letter);                }                inputTextField.setText("");            }        }  * check the input letter  * @param letter input letter private void checkLetter(char letter) { int index = hiddenWord.indexOf(letter); // hiddenWord doesn't contain the given letter if (index   0) { incorrectCount++; hangmanPanel.incrementWrongGuesses(); // when wrong guess count greater than max wrong guesses, loss  if (incorrectCount  = MAX_WRONG_GUESSES) { String[] options = {"Restart", "Quit"}; int level = JOptionPane.showOptionDialog(null, "You lose - the answer was " + hiddenWord + ".", "You lose", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]); if (level == 0) restart(); else  System.exit(0); } else { if (!missedLettersSet.contains(letter)) { missedLettersSet.add(letter); if (missedLetters.isEmpty())  missedLetters = String.valueOf(letter); else  missedLetters += "," + letter; // displayed missed letters missesTextField.setText(missedLetters); } else { // if guess right, writes it in all its correct positions. for (int i = 0; i   hiddenWord.length(); i++) { if (hiddenWord.charAt(i) == letter) { displayedLetters[i * 2] = letter; correctLetters[i] = letter; displayedCharsTextField.setText(String.copyValueOf(displayedLetters)); // check whether guess win if (String.copyValueOf(correctLetters).equals(hiddenWord)) { String[] options = {"Restart", "Quit"}; int level = JOptionPane.showOptionDialog(null, "You win - the answer was " + hiddenWord + ".", "You win", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]); if (level == 0) restart(); else  System.exit(0); private class MenuHandler implements ActionListener { @Override public void actionPerformed (ActionEvent e) { if(e.getSource() == menuItemRestart) {  restart();   * restart game private void restart() { missedLettersSet = new HashSet(); incorrectCount = 0; missesTextField.setText(""); missedLetters = ""; hangmanPanel.reset(); selectDifficultyLevel(); public static void main(String[] args) { Hangman hangman = new Hangman();}HangmanLexicon.javaimport java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.Random;public class HangmanLexicon { public static final int EASY = 0; // words with 4~6 letters public static final int MEDIUM = 1; // words with 7~10 letters public static final int HARD = 2; // words with 10+ letters private ArrayList easyWordList = new ArrayList(); private ArrayList mediumWordList = new ArrayList(); private ArrayList hardWordList = new ArrayList(); public HangmanLexicon(String lexiconFileDir) { try { // read easy.txt BufferedReader hangmanWords = new BufferedReader(new FileReader(new File(lexiconFileDir, "easy.txt"))); String line; while((line = hangmanWords.readLine()) != null) { line = line.toUpperCase(); easyWordList.add(line); hangmanWords.close(); // read medium.txt hangmanWords = new BufferedReader(new FileReader(new File(lexiconFileDir, "medium.txt"))); while((line = hangmanWords.readLine()) != null) { line = line.toUpperCase(); mediumWordList.add(line); hangmanWords.close(); // read hard.txt hangmanWords = new BufferedReader(new FileReader(new File(lexiconFileDir, "hard.txt"))); while((line = hangmanWords.readLine()) != null) { line = line.toUpperCase(); hardWordList.add(line); hangmanWords.close(); } catch (IOException e) { e.printStackTrace(); /** Returns the word at the specified index. */ public String randomSelectWord(int level) { ArrayList wordList = null; switch (level) { case 0: wordList = easyWordList; break; case 1: wordList = mediumWordList; break; case 2: wordList = hardWordList; break; default: wordList = easyWordList; int size = wordList.size(); Random rand = new Random(); int index = rand.nextInt(size); return wordList.get(index);}HangmanPanel.javaimport java.awt.BasicStroke;import java.awt.Graphics;import java.awt.Graphics2D;import javax.swing.JPanel;public class HangmanPanel extends JPanel { // wrong guessed count private int wrongGuesses; /* Constants for the simple version of the picture (in pixels) */ private static final int SCAFFOLD_WIFTH = 20; private static final int BEAM_WIDTH = 12; private static final int HANGMAN_WIDTH = 5; private static final int SCAFFOLD_HEIGHT = 300; private static final int BEAM_LENGTH = 120; private static final int ROPE_LENGTH = 20; private static final int BASE_LENGTH = 200; private static final int HEAD_RADIUS = 36; private static final int BODY_LENGTH = 110; private static final int ARM_LENGTH = 70; private static final int ARM_ANGLE = 30; private static final int LEG_LENGTH = 85; private static final int LEG_ANGLE = 45;

所有的编程代写范围:essayghost为美国、加拿大、英国、澳洲的留学生提供C语言代写、代写C语言、C语言代做、代做C语言、数据库代写、代写数据库、数据库代做、代做数据库、Web作业代写、代写Web作业、Web作业代做、代做Web作业、Java代写、代写Java、Java代做、代做Java、Python代写、代写Python、Python代做、代做Python、C/C++代写、代写C/C++、C/C++代做、代做C/C++、数据结构代写、代写数据结构、数据结构代做、代做数据结构等留学生编程作业代写服务。