jeudi 19 octobre 2017

RockPaperScissorsRunner Project

Write a program to allow the play of the game Rock-Paper- Scissors. The game will ask for user input from the keyboard. The user will choose between using 1. Rock, 2. Paper or 3. Scissors.

Part One: Correctly setup the use of the keyboard to get user input for their choice of item. The game must be in a loop, the game will run until the user decides not to play anymore. You must ask the user if they want to continue to play and only except capital “Y” and “N” as you check to keep playing. Do not use charAt().

Part Two: Create a second class which will contain the methods and variables. The first method will create a random number between 1-3 that will be the computers choice. Use Math.random();

Part Three: Create a method to display the actual item played by the user and the computer.

Part Four: Create method will get the user input and the computer’s random number. Then, compare the player choice to the computer choice. Display the results of the game. Add in scoring to show how many games each player has won.

Part Five: Create a get method for each of the following: userWins, computerWins, and Ties. Use these get methods to display the victories for each.

For Example:

Pick an item: 1. Rock, 2. Paper, 3. Scissors 1 Player chose: Rock : Computer chose: Rock Tie User victories: 0 Computer victories: 0 Ties: 1

import java.util.Scanner;

public class RockPaperScissorsRunner_Cavazos {
   public static void main(String[] args) {
      Scanner keyboard = new Scanner(System.in);

      String again =  "Y";
      int c = (int)Math.random()*3;

      while(!again.equals("Y") || again.equals("Y")) { 
         System.out.print("Pick an item: 1.Rock, 2.Paper, 3.Scissors ");
         int u = keyboard.nextInt();
         System.out.print(RPS.use(u) + " " + RPS.comp(c));// Need to find how to get computer to generate and pass from method
         System.out.println();  
         System.out.print(RPS.compare());
         System.out.println();
         System.out.println("Play again(Y/N)");
         again = keyboard.next();
      }
   }
}

class RPS {

   static String User = "";
   static String Comput = "";
   static int user;
   static int comp = (int)(Math.random()*3) +1;

   public static String use(int user) { 

      if(user == 1) { // User
         User = "Player chose: Rock :";
         return User;
      }
      else if(user == 2) {
         User = "Player chose: Paper :";
         return User;
      }
      else if(user == 3) {
      User = "Player chose: Scissors :";
      return User;
      }
      return User;
    }

   public static String comp(int comp) {

      if(comp == 1) { // Comp
         Comput = "Computer chose: Rock";
         return Comput;
      }
      else if(comp == 2) {
         Comput = "Computer chose: Paper";
         return Comput;
      }
      Comput = "Computer chose: Scissors";
      return Comput;
   }

    public static String compare() {

    if(user == 1 && comp == 2) { //Paper beats rock
        return "Computer wins with paper beats rock";
    }
    else if(user == 1 && comp == 3) {// Rock beats scissors
        return "User wins with rock beats scissors";
    }
    else if(user == 2 && comp == 3) {// Scissors beats paper
        return "Computer wins with scissors beats paper";
    }
    else if(user == 1 && comp == 1) {
        return "Tie";
    }
    return "Tie";
    }
}

I'm wondering HOW I can get comp(math.random) into the print statement and print out what the random number from 1-3 to say what choice Computer chose and I need help on my methods for comparing both the user and the Computer.

Aucun commentaire:

Enregistrer un commentaire