/* * Copyright (c) 2010, Oracle. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * * Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* Anagram Game Application */ package com.toy.anagrams.ui; import com.toy.anagrams.lib.WordLibrary; import java.awt.Dimension; import java.awt.Point; import java.awt.Toolkit; import javax.swing.JFrame; import javax.swing.SwingUtilities; /** * Main window of the Anagram Game application. */ public class Anagrams extends JFrame { public static void main(String[] args) { /* Set the Nimbus look and feel */ // /* 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 { javax.swing.UIManager.LookAndFeelInfo[] installedLookAndFeels=javax.swing.UIManager.getInstalledLookAndFeels(); for (int idx=0; idx /* Create and display the form */ SwingUtilities.invokeLater(new Runnable() { public void run() { new Anagrams().setVisible(true); } }); } private int wordIdx = 0; private String hasToHaveOne = ""; private String hasToHaveTwo = ""; private String hasToHaveThree = ""; private String hasToHaveFour = ""; private String hasToHaveFive = ""; private String correctWord = ""; private WordLibrary wordLibrary; /** Creates new form Anagrams */ public Anagrams() { wordLibrary = WordLibrary.getDefault(); initComponents(); this.setTitle("Anagrams ... Optional 5 lowercase letters in Guess field before clicking New Word to enforce those letters being in word."); guessLabel.setText(guessLabel.getText().replace(":" , " (+ pre New Word 5 letters):")); getRootPane().setDefaultButton(guessButton); scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx)); pack(); guessedWord.requestFocusInWindow(); // Center in the screen Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); setLocation(new Point((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.width) / 2)); } /** 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. */ // //GEN-BEGIN:initComponents private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; mainPanel = new javax.swing.JPanel(); scrambledLabel = new javax.swing.JLabel(); scrambledWord = new javax.swing.JTextField(); guessLabel = new javax.swing.JLabel(); guessedWord = new javax.swing.JTextField(); feedbackLabel = new javax.swing.JLabel(); buttonsPanel = new javax.swing.JPanel(); guessButton = new javax.swing.JButton(); nextTrial = new javax.swing.JButton(); mainMenu = new javax.swing.JMenuBar(); fileMenu = new javax.swing.JMenu(); aboutMenuItem = new javax.swing.JMenuItem(); exitMenuItem = new javax.swing.JMenuItem(); setTitle("Anagrams"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12)); mainPanel.setMinimumSize(new java.awt.Dimension(297, 200)); mainPanel.setLayout(new java.awt.GridBagLayout()); scrambledLabel.setText("Scrambled Word:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6); mainPanel.add(scrambledLabel, gridBagConstraints); scrambledWord.setColumns(20); scrambledWord.setEditable(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0); mainPanel.add(scrambledWord, gridBagConstraints); guessLabel.setDisplayedMnemonic('Y'); guessLabel.setLabelFor(guessedWord); guessLabel.setText("Your Guess:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 6); mainPanel.add(guessLabel, gridBagConstraints); guessedWord.setColumns(20); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0); mainPanel.add(guessedWord, gridBagConstraints); feedbackLabel.setText(" "); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0); mainPanel.add(feedbackLabel, gridBagConstraints); buttonsPanel.setLayout(new java.awt.GridBagLayout()); guessButton.setMnemonic('G'); guessButton.setText("Guess"); guessButton.setToolTipText("Guess the scrambled word."); guessButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { guessedWordActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6); buttonsPanel.add(guessButton, gridBagConstraints); nextTrial.setMnemonic('N'); nextTrial.setText("New Word"); nextTrial.setToolTipText("Fetch a new word."); nextTrial.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { nextTrialActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST; gridBagConstraints.weighty = 1.0; buttonsPanel.add(nextTrial, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weighty = 1.0; mainPanel.add(buttonsPanel, gridBagConstraints); getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER); fileMenu.setMnemonic('F'); fileMenu.setText("File"); aboutMenuItem.setMnemonic('A'); aboutMenuItem.setText("About"); aboutMenuItem.setToolTipText("About"); aboutMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { aboutMenuItemActionPerformed(evt); } }); fileMenu.add(aboutMenuItem); exitMenuItem.setMnemonic('E'); exitMenuItem.setText("Exit"); exitMenuItem.setToolTipText("Quit Team, Quit!"); exitMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitMenuItemActionPerformed(evt); } }); fileMenu.add(exitMenuItem); mainMenu.add(fileMenu); setJMenuBar(mainMenu); }// //GEN-END:initComponents private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed new About(this).setVisible(true); }//GEN-LAST:event_aboutMenuItemActionPerformed private void nextTrialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextTrialActionPerformed int tries = 0, fivetest = 0, i; wordIdx = (wordIdx + 1) % wordLibrary.getSize(); String thewordbits = ""; if (guessedWord.getText().equals(correctWord)) { correctWord = ""; } else if (guessedWord.getText().replace(",", "").length() < 6 && guessedWord.getText().replace(",", "").length() > 0) { for (i=0; i 0 || hasToHaveTwo.length() > 0 || hasToHaveThree.length() > 0 || hasToHaveFour.length() > 0 || hasToHaveFive.length() > 0)) { fivetest = 0; if (hasToHaveOne.length() == 0 || thewordbits.contains(hasToHaveOne)) { fivetest++; if (hasToHaveOne.length() != 0) thewordbits = thewordbits.replaceFirst(hasToHaveOne, " "); if (hasToHaveTwo.length() == 0 || thewordbits.contains(hasToHaveTwo)) { fivetest++; if (hasToHaveTwo.length() != 0) thewordbits = thewordbits.replaceFirst(hasToHaveTwo, " "); if (hasToHaveThree.length() == 0 || thewordbits.contains(hasToHaveThree)) { fivetest++; if (hasToHaveThree.length() != 0) thewordbits = thewordbits.replaceFirst(hasToHaveThree, " "); if (hasToHaveFour.length() == 0 || thewordbits.contains(hasToHaveFour)) { fivetest++; if (hasToHaveFour.length() != 0) thewordbits = thewordbits.replaceFirst(hasToHaveFour, " "); if (hasToHaveFive.length() == 0 || thewordbits.contains(hasToHaveFive)) { fivetest++; } } } } } if (fivetest == 5) { tries = 14000; } else { wordIdx = (wordIdx + 1) % wordLibrary.getSize(); thewordbits = wordLibrary.getScrambledWord(wordIdx); } tries++; } hasToHaveOne = hasToHaveTwo = hasToHaveThree = hasToHaveFour = hasToHaveFive = ""; feedbackLabel.setText(" "); scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx)); guessedWord.setText(""); guessLabel.setText(guessLabel.getText().replace(" (+ pre New Word 5 letters)" , " ")); getRootPane().setDefaultButton(guessButton); guessedWord.requestFocusInWindow(); }//GEN-LAST:event_nextTrialActionPerformed private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed System.exit(0); }//GEN-LAST:event_exitMenuItemActionPerformed private void guessedWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guessedWordActionPerformed if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){ correctWord = guessedWord.getText(); feedbackLabel.setText("Correct! Try a new word!"); getRootPane().setDefaultButton(nextTrial); guessLabel.setText(guessLabel.getText().replace(" (+ pre New Word 5 letters)" , " ").replace(":" , " (+ pre New Word 5 letters):")); } else { correctWord = ""; feedbackLabel.setText("Incorrect! Try again!"); guessedWord.setText(""); guessLabel.setText(guessLabel.getText().replace(" (+ pre New Word 5 letters)" , " ").replace(":" , " (+ pre New Word 5 letters):")); } guessedWord.requestFocusInWindow(); }//GEN-LAST:event_guessedWordActionPerformed private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm System.exit(0); }//GEN-LAST:event_exitForm // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenuItem aboutMenuItem; private javax.swing.JPanel buttonsPanel; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JLabel feedbackLabel; private javax.swing.JMenu fileMenu; private javax.swing.JButton guessButton; private javax.swing.JLabel guessLabel; private javax.swing.JTextField guessedWord; private javax.swing.JMenuBar mainMenu; private javax.swing.JPanel mainPanel; private javax.swing.JButton nextTrial; private javax.swing.JLabel scrambledLabel; private javax.swing.JTextField scrambledWord; // End of variables declaration//GEN-END:variables }