Rock Paper Scissors Difficulty Java - 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.

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 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.

Rock paper scissors, play again?

For my assignment I'm supposed make the program rock paper scissors which I figured that out my main problem is I can't get the program to play again right or get the program to calculate the game scores correctly pleas help I'm going crazy trying to figure this out?!
//Tayler Dorsey
import java.util.Random;
import java.util.Scanner;
public class PRS {
static Scanner keyboard = new Scanner(System. in );
public static void instructions() {
System.out.println("This is the popular game of paper, rock, scissors. Enter your\nchoice by typing the word \"paper\", the word \"rock\" or the word\n\"scissors\". The computer will also make a choice from the three\noptions. After you have entered your choice, the winner of the\ngame will be determined according to the following rules:");
System.out.println("Paper wraps rock (paper wins)\nRock breaks scissors (rock wins)\nScissors cuts paper (scissors wins)");
System.out.println("If both you and the computer enter the same choice, then the game is tied.");
}
public static int playGame() {
int ties = 0, wins = 0, losts = 0;
String userchoice, computerchoice;
System.out.println("Enter your choice: ");
userchoice = keyboard.next();
computerchoice = computerChoose();
System.out.println("You entered: " + userchoice);
System.out.println("Computer choose: " + computerchoice);
if ((userchoice.equals("paper") && computerchoice.equals("paper")) || (userchoice.equals("rock") && computerchoice.equals("rock")) || (userchoice.equals("scissors") && computerchoice.equals("scissors"))) {
System.out.println("IT'S A TIE!");
ties++;
return 3;
} else if ((userchoice.equals("paper") && computerchoice.equals("rock")) || (userchoice.equals("rock") && computerchoice.equals("scissors")) || (userchoice.equals("scissors") && computerchoice.equals("paper"))) {
System.out.println("YOU WIN!");
wins++;
return 1;
} else {
System.out.println("YOU LOSE!");
losts++;
return 2;
}
}
public static String computerChoose() {
Random generator = new Random();
String[] answer = new String[3];
answer[0] = "paper";
answer[1] = "rock";
answer[2] = "scissors";
return answer[generator.nextInt(3)];
}
public static void main(String[] args) {
String play;
Scanner keyboard = new Scanner(System. in );
System.out.println("The Game of Paper, Rock, Scissors");
System.out.println("Do you need instructions (y or n)?");
String help = keyboard.nextLine();
if (help.equals("y")) instructions();
int result = playGame();
System.out.println("Play again (y or n)?");
play = keyboard.nextLine();
if (play.equals("y"));
else {
int count = 0, wins = 0, losts = 0, ties = 0;
System.out.println("Games played: " + count);
System.out.println("Wins for you: " + wins);
System.out.println("Wins for me: " + losts);
System.out.println("Tied games: " + ties);
}
do {} while (play == "y"); {
playGame();
int count = 0;
count++;
}
}
}
There are two issues here:
The code isn't inside the do-while loop, it's after it.
String equality should be checked with equals not with ==.
So:
int count = 0;
do { // Note the code inside the do-while block
playGame();
count++;
} while (play.equals("y")); // Note the use of equals
Do the following:
do {
play = keyboard.nextLine();
if (play.equals("y")){
}
else {
int count = 0, wins = 0, losts = 0, ties = 0;
System.out.println("Games played: " + count);
System.out.println("Wins for you: " + wins);
System.out.println("Wins for me: " + losts);
System.out.println("Tied games: " + ties);
}
} while (play.equals("y"));
In order to calculate the games scores make those variable global.
public class PRS {
static Scanner keyboard = new Scanner(System. in );
int ties = 0, wins = 0, losts = 0;

Rock Paper Scissors. Java. Methods

I'm assuming this is a logical error. I can't get the results to add up and I can't get the right results. Every time I enter either rock, paper, scissors, it then decides from there if I've won, lost, or tied. What is wrong with my code?
public class RockPaperScissors {
public static void displayGreeting()
{
String intro = "This program is a game. A game of Rock, Paper, Scissors\n"+
"It is you against the computer. Rock beats scissors, Paper\n"+
" beats rock, and scissors beats paper. Good luck and may the\n"+
"odds be ever in your favor.";
JOptionPane.showMessageDialog(null, intro, "Rock Paper Scissors",1);
}
public static String generateComputersChoice()
{
Random randomGenrator = new Random();
int randomNumber = randomGenrator.nextInt(3);
String weapon = "nothing";
switch(randomNumber){
case 0: weapon = "rock";
break;
case 1: weapon = "paper";
break;
case 2: weapon = "scissors";
break;
}
return weapon;
}
public static String enterPlayersChoice(){
String prompt = "You have a choice of picking rock, paper, or scissors.\n"+
"Choose wisely.";
String input = "";
input = JOptionPane.showInputDialog(null,prompt,"Choose your weapon",1);
String inputLower = input.toLowerCase();
return inputLower;
}
public static void main(String[] args)
{
displayGreeting();
// generateComputersChoice();
//enterPlayersChoice();
// JOptionPane.showMessageDialog(null,generateComputersChoice()+ enterPlayersChoice(5));
String player = enterPlayersChoice();
String comp = generateComputersChoice();
int ties = 0;
int playerWins = 0;
int compWins = 0;
for(int i = 0; i < 3; i ++){
//enterPlayersChoice(); //method
//generateComputersChoice(); //method
//JOptionPane.showMessageDialog(null,generateComputersChoice()+ enterPlayersChoice(1));
//System.out.println(player+ " " + comp);
//JOptionPane.showMessageDialog(null,player+ " " +comp);
if(player.equals(comp)){
JOptionPane.showMessageDialog(null, "It's a tie!");
ties ++;
}
else if(player.equals("rock")){
if(comp.equals("scissors")){
JOptionPane.showMessageDialog(null, "You win!");
playerWins ++;
}
}else if(comp.equals("rock")){
if(player.equals("scissors")){
JOptionPane.showMessageDialog(null, "You lose!");
compWins ++;
}
}else if(player.equals("scissors")){
if(comp.equals("paper")){
JOptionPane.showMessageDialog(null, "You win!");
playerWins ++;
}
}else if(comp.equals("scissors")){
if(player.equals("paper")){
JOptionPane.showMessageDialog(null, "You lose");
compWins ++;
}
}else if(player.equals("paper")){
if(comp.equals("rock")){
JOptionPane.showMessageDialog(null, "You Win!");
playerWins ++;
}
}else if(comp.equals("paper")){
if(player.equals("rock")){
JOptionPane.showMessageDialog(null, "You lose!");
compWins ++;
}
}else{
JOptionPane.showMessageDialog(null, "Invalid user input");
i--;
}
}
//Results
JOptionPane.showMessageDialog(null,"Here are the results\n\n"+
"\nTies: " +ties+
"\nComputer Wins: " +compWins+
"\nPlayer Wins: " + playerWins+
"\n\n Program Terminating", "Results",1);
}
}
Then it decides whether I've won, tied or lost but it won't
"reiterate" after that. After that it just says i've either won, lost,
or tied 3 times in a row
What you're actually doing is asking the input one time and then run the loop, so obviously you'll get the same result 3 times.
You need to ask at each iteration the user input :
String player;
String comp;
int ties = 0;
int playerWins = 0;
int compWins = 0;
for(int i = 0; i < 3; i ++){
player = enterPlayersChoice();
comp = generateComputersChoice();
/**/
}
First, you must get the player's choice and the computer's choice each loop. Move
String player = enterPlayersChoice();
String comp = generateComputersChoice();
inside the for loop at the start.
Also, if you say
else if(player.equals("rock")){
if(comp.equals("scissors")){
JOptionPane.showMessageDialog(null, "You win!");
playerWins ++;
}
Then the else if block will match when the player chooses "rock" regardless of the computer's choice. Then, if the computer doesn't choose "scissors", nothing will happen.
You need both conditions to match for this scenario to execute, else the first condition will match but the second might not, and nothing will happen. Try
else if (player.equals("rock") && comp.equals("scissors")){
and likewise for the other conditions.
you have regenerate the the user and computer's choices each time through the loop.
move
String player = enterPlayersChoice();
String comp = generateComputersChoice();
into the loop else it will be the same all 3 times

Nim game - specify winner

import java.util.Scanner;
/**
*#author Andy
*#verison 21.11.2012
*/
public class NimGame
{
public static void main (String[] args)
{
System.out.println ("********** Hello Welcome to the game Nim *********");
System.out.println (" The game is relatively simple.... ");
System.out.println (" This is a game for two players. ");
System.out.println (" There is a heap containing 10 to 20 stones.");
System.out.println (" Players take turns to remove 1-4 stones ");
System.out.println (" The player who removes the last stone wins. ");
System.out.println ("******************************************************************");
Scanner scan = new Scanner (System.in);
int heapSize = 15;
int stones = 0;
boolean nextInteger = false;
boolean lessThanFour = false;
String player1 = "Player 1";
String player2 = "Player 2";
String player = player1;
System.out.println ("The number of stones currently in the heap is :- " + heapSize);
System.out.println();
while (heapSize > 0)
{
nextInteger = false;
lessThanFour = false;
System.out.println (player + ":" + "how many stones will you take from the heap?");
System.out.println();
while (nextInteger == false && lessThanFour == false)
{
if (scan.hasNextInt())
{
nextInteger = true;
stones = scan.nextInt();
if (stones <=4 && stones >0)
{
System.out.println();
System.out.println ("You picked " + stones);
heapSize = (heapSize - stones);
if (heapSize >= 0)
{
System.out.println();
System.out.println ("There are " + heapSize + "stones left");
System.out.println();
lessThanFour = true;
System.out.println();
if (player.equals(player1))
{
player = player2;
}
else
{
player = player1;
}
}
else
{
System.out.println ("Bad input, please try again");
nextInteger = false;
scan.nextLine();
}
}
else
{
System.out.println ("Bad input, please try again");
scan.nextLine();
}
}
}
}
}
}
I dunno how to implement a way to specify player 1 or player 2 being the winner once the sizeheap (number of stones left) reaches 0. Any help would be appreciated. Also when the sizeheap reaches a negative number, it will display 'bad input' but then any other number inserted after that also displays 'bad input.'
Thanks!
Basically, you just need to rewrite if (heapSize >= 0) so it displays a win message:
if (heapSize > 0) {...} else { ...win... }
Here's the critical part, fixed, streamlined a bit, and edited:
if (stones <= 4 && stones > 0) {
System.out.println ("\nYou picked " + stones);
heapSize = (heapSize - stones);
if (heapSize > 0) {
System.out.println ("\nThere are " + heapSize + " stones left\n\n");
// Could use a ternary operator here:
// player = (player.equals(player1) ? player2 : player1);
if (player.equals(player1)) {
player = player2;
}
else {
player = player1;
}
}
else {
if (player.equals(player1)) {
System.out.println("Player 1 wins!");
}
else {
System.out.println("Player 2 wins!");
}
}
}
Further tips:
You can just use a newline \n instead of an System.out.println().
The lessThanFour flag is probably unnecessary

Categories

Resources