View Single Post
  #2 (permalink)  
Old 04-30-2008, 08:25 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; public class KL4 extends JFrame { // private JButton one, two, three; private JTextArea results; Random generator = new Random(); int userChoice; int compChoice; public KL4() { super("GAME"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(350,550); // setVisible(true); // Random generator = new Random(); JButton one = new JButton("Rock"); JButton two = new JButton("Paper"); JButton three = new JButton("Scissors"); results = new JTextArea(6,20); Container content = getContentPane(); content.setLayout (new BorderLayout()); JPanel panel = new JPanel(new FlowLayout()); panel.add(one); panel.add(two); panel.add(three); content.add(panel, BorderLayout.NORTH); content.add(new JScrollPane(results), BorderLayout.CENTER); one.setActionCommand("rock"); two.setActionCommand("paper"); three.setActionCommand("scissors"); one.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { userChoice = 1; results.append("Rock\n"); compChoice = pickNum(); results.append(getString(compChoice) + "\n"); results.append("results = " + getResults(userChoice, compChoice) + "\n--------\n"); } }); two.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { userChoice = 2; results.append("Paper\n"); compChoice = pickNum(); results.append(getString(compChoice) + "\n"); results.append("results = " + getResults(userChoice, compChoice) + "\n--------\n"); } }); three.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { userChoice = 3; results.append("Scissors\n"); compChoice = pickNum(); results.append(getString(compChoice) + "\n"); results.append("results = " + getResults(userChoice, compChoice) + "\n--------\n"); } }); // The interface itself does not display the given // buttons unless you resize the screen. // If you add components after the top-level container is // realized it has no idea that you changed things. You // must tell it to do a new layout. // To avoid this always make this the last call: setVisible(true); } public int pickNum() { return 1 + generator.nextInt(3); } private String getString(int n) { switch(n) { case 1: return "Rock"; case 2: return "Paper"; case 3: return "Scissors"; default: return ""; } } public static void main(String[] args) { new KL4(); } public String getResults(int player, int computer) { String results; if(player == 1) { if(computer == 2) results = "computer wins"; else if(computer == 3) results = "player wins"; else results = "tie"; } else if(player == 2) { if(computer == 1) results = "player wins"; else if(computer == 3) results = "computer wins"; else results = "tie"; } else { // player == 3 if(computer == 1) results = "computer wins"; else if(computer == 2) results = "player wins"; else results = "tie"; } return results; } }
Reply With Quote