Public feed #530930

RPS

public text v1 · immutable
Raw Edit
·published 2007-06-02 08:52 UTC
rendered paste body
import java.io.PrintStream;
import java.util.Random;
import java.util.Scanner;

public class RPS extends javax.swing.JFrame {
    
    public RPS() {
        initComponents();
    }

    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        infoArea = new javax.swing.JTextArea();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Rock Paper Scissors");
        infoArea.setColumns(20);
        infoArea.setRows(5);
        jScrollPane1.setViewportView(infoArea);

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Rock Paper Scissors. By Yz85Racer");

        jMenu1.setText("File");
        jMenuItem1.setText("Item");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(211, 211, 211))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(146, 146, 146))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                int i = 0;
        int j = 0;
        int k = 0;
        String s = "y";
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
               while(s.equals("y")) 
        {
            int l = random.nextInt(3) + 1;
            infoArea.append("Enter 1 for Rock, 2 for Paper, and 3 for Scissors");
            int i1 = scanner.nextInt();
            if(l == i1)
            {
                infoArea.append("The game has resuleted in a tie.");
                k++;
            } else
            {
                switch(i1)
                {
                case 1:
                    if(l == 2)
                    {
						infoArea.append("The computer has chosen Paper...");
						infoArea.append("Paper beats Rock");
                        infoArea.append("Computer wins.");
                        i++;
                    } else
                    {
						infoArea.append("The computer has chosen Scissors...");
                        infoArea.append("Rock beats Scissors");
                        infoArea.append("Player wins.");
                        j++;
                    }
                    break;

                case 2:
                    if(l == 1)
                    {
						infoArea.append("The computer has chosen Rock...");
                        infoArea.append("Paper beats Rock");
						infoArea.append("Player wins.");
                        j++;
                    } else
                    {
						infoArea.append("The computer has chosen Scissors...");
                        infoArea.append("Scissors beat Paper");
                        infoArea.append("Computer wins.");
                        i++;
                    }
                    break;

                case 3:
                    if(l == 1)
                    {
						infoArea.append("The computer has chosen Rock...");
                        infoArea.append("Rock beats Scissors");
                        infoArea.append("Computer wins.");
                        i++;
                    } else
                    {
						infoArea.append("The computer has chosen Paper...");
                        infoArea.append("Scissors beat Paper");
                        infoArea.append("Player wins.");
                        j++;
                    }
                    break;

                default:
                    infoArea.append("Enter 1 2 or 3");
                    break;
                }
            }
            infoArea.append("Play again? y/n");
            if(!s.equals("y"))
            {
                infoArea.append("Scores:");
                infoArea.append((new StringBuilder()).append("Ties= ").append(k).append(" Wins= ").append(j).append(" Losses= ").append(i).toString());
            } else
            {
                infoArea.append("Playing again.");
            }
               }
    
    }                                        

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    infoArea.append("Rock Paper Scissors\n");
    infoArea.append("Made By Yz85Racer\n");
    infoArea.append("I seriously must say\n");
    infoArea.append("Thank you to Lothy.\n");
    infoArea.append("He put up with my\n");
    infoArea.append("Nooby questions, and helped me.");
    }                                          
    
   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RPS().setVisible(true);
            }
        });
    }
    
                   
    private javax.swing.JTextArea infoArea;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
           
    
}