9 Replies - 1648 Views - Last Post: 17 April 2011 - 07:52 AM Rate Topic: -----

#1 stephenk291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-April 11

Help with rock paper scissors function.

Posted 09 April 2011 - 09:04 AM

private static String getHumanChoice() {
        System.out.print("Please enter Rock, Scissor, or Paper: ");
        String inString = input.nextLine();
        return inString;



Basically what I need to switch with this function is to add a conditional that if the user doesn't input any of those correct terms, that then a print statement such as.."invalid user input, please pick again" will then prompt them to input either rock, paper , scissors again.

The bulk of my code is setup this way after the user picks his choice so would I need to change anything in here as well?


private static String computeWinner(String humanChoice, String computerChoice) {
        String winner;

        // A chain of nested if/else statements.  Notice that the tie is only
        // checked once. (If the choices are the same, it doesn't matter what
        // was specifically chosen!)
        if (humanChoice.equalsIgnoreCase(computerChoice)) {
            winner = "Game is a Tie!";
        } else if (humanChoice.equalsIgnoreCase(ROCK)) {
            if (computerChoice.equals(SCISSORS)) {
                winner = "Human";
            } else {
                winner = "Computer";
            }
        } else if (humanChoice.equalsIgnoreCase(PAPER)) {
            if (computerChoice.equals(ROCK)) {
                winner = "Human";
            } else {
                winner = "Computer";
            }
        } else if (humanChoice.equalsIgnoreCase(SCISSORS)) {
            if (computerChoice.equals(PAPER)) {
                winner = "Human";
            } else {
                winner = "Computer";
            }
        } else {
            winner = "There are no winners with invalid input!";
        }

        return winner;


Is This A Good Question/Topic? 0
  • +

Replies To: Help with rock paper scissors function.

#2 cd134  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-June 10

Re: Help with rock paper scissors function.

Posted 09 April 2011 - 09:12 AM

Use a do-while loop (http://download.oracle.com/javase/tutorial/java/nutsandbolts/while.html)
do{ // input stuff from user}
while(input is invalid);

Was This Post Helpful? 0
  • +
  • -

#3 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Help with rock paper scissors function.

Posted 09 April 2011 - 12:07 PM

You could also get crazy and use recursion:
private static String getHumanChoice() {
    System.out.print("Please enter Rock, Scissor, or Paper: ");
    String inString = input.nextLine();
    if (inString.equalsIgnoreCase("rock") ||
          inString.equalsIgnoreCase("paper") ||
          inString.equalsIgnoreCase("scissor")){
        return inString;
    }
    else{
        System.out.print("Invalid entry, ");
        return getHumanChoice();
    }
}

This post has been edited by Tryparse: 09 April 2011 - 02:21 PM

Was This Post Helpful? 1
  • +
  • -

#4 stephenk291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-April 11

Re: Help with rock paper scissors function.

Posted 09 April 2011 - 12:29 PM

Tryparse, thanks for the help that solved the problem, I had setup a different one but it was a bit more lengthy than that one and our professor wanted more trimmed down functions to be broken into pieces.

I have one more question if your up for it.


to setup the loop to run the game 10 times would something like this work?

for (int rnd=1; rnd<=10; rnd++) {



The last part needs to be setup to where it runs 10 times, then will the computer should stop and the total win/loss ratio should be displayed. Below should work to display (in theory)

static void displayGameWinner(int userScore,
				  int compScore) {

	System.out.println("\n\nFinal Score:");
	System.out.println("       User=" + userScore +
			   "   Computer=" + compScore);
	System.out.println();

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with rock paper scissors function.

Posted 09 April 2011 - 12:50 PM

I would return an int so you can switch() on it
I would also make it case insensitive

static int getUserChoice() {
   String[] choice = {"ROCK", "PAPER", "SCISSOR");

   while(true) {
      System.out.print("Enter...");
      String answer = input.nextLine().trim().toUpperCase();
      for(int i = 0; i < choice.length; ++i) {
         if(answer.equals(choice[i])
           return i;
      }
      System.out.println("Invalid answer");
   }
}



and your getComputerChoice() can now just be return random.nectInt(3);

This post has been edited by pbl: 09 April 2011 - 06:09 PM

Was This Post Helpful? 1
  • +
  • -

#6 stephenk291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-April 11

Re: Help with rock paper scissors function.

Posted 09 April 2011 - 02:01 PM

Thanks for the help,

I'm just stuck on the looping portion now

here's my main function for the entire game to run, what type of loop should I use and could i get a basic setup if possible.
 public static void main(String[] args) {
        // Get choices
        String computerChoice = getComputerChoice();
        String humanChoice = getHumanChoice();

        // Display the choices that were made
        System.out.println("Computer Chose: " + computerChoice);
        System.out.println("Human Chose: " + humanChoice);

        // Computer the winner
        String winner = computeWinner(humanChoice, computerChoice);

        // Display the winner
        System.out.println("The Winner is: " + winner);

Was This Post Helpful? 0
  • +
  • -

#7 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Help with rock paper scissors function.

Posted 09 April 2011 - 02:22 PM

Yeah, just wrap that in a for loop for however many times you want it to run, throw variables for comp score & human score, in there, increment those somewhere around computing & displaying the winner (based on who won, of course -- if (winner.equalsIgnoreCase("human")) ++humanScore;). Then after the loop, call your displayGameWinner method. Should be it . . .
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with rock paper scissors function.

Posted 09 April 2011 - 06:10 PM

which getUserChoide() getComputerChoice() method did you kept ?
Post your updated code
Was This Post Helpful? 0
  • +
  • -

#9 stephenk291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-April 11

Re: Help with rock paper scissors function.

Posted 09 April 2011 - 07:43 PM

Here's my entire code, I just got back so I haven't created any loop or display function just yet.

 // Create object for reading from the keyboard.
    static Scanner input = new Scanner(System.in);
    // Class Contastant to simplify comparisons.
    static final String ROCK = "Rock";
    static final String PAPER = "Paper";
    static final String SCISSORS = "Scissors";
    

    public static void main(String[] args) {
        // Get choices
        String computerChoice = getComputerChoice();
        String humanChoice = getHumanChoice();

        // Display the choices that were made
        System.out.println("Computer Chose: " + computerChoice);
        System.out.println("Human Chose: " + humanChoice);

        // Computer the winner
        String winner = computeWinner(humanChoice, computerChoice);

        // Display the winner
        System.out.println("The Winner is: " + winner);
    }

    // Calculate the winner from the computer and human choices.
    private static String computeWinner(String humanChoice, String computerChoice) {
        String winner;

        // A chain of nested if/else statements.  Notice that the tie is only
        // checked once. (If the choices are the same, it doesn't matter what
        // was specifically chosen!)
        if (humanChoice.equalsIgnoreCase(computerChoice)) {
            winner = "Game is a Tie!";
        } else if (humanChoice.equalsIgnoreCase(ROCK)) {
            if (computerChoice.equals(SCISSORS)) {
                winner = "Human";
            } else {
                winner = "Computer";
            }
        } else if (humanChoice.equalsIgnoreCase(PAPER)) {
            if (computerChoice.equals(ROCK)) {
                winner = "Human";
            } else {
                winner = "Computer";
            }
        } else if (humanChoice.equalsIgnoreCase(SCISSORS)) {
            if (computerChoice.equals(PAPER)) {
                winner = "Human";
            } else {
                winner = "Computer";
            }
        } else {
            winner = "There are no winners with invalid input!";
        }

        return winner;
    }

    // Get the computer's choice
    private static String getComputerChoice() {
        // Get a random number between 0 and 2
        int randomValue = (int) (Math.random() * 3.0);

        String computerChoice;

        if (randomValue == 0) {
            computerChoice = ROCK;
        } else if (randomValue == 1) {
            computerChoice = PAPER;
        } else {
            computerChoice = SCISSORS;
        }

        return computerChoice;
    }

    // Get the human's choice
    private static String getHumanChoice() {
    System.out.print("Please enter Rock, Scissor, or Paper: ");
    String inString = input.nextLine();
    if (inString.equalsIgnoreCase("rock") || inString.equalsIgnoreCase("paper") ||
          inString.equalsIgnoreCase("scissor")){
        return inString;
    }
    else{
        System.out.print("Invalid entry, ");
        return getHumanChoice();
    }
}
	}


Was This Post Helpful? 0
  • +
  • -

#10 stephenk291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-April 11

Re: Help with rock paper scissors function.

Posted 17 April 2011 - 07:52 AM

Hello,

It's been awhile and I decided to add some new stuff to my Java RPS Game, My next issue I'm running into is, I've setup the loop for the game to run 10 times, now I'd like to do something a bit different in terms of how the game is scored or decided overall for the 10 games.

Basically I'd like it to decide logically who the winner will be over all. So lets say 8 games out of the 10 have run and the user has won 6 of them. Now logically the overall winner cannot be the computer anymore so there is no point to play again. I'd like my game to end if such cases like this occur however I'm not quite sure what conditionals I need to setup. Here's my current display function.

static void displayGameWinner(int userScore,
                                  int compScore) {

        System.out.println("\n\nFinal Score:");
        System.out.println("       User=" + userScore + 
                           "   Computer=" + compScore);
        System.out.println();

        if (userScore > compScore) {
            System.out.println("The Winner is: The User!!");
        }
        else if (compScore > userScore) {
            System.out.println("The Winner is: The Computer!");
        }
        else {
            System.out.println("Its A Tie: Nobody Wins!");


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1