Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JFrame Question

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JFrame Question

    I am to make a mini game where the there are 10 boxes.. the user gets the first turn and asked to choose 1,2,or 3 boxes and click the complete button... then the computer is asked to choose 1,2,or 3 boxes and so on until the last player chooses the last box. That declares the winner.

    I have the user checking the boxes but when it comes to the computer doing it thats when i have trouble.

    please advise

    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    /**
     */
    public class LastManStanding extends JFrame implements ActionListener{
     
    	 JCheckBox[] checkBoxes = new JCheckBox[10];
     
    	    JButton turn = new JButton("Complete Turn");   
    	    JLabel header = new JLabel ("Please select 1, 2, or 3 boxes.");   
    	    JLabel footer = new JLabel ("Whoever selects the last box wins.");   
    	    Font headerFont = new Font("Century", Font.PLAIN, 15);   
    	    Font footerFont = new Font("Century", Font.PLAIN, 13);   
     
    	    public LastManStanding()   
    	    {   
    	        final int FRAME_WIDTH = 240;   
    	        final int FRAME_HEIGHT = 200;   
    	        JFrame gameFrame = new JFrame("Last Man Standing");   
    	        gameFrame.setBounds(600,400,FRAME_WIDTH, FRAME_HEIGHT);   
    	        header.setFont(headerFont);   
    	        footer.setFont(footerFont);   
    	        // a loop to create and modify
    	        for (int i = 0; i < checkBoxes.length; i++)
    	        {
    	            checkBoxes[i] = new JCheckBox(String.valueOf(i + 1)); 
    	            checkBoxes[i].addActionListener(this);
    	        }
    	        turn.addActionListener(this);   
    	        gameFrame.setLayout(new FlowLayout());   
    	        gameFrame.add(header);   
    	        for (JCheckBox checkBox: checkBoxes) // for-each loop
    	        {
    	            gameFrame.add(checkBox);   
    	        }
    	        gameFrame.add(turn);   
    	        gameFrame.add(footer);   
    	        gameFrame.setVisible(true);   
    	        gameFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);   
    	    }   
     
    	    public static void main(String[] args)
    	    {   
    	        LastManStanding game = new LastManStanding();   
    	        System.out.println(game.getComponents());   
    	    }   
     
    	    public void actionPerformed(ActionEvent e)   
    	    {   
    	        Object source = e.getSource();   
    	        if(source instanceof JCheckBox)   
    	        {   
    	            ((JCheckBox) source).setEnabled(false);   
    	        }   
    	        if (source instanceof JButton)   
    	        {   
    	            System.out.println("the button");   
    	        } 
     
    	    }    
    }

    I want the computer to pick 1,2, or 3 boxes and so on until all boxes are selected. the player to select the last box wins.

    Please advise.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: JFrame Question

    It depends if you want the computer to be dumb or smart.

    Dumb: randomly pick 1,2 or 3 boxes.

    Smart: very deep recursion to determine all possible outcomes and choose the optimal one.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: JFrame Question

    1. You haven't shown the code where your computer is selecting checkboxes.
    2. You can use random values to select checkboxes and each time, a random number is selected, mark that valued checkbox. (This case is not that efficient but a starting way to do, you can make it more efficient by coding it more intelligent.)

Similar Threads

  1. JFrame
    By M3ss1ah in forum AWT / Java Swing
    Replies: 3
    Last Post: February 23rd, 2011, 05:00 PM
  2. JFrame
    By M3ss1ah in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2011, 04:24 PM
  3. JFrame not repainting
    By dumb_terminal in forum AWT / Java Swing
    Replies: 3
    Last Post: November 4th, 2010, 08:51 AM
  4. JFrame help
    By Uden in forum AWT / Java Swing
    Replies: 0
    Last Post: August 14th, 2009, 01:37 PM

Tags for this Thread