Overall winner of a series of Rock Paper Scissors games [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm creating a Rock Paper Scissor program. At the end, I want it to determine the winner of all games played. It always prints out "Player1 beat Player2 and I" regardless of whoever has the highest score. Where is my mistake and how should I correct it?
import java.util.Scanner;
import java.util.Random;
public class RPS2Player{
public static void main(String[]args){
int onePlay, computerPlay, twoPlay, game = 0, win1 = 0, win2 = 0, compWin = 0, lose1 = 0, lose2 = 0, compLose = 0, playChoice;
//instantiate objects Scanner and Random
Scanner UI = new Scanner(System.in);
Random num = new Random();
//Get user input: Rock = 0, Paper = 1, Scissors = 2
System.out.println("Hello! would you like to play Rock, Paper, Scissors? 0=yes, 1=no");
playChoice = UI.nextInt(2);
while (playChoice == 0){
game++;
System.out.println("Round " + game);
System.out.println("Player1, choose 0=Rock, 1=Paper, 2=Scissors");
onePlay = UI.nextInt(3);
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("Player2, choose =Rock, 1=Paper, 2=Scissors");
twoPlay = UI.nextInt(3);
//get computer generated input: 0, 1, 2
computerPlay = num.nextInt(3);//defines a random choice of 0, 1, or 2
switch(onePlay){
case 0: System.out.println("Player1 chose Rock"); break;
case 1: System.out.println("Player1 chose Paper"); break;
case 2: System.out.println("Player1 chose Scissors"); break;
}//end switch statement for onePlay
switch (twoPlay){
case 0: System.out.println("Player2 chose Rock"); break;
case 1: System.out.println("Player2 chose Paper"); break;
case 2: System.out.println("Player2 chose Scissors"); break;
}//end switch statement for twoPlay
switch(computerPlay){
case 0: System.out.println("I chose Rock"); break;
case 1: System.out.println("I chose Paper"); break;
case 2: System.out.println("I chose Scissors"); break;
}//end switch statement for computerPlay
//comparisons Player1 vs computer
if (onePlay == 0){//Rock
if (computerPlay == 0){
System.out.println("Player 1 and I tie!");
//computer chose rock - tie game
}
if (computerPlay == 1){
System.out.println("I beat Player1!");
lose1++;
compWin++;//computer chose paper - computer wins
}
if (computerPlay == 2){
System.out.println("Player1 beat me!");
compLose++;
win1++;//computer chose scissors - player wins
}
}//end if statements for onePlay = 0 (Rock)
if (onePlay == 1){//Paper
if (computerPlay == 0){
System.out.println("Player1 beat me!");
compLose++;
win1++;//computer chose rock - player win1s
}
if (computerPlay == 1){
System.out.println("Player1 and I tie!");
// computer chose paper - tie game
}
if (computerPlay ==2){
System.out.println("I beat Player 1!");
lose1++;
compWin++;//computer chose scissor - computer win1s
}
}//end if statements for onePlay = 1 (Paper)
if (onePlay == 2){//Scissors
if (computerPlay == 0){
System.out.println("I beat Player1!");
lose1++;
compWin++;//computer chose rock - computer wins
}
if (computerPlay == 1){
System.out.println("Player1 beat me!");
compLose++;
win1++;//computer chose paper - play wins
}
if (computerPlay == 2){
System.out.println("Player1 and I tie!");
//computer chose scissors - tie game
}
}//end if statements for onePlay = 2 (Scissors)
//end comparisons Player1 vs computer
//comparisons Player2 vs computer
if (twoPlay == 0){//Rock
if (computerPlay == 0){
System.out.println("Player2 and I tie!");
//computer chose rock - tie game
}
if (computerPlay == 1){
System.out.println("I beat Player2!");
lose2++;
compWin++;//computer chose paper - computer wins
}
if (computerPlay == 2){
System.out.println("Player2 beat me!");
compLose++;
win2++;//computer chose scissors - player2 wins
}
}//end if statements for twoPlay = 0 (Rock)
if (twoPlay == 1){//Paper
if (computerPlay == 0){
System.out.println("Player2 beat me!");
compLose++;
win2++;//computer chose rock - player wins
}
if (computerPlay == 1){
System.out.println("Player2 and I tie!");
// computer chose paper - tie game
}
if (computerPlay ==2){
System.out.println("I beat Player2!");
lose2++;
compWin++;//computer chose scissor - computer wins
}
}//end if statements for twoPlay = 1 (Paper)
if (twoPlay == 2){//Scissors
if (computerPlay == 0){
System.out.println("I beat Player2!");
lose2++;
compWin++;//computer chose rock - computer wins
}
if (computerPlay == 1){
System.out.println("Player2 beat me!");
compLose++;
win2++;//computer chose paper - player2 wins
}
if (computerPlay == 2){
System.out.println("Player2 and I tie!");
//computer chose scissors - tie game
}
}//end if statements for twoPlay = 2 (Scissors)
//end comparisons Player2 vs computer
//comparison Player1 vs Player2
if (onePlay == 0){//Rock
if (twoPlay == 0){
System.out.println("Player1 and Player2 tie!");
//Player2 chose rock - tie game
}
if (twoPlay == 1){
System.out.println("Player2 beat Player1!");
lose1++;
win2++;//Player2 chose paper - Player2 wins
}
if (twoPlay == 2){
System.out.println("Player1 beat Player2!");
lose2++;
win1++;//Player2 chose scissors - player1 wins
}
}//end if statements for personPlay = 0 (Rock)
if (onePlay == 1){//Paper
if (twoPlay == 0){
System.out.println("Player1 beat Player2!");
lose2++;
win1++;//computer chose rock - player wins
}
if (twoPlay == 1){
System.out.println("Player1 and Player2 tie!");
// computer chose paper - tie game
}
if (twoPlay ==2){
System.out.println("Player2 beat Player1!");
lose1++;
win2++;//player2 chose scissor - player2 wins
}
}//end if statements for personPlay = 1 (Paper)
if (onePlay == 2){//Scissors
if (twoPlay == 0){
System.out.println("Player2 beat Player1!");
lose1++;
win2++;//Player2 chose rock - player2 wins
}
if (twoPlay == 1){
System.out.println("Player1 beat Player2!");
lose2++;
win1++;//player2 chose paper - player1 wins
}
if (twoPlay == 2){
System.out.println("Player1 and Player2 tie!");
//player2 chose scissors - tie game
}
}//end if statements for personPlay = 2 (Scissors)
//end //comparisons Player1 vs Player2
System.out.println("Would you like to play again? 0=yes, 1=no");
playChoice = UI.nextInt(2);
}//end while loop for play
//user decides not to play or decides to stop playing - Stats are given
if (playChoice == 1){
System.out.println("Ok let's play (again) sometime.");//game ended
System.out.println();
System.out.println("We played " + game + " games.");//number of games played
System.out.println("Player1 won " + win1 + " games, lost " + compWin + " games.");//Player1 wins and losses
System.out.println("Player2 won " + win2 + " games, lost " + lose2 + " games.");//Player2 wins and losses
System.out.println("I won " + compWin + " games and lost " + compLose + " games.");//computers wins and losses
System.out.print("In terms of wins, ");
//determine best player
System.out.println("Across all games played, ");
if (win1 > (win2 & compWin)){
System.out.println("Player1 beat Player2 and I!");
}
else if (win2 > (win1 & compWin)){
System.out.println("Player2 beat Player1 and I!");
}
else
System.out.println("I beat Player1 and Player2!");
}//end while loop
}//end method
}//end program

Your conditional should be
if ((win1 > win2) && (win1 > compWin)){
System.out.println("Player1 beat Player2 and I!");
}
else if ((win2 > win1) && (win2 > compWin)){
System.out.println("Player2 beat Player1 and I!");
}
else
System.out.println("I beat Player1 and Player2!");
}
because AND statement in java is "&&" not "&",
also your conditional checking is wrong

