looking for some solutions? You are welcome.

SOLVED: Is there a way of condensing this method of comparing multiple outcomes?

ThatOneSpartan:

Recently having learned how to create classes, objects, methods within those classes, how to access them from the main method, and why this is important so you're not always having to rewrite code/create chunky lines of code/save memory/etc., I ran into a bit of a problem.

So, I made a simple program of Rock Paper Scissors. Essentially, each time the program is run, the program takes user input -- assigns it to a variable, assigns a random number to another variable, and compares the two inputs using multiple if statements to decide an outcome. This is where my problem lies.

I was told that a class and methods tied to it are usually very broad, yet I feel like my method used to compare the two variables is too chunky, and could be reduced/condensed to a much simpler and efficient code.

I don't know how to achieve this. I'm not sure if adding more variables would be the way to go, thus making the comparisons simpler or something, or perhaps taking away some things/adding new types of code, etc. The point is, I want to make this code less chunky and more effective.

In the code, I have two ways of getting the exact values. I have your typical if statements, but I also made a switch statement. Both get me the same results, but I just thought that a switch statement would be easier to read.

ALSO, I realize that classes and methods probably aren't useful when making a Rock Paper Scissors game of this simplicity, but I just wanted to get the practice in and what not.

Here's my code:

along with the website you can run it on: https://repl.it/@ANGELRAMIREZ6/Rock-Paper-Scissors-UPGRADED

import java.util.Scanner;
import java.util.Random;

class Main {
  public static void main(String[] args) {
    action object = new action();

    System.out.println("\nYou will now play Rock Paper Scissors with a Computer.");
    object.getUserChoice();
    object.getComputerChoice();
    object.compareChoices();
  }
}

class action {
  Scanner sc = new Scanner(System.in);
  Random rand = new Random();
  int computerChoice;
  int userChoice; 

  int getUserChoice() {
    System.out.println("\nDo you choose (0)Rock, (1)Paper, (2)Scissors?");
    userChoice = sc.nextInt();
    return userChoice;
  }

  int getComputerChoice() {
    computerChoice = rand.nextInt(2);
    return computerChoice;
  }

  /*
  void compareChoices() {
    //IF YOU PICK ROCK
    if (userChoice == 0) {
      if (computerChoice == 1) {
        System.out.println("You lose!\nYou picked rock and the computer picked paper.");
      }
      if (computerChoice == 2) {
        System.out.println("You win!\nYou picked rock and the computer picked scissors.");
      }
      if (computerChoice == 0) System.out.println("It's a draw! You both picked rock!");
    }

    //IF YOU PICK PAPER
    if (userChoice == 1) {
      if (computerChoice == 0) {
        System.out.println("You win!\nYou picked paper and the computer picked rock.");
      }
      if (computerChoice == 2) {
        System.out.println("You lose!\nYou picked paper and the computer picked scissors.");
      }
      if (computerChoice == 1) System.out.println("It's a draw! You both picked paper!");
    }

    //IF YOU PICK SCISSORS
    if (userChoice == 2) {
      if (computerChoice == 1) {
        System.out.println("You win!\nYou picked scissors and the computer picked paper.");
      }
      if (computerChoice == 0) {
        System.out.println("You lose!\nYou picked scissors and the computer picked rock.");
      }
      if (computerChoice == 2) System.out.println("It's a draw! You both picked scissors!");
    }
  }
  */
  void compareChoices() {
    switch (userChoice) {
      //IF YOU PICK ROCK
      case 0:
      if (computerChoice == 1) {
        System.out.println("You lose!\nYou picked rock and the computer picked paper.");
      }
      if (computerChoice == 2) {
        System.out.println("You win!\nYou picked rock and the computer picked scissors.");
      }
      if (computerChoice == 0) System.out.println("It's a draw! You both picked rock!");
      break;

      //IF YOU PICK PAPER
      case 1:
      if (computerChoice == 0) {
        System.out.println("You win!\nYou picked paper and the computer picked rock.");
      }
      if (computerChoice == 2) {
        System.out.println("You lose!\nYou picked paper and the computer picked scissors.");
      }
      if (computerChoice == 1) System.out.println("It's a draw! You both picked paper!");
      break;

      //IF YOU PICK SCISSORS
      case 2:
      if (computerChoice == 1) {
        System.out.println("You win!\nYou picked scissors and the computer picked paper.");
      }
      if (computerChoice == 0) {
        System.out.println("You lose!\nYou picked scissors and the computer picked rock.");
      }
      if (computerChoice == 2) System.out.println("It's a draw! You both picked scissors!");
      break;
    }
  }
}



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent