rock paper scissors java

Solutions on MaxInterview for rock paper scissors java by the best coders in the world

showing results for - "rock paper scissors java"
Sara
26 Jan 2017
1import java.util.Random;
2import java.util.Scanner;
3
4public class RockPaperScissors {
5    private static boolean playAgain(Scanner scanner) {
6        System.out.println("Play again? Y(8), N(9)?");
7        switch (scanner.nextInt()) {
8        case 8:
9            System.out.println("Rock, Paper, Scissors!");
10            return true;
11        default:
12            System.out.println("Thanks for playing!");
13            return false;
14        }
15    }
16
17    public static void main(String[] args) {
18        Scanner scanner = new Scanner(System.in);
19        RPSPlayer computer = new RandomComputerPlayer(new Random());
20        RPSPlayer human = new HumanPlayer(scanner);
21
22        System.out.println("Rock Paper Scissors, by 200_success!");
23        do {
24            String comp = computer.play();
25            String you = human.play();
26
27            System.out.printf("%s vs. %s", comp, you);
28            if (you.equals(comp)) {
29                System.out.println(", IT'S A TIE!");
30            } else if ( ("Rock".equals(you) && "Scissors".equals(comp)) ||
31                        ("Scissors".equals(you) && "Paper".equals(comp)) ||
32                        ("Paper".equals(you) && "Rock".equals(comp)) ) {
33                System.out.println("! You win!");
34            } else {
35                assert (("Rock".equals(comp) && "Scissors".equals(you)) ||
36                        ("Scissors".equals(comp) && "Paper".equals(you)) ||
37                        ("Paper".equals(comp) && "Rock".equals(you)));
38                System.out.println("! You lose!");
39            }
40        } while (playAgain(scanner));
41    }
42}
Roberta
20 Jan 2018
1import java.util.Scanner;
2
3public class rps2 {
4    public void rps2(){
5        Scanner sage = new Scanner(System.in);
6        int b;
7        b = 1;
8        System.out.println("Play again? Y(8), N(9)?");
9        int yes= 8, no = 9;
10        int input;
11        input = sage.nextInt();
12
13            if(input == yes){
14                System.out.println("Rock,Paper,Scissors!");
15
16            }else{
17                System.out.println("Thanks for playing!");
18            }
19
20
21
22    }
23}
Mia
08 Aug 2018
1import java.util.Random;
2
3public class RandomComputerPlayer implements RPSPlayer {
4    private final Random random;
5
6    public RandomComputerPlayer(Random random) {
7        this.random = random;
8    }
9
10    public String play() {
11        return CHOICES[this.random.nextInt(CHOICES.length)];
12    }
13}
Jerónimo
27 Oct 2017
1import java.util.Scanner;
2
3public class HumanPlayer implements RPSPlayer {
4    private final Scanner scanner;
5
6    public HumanPlayer(Scanner scanner) {
7        this.scanner = scanner;
8    }
9
10    public String play() {
11        System.out.println("Select 1, 2, or 3 for Rock, Paper, Scissors");
12        int choice = this.scanner.nextInt();
13        // Keeping things simple, not doing any validation here
14        return CHOICES[choice - 1];
15    }
16}
Charlie
24 Mar 2019
1import java.util.Scanner;
2import java.util.Random;
3
4public class rps {
5    public static void main (String args[]){
6        int input;
7        int b = 1;
8        Scanner sage = new Scanner(System.in);
9        Random rnd = new Random();
10    System.out.println("Rock Paper Scissors, by Sage!");
11    System.out.println("Select 1, 2, 3, for Rock, Paper, Scissors");
12    //Menu Present, pretty bad still
13
14        while (b != 0){
15        int rock = 1, paper = 2, scissors = 3;
16        input = sage.nextInt();
17        int randomNumber = rnd.nextInt(3-1+1)+1;
18
19            if(randomNumber == rock){
20                if(input == rock){
21                    System.out.println("Rock vs. Rock, ITS A TIE!");
22            }    else if(input == paper){
23                    System.out.println("Rock vs. Paper! You win!" );
24            }    else if(input == scissors){
25                    System.out.println("Rock vs. Scissors! You lose!");
26            }  //These blocks establish options if the computer got Rock
27
28            else if(randomNumber == paper){
29                if(input == rock){
30                    System.out.println("Paper vs. Rock! You lose!");
31            }   else if(input == scissors){
32                    System.out.println("Paper vs. Scissors! You win!");
33            }   else if(input == paper){
34                    System.out.println("Paper vs. Paper! Its a tie!");
35            } //These blocks establish the options if comp. got paper
36
37            else if(randomNumber == scissors){
38                if(input == rock){
39                    System.out.println("Scissors vs. Rock! You win!");
40            }   else if(input == scissors){
41                    System.out.println("Scissors vs. Scissors, ITS A TIE!");
42            }   else if(input == paper){
43                    System.out.println("Scissors vs Paper! You lose!");
44            } //These blocks establish if the computer got scissors.
45
46            }
47            }
48            rps2 rps2Object = new rps2();
49            rps2Object.rps2();
50
51
52        }
53
54    }
55    }
56}