Java Programming 1 | Rock, Paper and Scissors

827

16

2

Posted

Introduction: Java Programming 1 | Rock, Paper and Scissors

Are you lonely? Would you like to play Rock, Paper, Scissors? Than this is the right code!

Step 1: Setup

Step 1:

Open Java Eclipse (https://eclipse.org/downloads/)

Step 2:

Copy this code:

package com.rps.ZemByte;

import java.awt.*; import java.awt.event.*; import java.util.Random;

import javax.swing.*;

public class Main

{

JFrame RPS;

JButton Rock, Paper, Scissors, VersionButton;

JTextField HRPS;

JTextField CRPS;

JTextField Results;

JTextField Counter;

JPanel hInput;

char hChoice = ' ';

char cChoice = ' ';

char[] cc = {'R', 'P', 'S'};

int r3;

int tie = 0;

int computerwins = 0;

int humanwins = 0;

JTextArea Chat;

Random rGen = new Random();

public Main()

{

RPS = new JFrame("Rock, Paper, Scissors, Pointless");

RPS.setSize(600, 600);

Rock = new JButton("Rock");

Paper = new JButton("Paper");

Scissors = new JButton("Scissors"); Chat = new JTextArea(12,25); VersionButton = new JButton("Version 0.0.7");

hInput = new JPanel(); HRPS = new JTextField(20);

CRPS = new JTextField(20); Results = new JTextField(20); Counter = new JTextField(30);

Rock.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

HRPS.setText("You : Rock");

hChoice = 'R';

play() ;

}

});

Paper.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

HRPS.setText("You : Paper");

hChoice = 'P';

play() ;

}

});

Scissors.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

HRPS.setText("You : Scissors");

hChoice = 'S';

play() ;

}

});

hInput.setLayout(new FlowLayout()); hInput.add(VersionButton);

hInput.add(HRPS);

hInput.add(CRPS); hInput.add(Counter);

hInput.add(Rock);

hInput.add(Paper);

hInput.add(Scissors); hInput.add(Chat);

RPS.setResizable(true); RPS.setExtendedState(JFrame.MAXIMIZED_BOTH); RPS.getContentPane().add(hInput, BorderLayout.CENTER);

RPS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

RPS.pack();

RPS.setVisible(true); RPS.setResizable(false); Chat.setText("Welcome to" + " ".replaceAll("\\s+","\n") + " Rock! Paper! Scissors! ".replaceAll("\\s+","\n") + " ".replaceAll("\\s+","\n") + " ".replaceAll("\\s+","\n") + "Chat:" + " ".replaceAll("\\s+","\n") + " ::Player1 joined the game ");

}

private void play()

{

r3 = rGen.nextInt(3); cChoice = cc[r3];

switch (cChoice)

{

case 'R':

CRPS.setText("Computer : Rock");

break;

case 'P':

CRPS.setText("Computer : Paper");

break;

case 'S':

CRPS.setText("Computer : Scisssors");

break;

}

if(hChoice == cChoice) { tie++; } if(hChoice == 'R' && cChoice == 'S') { humanwins ++; } if(hChoice == 'P' && cChoice == 'R') { humanwins++; } if(hChoice == 'S' && cChoice == 'P') { humanwins++; } if(cChoice == 'R' && hChoice == 'S') { computerwins ++; } if(cChoice == 'P' && hChoice == 'R') { computerwins++; } if(cChoice == 'S' && hChoice == 'P') { computerwins++; } Counter.setText("Ties = " + tie + " | Player Wins = " + humanwins + " | Computer Wins = " + computerwins); }

public static void main(String[] args) {

try {

UIManager.setLookAndFeel(

UIManager.getCrossPlatformLookAndFeelClassName());

} catch(Exception e) {}

@SuppressWarnings("unused") Main application = new Main();

}

}

Step 2: Have Fun!

Step 3:

Change "ZemByte" to your package name! Also be sure to name your package com.something.something (You can change "something" to whatever you like!

Step 4:

Paste it into Eclipse!

Step 5:

Have fun!

Share

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
  • Gluten Free Challenge

    Gluten Free Challenge
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

2 Comments

This doesn't tell a beginner what to do with the code once it's in Eclipse.

Hey GruntBug!

It is really simple, but tough for beginners indeed! We are sorry for not putting that in. The first thing that you have to do is make a folder. Than, make a new package. In that package, make a class. Then put the code in there. After that, you can hit save. Then click "file" and then "export". Export it as a runnable jar file (Under java). You can save it to your desktop and then double-click the jarfile!

Have fun beating the computer!

Kind regards,

Max, programmer of ZemByte