if (win1 > (win2 & compWin))
This should be
if (win1 > win2 && win1 > compWin)
And same thing for the else if.
The logical AND operator is &&. & is a bitwise AND, which is an entirely different operation. That's a somewhat advanced capability you won't be using just yet.
What you wrote is a common mistake where translating English directly into code doesn't work. && and || don't work quite the same as "and" and "or" do in English.
(By the way, grammatically it should say "Player1 beat Player2 and me.")

You are using a bitwise operator in your comparison. I don't think that is what you intended.
if (win1 > (win2 & compWin)){
// if win2 = 14 and compWin = 9
// 9 (base 10) = 00000000000000000000000000001001 (base 2)
// 14 (base 10) = 00000000000000000000000000001110 (base 2)
// --------------------------------
//14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10)
// if win2 = 14 and compWin = 0; 14 & 0 = 0
System.out.println("Player1 beat Player2 and I!");
}
I think you meant to compare win1 to win2 and compWin using the logical && operator.
// Should be:
if(win1 > win2 && win1 > compWin) {
System.out.println("Player1 beat Player2 and I!");
}
else if (win2 > win1 && win2 > compWin)){
System.out.println("Player2 beat Player1 and I!");
}
else
System.out.println("I beat Player1 and Player2!");
}
Logical expressions using && or || are evaluated on one side, then the other. If the left side is true, && the right side is true, return true; otherwise return false.

Related

For loop not working correctly in Rock Paper Scissors Java program

I'm new to programming and have been attempting to code a simple Rock Paper Scissors game in Java, but I'm stuck with two problems. The game worked correctly prior to me adding a for loop in order to make the game last for 5 rounds each time, but now with the loop it just repeats the same result line 4 times after entering 1 input, rather than allowing me to enter more inputs and generate several results. I also tried putting in an invalid input message, where if the user inputs anything other than 1, 2 or 3 the program outputs "Invalid user input.", but this doesn't work when ran and has just resulted in compiler error messages.
Any help would be greatly appreciated, thanks!
Here is the entirety of my code:
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static final int ROCK = 1;
public static final int PAPER = 2;
public static final int SCISSORS =3;
public static void main(String[] args) {
System.out.println("Let's play Rock, Paper, Scissors! (best out of 5)");
System.out.println("Enter either 1 for Rock, 2 for Paper or 3 for Scissors.");
Scanner input = new Scanner(System.in);
int numberGuessed = input.nextInt();
Random generator = new Random();
int computerNumber = generator.nextInt(3) + 1;
for (int round = 0; round < 4; round++) {
if (numberGuessed == computerNumber) {
System.out.print("It's a tie!");
}
if (numberGuessed == 1 && computerNumber == 2) {
System.out.println("You lose! I chose paper and paper smothers rock!");
}
else if (numberGuessed == 1 && computerNumber == 3) {
System.out.println("You win! I chose scissors and rock smashes scissors!");
}
else if (numberGuessed == 2 && computerNumber == 1) {
System.out.println("You win! I chose rock and paper smothers rock!");
}
else if (numberGuessed == 2 && computerNumber == 3) {
System.out.println("You lose! I chose scissors and scissors cut paper!");
}
else if (numberGuessed == 3 && computerNumber == 2) {
System.out.println("You win! I chose paper and scissors cut paper!");
}
else if (numberGuessed == 3 && computerNumber == 1) {
System.out.println("You lose! I chose rock and rock smashes scissors!");
}
else if (numberGuessed != 1 || numberGuessed != 2 || numberGuessed != 3) {
System.out.println("Invalid user input.");
}
}
You have not ended all of the curly braces properly which is leading to compiler error.
for (int round = 0; round < 4; round++) will end for upto 4 rounds, for 5 rounds of loop the condition can be like this: for (int round = 0; round < 5; round++)
The for loop should start before the input is taken each time so that it can process each input separately.
Try this:
import java.util.Random;
import java.util.Scanner;
class RockPaperScissors {
public static final int ROCK = 1;
public static final int PAPER = 2;
public static final int SCISSORS = 3;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Let's play Rock, Paper, Scissors! (best out of 5)");
for (int round = 0; round < 5; round++) {
System.out.println("Enter either 1 for Rock, 2 for Paper or 3 for Scissors.");
int numberGuessed = input.nextInt();
Random generator = new Random();
int computerNumber = generator.nextInt(3) + 1;
if (numberGuessed == computerNumber) {
System.out.println("It's a tie!");
} else if (numberGuessed == 1 && computerNumber == 2) {
System.out.println("You lose! I chose paper and paper smothers rock!");
} else if (numberGuessed == 1 && computerNumber == 3) {
System.out.println("You win! I chose scissors and rock smashes scissors!");
} else if (numberGuessed == 2 && computerNumber == 1) {
System.out.println("You win! I chose rock and paper smothers rock!");
} else if (numberGuessed == 2 && computerNumber == 3) {
System.out.println("You lose! I chose scissors and scissors cut paper!");
} else if (numberGuessed == 3 && computerNumber == 2) {
System.out.println("You win! I chose paper and scissors cut paper!");
} else if (numberGuessed == 3 && computerNumber == 1) {
System.out.println("You lose! I chose rock and rock smashes scissors!");
} else if (numberGuessed != 1 || numberGuessed != 2 || numberGuessed != 3) {
System.out.println("Invalid user input.");
}
System.out.println();
}
}
}
You set numbers and do 5 rounds on the same data. Move initialization of numberGuessed and computerNumber inside for loop.
Observe where your input and random number generator is. Think if that will repeat 5 times when your loop runs. Done?
If you haven't observed yet, let me elaborate: your input and random number generator is outside you for loop. That's IT.

How to restart Rock Paper and Scissors game mutliple times until user quits in Java

Hello I'm stuck on how to keep the game going until the user decides to quit. I want to try any way possible to get the game to end and display the amount of losses, ties, and wins. I'm trying to use a while loop.
import java.util.Scanner;
import java.util.Random;
public class JavaApplication15 {
public static void main(String[] args) {
int ties = 0, wins = 0, losses = 0;
String comp = "";
String user = "";
Random choice = new Random();
String startover = "y";
while (startover.equals("y")) {
int computerchoice = choice.nextInt(3) + 1;
if (computerchoice == 1)
comp = "R";
else if (computerchoice == 2)
comp = "P";
else if (computerchoice == 3)
comp = "S";
Scanner scanner = new Scanner(System.in);
System.out.println("Enter rock (1), paper (2), or scissors (3) [no to quit]: ");
int player = scanner.nextInt();
if (player == 1) {
user = "R";
} else if (player == 2) {
user = "P";
} else if (player == 3) {
user = "S";
}
System.out.println("You choose: " + user);
System.out.println("Computer chooses: " + comp);
if (comp.equals(user)) {
System.out.println("It's a tie!");
ties++;
} else if (user.equals("R")) {
if (comp.equals("P"))
System.out.println("Computer wins! Paper beats rock!");
losses++;
} else if (comp.equals("S")) {
System.out.println("You win! Your rock beats the scissors!");
wins++;
} else if (user.equals("S")) {
if (comp.equals("P"))
System.out.println("You win! Scissor beats paper!");
wins++;
} else if (comp.equals("R")) {
System.out.println("Computer wins! Rock beats paper!");
losses++;
} else if (user.equals("P"))
if (comp.equals("R")) {
System.out.println("You win! Paper beats rock!");
wins++;
} else if (comp.equals("S")) {
System.out.println("Computer wins! Scissors beats paper!");
losses++;
}
}
{
System.out.println("Invalid input! Try again");
System.out.println("Your ties: " + ties);
System.out.println("Your wins: " + wins);
System.out.println("You losses: " + losses);
startover = "no";
}
}
instead of doing scanner.nextInt(); you could do
System.out.println("Enter rock (1), paper (2), or scissors (3) [\'n\' to quit]: ");
char selection = scanner.nextLine().charAt(0)
if(selection == '1'){ user = "R"; }
else if(selection == '2'){ user = "P"; }
else if(selection == '3'){ user = "S"; }
else if(selection == 'n'){
System.out.println(ties)
System.out.println(wins);
System.out.println(losses);
return;
}
this makes the selection variable a char, and you can test if it's 1, 2, or 3 (that is, the characters 1 2 or 3), and also check for 'n'.
since you're working in the main method, simply putting "return" will exit the program at that point.
You just need add a new option 4
else if (computerchoice == 4)
comp = "E";
} else if (comp.equals("E")) {
System.out.println("Your ties: " + ties);
System.out.println("Your wins: " + wins);
System.out.println("You losses: " + losses);
startover = "no";
}
I tried out your code as-is, and what I found out is that your code infact never stops. The while loop has no proper condition to exit. So I made some minor changes to make it work.
int ties = 0, wins = 0, losses = 0;
String comp = "";
String user = "";
Random choice = new Random();
String startover = "y";
Scanner scanner = new Scanner(System.in); //Scanner object should not be open in loop, it is a resource wastage
while (startover.equals("y")) {
int computerchoice = choice.nextInt(3) + 1;
if (computerchoice == 1)
comp = "R";
else if (computerchoice == 2)
comp = "P";
else if (computerchoice == 3)
comp = "S";
System.out.println("Enter rock (1), paper (2), or scissors (3) [no to quit]: ");
String input = scanner.nextLine(); //take input in string
int player ;
try {
player = Integer.parseInt(input); //see if actually number was given as input
} catch(Exception ex) {
break; //if not, then we assume player wants to quit
}
if (player == 1) {
user = "R";
} else if (player == 2) {
user = "P";
} else if (player == 3) {
user = "S";
}
System.out.println("You choose: " + user);
System.out.println("Computer chooses: " + comp);
if (comp.equals(user)) {
System.out.println("It's a tie!");
ties++;
} else if (user.equals("R")) {
if (comp.equals("P"))
System.out.println("Computer wins! Paper beats rock!");
losses++;
} else if (comp.equals("S")) {
System.out.println("You win! Your rock beats the scissors!");
wins++;
} else if (user.equals("S")) {
if (comp.equals("P"))
System.out.println("You win! Scissor beats paper!");
wins++;
} else if (comp.equals("R")) {
System.out.println("Computer wins! Rock beats paper!");
losses++;
} else if (user.equals("P"))
if (comp.equals("R")) {
System.out.println("You win! Paper beats rock!");
wins++;
} else if (comp.equals("S")) {
System.out.println("Computer wins! Scissors beats paper!");
losses++;
}
}
scanner.close(); //scanner was not closed before
System.out.println("Your ties: " + ties);
System.out.println("Your wins: " + wins);
System.out.println("You losses: " + losses);
startover = "no"; //redundant actually

Rock Paper Scissors Game Best 2 out of 3 Loop Issues

How do I make the computer automatically win when the wrong input (something aside from the options; rock, paper, scissors) is entered? It need these to be counted as well. Also, when the players chooses to play again, how can I remove "Welcome..." from reappearing; It is only needed when program first starts.
import java.util.Scanner;
import java.util.Random;
/**
*
* #author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Set integers for wins, losses, rounds, and user
while(true){
int wins = 0;
int losses = 0;
int round = 0;
int Player = 0;
//Plays 3 rounds before terminating
//Prompt user to input Rock Paper Scissors
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");
Scanner keyboard = new Scanner (System.in);
while(round<3) {
System.out.println("Enter \"Rock\", \"Paper\" or \"Scissors\"");
Random Game = new Random();
int Computer = 1+Game.nextInt(3);
int Scissors, Rock, Paper;
Rock = 1;
Paper = 2;
Scissors= 3;
String UserInput = keyboard.next();
if(UserInput.equals("Rock")) {
Player = 1;
}
if(UserInput.equals("Paper")) {
Player = 2;
}
if(UserInput.equals("Scissors")) {
Player = 3;
}
//If the user enters a value greater then 3 (Scissors) or less than 1 (Rock)
//it will terminate the program and display an error message
while (Player > 3 || Player < 1) {
losses++;
round++;
System.out.println("Not a valid input! Computer wins");
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish tie scenarios using if statements
if(Player == Computer){
if(Player == Scissors){
System.out.println("Scissors v Scissors! Tie!");
round++;
}
if(Player == Rock){
System.out.println("Rock v Rock! Tie!");
round++;
}
if(Player == Paper){
System.out.println("Paper v Paper! Tie!");
round++;
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish the various winning scenarios using if and else if statements
//Player wins
if(Player == Scissors)
if(Computer == Paper){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if(Computer == Rock){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player wins
if(Player == Rock)
if(Computer == Scissors ){
System.out.println("Rock v Scissors! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if (Computer == Paper){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player Wins
if(Player == Paper)
if(Computer == Rock){
System.out.println("Paper v Rock! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer Wins
else if (Computer == Scissors){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
}
//Determine final winner using if statements
//Ask if player would like to play again by setting up string
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? \"Yes\" or \"No\"");
Scanner YesNo = new Scanner(System.in);
String YesNo_String = YesNo.next();
if(YesNo_String.equalsIgnoreCase("yes")) {
}if(YesNo_String.equalsIgnoreCase("No")) {
System.out.println ("Goodbye!");
}
}
}
}
You could always just change your if statements to the following:
if(UserInput.equals("Rock")) {
Player = 1;
}
else if(UserInput.equals("Paper")) {
Player = 2;
}
else if(UserInput.equals("Scissors")) {
Player = 3;
}
else {
Player = 0;
}
You already have it set up so that if player < 1, the computer wins automatically.
Note: your while (Player > 3 || Player < 1) statement should actually be an if statement. Otherwise, you'll have an infinite loop there.
To answer your second question, just move the statement System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n"); above your loop. That way it will only be asked once: at the beginning.
For both the player entering in the wrong data and checking if they have already played, create Booleans and check those Booleans in an if/else statement. Since this may or may not be homework I'll solve this for the "Welcome... " message.
public static void main(String[] args) {
// TODO code application logic here
Boolean firstPlay = true;
//Set integers for wins, losses, rounds, and user
while(true){
int wins = 0;
int losses = 0;
int round = 0;
int Player = 0;
//Plays 3 rounds before terminating
//Prompt user to input Rock Paper Scissors
if(firstPlay) {
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");
firstPlay = false;
}
It seems like you are setting the value of "Player" to either 1, 2, or 3 with the default being 0. I would say set it to the default value at the start of every loop, and if its 0 allow the computer to win. Or change your if satements to if-elseif. I'll provide an example below.
....
if(UserInput.equals("Rock")) {
Player = 1;
} else if(UserInput.equals("Paper")) {
Player = 2;
} else if(UserInput.equals("Scissors")) {
Player = 3;
} else {
Player = 0;
}
Since you already have a statement saying if Player < 1 or > 3 that the computer wins, you are set. Also your "Welcome" statement is inside of your while loop. Because of this every time it iterates through the loop you will see it. Try moving it outside of the loop.

How to output game code properly

So I am making a snakes and ladders game. If player1 and player2 land on the same grid they must have a duel. In my game the two players must have a game of rock paper scissors. I am having trouble getting my method to return the value int win to my subprogram Begin(). The win value will tell the begin method if either Player 1 or 2 won the rock paper scissors battle.
My code:
public static String Begin // Recieves data from the main method
{
// start Begin method
int win=0;
if(P1Position==P2Position||P2Position==P1Position){
System.out.println("==========================================================================");
System.out.println (player1+" is currently on square " + P1Position);
System.out.println (player2+" is currently on square " + P2Position);
System.out.println("==========================================================================");
firstplay(choice1,choice2);
determineOutcome(choice1,choice2,win);
if(win==1){
determineOutcome(choice1,choice2,win);
P2Position=P1Position-P1Roll;
}
else{
determineOutcome(choice1,choice2,win);
P1Position=P2Position-P2Roll;
}
}
}
public static void firstplay(int choice1,int choice2)throws IOException{//USER
// Initializes the BufferReader for user input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("DUEL!!!\nThe Player who wins this round of rock paper scissors will keep their spot,and the loser ther that player's old spot");
System.out.println("1=Rock\n2=Paper\n3=Scissors\n===========");
System.out.println(player1+" choose:");
choice1=Integer.parseInt(br.readLine());
System.out.println(player2+" choose:");
choice2=Integer.parseInt(br.readLine());
if(choice1==1){
System.out.println(player1+" chose Rock");//If user picked ROck
}
else if(choice1==2){
System.out.println(player1+" chose Paper");//If user picked Paper
}
else if(choice1==3){
System.out.println(player1+" chose Scissors");//If user picked Scissors
}
if(choice2==1){
System.out.println(player2+" chose Rock");
}
else if(choice2==2){
System.out.println(player2+" chose Paper");
}
else if(choice2==3){
System.out.println(player2+" chose Scissors");
}
}
public static int determineOutcome(int choice1,int choice2,int win)throws IOException{
// Initializes the BufferReader for user input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int die=0;
//Rock vs Papaer
if(choice2==1&&choice1==2){
System.out.println(player1+" WON PAPER BEATS ROCK");
System.out.println(player1+" keeps their spot");
win=1;
}
else if(choice2==2&&choice1==1){
System.out.println(player2+" WON PAPAER BEATS ROCK ");
System.out.println(player1+" keeps their spot");
win=0;
}
//Scissors vs Paper
else if(choice2==2&&choice1==3){
System.out.println(player1+" WON SCISSORS BEAT PAPER ");
System.out.println(player1+" keeps their spot");
win=1;
}
else if(choice2==3&&choice1==2){
System.out.println(player2+" WON SCISSORS BEAT PAPER ");
System.out.println(player2+" keeps their spot");
win=0;
}
//Rock vs Scissors
else if(choice2==3&&choice1==1){
System.out.println(player1+" WON ROCK BEATS SCISSORS ");
System.out.println(player1+" keeps their spot");
win=1;
}
else if(choice2==1&&choice1==3){
System.out.println(player2+" WON ROCK BEATS SCISSORS ");
System.out.println(player2+" keeps their spot");
win=0;
}
//Ties
else if(choice2==1&&choice1==1){
System.out.println("YOU'VE TIED. Play once again");
}
else if(choice2==2&&choice1==2){
System.out.println("YOU'VE TIED. Play once again");
}
else if(choice2==3&&choice1==3){
System.out.println("YOU'VE TIED. Play once again");
}
return win;
}
}//end class
Output:
yo Rolled a 2
po Rolled a 2
------------------------------------------------------------------------
==========================================================================
yo is currently on square 2
po is currently on square 2
==========================================================================
DUEL!!!
The Player who wins this round of rock paper scissors will keep their spot,and the loser ther that player's old spot
1=Rock
2=Paper
3=Scissors
===========
yo choose:
1
po choose:
3
yo chose Rock
po chose Scissors
==========================================================================
yo is currently on square 0
po is currently on square 2
==========================================================================
yo press r to roll
Expected:
Output:
yo Rolled a 2
po Rolled a 2
------------------------------------------------------------------------
==========================================================================
yo is currently on square 2
po is currently on square 2
==========================================================================
DUEL!!!
The Player who wins this round of rock paper scissors will keep their spot,and the loser ther that player's old spot
1=Rock
2=Paper
3=Scissors
===========
yo choose:
1
po choose:
3
yo chose Rock
po chose Scissors
yo won rock beats scissors.
yo keeps their spot
==========================================================================
yo is currently on square 2
po is currently on square 0
==========================================================================
yo press r to roll
You have to store the int value returned by the method determineOutcome.
win = determineOutcome(choice1,choice2,win);
if(win==1){
...
}
Note that is not necessary to pass win as a parameter to determineOutcome(), since you are not using its actual value. You could try this instead:
public static int determineOutcome(int choice1, int choice2) throws IOException
{
// Initializes the BufferReader for user input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int die = 0;
int win = 2; // 2 == TIE
//Rock vs Papaer
if (choice2 == 1 && choice1 == 2) {
System.out.println(player1 + " WON PAPER BEATS ROCK");
System.out.println(player1 + " keeps their spot");
win = 1;
} else if (choice2 == 2 && choice1 == 1) {
System.out.println(player2 + " WON PAPAER BEATS ROCK ");
System.out.println(player1 + " keeps their spot");
win = 0;
}
//Scissors vs Paper
else if (choice2 == 2 && choice1 == 3) {
System.out.println(player1 + " WON SCISSORS BEAT PAPER ");
System.out.println(player1 + " keeps their spot");
win = 1;
} else if (choice2 == 3 && choice1 == 2) {
System.out.println(player2 + " WON SCISSORS BEAT PAPER ");
System.out.println(player2 + " keeps their spot");
win = 0;
}
//Rock vs Scissors
else if (choice2 == 3 && choice1 == 1) {
System.out.println(player1 + " WON ROCK BEATS SCISSORS ");
System.out.println(player1 + " keeps their spot");
win = 1;
} else if (choice2 == 1 && choice1 == 3) {
System.out.println(player2 + " WON ROCK BEATS SCISSORS ");
System.out.println(player2 + " keeps their spot");
win = 0;
}
//Ties
else if (choice2 == 1 && choice1 == 1) {
System.out.println("YOU'VE TIED. Play once again");
} else if (choice2 == 2 && choice1 == 2) {
System.out.println("YOU'VE TIED. Play once again");
} else if (choice2 == 3 && choice1 == 3) {
System.out.println("YOU'VE TIED. Play once again");
}
return win;
}
Here you are declaring a local variable win, set it (0, 1 or 2) in the respective conditionals and return it at the end.
Note: If your conditionals to determine if a player won covers all the cases, you can replace the "Ties cases" with an else:
//Rock vs Scissors
else if (choice2 == 3 && choice1 == 1) {
System.out.println(player1 + " WON ROCK BEATS SCISSORS ");
System.out.println(player1 + " keeps their spot");
win = 1;
} else if (choice2 == 1 && choice1 == 3) {
System.out.println(player2 + " WON ROCK BEATS SCISSORS ");
System.out.println(player2 + " keeps their spot");
win = 0;
}
//Ties
else {
System.out.println("YOU'VE TIED. Play once again");
}

Java rock paper scissors loop

I'm having to make a paper rock scissors program that has the user enter in a choice, then tests it against the computer's choice. After every game, it should ask the player if they want to continue, and they should enter in 'Y' or 'N' to continue or quit. The best I could think was a while loop, and everything works fine except the very last bit.
import java.util.Scanner;
public class rockpaperscissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char cont = 'y';
while (cont == 'y'){
int com = (int)(Math.random() * 3);
System.out.println("Paper (0), Rock (1), or Scizzors (2)?");
int hum = input.nextInt();
if (com==(hum))
System.out.println("It's a tie!");
else if (hum == 0)
{
if (com == 1)
System.out.println ("You chose paper, computer chose rock You Win!");
else if (com == 2)
System.out.println ("You chose paper, Computer chose scissors You Lose!");
}
else if (hum == 1)
{
if (com == 2)
System.out.println ("You chose Rock, computer chose scissors You Win!");
else if (com == 0)
System.out.println ("You chose Rock, Computer chose paper You Lose!");
}
else if (hum == 2)
{
if (com == 0)
System.out.println ("You chose Scissors, computer chose paper You Win!");
else if (com == 1)
System.out.println ("You chose Scissors, Computer chose rock You Lose!");
}
System.out.println("Would you like to continue? (Y/N)");
cont = input.nextLine().charAt(0);
}
}
}
When I run it, the loop runs fine, the game is played, but then I get a 'string out of index range' error. Any idea how to resolve this?
Your nextInt() just reads the number from the input buffer, leaving the new line in it. So when you call input.nextLine() you're getting an empty line - the rest of the first line after the number. You should read the next-line and make sure it's not empty. If it is, just read it again.
Incidentally, your code that figures out who won is a bit cumbersome. If I were you, I would try to make it a little more general and clean. Think about a solution that can handle a more complex game, such as Rock Paper Scissors Lizard Spock without adding too much code.
When you get the answer from the user, you don't read the next line so the scanner still has a new line character. Then when you read the nextline you read that new line, and therefore there is no charat(0).
Change:
cont = input.nextLine().charAt(0);
to:
cont = input.next().charAt(0);
package rockpaper;
import java.util.Scanner;
/**
*
* #author Allen E.
*/
public class RockPaper {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int rock = 0;
int paper = 1;
int Scissors = 2;
int user = 0;
int computer = 0;
int gamesplayed = 0;
Scanner scan = new Scanner(System.in);
while (gamesplayed < 3)
{
System.out.println("Rock = 0 , Paper = 1, Scissors = 2");
String userinput = scan.nextLine();
int convertinput = Integer.valueOf(userinput);
int Computerinput = (int)(Math.random()*3);
if (Computerinput == 1 && convertinput == 0)
{
System.out.println("Paper beats Rock " +
"\nThe computer won");
gamesplayed++;
computer++;
}
else if (convertinput == 1 && Computerinput == 0)
{
System.out.println("Paper beats Rock " +
"\nYou Win!");
gamesplayed++;
user++;
}
if (Computerinput == 0 && convertinput == 2)
{
System.out.println("Rock beats Scissors " +
"\nThe computer won");
gamesplayed++;
computer++;
}
else if (convertinput == 0 && Computerinput == 2)
{
System.out.println("Rock beats Scissors " +
"\nYou Win!");
gamesplayed++;
user++;
}
if (Computerinput == 2 && convertinput == 1)
{
System.out.println("Scissors beats Paper " +
"\nThe computer won");
gamesplayed++;
computer++;
}
else if (convertinput == 2 && Computerinput == 1 )
{
System.out.println("Scissors beats Paper " +
"\nYou Win");
gamesplayed++;
user++;
}
/*************************************************
* *
* *
* Handling a tie *
* *
* *
*************************************************/
if (Computerinput == 0 && convertinput == 0)
{
System.out.println("Rock ties Rock " +
"\nTie");
}
if (Computerinput == 1 && convertinput == 1)
{
System.out.println("Paper ties Paper " +
"\nTie");
}
if (Computerinput == 2 && convertinput == 2)
{
System.out.println("Scissors ties Scissors " +
"\nTie");
}/*End of While Loop*/
}
}
}

Categories

Resources