Progamminnoob
Progamminnoob

Reputation: 293

Rock Paper Scissors simple java using methods

So my task today is to make a rock paper scissors game using methods. My first problem with my code is that I need it to ask the user if they want to play again or not. (y or n) Also, I need to implement scoring, 1 point if user wins, -1 if they lose, and for each time they play again add that score to total score. Any ideas on how to implement this? or what I need to change to my code. Also I am a rookie so sorry for the ugly formatting and feel free to critic every little detail, ill soak up all the information I can.

 public static void main(String[] args){
 boolean tie = true;
    do{
        String computer = computerChoice();
        String user = userChoice();
        tie = (computer.compareTo(user) == 0);
        determineWinner(computer, user);
    }while(tie);

  }

  public static String computerChoice( ){
  Random rand = new Random();
 int cinput = rand.nextInt(3)+ 1;
  String computer = "thing";
  if (cinput == 1)
  computer = "Rock";
   if (cinput == 2)
  computer = "Paper";
 if (cinput == 3)
  computer = "Scissors";
    return computer;
  }
  public static String userChoice(){
  Scanner sc = new Scanner (System.in);
  String user = "default";
  do{
    System.out.println("choose your weapon(Paper,Scissors or Rock)");
    user = sc.nextLine();
  }
  while (isValidChoice (user) == false);
  return user;
  }
  public static boolean isValidChoice(String choice){
  boolean status;
  if (choice.compareTo("Rock")== 0)
    status = true;
  else if (choice.compareTo("Paper")== 0)
    status = true;
  else if (choice.compareTo("Scissors")== 0)
    status = true;
  else{
    status = false;
    System.out.println("Error! Make sure you are capitalizing your                    choices");
}

return status;
  }
  public static boolean determineWinner(String computer, String user){
  System.out.println (" Computer Choice: " + computer);
  System.out.println ("Your Choice : " + user);
  if (computer.compareTo( "Rock" ) == 0 && user.compareTo  ("Scissors") == 0)
  System.out.println (" Computer wins! Better luck next time!");
  if (computer.compareTo("Scissors")== 0 && user.compareTo("Paper") == 0)
  System.out.println (" Computer wins! Better luck next time!");
  if (computer.compareTo("Paper") == 0 && user.compareTo("Rock") == 0)
  System.out.println (" Computer wins! Better luck next time!");
   if (computer.compareTo("Rock") == 0 && user.compareTo("Paper") == 0)
   System.out.println (" You win!!");
  if (computer.compareTo("Scissors") == 0 && user.compareTo("Rock") == 0)
  System.out.println (" You win!!");
  if (computer.compareTo("Paper") == 0 && user.compareTo("Scissors") == 0)
  System.out.println (" You win!!");
  else if (computer.compareTo(user) == 0 ){
    System.out.println(" Tie! the game must be played again.");
    return false;
  }
  return true;
}
}

a output that my professor gave us as an example is:

Choose your weapon!
1. Paper
2. Scissors
3. Rock
5
 Choose your weapon!
 1. Paper
 2. Scissors
 3. Rock
  1
 You chose paper!
 I choose rock!
 I have been vanquished!
 We have matched wits 1 times, and your score is 1
 Do you want to play again (y or n)? y
 Choose your weapon!
 1. Paper
 2. Scissors
 3. Rock
 1
 You chose paper!
  I choose paper!
  We are equally matched. You are a worthy adversary.
  We have matched wits 2 times, and your score is 1
  Do you want to play again (y or n)? n

Upvotes: 1

Views: 12707

Answers (1)

WIR3D
WIR3D

Reputation: 532

Here is the finished code. I added two functions, one to call the actual game and one to check if the player wanted to play again. Also, there is the concluding sentence in the end

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

public class RPC
{
    public static Scanner   sc          = new Scanner(System.in);
    public static int       score       = 0;
    public static int       gameCount   = 0;

    public static void main(String[] args)
    {
        play();
        while (playAgain())
        {
            play();
        }
    }

    public static void play()
    {
        String computer = computerChoice();
        String user = userChoice();
        determineWinner(computer, user);
    }

    public static String computerChoice()
    {
        Random rand = new Random();
        int cinput = rand.nextInt(3) + 1;
        String computer = "thing";
        if (cinput == 1)
            computer = "Rock";
        if (cinput == 2)
            computer = "Paper";
        if (cinput == 3)
            computer = "Scissors";
        return computer;
    }

    public static boolean playAgain()
    {
        System.out.println("Play again?(y/n)");
        String input = sc.nextLine();
        if (input.toLowerCase().equals("y"))
        {
            return true;
        } else if (input.toLowerCase().equals("n"))
        {
            return false;
        } else
        {
            System.out.println("Invalid Input");
            return playAgain();
        }

    }

    public static String userChoice()
    {

        String user = "default";
        do
        {
            System.out.println("choose your weapon(Paper,Scissors or Rock)");
            user = sc.nextLine();
        } while (!isValidChoice(user));
        return user;
    }

    public static boolean isValidChoice(String choice)
    {
        boolean status;
        if (choice.equals("Rock"))
            status = true;
        else if (choice.equals("Paper"))
            status = true;
        else if (choice.equals("Scissors"))
            status = true;
        else
        {
            status = false;
            System.out.println("Error! Make sure you are capitalizing your choices");
        }

        return status;
    }

    public static void determineWinner(String computer, String user)
    {
        gameCount++;
        System.out.println(" Computer Choice: " + computer);
        System.out.println("Your Choice : " + user);
        if (computer.equals("Rock") && user.equals("Scissors"))
        {
            score--;
            System.out.println(" Computer wins! Better luck next time!");
        }
        if (computer.equals("Scissors") && user.equals("Paper"))
        {
            score--;
            System.out.println(" Computer wins! Better luck next time!");
        }
        if (computer.equals("Paper") && user.equals("Rock"))
        {
            score--;
            System.out.println(" Computer wins! Better luck next time!");
        }
        if (computer.equals("Rock") && user.equals("Paper"))
        {
            score++;
            System.out.println(" You win!!");
        }
        if (computer.equals("Scissors") && user.equals("Rock"))
        {
            score++;
            System.out.println(" You win!!");
        }
        if (computer.equals("Paper") && user.equals("Scissors"))
        {
            score++;
            System.out.println(" You win!!");
        } else if (computer.equals(user))
        {
            System.out.println(" Tie! the game must be played again.");
        }
        System.out.println("We have matched wits" + gameCount + "times, and your score is" + score);
        return;
    }

}

Upvotes: 2

Related Questions