Java Practice Project [Rock Paper Scissors Game]

 


This is a Practice Project and in this Project, we'll create a Rock Paper Scissors Game in Java.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.xdroiddev.rps;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        // Welcoming the User
        System.out.println("Welcome to Rock, paper and Scissor Game!");

        System.out.println("Type \"Rock\", \"Paper\" or \"Scissor\" to Enter Your Move and Type Quit to Exit");


        while (true) {
            Scanner scanner = new Scanner(System.in);
            String user_input = scanner.next();

            if (user_input.equals("Quit")) {
                break;
            }

            if (!user_input.equals("Rock") && !user_input.equals("Paper") &&
                    !user_input.equals("Scissor")) {

                System.out.println("Invalid Input");

            } else {
                int computer_move = (int) (Math.random() * 3);

                String computer_final_move = "";
                if (computer_move == 0) {
                    computer_final_move = "Rock";

                } else if (computer_move == 1) {
                    computer_final_move = "Paper";

                } else {
                    computer_final_move = "Scissor";
                }

                if (computer_final_move.equals(user_input)) {
                    System.out.println("Computer Move " + computer_final_move);
                    System.out.println("Game Tied!");

                } else if ((user_input.equals("Rock") && computer_final_move.equals("Scissor")) ||
                        (user_input.equals("Paper") && computer_final_move.equals("Rock")) ||
                        (user_input.equals("Scissor") && computer_final_move.equals("Paper"))) {

                    System.out.println("Computer Move " + computer_final_move);
                    System.out.println("You Win");
                } else {
                    System.out.println("Computer Move " + computer_final_move);
                    System.out.println("You Lost");
                }
            }

            System.out.println("Thank You for Playing Rock, Paper, Scissor.");
        }
    }

}

Post a Comment

1 Comments

  1. i guess equalsIgnoreCase would be more effective
    It will not be case sensitive then

    ReplyDelete