BossC2018

Hangman WIP

Mar 21st, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.05 KB | None | 0 0
  1.  
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.net.URL;
  5. import java.util.Random;
  6. import javax.swing.ImageIcon;
  7. import javax.swing.JLabel;
  8. import javax.swing.JOptionPane;
  9.  
  10. /*
  11.  * To change this license header, choose License Headers in Project Properties.
  12.  * To change this template file, choose Tools | Templates
  13.  * and open the template in the editor.
  14.  */
  15. /**
  16.  *
  17.  * @author Maikelele
  18.  */
  19. public final class Hangman extends javax.swing.JFrame {
  20.  
  21.     /**
  22.      * Creates new form Hangman
  23.      */
  24.     public Hangman() {
  25.         initComponents();
  26.         getRootPane().setDefaultButton(guessButton);
  27.         gameSetup();
  28.         loadText();
  29.         setWord();
  30.        
  31.     }
  32.  
  33.     //Make sure you add an array to stop dupliates
  34.     Random rand = new Random();
  35.     int gameNumber = 1000;
  36.     String[] wordBank = new String[gameNumber];
  37.     String currentWord = "";
  38.     int wordCount;
  39.     int wrongGuesses = 0;
  40.     String[] currentWordCharacter = new String[100];
  41.     String[] currentWordGuesses = new String[100];
  42.     int rNumber = 0;
  43.     int difficulty = 0;
  44.     int wordMode = 1;
  45.  
  46.     public void gameSetup() {
  47.  
  48.         Object[] wordPlay = {"Dictionary", "Custom", "Half and Half"};
  49.         wordMode = JOptionPane.showOptionDialog(this,
  50.                 "What words will you play with?"
  51.                 + "\nCustom: User Submitted Words"
  52.                 + "\nDictionary: List of 354k words"
  53.                 + "\nHalf and Half: Mixture of Both",
  54.                 "Hangman Setup",
  55.                 JOptionPane.YES_NO_CANCEL_OPTION,
  56.                 JOptionPane.QUESTION_MESSAGE,
  57.                 null,
  58.                 wordPlay,
  59.                 wordPlay[2]);
  60.         System.out.println(wordMode);
  61.         if (wordMode == 0) {
  62.             Object[] difficultyLevels = {"Easy", "Normal", "Hard"};
  63.             difficulty = JOptionPane.showOptionDialog(this,
  64.                     "What will the difficulty be?"
  65.                     + "\nEasy: 8-10 letter words"
  66.                     + "\nNormal: 6-12 letter words"
  67.                     + "\nHard: < 6 & > 12 letter words",
  68.                     "Hangman Setup",
  69.                     JOptionPane.YES_NO_CANCEL_OPTION,
  70.                     JOptionPane.QUESTION_MESSAGE,
  71.                     null,
  72.                     difficultyLevels,
  73.                     difficultyLevels[2]);
  74.         }
  75.     }
  76.  
  77.     public void loadText() {
  78.         String[][] wordBankTemp = new String[2][1000000];
  79.         FileReader reader = null;
  80.         String wordValue = "";
  81.         int character = 0;
  82.         int lastCharacter = 0;
  83.         int wordCountCustom = 0;
  84.         wordCount = 0;
  85.  
  86.         try {
  87.             reader = new FileReader("src/hangman.txt");
  88.  
  89.             while ((character = reader.read()) != -1) {
  90.                 if ((char) lastCharacter == '\n') {
  91.                     wordCount++;
  92.                 }
  93.                
  94.                 if ((char) character != '\n') {
  95.                     wordValue += (char) character;
  96.                     wordBankTemp[0][wordCount] = wordValue;
  97.                 } else {
  98.                     wordValue = "";
  99.                    
  100.                 }
  101.                 lastCharacter = character;
  102.             }
  103.  
  104.             reader.close();
  105.         } catch (IOException e) {
  106.             System.out.println(e);
  107.         }
  108.  
  109.        
  110.         character = 0;
  111.         wordValue = "";
  112.        
  113.         try {
  114.             reader = new FileReader("src/hangman_custom.txt");
  115.             while ((character = reader.read()) != -1) {
  116.                 if ((char) lastCharacter == '\n') {
  117.                     wordCountCustom++;
  118.                 }
  119.                
  120.                
  121.                
  122.                 if ((char) character != '\n') {
  123.                     wordValue += (char) character;
  124.                     wordBankTemp[1][wordCountCustom] = wordValue;
  125.                 } else {
  126.                     wordValue = "";
  127.                    
  128.                 }
  129.                 lastCharacter = character;
  130.             }
  131.            
  132.             reader.close();
  133.         } catch (IOException e) {
  134.             System.out.println(e);
  135.         }
  136.  
  137.         wordCount++;
  138.         wordCountCustom++;
  139.  
  140.         for (int i = 0; i < gameNumber; i++) {//Add something here to cancel out if there is any character that isn't a letter
  141.             rNumber = rand.nextInt(2);
  142.            
  143.             if (wordMode == 0 || (wordMode == 2 && rNumber == 0)) {
  144.                 rNumber = rand.nextInt(wordCount);
  145.  
  146.                 for (int j = 0; j < wordBankTemp[0][rNumber].length(); j++) {
  147.                     if (wordBankTemp[0][rNumber].charAt(j) == '\'') {
  148.                         i--;
  149.                         break;
  150.                     } else {
  151.                         wordBank[i] = wordBankTemp[0][rNumber];
  152.                     }
  153.                 }
  154.             } else {
  155.                 rNumber = rand.nextInt(wordCountCustom);
  156.  
  157.                 for (int j = 0; j < wordBankTemp[1][rNumber].length(); j++) {
  158.                     if (wordBankTemp[1][rNumber].charAt(j) == '\'') {
  159.                         i--;
  160.                         break;
  161.                     } else {
  162.                         wordBank[i] = wordBankTemp[1][rNumber];
  163.                     }
  164.                 }
  165.             }
  166.         }
  167.  
  168.         wordCount = 1000;
  169.     }
  170.  
  171.     public void setWord() {
  172.         //wordCount = 1000;
  173.        
  174.         wordLabel.setText("");
  175.         currentWord = wordBank[rand.nextInt(gameNumber)];
  176.         System.out.println(currentWord);
  177.         //System.out.println(currentWord);
  178.         //System.out.println(currentWord.length());
  179.        
  180.         for (int i = 0; i < currentWordCharacter.length; i++) {
  181.             currentWordCharacter[i] = " ";
  182.         }
  183.  
  184.         for (int i = 0; i < currentWordGuesses.length; i++) {
  185.             currentWordGuesses[i] = " ";
  186.         }
  187.  
  188.         try {
  189.             for (int i = 0; i <= currentWord.length(); i++) {
  190.                 currentWordCharacter[i] = "end";
  191.                 currentWordCharacter[i] = "" + currentWord.charAt(i);
  192.                 wordLabel.setText(wordLabel.getText() + "_ ");
  193.             }
  194.         } catch (StringIndexOutOfBoundsException e) {
  195.             /*for (int i = 0; i < currentWordCharacter.length; i++) {
  196.                 if (currentWordCharacter[i].equals(" ")) {
  197.                     break;
  198.                 }
  199.                 System.out.println(currentWordCharacter[i]);
  200.             }*/
  201.         }
  202.  
  203.         //System.out.println(currentWord);
  204.         //System.out.println(currentWord + "   " + currentWord);
  205.         //System.out.println(currentWord.length());
  206.         wrongGuesses = 0;
  207.         updateGraphics();
  208.         updateWord();
  209.     }
  210.  
  211.     public void updateWord() {
  212.         wordLabel.setText("");
  213.  
  214.         for (int i = 0; i < currentWord.length(); i++) {
  215.             if (currentWordCharacter[i].equals("end")) {
  216.                 break;
  217.             } else if (currentWordCharacter[i].length() > 1) {
  218.                 wordLabel.setText(wordLabel.getText() + currentWordCharacter[i].charAt(0) + " ");
  219.             } else {
  220.                 wordLabel.setText(wordLabel.getText() + "_ ");
  221.             }
  222.         }
  223.  
  224.         guessLabel.setText("");
  225.  
  226.         for (int i = 65; i < 91; i++) {
  227.             char character = (char) i;
  228.  
  229.             for (int j = 0; j < currentWordGuesses.length; j++) {
  230.                 if (character == currentWordGuesses[j].charAt(0)) {
  231.                     guessLabel.setText(guessLabel.getText() + character);
  232.                 } else if ((character + 32) == currentWordGuesses[j].charAt(0)) {
  233.                     guessLabel.setText(guessLabel.getText() + character);
  234.                 }
  235.             }
  236.         }
  237.     }
  238.  
  239.     public void updateGraphics() {
  240.         if (wrongGuesses > 6) {
  241.             wrongGuesses = 6;
  242.         } else if (wrongGuesses < 0) {
  243.             wrongGuesses = 0;
  244.         }
  245.  
  246.         iconLabel.setIcon(new ImageIcon(getClass().getResource("/hangman_graphics/hangman" + String.valueOf(wrongGuesses) + ".png")));
  247.     }
  248.  
  249.     public void testWin() {
  250.         boolean win = true;
  251.  
  252.         for (int i = 0; i < currentWordCharacter.length; i++) {
  253.             if (currentWordCharacter[i].equals("end")) {
  254.                 break;
  255.             } else if (currentWordCharacter[i].length() != 2) {
  256.                 win = false;
  257.                 break;
  258.             }
  259.         }
  260.  
  261.         if (win) {
  262.             System.out.println("Winner");
  263.             Object[] options = {"Yes", "No"};
  264.             String winMessage = "";
  265.             if (wrongGuesses < 1) {
  266.                 winMessage = "You are a legend at this!";
  267.             } else if (wrongGuesses < 2) {
  268.                 winMessage = "You did pretty well!";
  269.             } else if (wrongGuesses < 3) {
  270.                 winMessage = "Not too shabby.";
  271.             } else if (wrongGuesses < 4) {
  272.                 winMessage = "Not very good, but you got it.";
  273.             } else {
  274.                 winMessage = "You got lucky, nice job.";
  275.             }
  276.  
  277.             ImageIcon icon = new ImageIcon(getClass().getResource("/hangman_graphics/win.png"));
  278.             int n = JOptionPane.showOptionDialog(this,
  279.                     "" + winMessage + "\nThe word was " + currentWord + ".\nWould you like to play again?",
  280.                     "Winner",
  281.                     JOptionPane.YES_NO_OPTION,
  282.                     JOptionPane.QUESTION_MESSAGE,
  283.                     icon,
  284.                     options,
  285.                     options[0]);
  286.             if (n == 0) {
  287.                 setWord();
  288.             } else {
  289.                 System.exit(0);
  290.             }
  291.         }
  292.     }
  293.  
  294.     /**
  295.      * This method is called from within the constructor to initialize the form.
  296.      * WARNING: Do NOT modify this code. The content of this method is always
  297.      * regenerated by the Form Editor.
  298.      */
  299.     @SuppressWarnings("unchecked")
  300.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  301.     private void initComponents() {
  302.  
  303.         iconLabel = new javax.swing.JLabel();
  304.         answerTextField = new javax.swing.JTextField();
  305.         guessButton = new javax.swing.JButton();
  306.         wordLabel = new javax.swing.JLabel();
  307.         nextButton = new javax.swing.JButton();
  308.         jButton1 = new javax.swing.JButton();
  309.         guessLabel = new javax.swing.JLabel();
  310.  
  311.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  312.  
  313.         iconLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hangman_graphics/hangman0.png"))); // NOI18N
  314.  
  315.         guessButton.setText("Guess");
  316.         guessButton.addActionListener(new java.awt.event.ActionListener() {
  317.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  318.                 guessButtonActionPerformed(evt);
  319.             }
  320.         });
  321.  
  322.         wordLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
  323.         wordLabel.setText("TEXT");
  324.  
  325.         nextButton.setText("Skip");
  326.         nextButton.addActionListener(new java.awt.event.ActionListener() {
  327.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  328.                 nextButtonActionPerformed(evt);
  329.             }
  330.         });
  331.  
  332.         jButton1.setText("Answer");
  333.  
  334.         guessLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
  335.         guessLabel.setText("TEXT");
  336.  
  337.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  338.         getContentPane().setLayout(layout);
  339.         layout.setHorizontalGroup(
  340.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  341.             .addGroup(layout.createSequentialGroup()
  342.                 .addContainerGap()
  343.                 .addComponent(answerTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  344.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  345.                 .addComponent(guessButton)
  346.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  347.                 .addComponent(jButton1)
  348.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  349.                 .addComponent(nextButton)
  350.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  351.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  352.                 .addGap(0, 0, Short.MAX_VALUE)
  353.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  354.                     .addComponent(iconLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
  355.                     .addComponent(wordLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
  356.                     .addComponent(guessLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)))
  357.         );
  358.         layout.setVerticalGroup(
  359.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  360.             .addGroup(layout.createSequentialGroup()
  361.                 .addComponent(iconLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
  362.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  363.                 .addComponent(wordLabel)
  364.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  365.                 .addComponent(guessLabel)
  366.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 115, Short.MAX_VALUE)
  367.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  368.                     .addComponent(answerTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  369.                     .addComponent(guessButton)
  370.                     .addComponent(nextButton)
  371.                     .addComponent(jButton1))
  372.                 .addContainerGap())
  373.         );
  374.  
  375.         pack();
  376.     }// </editor-fold>                        
  377.  
  378.     private void guessButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  379.         boolean guess = false;
  380.  
  381.         for (int i = 0; i < currentWord.length(); i++) {
  382.             if (answerTextField.getText().length() > 0 && answerTextField.getText().charAt(0) == currentWord.charAt(i)) {
  383.                 for (int j = 0; j < currentWordCharacter.length; j++) {
  384.                     if (currentWordCharacter[j].length() == 1 && currentWordCharacter[j].charAt(0) == answerTextField.getText().charAt(0)) {
  385.                         currentWordCharacter[j] += "1";
  386.                         guess = true;
  387.                     } else if (!currentWordCharacter[j].equals("end") && currentWordCharacter[j].length() > 1 && currentWordCharacter[j].charAt(0) == answerTextField.getText().charAt(0)) {
  388.                         guess = true;
  389.                     }
  390.                 }
  391.             }
  392.         }
  393.  
  394.         for (int i = 0; i < currentWordGuesses.length; i++) {
  395.             if (answerTextField.getText().length() > 0 && currentWordGuesses[i].length() > 0 && answerTextField.getText().charAt(0) == currentWordGuesses[i].charAt(0)) {
  396.                 guess = true;
  397.                 break;
  398.             } else if (answerTextField.getText().length() > 0 && currentWordGuesses[i].equals(" ")) {
  399.                 currentWordGuesses[i] = "" + answerTextField.getText().charAt(0);
  400.                 break;
  401.             }
  402.         }
  403.  
  404.         if (answerTextField.getText().length() == 0 || answerTextField.getText().equals(" ") || answerTextField.getText().charAt(0) == ' ') {
  405.         } else if (guess == false) {
  406.             wrongGuesses++;
  407.  
  408.             if (wrongGuesses > 5) {
  409.                 System.out.println("You lose");
  410.  
  411.                 Object[] options = {"Yes", "No"};
  412.  
  413.                 ImageIcon icon = new ImageIcon(getClass().getResource("/hangman_graphics/lose.png"));
  414.  
  415.                 int n = JOptionPane.showOptionDialog(this,
  416.                         "You lost!\nThe word was " + currentWord + ".\nWould you like to play again?",
  417.                         "Loser",
  418.                         JOptionPane.YES_NO_OPTION,
  419.                         JOptionPane.QUESTION_MESSAGE,
  420.                         icon,
  421.                         options,
  422.                         options[0]);
  423.                 if (n == 0) {
  424.                     setWord();
  425.                 } else {
  426.                     System.exit(0);
  427.                 }
  428.             }
  429.         }
  430.  
  431.         answerTextField.setText("");
  432.         updateWord();
  433.         updateGraphics();
  434.         testWin();
  435.     }                                          
  436.  
  437.     private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  438.         setWord();
  439.     }                                          
  440.  
  441.     /**
  442.      * @param args the command line arguments
  443.      */
  444.     public static void main(String args[]) {
  445.         /* Set the Nimbus look and feel */
  446.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  447.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  448.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  449.          */
  450.         try {
  451.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  452.                 if ("Nimbus".equals(info.getName())) {
  453.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  454.                     break;
  455.                 }
  456.             }
  457.         } catch (ClassNotFoundException ex) {
  458.             java.util.logging.Logger.getLogger(Hangman.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  459.         } catch (InstantiationException ex) {
  460.             java.util.logging.Logger.getLogger(Hangman.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  461.         } catch (IllegalAccessException ex) {
  462.             java.util.logging.Logger.getLogger(Hangman.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  463.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  464.             java.util.logging.Logger.getLogger(Hangman.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  465.         }
  466.         //</editor-fold>
  467.  
  468.         /* Create and display the form */
  469.         java.awt.EventQueue.invokeLater(new Runnable() {
  470.             public void run() {
  471.                 new Hangman().setVisible(true);
  472.             }
  473.         });
  474.     }
  475.  
  476.     // Variables declaration - do not modify                    
  477.     private javax.swing.JTextField answerTextField;
  478.     private javax.swing.JButton guessButton;
  479.     private javax.swing.JLabel guessLabel;
  480.     private javax.swing.JLabel iconLabel;
  481.     private javax.swing.JButton jButton1;
  482.     private javax.swing.JButton nextButton;
  483.     private javax.swing.JLabel wordLabel;
  484.     // End of variables declaration                  
  485. }
Advertisement
Add Comment
Please, Sign In to add comment