Results 1 to 2 of 2
  1. #1
    VaBhoy is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default Anagram game - method help - is answer correct

    Working on what I'd hoped would be a painless anagram game: the user is presented with a group of mixed letters, and asked to solve the anagram.

    I know NetBeans does a game similar to this, but I'm really trying to figure this out without borrowing too much of that code.

    Any advice appreciated:
    Java Code:
    package anagram;
    
    import java.util.*;
    
    public class Countdown {
    
    	public String getScrambled(ArrayList<String> s){
    	int j = 1;
    		String t = s.get(j);
    		j++;
    		
    		
    		return t;
    	}
    	
    
    	
    	public String getResult(ArrayList<String> r, String guess){
    		
    		for(int i=0;i<r.size();i++){
    						
    			boolean m = guess.equals(r.get(i));
    						
    			if (m=true){
    				
    				String c = "Correct!";
    				return c;
    			
    			}
    			else {
    				
    				String c = "Incorrect!";
    				return c;
    			}
    			
    			}
    			String c = "Game Over";
    			return c;
    		}		
    }
    Java Code:
    package anagram;
    
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.*;
    import javax.swing.*;
    
    
    
    public class CountdownFrame extends JFrame {
    
    	
        public CountdownFrame() {
    		setTitle("Count Down!");
    		Toolkit tk = Toolkit.getDefaultToolkit();
    		Dimension d = tk.getScreenSize();
    		int width = 230;
    		int height = 300;
    		
    		setBounds((d.width - width)/2, (d.height - height)/2, width, height);
    		
    		setResizable(false);
    		
    		addWindowListener(new WindowAdapter(){
    			
    			public void windowClosing(WindowEvent e){
    				System.exit(0);
    			}
    		});
        	
        
    
    	Container contentPane = getContentPane();
    	// instantiate a JPanel for my widgets.  The actual definition is below
    	JPanel panel = new CountdownPanel();
    	//add this panel to the content pane
    	contentPane.add(panel);
        }
        
        public static void main(String args[]) {
    
        	
        	java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new CountdownFrame().setVisible(true);
                }
            });
        
        	
    
        }
    }
    
    
    class CountdownPanel extends JPanel implements ActionListener{
    
        	private JLabel title, rule1, rule2, rule3, wordLabel, strikeLabel, scoreLabel, answerLabel, ansTip, rule4;
        	
        	private JTextField scrambled, answer, strikes, score;
        	
        	private JButton start, guess, next, exit;
        	
        	
        	public CountdownPanel(){
        		
    
        		
        		JPanel rPanel = new JPanel();
        		rPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        		
        		rPanel.setPreferredSize(new Dimension(200, 100));
    
        		
        		rule1 = new JLabel("* Guess the scrambled word!");
        		rule2 = new JLabel("* Three strikes and you're out!");
        		rule3 = new JLabel("* An incorrect guess is a strike!");
        		rule4 = new JLabel("* Press Start to begin!");
        		start = new JButton("Start!");
        		
        		rPanel.add(rule1);
        		rPanel.add(rule2);
        		rPanel.add(rule3);
        		rPanel.add(rule4);
        		
    
        		JPanel lPanel = new JPanel();
        		lPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        		lPanel.setPreferredSize(new Dimension(250,100));
        		wordLabel=new JLabel("Scrambled:");
        		scrambled = new JTextField("", 10);
        		
        		
        		
        		answerLabel = new JLabel ("Answer:");
        		answer = new JTextField ("", 10);
        		scrambled.setEditable(false);
        		
        		guess = new JButton ("Guess!");
        		guess.setVisible(false);
        		ansTip =new JLabel("");
        		next = new JButton("Next!");
        		exit = new JButton ("Quit");
        		next.setVisible(false);
        		
        		lPanel.add(wordLabel);
        		lPanel.add(scrambled);
        		lPanel.add(answerLabel);
        		lPanel.add(answer);
        		lPanel.add(next);
        		lPanel.add(start);
        		lPanel.add(guess);
        		lPanel.add(exit);
        		lPanel.add(ansTip);
    
        		
        		JPanel sPanel=new JPanel();	
        		sPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        		
        		strikeLabel = new JLabel("Strikes:");
        		
        		strikes = new JTextField("0",2);
        		strikes.setEditable(false);
        		scoreLabel = new JLabel("Your Score:");
        		
        		score = new JTextField("0",2);
        		score.setEditable(false);
        		
    
        		
        		sPanel.add(strikeLabel);
        		sPanel.add(strikes);
        		sPanel.add(scoreLabel);
        		sPanel.add(score);
    
        		next.addActionListener(this);
        		start.addActionListener(this);
        		guess.addActionListener(this);
        		exit.addActionListener(this);
        		
        		
        		setLayout(new BorderLayout());
        		
        		
        		
        		add(rPanel, BorderLayout.NORTH);
        		add(lPanel, BorderLayout.CENTER);
        		add(sPanel, BorderLayout.SOUTH);
        		
    
        	}
    
    
    		public void actionPerformed(ActionEvent e) {
    			
    			Countdown c = new Countdown();
    			
    
    			ArrayList<String> scr = new ArrayList<String>();
    			
    			
    			String s1 =new String ("aeiilorst");
    			String s2 =new String ("aaeilnort");
    			String s3 =new String ("aegiinort");
    			String s4 =new String ("adeiilort");
    			String s5 =new String ("deeinorst");
    			String s6 =new String ("adeenorst");
    			String s7 =new String ("aeinoprst");
    			String s8 =new String ("aceinorst");
    			String s9 =new String ("acdeinort");
    			String s10 =new String ("aegilnors");
    			
    			scr.add(s1);
    			scr.add(s2);
    			scr.add(s3);
    			scr.add(s4);
    			scr.add(s5);
    			scr.add(s6);
    			scr.add(s7);
    			scr.add(s8);
    			scr.add(s9);
    			scr.add(s10);
    		
    			
    			ArrayList<String> ans = new ArrayList<String>();
    
    			String a1 =new String  ("solitaire");
    			String a2 =new String ("rationale");
    			String a3 =new String ("originate");
    			String a4 =new String ("editorial");
    			String a5 =new String ("desertion");
    			String a6 =new String ("resonated");
    			String a7 =new String ("patronise");
    			String a8 =new String ("reactions");
    			String a9 =new String ("redaction");
    			String a10 =new String ("regionals");
    
    			ans.add(a1);
    			ans.add(a2);
    			ans.add(a3);
    			ans.add(a4);
    			ans.add(a5);
    			ans.add(a6);
    			ans.add(a7);
    			ans.add(a8);
    			ans.add(a9);
    			ans.add(a10);
    								
    			Object source = e.getSource();
    
    	        			
    			
    			if (source == exit){
    				System.exit(0);
    			}
    			
    			else if (source ==start){
    				scrambled.setText(scr.get(0));
    				start.setVisible(false);
    				guess.setVisible(true);
    				rule4.setVisible(false);
    				
    			}
    			
    			else if (source == guess){
    				
    				ansTip.setText(c.getResult(ans, answer.getText()));
    				
    				scrambled.setText(c.getScrambled(scr));
    				answer.setText("");
    				
    			}	
    		}
    }
    Last edited by VaBhoy; 11-28-2009 at 06:51 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    You forgot to post your question; it better not be "fix this code for me please" because we are not a free coder shop.

    kind regards,

    Jos

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. Replies: 2
    Last Post: 04-03-2009, 05:04 AM
  3. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM
  4. Help! Anagram Games
    By geeky123 in forum New To Java
    Replies: 6
    Last Post: 10-13-2008, 04:16 PM
  5. Recursive Anagram
    By zoe in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •