Rock Paper Scissors Game Best 2 out of 3 Loop Issues - java

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.

Related

java beginner rock paper scissors game

I am working on a "Rock, Paper, Scissors" game for my intro java class. Here is the prompt: Create a game of "Rock, Paper, Scissors" where the computer randomly chooses rock, paper, or scissors. Let the user enter a number of 1, 2, or 3, each representing one of three choices. Determine a winner. Game should ask the user to play again and continue if yes and stop if no. Once the user stops playing the program should print the total number of wins.
I am having issues with declaring my variables in the correct places since I am trying to use a method so I can call it to play the game again.
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
String answer = input.next();
}
public static int letsPlay()
{
int cMove;
int userMove = 0;
int cScore = 0;
int pScore = 0;
int tie = 0;
int rounds = 0;
Random r = new Random();
while (answer.equalsIgnoreCase("yes"))
cMove = r.nextInt(3)+1;
System.out.println("Choose your move!");
System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
userMove = input.nextInt();
while(input.hasNextInt()) {
if (userMove!=1 && userMove!=2 && userMove!=3)
{
System.out.println("Invalid move. Try again.");
System.out.println("Enter your move: ");
input.nextInt();
}
}
if(userMove==1)
{
System.out.println("You have chosen Rock!");
}
else if(userMove==2)
{
System.out.println("You have chosen Paper!");
}
else if(userMove==3)
{
System.out.println("You have chosen Scissors!");
}
if (userMove == cMove)
{
System.out.println("Tie Game!");
System.out.println("");
tie++;
rounds++;
} else if (cMove==1 && userMove==3)
{
System.out.println("Computer chose Rock!");
System.out.println("Rock beats Scissors!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==1 && userMove==2)
{
System.out.println("Computer chose Rock!");
System.out.println("Paper beats Rock!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==3)
{
System.out.println("Computer chose Paper!");
System.out.println("Scissors beats Paper!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==1)
{
System.out.println("Computer chose Paper!");
System.out.println("Paper beats Rock!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==3 && userMove==1)
{
System.out.println("Computer chose Scissors!");
System.out.println("Rock beats Scissors!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==3 && userMove==2)
{
System.out.println("Computer chose Scissors!");
System.out.println("Scissors beats Paper!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
System.out.println("Would you like to play again?");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
String yesorno = input.next();
if(yesorno.equalsIgnoreCase("yes"))
{
letsPlay();
}
else {
System.out.println ("Here are the final scores after " + rounds +" rounds:");
System.out.println ("You: "+ pScore + "Computer: "+ cScore + "Ties: " + tie);
}
}
}
Edited code so far, it says missing return statement from my letsPlay method:
Not sure how to proceed..
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
String answer = input.next();
letsPlay(answer);
}
public static int letsPlay(String answer)
{
int cMove;
int userMove = 0;
int cScore = 0;
int pScore = 0;
int tie = 0;
int rounds = 0;
Random r = new Random();
Scanner input = new Scanner(System.in);
cMove = r.nextInt(3)+1;
while (answer.equalsIgnoreCase("yes"))
System.out.println("Choose your move!");
System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
userMove = input.nextInt();
while(input.hasNextInt()) {
if (userMove!=1 && userMove!=2 && userMove!=3)
{
System.out.println("Invalid move. Try again.");
System.out.println("Enter your move: ");
input.nextInt();
}
}
if(userMove==1)
{
System.out.println("You have chosen Rock!");
}
else if(userMove==2)
{
System.out.println("You have chosen Paper!");
}
else if(userMove==3)
{
System.out.println("You have chosen Scissors!");
}
if (userMove == cMove)
{
System.out.println("Tie Game!");
System.out.println("");
tie++;
rounds++;
} else if (cMove==1 && userMove==3)
{
System.out.println("Computer chose Rock!");
System.out.println("Rock beats Scissors!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==1 && userMove==2)
{
System.out.println("Computer chose Rock!");
System.out.println("Paper beats Rock!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==3)
{
System.out.println("Computer chose Paper!");
System.out.println("Scissors beats Paper!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==1)
{
System.out.println("Computer chose Paper!");
System.out.println("Paper beats Rock!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==3 && userMove==1)
{
System.out.println("Computer chose Scissors!");
System.out.println("Rock beats Scissors!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==3 && userMove==2)
{
System.out.println("Computer chose Scissors!");
System.out.println("Scissors beats Paper!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
System.out.println("Would you like to play again?");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
answer = input.next();
if(answer.equalsIgnoreCase("yes"))
{
main(null);
}
else {
System.out.println ("Here are the final scores after " + rounds +"
rounds:");
System.out.println ("You: "+ pScore + "Computer: "+ cScore + "Ties: "
+ tie);
}
}
}
You aren't passing the String answer to your letsPlay() method and that's because your letsPlay() method can't take a String as a parameter because it is defined without parameters being passed. A solution to this problem is to change the method definition to require a String variable.
public static int letsPlay()
turns into
public static int letsPlay(String userInput)
then inside your method you use the variable userInput instead of String answer in the letsPLay(String userInput) method.
The next issue you run into is you're calling the method again within the method. This is called recursion and it's perfectly legal, however it is not ideal in this circumstance. You should exit the game once it's over and ask the user in your main() method if they'd like to play again.
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
do
{
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
String answer = input.nextLine();
letsPlay(answer);
}while(answer.equalsIgnoreCase("yes"));
}
Firstly, in your main method one input.next() is extra and of no use so remove it.
Now write a statement in main method as follows after String answer = input.next();
:
letsPlay(answer);
Put a parameter in letsPlay() method as follows:
public static void letsPlay(String answer) {
//Your code..........
//Some last edits...
Scanner input = new Scanner(System.in);
answer = input.next();
if(!(answer.equalsIgnoreCase("yes")))
{
System.out.println ("Here are the final scores after "+rounds+"
rounds:");
System.out.println("You:"+pScore+"Computer: "+cScore+"Ties: "+tie);
}
}
No required extra method for calling any line.
You can call main
Move codes to main from letsPlay method.
remove: letsPlay()
use: main(null)
import java.util.Scanner;
import java.util.Random;
public class Main
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
String answer = input.next();
// moved codes to following place from letsPlay
int cMove = 0;
...
if(yesorno.equalsIgnoreCase("yes"))
{
main(null); // changed with letsPlay()
}
...
}
}
cMove not initilazed exception occurred. So use this:
int cMove = 0;
Now, any errors not occurred.
Move the Scanner object inside the constructor. Currently you have it outside and your code does not know what it is.
Here is the modified portion of your code:
public static int letsPlay()
{
int cMove;
int userMove = 0;
int cScore = 0;
int pScore = 0;
int tie = 0;
int rounds = 0;
Random r = new Random();
Scanner input = new Scanner(System.in); // move the input object inside the constructor
Hope it helps.
There are many errors in your code. You can check out the comments in the code for a proper understanding of the code.
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
// initialising variable to 0 for score calculation
int cScore = 0;
int pScore = 0;
int tie = 0;
int rounds = 0;
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"Yes\" or \"No\"");
String answer = input.next();
if (answer.equalsIgnoreCase("yes")) {
// Calling method letsPlay with arguments answer, cScore, pScore, tie, rounds
// initially cScore = pScore = tie = rounds = 0
letsPlay(answer, cScore, pScore, tie, rounds);
}
}
// letsPlay Method
public static void letsPlay(String answer, int cScore, int pScore, int tie, int rounds) {
int cMove;
int userMove;
Random r = new Random();
Scanner input = new Scanner(System.in);
// loop untill user chose no
while (true) {
// to get random move of computer on every iteration
cMove = r.nextInt(3) + 1;
System.out.println("--------------------------------------------------");
System.out.println("Choose your move!");
System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
userMove = input.nextInt();
// loop until user input number 1 or 2 or 3
while (userMove != 1 && userMove != 2 && userMove != 3) {
System.out.println("Invalid move. Try again.");
System.out.println("--------------------------------------------------");
System.out.println("Choose your move: ");
System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
userMove = input.nextInt();
}
// Print statement for user move
if (userMove == 1) {
System.out.println("You have chosen Rock!");
} else if (userMove == 2) {
System.out.println("You have chosen Paper!");
} else {
System.out.println("You have chosen Scissors!");
}
// Print statement for computer move
if (cMove == 1) {
System.out.println("Computer chose Rock!");
} else if (cMove == 2) {
System.out.println("Computer chose Paper!");
} else {
System.out.println("Computer chose Scissors!");
}
// Winning, Loosing and Tie conditions
// increment round to 1 every time
// increment the winner, looser or tie on every iteration
if (userMove == cMove) {
System.out.println("Tie Game!");
tie++;
rounds++;
} else if (cMove == 1 && userMove == 3) {
System.out.println("Rock beats Scissors!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
} else if (cMove == 1 && userMove == 2) {
System.out.println("Paper beats Rock!");
System.out.println("Player Wins!");
pScore++;
rounds++;
} else if (cMove == 2 && userMove == 3) {
System.out.println("Scissors beats Paper!");
System.out.println("Player Wins!");
pScore++;
rounds++;
} else if (cMove == 2 && userMove == 1) {
System.out.println("Paper beats Rock!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
} else if (cMove == 3 && userMove == 1) {
System.out.println("Rock beats Scissors!");
System.out.println("Player Wins!");
pScore++;
rounds++;
} else if (cMove == 3 && userMove == 2) {
System.out.println("Scissors beats Paper!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
// Asking again to play or not
System.out.println("\nWould you like to play again?");
System.out.println("Answer \"Yes\" or \"No\"");
answer = input.next();
if (answer.equalsIgnoreCase("yes")) {
// If yes the call letsPlay(answer, cScore, pScore, tie, rounds);
// But this time value of cScore, pScore, tie, rounds is changed
// according to conditions
letsPlay(answer, cScore, pScore, tie, rounds);
} else {
// Print if user says didn't want to play again
System.out.println("==========================================");
System.out.println("\nHere are the final scores after " + rounds + " rounds:");
System.out.println("You : " + pScore + "\nComputer : " + cScore + "\nTies : " + tie);
}
// Exit if user didn't want to play again
break;
}
}
}

Rock Paper Scissors Difficulty Java

Im trying to make a rock, paper, scissors game where you choose 1,2 or 3 for rock then displays what you've chosen and what the computer has chosen. I then need to keep track of score for wins and at the end of every round ask the user if they want to play again.
import java.util.Scanner;
public class Rock5
{
//-----------------------------------------------------------------
// Plays the Rock-Paper-Scissors game with the user.
//-----------------------------------------------------------------
public static void main (String[] args)
{
final int OPTIONS = 3;
final int ROCK = 1, PAPER = 2, SCISSORS = 3;
final int COMPUTER = 1, PLAYER = 2, TIE = 3;
int computer, player, winner = 0;
int wins = 0, losses = 0, ties = 0;
String again;
Scanner in = new Scanner(System.in);
do
{
computer = (int) (Math.random() * OPTIONS) + 1;
System.out.println();
System.out.print ("Enter your choice - 1 for Rock, 2 for " +
"Paper, and 3 for Scissors: ");
player = in.nextInt();
System.out.print ("My choice was ");
// Determine the winner
switch (computer)
{
case ROCK:
System.out.println ("Rock.");
if (player == SCISSORS)
winner = COMPUTER;
else
if (player == PAPER)
winner = PLAYER;
else
winner = TIE;
break;
case PAPER:
System.out.println ("Paper.");
if (player == ROCK)
winner = COMPUTER;
else
if (player == SCISSORS)
winner = PLAYER;
else
winner = TIE;
break;
case SCISSORS:
System.out.println ("Scissors.");
if (player == PAPER)
winner = COMPUTER;
else
if (player == ROCK)
winner = PLAYER;
else
winner = TIE;
}
// Print results and increment appropriate counter
if (winner == COMPUTER)
{
System.out.println ("I win!");
losses++;
}
else
if (winner == PLAYER)
{
System.out.println ("You win!");
wins++;
}
else
{
System.out.println ("We tied!");
ties++;
}
System.out.println();
System.out.print ("Play again (y/n)?: ");
again = in.nextLine();
}
while (again.equalsIgnoreCase ("y"));
// Print final results
System.out.println();
System.out.println ("You won " + wins + " times.");
System.out.println ("You lost " + losses + " times.");
System.out.println ("We tied " + ties + " times.");
}
}
Use
player = Integer.valueOf(in.nextLine());
Instead of
player = in.nextInt();
and it'll work, because if not, the number you enter goes into player, and the \n characater goes into again = in.nextLine() and because it's different of y it stops.

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

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