Coding Horror, be scared

UnScramble Game GUI

I am working on a GUI for a game where you guess scrambled words. This code is a work-in-progress so I had to break some things that were working to try to get the GUI to run. I want for the user to press the "Begin Game" button in the GUI_Project class and then print the "printwords" method from the Scramble class into the "Word List" text area and then I want for the user to input the answers by printing the "question" method from the Scramble class and allowing user input in the "Answers Here:" text area. Originally, the "printwords" method printed the words, but I have since converted those into return types in an attempt to make it easier to print them in the "Words List" text area. I am now confused on how to make the "question" method have return types to print into the "Answers Here:" text area. Hopefully, I explained this well as I am a coding novice. If you have any questions, I will do my best to answer them. I just need severe help with this mess.

Scramble
package ScramberGame; import java.util.Random; import java.io.*; import java.util.Scanner; import java.util.Arrays; public class Scramble { public static void main(String[] args) { new Scramble().question(); } public String[] printwords(String[] wordList){ Random r = new Random(); String word1 = "application"; String word2 = "programming"; String word3 = "variable"; String word4 = "method"; String word5 = "class"; String word6 = "package"; String word7 = "import"; String word8 = "boolean"; String word9 = "debugger"; String word10 = "syntax"; String word11 = "compile"; String word12 = "computer"; String line1 = "-----------------------------------------"; word1 = scramble( r, word1 ); String one= "#1- Unscramble: " + word1; word2 = scramble( r, word2 ); String two = "#2- Unscramble: " + word2 ; word3 = scramble( r, word3 ); String three = "#3- Unscramble: " + word3; word4 = scramble( r, word4 ); String four = "#4- Unscramble: " + word4; word5 = scramble( r, word5 ); String five = "#5- Unscramble: " + word5; word6 = scramble( r, word6 ); String six = "#6- Unscramble: " + word6; word7 = scramble( r, word7 ); String seven = "#7- Unscramble: " + word7; word8 = scramble( r, word8 ); String eight = "#8- Unscramble: " + word8; word9 = scramble( r, word9 ); String nine = "#9- Unscramble: " + word9; word10 = scramble( r, word10 ); String ten = "#10- Unscramble: " + word10; word11 = scramble( r, word11 ); String elev = "#11- Unscramble: " + word11; word12 = scramble( r, word12 ); String twel = "#12- Unscramble: " + word12; String line2 = ("-----------------------------------------"); String[] wList = new String[] {line1, one, two, three, four, five, six, seven, eight, nine, ten, elev, twel, line2}; wordList = wList; return wordList; } public void question(){ boolean incorrect = true; for (int attempts = 0; attempts < 3 && incorrect; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #1?"); String answer1 = input.nextLine(); if(!"application".equals(answer1)){ System.out.println("Incorrect. Hint: This runs on your computer."); } else { System.out.println("Correct."); incorrect = false; } } boolean incorrect2 = true; for (int attempts = 0; attempts < 3 && incorrect2; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #2?"); String answer2 = input.nextLine(); if(!"programming".equals(answer2)){ System.out.println("Incorrect. Hint: Writing code for a computer."); }else{ System.out.println("Correct."); incorrect2 = false; } } boolean incorrect3 = true; for (int attempts = 0; attempts < 3 && incorrect3; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #3?"); String answer3 = input.nextLine(); if(!"variable".equals(answer3)){ System.out.println("Incorrect. Hint: A value in a program that can be changed."); }else{ System.out.println("Correct."); incorrect3 = false; } } boolean incorrect4 = true; for (int attempts = 0; attempts < 3 && incorrect4; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #4?"); String answer4 = input.nextLine(); if(!"method".equals(answer4)){ System.out.println("Incorrect. Hint: Tells the computer to do a procedure."); }else{ System.out.println("Correct."); incorrect4 = false; } } boolean incorrect5 = true; for (int attempts = 0; attempts < 3 && incorrect5; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #5?"); String answer5 = input.nextLine(); if(!"class".equals(answer5)){ System.out.println("Incorrect. Hint: Used to describe one or more objects in a program."); }else{ System.out.println("Correct."); incorrect5 = false; } } boolean incorrect6 = true; for (int attempts = 0; attempts < 3 && incorrect6; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #6?"); String answer6 = input.nextLine(); if(!"package".equals(answer6)){ System.out.println("Incorrect. Hint: Organizes a set of classes or interfaces."); }else{ System.out.println("Correct."); incorrect6 = false; } } boolean incorrect7 = true; for (int attempts = 0; attempts < 3 && incorrect7; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #7?"); String answer7 = input.nextLine(); if(!"import".equals(answer7)){ System.out.println("Incorrect. Hint: To send data from one program to another."); }else{ System.out.println("Correct."); incorrect7 = false; } } boolean incorrect8 = true; for (int attempts = 0; attempts < 3 && incorrect8; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #8?"); String answer8 = input.nextLine(); if(!"boolean".equals(answer8)){ System.out.println("Incorrect. Hint: True or False?"); }else{ System.out.println("Correct."); incorrect8 = false; } } boolean incorrect9 = true; for (int attempts = 0; attempts < 3 && incorrect9; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #9?"); String answer9 = input.nextLine(); if(!"debugger".equals(answer9)){ System.out.println("Incorrect. Hint: Used to exterminate bugs."); }else{ System.out.println("Correct."); incorrect9 = false; } } boolean incorrect10 = true; for (int attempts = 0; attempts < 3 && incorrect10; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #10?"); String answer10 = input.nextLine(); if(!"syntax".equals(answer10)){ System.out.println("Incorrect. Hint: Set of rules to follow while programming."); }else{ System.out.println("Correct."); incorrect10 = false; } } boolean incorrect11 = true; for (int attempts = 0; attempts < 3 && incorrect11; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #11?"); String answer11 = input.nextLine(); if(!"compile".equals(answer11)){ System.out.println("Incorrect. Hint: To translate code into a understandable language for a machine."); }else{ System.out.println("Correct."); incorrect11 = false; } } boolean incorrect12 = true; for (int attempts = 0; attempts < 3 && incorrect12; attempts++) { Scanner input = new Scanner(System.in); System.out.println("What is the answer to #12?"); String answer12 = input.nextLine(); if(!"computer".equals(answer12)){ System.out.println("Incorrect. Hint: What you are using right now."); }else{ System.out.println("Correct."); incorrect12 = false; } }} public static String scramble( Random random, String inputString ) { char a[] = inputString.toCharArray(); for( int n=0 ; n<a.length-1 ; n++ ) { int k = random.nextInt(a.length-1); char temp = a[n]; a[n] = a[k]; a[k] = temp; } return new String( a ); } }
GUI_Project
package ScramberGame; /** *Text genereted by Simple GUI Extension for BlueJ */ import ScramberGame.Scramble; import javax.swing.UIManager.LookAndFeelInfo; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.border.Border; import javax.swing.*; public class GUI_project extends JFrame implements ActionListener { private JMenuBar menuBar; private JButton button1; private JPanel panel1; private JPanel panel2; private JTextArea textarea4; private JTextArea textarea5; private JTextArea textarea6; private JTextField textfield2; private JTextField textfield3; //Constructor public GUI_project(){ this.setTitle("Unscrambler Game"); this.setSize(500,400); //menu generate method generateMenu(); this.setJMenuBar(menuBar); //pane with null layout JPanel contentPane = new JPanel(null); contentPane.setPreferredSize(new Dimension(500,400)); contentPane.setBackground(new Color(204,204,204)); button1 = new JButton(); button1.addActionListener(this); button1.setBounds(195,51,103,34); button1.setBackground(new Color(214,217,223)); button1.setForeground(new Color(0,153,51)); button1.setEnabled(true); button1.setFont(new Font("Tahoma",0,12)); button1.setText("Begin Game"); button1.setVisible(true); panel1 = new JPanel(null); panel1.setBorder(BorderFactory.createEtchedBorder(1)); panel1.setBounds(12,122,173,233); panel1.setBackground(new Color(214,217,223)); panel1.setForeground(new Color(0,0,0)); panel1.setEnabled(true); panel1.setFont(new Font("sansserif",0,12)); panel1.setVisible(true); panel2 = new JPanel(null); panel2.setBorder(BorderFactory.createEtchedBorder(1)); panel2.setBounds(241,233,223,125); panel2.setBackground(new Color(214,217,223)); panel2.setForeground(new Color(0,0,0)); panel2.setEnabled(true); panel2.setFont(new Font("sansserif",0,12)); panel2.setVisible(true); textarea4 = new JTextArea(); textarea4.setBounds(300,207,108,20); textarea4.setBackground(new Color(153,153,153)); textarea4.setForeground(new Color(255,255,255)); textarea4.setEnabled(true); textarea4.setFont(new Font("Tahoma",0,12)); textarea4.setText(" Answer Here:"); textarea4.setBorder(BorderFactory.createBevelBorder(1)); textarea4.setVisible(true); textarea5 = new JTextArea(); textarea5.setBounds(11,13,152,204); textarea5.setBackground(new Color(255,255,255)); textarea5.setForeground(new Color(0,0,0)); textarea5.setEnabled(true); textarea5.setFont(new Font("sansserif",0,12)); textarea5.setText(""); textarea5.setBorder(BorderFactory.createBevelBorder(1)); textarea5.setVisible(true); textarea6 = new JTextArea(); textarea6.setBounds(12,10,201,106); textarea6.setBackground(new Color(255,255,255)); textarea6.setForeground(new Color(0,0,0)); textarea6.setEnabled(true); textarea6.setFont(new Font("sansserif",0,12)); textarea6.setText(""); textarea6.setBorder(BorderFactory.createBevelBorder(1)); textarea6.setVisible(true); textfield2 = new JTextField(); textfield2.setBounds(47,88,96,29); textfield2.setBackground(new Color(153,153,153)); textfield2.setForeground(new Color(255,255,255)); textfield2.setEnabled(true); textfield2.setFont(new Font("Tahoma",0,12)); textfield2.setText(" Word List"); textfield2.setVisible(true); textfield3 = new JTextField(); textfield3.setBounds(146,11,200,37); textfield3.setBackground(new Color(102,102,102)); textfield3.setForeground(new Color(255,255,255)); textfield3.setEnabled(true); textfield3.setFont(new Font("Tahoma",0,18)); textfield3.setText(" Unscrambler Game "); textfield3.setVisible(true); //adding components to contentPane panel contentPane.add(button1); contentPane.add(panel1); contentPane.add(panel2); contentPane.add(textarea4); panel1.add(textarea5); panel2.add(textarea6); contentPane.add(textfield2); contentPane.add(textfield3); //adding panel to JFrame and seting of window position and close operation this.add(contentPane); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.pack(); this.setVisible(true); } public void actionPerformed(ActionEvent a) { String[] args = {}; Scramble.main(args); } //method for generate menu public void generateMenu(){ menuBar = new JMenuBar(); JMenu file = new JMenu("File"); JMenu tools = new JMenu("Tools"); JMenu help = new JMenu("Help"); JMenuItem open = new JMenuItem("Open "); JMenuItem save = new JMenuItem("Save "); JMenuItem exit = new JMenuItem("Exit "); JMenuItem preferences = new JMenuItem("Preferences "); JMenuItem about = new JMenuItem("About "); file.add(open); file.add(save); file.addSeparator(); file.add(exit); tools.add(preferences); help.add(about); menuBar.add(file); menuBar.add(tools); menuBar.add(help); } public static void main(String[] args){ System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new GUI_project(); } }); } }

Reactions 0 reactions

No reactions yet, be the first: comment or tweak the code

Comment on this code:

Please Login or Register to leave a comment.