Anonymous Anonymous - 3 months ago
1252 0

rock paper scissors

Java

Main thing

package Janken;

import java.util.*;

public class Main {
    //Referenced multiple in disconnected functions
    private static int score = 0;
    private static boolean inSession;
    //1 = rock, 2 = paper, 3 = scissors

    public static void main(String[] args) {
        String sessionConfirmation;
        Scanner sc = new Scanner(System.in);
        System.out.println("Play?" + "\n" + "Y/N");
        sessionConfirmation = sc.nextLine();
        if (sessionConfirmation.equalsIgnoreCase("Y") | sessionConfirmation.equalsIgnoreCase("Yes")) { //plays session
            inSession = true;
            //loops until "N" | "NO" is input
            while (inSession) {
                pick();
            }
        } else if (sessionConfirmation.equalsIgnoreCase("N") | sessionConfirmation.equalsIgnoreCase("No")) { //kills session
            inSession = false;
            //scoreSave();
        } else {
            System.out.println("Input Y/N");
        }
    }


    private static void pick() {
        String choice;
        Scanner sc = new Scanner(System.in);
        System.out.println("Pick: Rock|Paper|Scissors");
        choice = sc.nextLine();
        //Just grabs user choice
        if (choice.equalsIgnoreCase("rock") | choice.equalsIgnoreCase("Paper") | choice.equalsIgnoreCase("Scissors") | choice.equalsIgnoreCase("Dynamite")) {
            play(choice);
        } else if (choice.equalsIgnoreCase("N") | choice.equalsIgnoreCase("No") | choice.equalsIgnoreCase("end") | choice.equalsIgnoreCase("stop")) { //kills session
            inSession = false;
        } else {
            System.out.println("Error Statement - pick(valid string)");
        }
    }

    private static void play(String input) {
        String userChoice;
        userChoice = input;
        System.out.println("Your choice: " + userChoice);
        BottyBot opponent = new BottyBot();
        opponent.choose();
        System.out.println("Bot's choice: " + opponent.botChoice);

        //All possible outcomes
        if (userChoice.equalsIgnoreCase("Rock") && opponent.botChoice.equalsIgnoreCase("Rock")) {
            System.out.println("Tie!");
            score += 0;
        } else if (userChoice.equalsIgnoreCase("Rock") && opponent.botChoice.equalsIgnoreCase("Paper")) {
            System.out.println("Lose!");
            score += -100;
        } else if (userChoice.equalsIgnoreCase("Rock") && opponent.botChoice.equalsIgnoreCase("Scissors")) {
            System.out.println("Win!");
            score += 100;
        } else if (userChoice.equalsIgnoreCase("Paper") && opponent.botChoice.equalsIgnoreCase("Rock")) {
            System.out.println("Win!");
            score += 100;
        } else if (userChoice.equalsIgnoreCase("Paper") && opponent.botChoice.equalsIgnoreCase("Paper")) {
            System.out.println("Tie!");
            score += 0;
        } else if (userChoice.equalsIgnoreCase("Paper") && opponent.botChoice.equalsIgnoreCase("Scissors")) {
            System.out.println("Lose!");
            score += -100;
        } else if (userChoice.equalsIgnoreCase("Scissors") && opponent.botChoice.equalsIgnoreCase("Rock")) {
            System.out.println("Lose!");
            score += -100;
        } else if (userChoice.equalsIgnoreCase("Scissors") && opponent.botChoice.equalsIgnoreCase("Paper")) {
            System.out.println("Win!");
            score += 100;
        } else if (userChoice.equalsIgnoreCase("Scissors") && opponent.botChoice.equalsIgnoreCase("Scissors")) {
            System.out.println("Tie!");
            score += 0;
        } else if (userChoice.equalsIgnoreCase("Dynamite")) {
            System.out.println("Win!");
            score += 100;
        }
        //outputs current score, may store in text file upon closing session
        System.out.println("Current Score: " + score);
    }
}
Java

Object

package Janken;

import java.util.*;

public class BottyBot {
    protected static int botNum;
    protected static String botChoice;

    protected static void choose() {
        Random rand = new Random();
        botNum = rand.nextInt(3) + 1;
        if (botNum == 1) {
            botChoice = "rock";
        } else if (botNum == 2) {
            botChoice = "paper";
        } else if (botNum == 3) {
            botChoice = "Scissors";
        } else {
            System.out.println("Error Statement - choose(out of range), check code/restart program");
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download