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.