diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/chimes.wav b/chimes.wav new file mode 100644 index 0000000..94f348b Binary files /dev/null and b/chimes.wav differ diff --git a/chord.wav b/chord.wav new file mode 100644 index 0000000..8c2dcd0 Binary files /dev/null and b/chord.wav differ diff --git a/critical-stop.wav b/critical-stop.wav new file mode 100644 index 0000000..1e46781 Binary files /dev/null and b/critical-stop.wav differ diff --git a/ding.wav b/ding.wav new file mode 100644 index 0000000..453b231 Binary files /dev/null and b/ding.wav differ diff --git a/error.wav b/error.wav new file mode 100644 index 0000000..01d628e Binary files /dev/null and b/error.wav differ diff --git a/logoff.wav b/logoff.wav new file mode 100644 index 0000000..4b218db Binary files /dev/null and b/logoff.wav differ diff --git a/logon.wav b/logon.wav new file mode 100644 index 0000000..126923b Binary files /dev/null and b/logon.wav differ diff --git a/out/production/randomNumberGenerator/Frame$1.class b/out/production/randomNumberGenerator/Frame$1.class new file mode 100644 index 0000000..785e5bd Binary files /dev/null and b/out/production/randomNumberGenerator/Frame$1.class differ diff --git a/out/production/randomNumberGenerator/Frame$2.class b/out/production/randomNumberGenerator/Frame$2.class new file mode 100644 index 0000000..db183e8 Binary files /dev/null and b/out/production/randomNumberGenerator/Frame$2.class differ diff --git a/out/production/randomNumberGenerator/Frame$3.class b/out/production/randomNumberGenerator/Frame$3.class new file mode 100644 index 0000000..ce69833 Binary files /dev/null and b/out/production/randomNumberGenerator/Frame$3.class differ diff --git a/out/production/randomNumberGenerator/Frame$4.class b/out/production/randomNumberGenerator/Frame$4.class new file mode 100644 index 0000000..3697b4f Binary files /dev/null and b/out/production/randomNumberGenerator/Frame$4.class differ diff --git a/out/production/randomNumberGenerator/Frame.class b/out/production/randomNumberGenerator/Frame.class new file mode 100644 index 0000000..afd3529 Binary files /dev/null and b/out/production/randomNumberGenerator/Frame.class differ diff --git a/out/production/randomNumberGenerator/Game.class b/out/production/randomNumberGenerator/Game.class new file mode 100644 index 0000000..5370027 Binary files /dev/null and b/out/production/randomNumberGenerator/Game.class differ diff --git a/out/production/randomNumberGenerator/IncorrectAnswerException.class b/out/production/randomNumberGenerator/IncorrectAnswerException.class new file mode 100644 index 0000000..e1f481d Binary files /dev/null and b/out/production/randomNumberGenerator/IncorrectAnswerException.class differ diff --git a/out/production/randomNumberGenerator/Interval.class b/out/production/randomNumberGenerator/Interval.class new file mode 100644 index 0000000..226494f Binary files /dev/null and b/out/production/randomNumberGenerator/Interval.class differ diff --git a/out/production/randomNumberGenerator/InvalidInputException.class b/out/production/randomNumberGenerator/InvalidInputException.class new file mode 100644 index 0000000..8bd6e69 Binary files /dev/null and b/out/production/randomNumberGenerator/InvalidInputException.class differ diff --git a/out/production/randomNumberGenerator/Main.class b/out/production/randomNumberGenerator/Main.class index 993553a..55cb23a 100644 Binary files a/out/production/randomNumberGenerator/Main.class and b/out/production/randomNumberGenerator/Main.class differ diff --git a/out/production/randomNumberGenerator/MultipleFrames.class b/out/production/randomNumberGenerator/MultipleFrames.class new file mode 100644 index 0000000..f5dddf2 Binary files /dev/null and b/out/production/randomNumberGenerator/MultipleFrames.class differ diff --git a/out/production/randomNumberGenerator/OutOfBoundaryException.class b/out/production/randomNumberGenerator/OutOfBoundaryException.class new file mode 100644 index 0000000..aca4166 Binary files /dev/null and b/out/production/randomNumberGenerator/OutOfBoundaryException.class differ diff --git a/src/Frame.java b/src/Frame.java new file mode 100644 index 0000000..1352691 --- /dev/null +++ b/src/Frame.java @@ -0,0 +1,232 @@ +import javax.sound.sampled.*; +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.io.File; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +public class Frame { + + // variables declaration and initialization + private static JButton play = new JButton("PLAY"); + private static JButton quit = new JButton("QUIT"); + private static JButton enter = new JButton("ENTER"); + + private static JLabel lblGuess = new JLabel("Enter your guess:"); + private static JLabel errorMsg = new JLabel(""); + + private static JTextField guessField = new JTextField(10); + + private static JLabel interval = new JLabel(); + private static JFrame main, f2; + + static int tries, start, end, randomNum; + static Interval ivl; + + private static void GUI() { + + main = new JFrame("Game"); + f2 = new JFrame("Game"); + main.setSize(280, 150); + f2.setSize(600, 330); + main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // buttons layout + main.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 40)); + main.add(play); + main.add(quit); + main.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); + + // make buttons work + makePlayWork(); + makeQuitWork(); + makeEnterWork(); + + // center windows + main.setLocationRelativeTo(null); + f2.setLocationRelativeTo(null); + + main.setVisible(true); + } + + // place buttons, text labels, and text fields + private static void placeStuff() { + + f2.setLayout(new GridBagLayout()); + GridBagConstraints gbc = new GridBagConstraints(); + gbc.gridwidth = GridBagConstraints.REMAINDER; + + gbc.insets = new Insets(35, 0, 30, 0); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.gridx = 0; + gbc.gridy = 0; + f2.add(interval, gbc); + + gbc.insets = new Insets(35, 0, 0, 0); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.gridx = 0; + gbc.gridy = 1; + f2.add(lblGuess, gbc); + + gbc.insets = new Insets(5, 0, 0, 0); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.ipady = 10; + gbc.gridx = 0; + gbc.gridy = 3; + f2.add(guessField, gbc); + + gbc.insets = new Insets(0, 0, 0, 0); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.gridx = 0; + gbc.gridy = 4; + f2.add(errorMsg, gbc); + + gbc.insets = new Insets(15, 0, 10, 0); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.gridx = 0; + gbc.gridy = 5; + f2.add(enter, gbc); + } + + private static void secondFrame() { + + main.dispose(); + placeStuff(); + + // accepts only numeric digits and no other keys + guessField.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent ke) { + char c = ke.getKeyChar(); + if ((c < '0') || (c > '9')) { + ke.consume(); + if (Character.isLetter(c)) { + errorMsg.setText("Invalid input"); + errorMsg.setForeground(Color.RED); + } + } else errorMsg.setForeground(f2.getBackground()); + } + }); + + guessField.setHorizontalAlignment(JTextField.CENTER); + guessField.setFont(new Font("Arial", Font.PLAIN, 20)); + interval.setFont(new Font("Arial", Font.BOLD, 25)); + ivl = new Interval(0, 100); + start = ivl.getStart(); + end = ivl.getEnd(); + randomNum = (int) (Math.random() * (end - start + 1) + start); + updateInterval(); + JOptionPane.showMessageDialog(f2, "Welcome to BINGO! A random number has been " + + "generated 1 and 100.", "BINGO! Welcome Message", JOptionPane.INFORMATION_MESSAGE, null); + playSound("start.wav"); + f2.setVisible(true); + f2.getRootPane().setDefaultButton(enter); + } + + // from http://suavesnippets.blogspot.com/2011/06/add-sound-on-jbutton-click-in-java.html + private static void playSound(String soundName) + { + try + { + AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile()); + Clip clip = AudioSystem.getClip(); + clip.open(audioInputStream); + clip.start(); + } + catch(Exception ex) + { + System.out.println("Error with playing sound."); + ex.printStackTrace(); + } + } + + // action listener for play button + private static void makePlayWork() { + play.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + playSound("start.wav"); + playSound("logon.wav"); + secondFrame(); + } + }); + } + + // action listener for quit button + private static void makeQuitWork() { + quit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + playSound("start.wav"); + int confirmation = JOptionPane.showConfirmDialog(null, "Are you sure" + + " you want to quit the game?", "Exit Game?", JOptionPane.YES_NO_CANCEL_OPTION); + if (confirmation == JOptionPane.YES_OPTION) { + playSound("start.wav"); + playSound("logoff.wav"); + main.dispose(); + } else playSound("start.wav"); + } + }); + } + + // action listener for enter button + private static void makeEnterWork() { + enter.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + playSound("start.wav"); + if (!guessField.getText().equals("")) { + int guessNum = Integer.parseInt(guessField.getText()); + if (ivl.withinBoundary(guessNum)) { + if (guessNum != randomNum) { + playSound("chord.wav"); + tries++; + ivl.updateInterval(guessNum, randomNum); + JOptionPane.showMessageDialog(null, "WRONG!", "ERROR", JOptionPane.ERROR_MESSAGE); + updateInterval(); + } else { + playSound("tada.wav"); + JOptionPane.showMessageDialog(null, "BINGO! The secret number is " + guessNum + ". And it took you " + tries + " trial(s)."); + playSound("ding.wav"); + int res = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "One More?", JOptionPane.YES_NO_OPTION); + if (res == JOptionPane.YES_OPTION) { + playSound("start.wav"); + gameReset(); + playSound("logon.wav"); + JOptionPane.showMessageDialog(null, "A new game has started. Good Luck!"); + playSound("start.wav"); + } else { + playSound("start.wav"); + JOptionPane.showMessageDialog(null, "Thanks for your participation! Please come again!"); + playSound("start.wav"); + playSound("logoff.wav"); + System.exit(0); + } + } + } else { + playSound("critical-stop.wav"); + JOptionPane.showMessageDialog(null, "OUT OF BOUNDARY!", "ERROR", JOptionPane.ERROR_MESSAGE); + playSound("start.wav"); + } + guessField.setText(""); + } + } + }); + } + + // update interval displayed + private static void updateInterval() { + interval.setText(ivl.getStart() + " - " + ivl.getEnd()); + } + + // function for resetting game + private static void gameReset() { + tries = 0; + ivl = new Interval(0, 100); + updateInterval(); + } + + public static void main(String[] args) { GUI(); } + + +} diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..4b8e20a --- /dev/null +++ b/src/Game.java @@ -0,0 +1,56 @@ +import java.util.Scanner; +import java.io.IOException; + +public class Game { + + int randomNum, guessNum, start, end, trials; + Interval interval; + Scanner scanner; + + public Game() throws Exception { + scanner = new Scanner(System.in); + interval = new Interval (1, 100); + this.start = interval.getStart(); + this.end = interval.getEnd(); + this.randomNum = (int) (Math.random() * (end - start + 1) + start); +// System.out.println(randomNum); + Go(); + } + + private void Go() throws Exception { + System.out.println("Welcome to BINGO! A random number has been generated between 1 and 100."); + while (guessNum != randomNum) { + trials++; + try { + System.out.println("Please enter a guess:"); + Guess(); + } catch (IncorrectAnswerException e) { + System.out.println("Answer is incorrect. Please try again."); + } catch (OutOfBoundaryException e) { + System.out.println("Answer is out of possible boundary. Please try again."); + } catch (InvalidInputException e) { + System.out.println("Answer is invalid. Please try again."); + } + } + System.out.println("Thanks for playing BINGO! See you next time!"); + } + + private void Guess() throws Exception { + if (scanner.hasNextInt()) { + guessNum = scanner.nextInt(); + if (interval.withinBoundary(guessNum)) { + if (guessNum != randomNum) { + interval.updateInterval(guessNum, randomNum); + interval.printInterval(); + throw new IncorrectAnswerException(); + } else { + System.out.println("BINGO! The secret number is " + guessNum + ". And it took you " + trials + " trial(s)."); + guessNum = randomNum; + } + } else throw new OutOfBoundaryException(); + } else throw new InvalidInputException(); + } + + + +} diff --git a/src/IncorrectAnswerException.java b/src/IncorrectAnswerException.java new file mode 100644 index 0000000..1818192 --- /dev/null +++ b/src/IncorrectAnswerException.java @@ -0,0 +1,2 @@ +public class IncorrectAnswerException extends Exception{ +} diff --git a/src/Interval.java b/src/Interval.java new file mode 100644 index 0000000..d30cc28 --- /dev/null +++ b/src/Interval.java @@ -0,0 +1,49 @@ +import java.util.ArrayList; + +public class Interval { + + private ArrayList numInterval; + private int start, end; + +// void fillInterval (int start, int end) { +// if (start !=) +// this.numInterval = new ArrayList(end - start); +// int i=start; +// while (i= start && guessNum <= end; + } + + public void updateInterval (int guessNum, int randomNum) { + if (guessNum <= randomNum) setStart (guessNum); + else setEnd (guessNum); + } + + public void printInterval() { + System.out.println("The Range is now: " + start + " - " + end); + } + + + + + + + + +} diff --git a/src/InvalidInputException.java b/src/InvalidInputException.java new file mode 100644 index 0000000..3573a67 --- /dev/null +++ b/src/InvalidInputException.java @@ -0,0 +1,2 @@ +public class InvalidInputException extends Exception { +} diff --git a/src/Main.java b/src/Main.java index a0428da..992dd3e 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,11 @@ +import java.util.ArrayList; +import java.util.Scanner; + public class Main { - public static void main(String[] args) { - System.out.println("Hello World!"); + public static void main(String[] args) throws Exception{ + + new Game(); + } } diff --git a/src/MultipleFrames.java b/src/MultipleFrames.java new file mode 100644 index 0000000..1a53a01 --- /dev/null +++ b/src/MultipleFrames.java @@ -0,0 +1,39 @@ +import javax.swing.*; +import javax.swing.event.*; +import java.awt.*; +import java.awt.event.*; + +public class MultipleFrames { + JDesktopPane desk; + JInternalFrame frame1, frame2, frame3, frame4; + JFrame frame; + + public static void main(String[] args) { + MultipleFrames d = new MultipleFrames(); + } + + public MultipleFrames() { + frame = new JFrame("Multiple Frames"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + desk = new JDesktopPane(); + frame1 = new JInternalFrame("Frame1", true, true, true, true); + frame1.setBounds(20, 200, 150, 100); + frame1.setVisible(true); + frame2 = new JInternalFrame("Frame2", true, true, true, true); + frame2.setBounds(20, 140, 150, 100); + frame2.setVisible(true); + frame3 = new JInternalFrame("Frame3", true, true, true, true); + frame3.setBounds(20, 80, 150, 100); + frame3.setVisible(true); + frame4 = new JInternalFrame("Frame4", true, true, true, true); + frame4.setBounds(20, 20, 150, 100); + frame4.setVisible(true); + desk.add(frame1); + desk.add(frame2); + desk.add(frame3); + desk.add(frame4); + frame.add(desk); + frame.setSize(400, 400); + frame.setVisible(true); + } +} \ No newline at end of file diff --git a/src/OutOfBoundaryException.java b/src/OutOfBoundaryException.java new file mode 100644 index 0000000..6987818 --- /dev/null +++ b/src/OutOfBoundaryException.java @@ -0,0 +1,2 @@ +public class OutOfBoundaryException extends Exception { +} diff --git a/start.wav b/start.wav new file mode 100644 index 0000000..2462b27 Binary files /dev/null and b/start.wav differ diff --git a/tada.wav b/tada.wav new file mode 100644 index 0000000..33a374b Binary files /dev/null and b/tada.wav differ