Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package test3;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.Random;
- import javax.swing.Timer;
- /**
- *
- * @author Nomi
- */
- public class NewJFrame extends javax.swing.JFrame {
- ActionListener timerListener; // Swing's timer listens to timerListener.
- private String currentRandomWord = ""; // User will see this word with a vowel pulled out at random position.
- private String userInput = ""; // Get the user input.
- Game1 g = new Game1(); // Game1 has the words list.
- private int minutes = 1; // This is how long the game will last in minutes. Make it final?
- private int seconds = 60; // This is how long the game will last in seconds. Make it final?
- private final int milliSecond = 1000;
- javax.swing.Timer timer; // Swing's timer
- /*
- * Sets up the timer Each event is fired at a second's interval.
- */
- public void setupTimer() {
- System.out.println("going in setupTimer"); // For Debugging. To be taken out.
- // labelRandomWord.setText(g1.getRandomWord());
- // This should go in a better place?
- labelTimer.setText(minutes + ":" + seconds); // Sets up the labelTimer.
- timerListener = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- javax.swing.SwingUtilities.invokeLater(new updateTimer());
- }
- };
- timer = new Timer(milliSecond, timerListener); // Each event is fired at a second's interval. Make the second final?
- currentRandomWord = g.getRandomWord().toLowerCase();
- labelRandomWord.setText(g.takeArandomVowelOut(currentRandomWord));
- timer.start(); // Starts the timer.
- }
- /*
- * Timer uses a thread. Can sleep the thread too?
- */
- private class updateTimer implements Runnable {
- public void run() {
- System.out.println("going in run()"); // For debugging. To be taken out.
- seconds--;
- if (seconds <= 0) {
- seconds = 60;
- minutes--;
- if (minutes < 0) {
- timer.stop();
- }
- }
- if (timer.isRunning()) {
- userTest();
- }
- labelTimer.setText(minutes + ":" + seconds);
- if (!timer.isRunning()) {
- labelTimer.setText("Time's up");
- textFieldWordInput.setEditable(false);
- textFieldWordInput.setEnabled(false);
- }
- }
- }
- public void userTest() {
- System.out.println(currentRandomWord);
- // This listens if the user has hit enter
- // Need to go over the game rules.
- // What to do if the user inputs a wrong word?
- // For now I will generate a new random word, maybe come back to it later?
- textFieldWordInput.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- userInput = textFieldWordInput.getText();
- if ((userInput.equalsIgnoreCase(currentRandomWord)) || (g.check(userInput))) {
- System.out.println("Test 1");
- //textFieldWordInput.setText("");
- labelStatus.setText("Word Exists");
- currentRandomWord = g.getRandomWord();
- labelRandomWord.setText(g.takeArandomVowelOut(currentRandomWord));
- } else {
- System.out.println("Test 2");
- labelStatus.setText("Word Does Not Exists");
- textFieldWordInput.setText("");
- }
- //textFieldWordInput.setText("");
- }
- });
- }
- /**
- * Creates new form NewJFrame
- */
- public NewJFrame() {
- g.buildList();
- initComponents();
- setupTimer();
- }
- /**
- * 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() {
- labelRandomWord = new javax.swing.JLabel();
- textFieldWordInput = new javax.swing.JTextField();
- labelTimer = new javax.swing.JLabel();
- labelStatus = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- labelRandomWord.setText("jLabel1");
- textFieldWordInput.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- textFieldWordInputActionPerformed(evt);
- }
- });
- labelTimer.setText("jLabel2");
- labelStatus.setText("jLabel1");
- 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()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(labelRandomWord, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(textFieldWordInput, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 150, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(labelTimer, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
- .addComponent(labelStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(labelRandomWord)
- .addComponent(labelTimer))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(textFieldWordInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(labelStatus))
- .addContainerGap(237, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void textFieldWordInputActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- /**
- * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JLabel labelRandomWord;
- private javax.swing.JLabel labelStatus;
- private javax.swing.JLabel labelTimer;
- private javax.swing.JTextField textFieldWordInput;
- // End of variables declaration
- }
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package test3;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.Random;
- import java.io.File;
- /**
- *
- * @author Nomi
- */
- public class Game1 {
- public ArrayList<String> wordsList = new ArrayList<String>();
- private ArrayList<Integer> randomIntegers = new ArrayList<Integer>(); // Store random words so we don't get repeats.
- private final int randomWordSize = 71447; // Change it back to 71447
- private ArrayList<Integer> saveRandom = new ArrayList<Integer>();
- private Random random = new Random();
- public void buildList() {
- try {
- File dir1 = new File(".");
- System.out.println("Current dir : " + dir1.getCanonicalPath());
- InputStream in = getClass().getResourceAsStream("SINGLE.txt");
- //FileInputStream fstream = new FileInputStream(getClass().getResource("SINGLE.txt"));
- //DataInputStream in = new DataInputStream(fstream);
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String strLine = "";
- while ((strLine = br.readLine()) != null) {
- if (strLine.length() >= 5 && strLine.length() <= 9) {
- wordsList.add(strLine);
- }
- }
- System.out.println("Success");
- getRandomWordsListSize();
- } catch (Exception e) {
- System.out.println("Words of length between 5 and 9 were not added to the arrayList.");
- e.printStackTrace();
- }
- }
- public boolean check(String word) {
- for (int i = 0; i < wordsList.size(); i++) {
- if (word.equalsIgnoreCase(wordsList.get(i))) {
- return true;
- }
- }
- return false;
- }
- // Maybe make this final?
- public int getRandomWordsListSize() {
- return randomWordSize;
- }
- // I don't think this method is doing what I think it is.
- // Should test this method throughly.
- // For now this suffice.
- public String getRandomWord() {
- int temp = random.nextInt(randomWordSize);
- // I am actually not 100% sure what this if statement is doing.
- // Should test to make sure. Is this saying
- // generate a random number until a new number has
- // has not been generated that was not in the
- // randomIntegers list?
- if (randomIntegers.contains(temp)) {
- temp = random.nextInt(randomWordSize);
- }
- randomIntegers.add(temp);
- return wordsList.get(temp);
- }
- public String takeArandomVowelOut(String word) {
- ArrayList<Integer> vowelPositions = new ArrayList<Integer>();
- char cTemp;
- int temp = 0;
- int temp2 = 0;
- for (int i = 0; i < word.length(); i++) {
- cTemp = word.toLowerCase().charAt(i);
- if (cTemp == 'a' || cTemp == 'e' || cTemp == 'i' || cTemp == 'o' || cTemp == 'u' || cTemp == 'y') {
- vowelPositions.add(i);
- }
- }
- System.out.println(word);
- System.out.println("vowelPositions.size = " + vowelPositions.size());
- temp = random.nextInt(vowelPositions.size());
- temp2 = vowelPositions.get(temp);
- String sTemp = word.substring(0, temp2);
- sTemp = sTemp.concat(word.substring(temp2 + 1, word.length()));
- vowelPositions.clear();
- return sTemp;
- }
- public String getWord(int i)
- {
- return wordsList.get(i);
- }
- /**
- * @param args the command line arguments
- */
- // public static void main(String[] args)
- // {
- // // TODO code application logic here
- // Game1 gg = new Game1();
- // gg.takeAvowelOut("white");
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment