How to output game code properly - java

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");
}

Related

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

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.

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.

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*/
}
}
}

I made a simple game in Java. Is it possible to refresh the console, so that everytime it receives new data it displays only that?

My game will play 10 rounds, so each time you advance to a new round, the preceding rounds are still visible. Is there anyway to have the console refresh itself so that only the round you are on is visible?
// Rock Paper Scissor Shoot Game
import java.util.Random;
import java.util.Scanner;
public class RockPaperSciccor {
public static void main(String[] args){
int wins = 0;
int losses = 0;
int rnd;
System.out.print("*************************************\n*");
System.out.println(" Rock, Paper, Scissor, Shoot! *\n* \tBy: Alex Hollander \t *");
System.out.println("*\t\t\t\t *");
System.out.print("*************************************\n");
System.out.println("\t\t\t\t\t Instructions:\n\t\t\t\t\t Choose a number 1-3\n \t\t\t\t\t to play the computer.");
// Play's 10 rounds before terminating
for(rnd=10;rnd>=1;rnd--){
//Display's rounds left on the right side
System.out.println("\t\t\t\t\t ____________\n\t\t\t\t\t |1 = Rock |\n\t\t\t\t\t |2 = Paper |\n\t\t\t\t\t |3 = Scissor|");
System.out.println("\t\t\t\t\t ~~~~~~~~~~~~~~");
System.out.println("\t\t\t\t\t|Rounds Left:" + (rnd) + "|");
System.out.println("\t\t\t\t\t ~~~~~~~~~~~~~~");
// Creates the PC, who chooses a random # 1-3
Random GAME = new Random();
int PC = 1+GAME.nextInt(3);
//User input
Scanner input = new Scanner (System.in);
int SCISSOR, ROCK, PAPER;
ROCK = 1;
PAPER = 2;
SCISSOR= 3;
int USER = input.nextInt();
//If the user enters a value greater then 3 or less than 1 it will terminate the prgm
//and display an error msg
while (USER > 3 || USER < 1) {
System.err.println("Incorrect value entered. Restart the Game\nand choose a number 1-3");
return;
}
System.out.println("___________________");
//All Possible Outcomes
//Draw
if(USER == PC){
if(USER == SCISSOR){
System.out.println("You Both Played Scissor");
}
if(USER == ROCK){
System.out.println("You Both Played Rock");
}
if(USER == PAPER){
System.out.println("You Both Played Paper");
}
System.out.println("Draw :\\");
System.out.println("|~~~~~~~~~~~~~~~~~~|");
System.out.println(" Wins: " + wins + "|Losses: " + losses);
System.out.println("|~~~~~~~~~~~~~~~~~~|");
}
//User wins
if(USER == SCISSOR)
if(PC == PAPER){
System.out.println("You: Scissor\nPC: Paper");
System.out.println("Scissor Cuts Paper");
System.out.println("You Win! :]");
wins++;
System.out.println("|~~~~~~~~~~~~~~~~~~|");
System.out.println(" Wins: " + wins + "|Losses: " + losses);
System.out.println("|~~~~~~~~~~~~~~~~~~|");
}
//Pc wins
else if(PC == ROCK){
System.out.println("You: Scissor \nPC: Rock");
System.out.println("Rock Breaks Scissor!");
System.out.println("PC Wins! >:D");
losses++;
System.out.println("|~~~~~~~~~~~~~~~~~~|");
System.out.println(" Wins: " + wins + "|Losses: " + losses);
System.out.println("|~~~~~~~~~~~~~~~~~~|");
}
//User wins
if(USER == ROCK)
if(PC == SCISSOR ){
System.out.println("You: Rock\nPC: Scissor");
System.out.println("Rock Breaks Scissor");
System.out.println("You Win! :]");
wins++;
System.out.println("|~~~~~~~~~~~~~~~~~~|");
System.out.println(" Wins: " + wins + "|Losses: " + losses);
System.out.println("|~~~~~~~~~~~~~~~~~~|");
}
//Pc wins
else if (PC == PAPER){
System.out.println("You: Rock\nPC: Paper");
System.out.println("Paper Covers Rock!");
System.out.println("PC Wins! >:D");
losses++;
System.out.println("|~~~~~~~~~~~~~~~~~~|");
System.out.println(" Wins: " + wins + "|Losses: " + losses);
System.out.println("|~~~~~~~~~~~~~~~~~~|");
}
//User Wins
if(USER == PAPER)
if(PC == ROCK){
System.out.println("You: Paper\nPC: Rock");
System.out.println("Paper Covers Rock");
System.out.println("You Win! :]");
wins++;
System.out.println("|~~~~~~~~~~~~~~~~~~|");
System.out.println(" Wins: " + wins + "|Losses: " + losses);
System.out.println("|~~~~~~~~~~~~~~~~~~|");
}
// Pc Wins
else if (PC == SCISSOR){
System.out.println("You: Paper\nPC: Scissor");
System.out.println("Scissor Cuts Paper!");
System.out.println("PC Wins! >:D");
losses++;
System.out.println("|~~~~~~~~~~~~~~~~~~|");
System.out.println(" Wins: " + wins + "|Losses: " + losses);
System.out.println("|~~~~~~~~~~~~~~~~~~|");
}
if(rnd==1){// I used .err so that in eclipse, the text would be red
System.err.println("\t\t\t\t\t" +
" Game Over!");
if(wins<losses){
System.err.println("\t\t\t\t\t YOU HAVE BEEN BEATEN BY THE PC!\n \t\t\t\t\t\t>:D");
}else if(wins>losses){
System.err.println("\t\t\t\t\t YOU BEAT THE COMPUTER!\n \t\t\t\t\t\t:]");
}else if(wins==losses){
System.err.println("\t\t\t\t\t STALEMATE!\n \t\t\t\t\t\t:\\");
}
}
}
}
}
If your platform is UNIX/Linux and you run your game on a terminal, all you need to do is send a control sequence which clears the screen. You can find out what the sequence is by checking what the clear command does, e.g. with the following command:
clear | od -t c
On most terminals this displays
0000000 033 [ H 033 [ 2 J
0000007
which is really two ANSI/VT100 control sequences: Cursor Home and Erase Screen. See ANSI/VT100 control sequences for details.
You can send the sequence from Java like this:
System.out.println("\033[H\033[2J");
If you're looking for a more portable way of doing this, you can just display a sufficiently large number of newline characters. This however will position the cursor at the bottom of the screen.
You want to "clear the screen" or "clear the console"
This is a duplicate of this question

Categories

Resources