/* * MyersAssignment3.java * * Created on January 31, 2008, 1:58 PM by Brian Myers, myersb1@lasalle.edu * Part of Scrabble to draw 7 tiles and allow the user to search the drawn tiles for the position of a tile * */ package Myers3; //import statements import java.util.Random; import javax.swing.JOptionPane; import java.awt.*; public class MyersAssignment3 extends javax.swing.JFrame { //declare a long string and instantiate it with each possible tile in the proper distribution String Letters = "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ "; //declare a new string that when the button is clicked will store the remaining tiles String newLetters = Letters; //declare another string which will hold the drawn tiles String tiles = ""; /** Creates new form MyersAssignment3 */ public MyersAssignment3() { initComponents(); } /** 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. */ .... all the GUI creation stuff is omitted private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed //get from the text field what the user is searching for String search = txtSearch.getText(); //find its position in the drawn tiles int foundAt = tiles.indexOf(search); //display the position, or if negative it was not found JOptionPane.showMessageDialog(null, " '" + search + "'" + " was found at: " + foundAt + " (if negative, it wasn't found)."); }//GEN-LAST:event_btnSearchActionPerformed private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed // exit the program System.exit(0); }//GEN-LAST:event_btnExitActionPerformed private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed //make the tiles and search invisible txtSearch.setVisible(false); btnSearch.setVisible(false); lblTilesDrawn.setVisible(false); lblSearch.setVisible(false); }//GEN-LAST:event_btnClearActionPerformed private void btnDrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDrawActionPerformed //make the tiles and search visible txtSearch.setVisible(true); btnSearch.setVisible(true); lblTilesDrawn.setVisible(true); lblSearch.setVisible(true); //create a random number generator Random generator = new Random(); //declare variables for length of and position in the string int length; int position; //declare variables which will be needed in storing the remaining tiles int beginRemove; String startNew = ""; String endNew = ""; //randomly generate the first tile and remove it from the remaining tiles length = newLetters.length(); position = generator.nextInt(length); String tile1 = newLetters.substring(position,position + 1); beginRemove = position; startNew = newLetters.substring(0,beginRemove); endNew = newLetters.substring(position + 1); newLetters = startNew + endNew; //randomly generate the second tile and remove it from the remaining tiles length = newLetters.length(); position = generator.nextInt(length); String tile2 = newLetters.substring(position,position + 1); beginRemove = position; startNew = newLetters.substring(0,beginRemove); endNew = newLetters.substring(position + 1); newLetters = startNew + endNew; //randomly generate the third tile and remove it from the remaining tiles length = newLetters.length(); position = generator.nextInt(length); String tile3 = newLetters.substring(position,position + 1); beginRemove = position; startNew = newLetters.substring(0,beginRemove); endNew = newLetters.substring(position + 1); newLetters = startNew + endNew; //randomly generate the fourth tile and remove it from the remaining tiles length = newLetters.length(); position = generator.nextInt(length); String tile4 = newLetters.substring(position,position + 1); beginRemove = position; startNew = newLetters.substring(0,beginRemove); endNew = newLetters.substring(position + 1); newLetters = startNew + endNew; //randomly generate the fifth tile and remove it from the remaining tiles length = newLetters.length(); position = generator.nextInt(length); String tile5 = newLetters.substring(position,position + 1); beginRemove = position; startNew = newLetters.substring(0,beginRemove); endNew = newLetters.substring(position + 1); newLetters = startNew + endNew; //randomly generate the sixth tile and remove it from the remaining tiles length = newLetters.length(); position = generator.nextInt(length); String tile6 = newLetters.substring(position,position + 1); beginRemove = position; startNew = newLetters.substring(0,beginRemove); endNew = newLetters.substring(position + 1); newLetters = startNew + endNew; //randomly generate the seventh tile and remove it from the remaining tiles length = newLetters.length(); position = generator.nextInt(length); String tile7 = newLetters.substring(position,position + 1); beginRemove = position; startNew = newLetters.substring(0,beginRemove); endNew = newLetters.substring(position + 1); newLetters = startNew + endNew; //assign the seven tiles into the 'tiles' string tiles = tile1 + tile2 + tile3 + tile4 + tile5 + tile6 + tile7; //draw the tiles lblTilesDrawn.setText(tiles); }//GEN-LAST:event_btnDrawActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MyersAssignment3().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnClear; private javax.swing.JButton btnDraw; private javax.swing.JButton btnExit; private javax.swing.JButton btnSearch; private javax.swing.JLabel lblHeading; private javax.swing.JLabel lblSearch; private javax.swing.JLabel lblTiles; private javax.swing.JLabel lblTilesDrawn; private javax.swing.JTextField txtSearch; // End of variables declaration//GEN-END:variables }