Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileReader;
- import java.io.IOException;
- import java.net.URL;
- import java.util.Random;
- import javax.swing.ImageIcon;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Maikelele
- */
- public final class Hangman extends javax.swing.JFrame {
- /**
- * Creates new form Hangman
- */
- public Hangman() {
- initComponents();
- getRootPane().setDefaultButton(guessButton);
- gameSetup();
- loadText();
- setWord();
- }
- //Make sure you add an array to stop dupliates
- Random rand = new Random();
- int gameNumber = 1000;
- String[] wordBank = new String[gameNumber];
- String currentWord = "";
- int wordCount;
- int wrongGuesses = 0;
- String[] currentWordCharacter = new String[100];
- String[] currentWordGuesses = new String[100];
- int rNumber = 0;
- int difficulty = 0;
- int wordMode = 1;
- public void gameSetup() {
- Object[] wordPlay = {"Dictionary", "Custom", "Half and Half"};
- wordMode = JOptionPane.showOptionDialog(this,
- "What words will you play with?"
- + "\nCustom: User Submitted Words"
- + "\nDictionary: List of 354k words"
- + "\nHalf and Half: Mixture of Both",
- "Hangman Setup",
- JOptionPane.YES_NO_CANCEL_OPTION,
- JOptionPane.QUESTION_MESSAGE,
- null,
- wordPlay,
- wordPlay[2]);
- System.out.println(wordMode);
- if (wordMode == 0) {
- Object[] difficultyLevels = {"Easy", "Normal", "Hard"};
- difficulty = JOptionPane.showOptionDialog(this,
- "What will the difficulty be?"
- + "\nEasy: 8-10 letter words"
- + "\nNormal: 6-12 letter words"
- + "\nHard: < 6 & > 12 letter words",
- "Hangman Setup",
- JOptionPane.YES_NO_CANCEL_OPTION,
- JOptionPane.QUESTION_MESSAGE,
- null,
- difficultyLevels,
- difficultyLevels[2]);
- }
- }
- public void loadText() {
- String[][] wordBankTemp = new String[2][1000000];
- FileReader reader = null;
- String wordValue = "";
- int character = 0;
- int lastCharacter = 0;
- int wordCountCustom = 0;
- wordCount = 0;
- try {
- reader = new FileReader("src/hangman.txt");
- while ((character = reader.read()) != -1) {
- if ((char) lastCharacter == '\n') {
- wordCount++;
- }
- if ((char) character != '\n') {
- wordValue += (char) character;
- wordBankTemp[0][wordCount] = wordValue;
- } else {
- wordValue = "";
- }
- lastCharacter = character;
- }
- reader.close();
- } catch (IOException e) {
- System.out.println(e);
- }
- character = 0;
- wordValue = "";
- try {
- reader = new FileReader("src/hangman_custom.txt");
- while ((character = reader.read()) != -1) {
- if ((char) lastCharacter == '\n') {
- wordCountCustom++;
- }
- if ((char) character != '\n') {
- wordValue += (char) character;
- wordBankTemp[1][wordCountCustom] = wordValue;
- } else {
- wordValue = "";
- }
- lastCharacter = character;
- }
- reader.close();
- } catch (IOException e) {
- System.out.println(e);
- }
- wordCount++;
- wordCountCustom++;
- for (int i = 0; i < gameNumber; i++) {//Add something here to cancel out if there is any character that isn't a letter
- rNumber = rand.nextInt(2);
- if (wordMode == 0 || (wordMode == 2 && rNumber == 0)) {
- rNumber = rand.nextInt(wordCount);
- for (int j = 0; j < wordBankTemp[0][rNumber].length(); j++) {
- if (wordBankTemp[0][rNumber].charAt(j) == '\'') {
- i--;
- break;
- } else {
- wordBank[i] = wordBankTemp[0][rNumber];
- }
- }
- } else {
- rNumber = rand.nextInt(wordCountCustom);
- for (int j = 0; j < wordBankTemp[1][rNumber].length(); j++) {
- if (wordBankTemp[1][rNumber].charAt(j) == '\'') {
- i--;
- break;
- } else {
- wordBank[i] = wordBankTemp[1][rNumber];
- }
- }
- }
- }
- wordCount = 1000;
- }
- public void setWord() {
- //wordCount = 1000;
- wordLabel.setText("");
- currentWord = wordBank[rand.nextInt(gameNumber)];
- System.out.println(currentWord);
- //System.out.println(currentWord);
- //System.out.println(currentWord.length());
- for (int i = 0; i < currentWordCharacter.length; i++) {
- currentWordCharacter[i] = " ";
- }
- for (int i = 0; i < currentWordGuesses.length; i++) {
- currentWordGuesses[i] = " ";
- }
- try {
- for (int i = 0; i <= currentWord.length(); i++) {
- currentWordCharacter[i] = "end";
- currentWordCharacter[i] = "" + currentWord.charAt(i);
- wordLabel.setText(wordLabel.getText() + "_ ");
- }
- } catch (StringIndexOutOfBoundsException e) {
- /*for (int i = 0; i < currentWordCharacter.length; i++) {
- if (currentWordCharacter[i].equals(" ")) {
- break;
- }
- System.out.println(currentWordCharacter[i]);
- }*/
- }
- //System.out.println(currentWord);
- //System.out.println(currentWord + " " + currentWord);
- //System.out.println(currentWord.length());
- wrongGuesses = 0;
- updateGraphics();
- updateWord();
- }
- public void updateWord() {
- wordLabel.setText("");
- for (int i = 0; i < currentWord.length(); i++) {
- if (currentWordCharacter[i].equals("end")) {
- break;
- } else if (currentWordCharacter[i].length() > 1) {
- wordLabel.setText(wordLabel.getText() + currentWordCharacter[i].charAt(0) + " ");
- } else {
- wordLabel.setText(wordLabel.getText() + "_ ");
- }
- }
- guessLabel.setText("");
- for (int i = 65; i < 91; i++) {
- char character = (char) i;
- for (int j = 0; j < currentWordGuesses.length; j++) {
- if (character == currentWordGuesses[j].charAt(0)) {
- guessLabel.setText(guessLabel.getText() + character);
- } else if ((character + 32) == currentWordGuesses[j].charAt(0)) {
- guessLabel.setText(guessLabel.getText() + character);
- }
- }
- }
- }
- public void updateGraphics() {
- if (wrongGuesses > 6) {
- wrongGuesses = 6;
- } else if (wrongGuesses < 0) {
- wrongGuesses = 0;
- }
- iconLabel.setIcon(new ImageIcon(getClass().getResource("/hangman_graphics/hangman" + String.valueOf(wrongGuesses) + ".png")));
- }
- public void testWin() {
- boolean win = true;
- for (int i = 0; i < currentWordCharacter.length; i++) {
- if (currentWordCharacter[i].equals("end")) {
- break;
- } else if (currentWordCharacter[i].length() != 2) {
- win = false;
- break;
- }
- }
- if (win) {
- System.out.println("Winner");
- Object[] options = {"Yes", "No"};
- String winMessage = "";
- if (wrongGuesses < 1) {
- winMessage = "You are a legend at this!";
- } else if (wrongGuesses < 2) {
- winMessage = "You did pretty well!";
- } else if (wrongGuesses < 3) {
- winMessage = "Not too shabby.";
- } else if (wrongGuesses < 4) {
- winMessage = "Not very good, but you got it.";
- } else {
- winMessage = "You got lucky, nice job.";
- }
- ImageIcon icon = new ImageIcon(getClass().getResource("/hangman_graphics/win.png"));
- int n = JOptionPane.showOptionDialog(this,
- "" + winMessage + "\nThe word was " + currentWord + ".\nWould you like to play again?",
- "Winner",
- JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE,
- icon,
- options,
- options[0]);
- if (n == 0) {
- setWord();
- } else {
- System.exit(0);
- }
- }
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- iconLabel = new javax.swing.JLabel();
- answerTextField = new javax.swing.JTextField();
- guessButton = new javax.swing.JButton();
- wordLabel = new javax.swing.JLabel();
- nextButton = new javax.swing.JButton();
- jButton1 = new javax.swing.JButton();
- guessLabel = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- iconLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hangman_graphics/hangman0.png"))); // NOI18N
- guessButton.setText("Guess");
- guessButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- guessButtonActionPerformed(evt);
- }
- });
- wordLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
- wordLabel.setText("TEXT");
- nextButton.setText("Skip");
- nextButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- nextButtonActionPerformed(evt);
- }
- });
- jButton1.setText("Answer");
- guessLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
- guessLabel.setText("TEXT");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(answerTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(guessButton)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(nextButton)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGap(0, 0, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(iconLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(wordLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(guessLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(iconLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(wordLabel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(guessLabel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 115, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(answerTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(guessButton)
- .addComponent(nextButton)
- .addComponent(jButton1))
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void guessButtonActionPerformed(java.awt.event.ActionEvent evt) {
- boolean guess = false;
- for (int i = 0; i < currentWord.length(); i++) {
- if (answerTextField.getText().length() > 0 && answerTextField.getText().charAt(0) == currentWord.charAt(i)) {
- for (int j = 0; j < currentWordCharacter.length; j++) {
- if (currentWordCharacter[j].length() == 1 && currentWordCharacter[j].charAt(0) == answerTextField.getText().charAt(0)) {
- currentWordCharacter[j] += "1";
- guess = true;
- } else if (!currentWordCharacter[j].equals("end") && currentWordCharacter[j].length() > 1 && currentWordCharacter[j].charAt(0) == answerTextField.getText().charAt(0)) {
- guess = true;
- }
- }
- }
- }
- for (int i = 0; i < currentWordGuesses.length; i++) {
- if (answerTextField.getText().length() > 0 && currentWordGuesses[i].length() > 0 && answerTextField.getText().charAt(0) == currentWordGuesses[i].charAt(0)) {
- guess = true;
- break;
- } else if (answerTextField.getText().length() > 0 && currentWordGuesses[i].equals(" ")) {
- currentWordGuesses[i] = "" + answerTextField.getText().charAt(0);
- break;
- }
- }
- if (answerTextField.getText().length() == 0 || answerTextField.getText().equals(" ") || answerTextField.getText().charAt(0) == ' ') {
- } else if (guess == false) {
- wrongGuesses++;
- if (wrongGuesses > 5) {
- System.out.println("You lose");
- Object[] options = {"Yes", "No"};
- ImageIcon icon = new ImageIcon(getClass().getResource("/hangman_graphics/lose.png"));
- int n = JOptionPane.showOptionDialog(this,
- "You lost!\nThe word was " + currentWord + ".\nWould you like to play again?",
- "Loser",
- JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE,
- icon,
- options,
- options[0]);
- if (n == 0) {
- setWord();
- } else {
- System.exit(0);
- }
- }
- }
- answerTextField.setText("");
- updateWord();
- updateGraphics();
- testWin();
- }
- private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
- setWord();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Hangman.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Hangman.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Hangman.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Hangman.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Hangman().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JTextField answerTextField;
- private javax.swing.JButton guessButton;
- private javax.swing.JLabel guessLabel;
- private javax.swing.JLabel iconLabel;
- private javax.swing.JButton jButton1;
- private javax.swing.JButton nextButton;
- private javax.swing.JLabel wordLabel;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment