Question

My group and I have decided to write a java program for a game of rock, paper, scissors for an end of semester project for CSIS 1400 and have it written for the most part but are unsure on how to terminate a while loop using integer user input. We want the user to type 0 and have the loop stop. We attempted to use a do-while loop but we would have to initialize pChoice outside of the loop which would interfere with the functionality of the program.

Here is the code we have so far:

package RPS;

import java.util.Scanner;
import java.util.Random;
//import java.io.Console;
public class Game 
{

public static void main (String[] args) 
{

String username;
String password;

System.out.print ("Username: ");
username = Security.user();

System.out.print ("Password: ");
password = Security.pswd ();

//Console console = System.console ();
//password = new String (console.readPassword ("Password: ")); 

if ( username.equals (Security.jacobUser  ()) && password.equals (Security.jacobPswd  ()) || 
 username.equals (Security.tuckerUser ()) && password.equals (Security.tuckerPswd ()) || 
 username.equals (Security.austinUser ()) && password.equals (Security.austinPswd ())  )
{

Scanner input = new Scanner(System.in);
Random randRoll = new Random();

int choiceArray[] = {1, 2, 3};

System.out.println ("Hello, Welcome to Rock-Paper-Scissors!");
System.out.println ("Here are your choices:");
System.out.printf ("%d for Rock \n", choiceArray[0]);
System.out.printf ("%d for Paper \n", choiceArray[1]);
System.out.printf ("%d for Scissors \n", choiceArray[2]);

String doPlay = "yes";
String dontPlay = "no";
String playChoice;
int choice;

System.out.println ("Do you want to play?");
playChoice = input.nextLine ();

if (playChoice.equals (dontPlay))
{
System.out.println ("Program Terminated");
}

int cChoice;
int pChoice;

int cScore = 0, pScore = 0, tie = 0, rounds = 0;

    while (playChoice.equals (doPlay) || Game.done ().equals (dontPlay))
        {

            System.out.println ("Please make a selection, or use 0 to end the game:");
            cChoice = randRoll.nextInt (3) + 1;
            pChoice = input.nextInt ();

            if (pChoice == choiceArray[0] || pChoice == choiceArray[1] || pChoice == choiceArray[2])
            {



            if (pChoice == cChoice)
                { 
                    System.out.println ("Tie Game!");
                    System.out.println ();
                    tie++;
                    rounds++;
                } 

            else
                {
                    if (cChoice==1 && pChoice==3)
                        {
                            System.out.println ("Computer picked Rock!");
                            System.out.println ("Rock beats Scissors!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        } 

                    if (cChoice==1 && pChoice==2)
                        {
                            System.out.println ("Computer picked Rock!");
                            System.out.println ("Paper beats Rock!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==2 && pChoice==3)
                        {
                            System.out.println ("Computer picked Paper!");
                            System.out.println ("Scissors beats Paper!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==2 && pChoice==1) 
                        { 
                            System.out.println ("Computer picked Paper!");
                            System.out.println ("Paper beats Rock!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        } 

                    if (cChoice==3 && pChoice==1)  
                        {
                            System.out.println ("Computer picked Scissors!");
                            System.out.println ("Rock beats Scissors!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==3 && pChoice==2) 
                        {
                            System.out.println ("Computer picked Scissors!");
                            System.out.println ("Scissors beats Paper!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        }
                }

                System.out.println ("Scores: " + rounds +" rounds:");
                System.out.println ("You\tComputer\tTies");
                System.out.println (" "+ pScore +"\t   " + cScore + "\t\t " + tie);
            }
        }
}
else
{
System.out.println ("Incorrect Credentials");
}
}
}

All of the console stuff is in comments because netbeans throws a null pointer exception upon run but it works in the command line just fine and we just want to continue working in the IDE for now until it is finished. We also want it to terminate if playChoice = dontPlay.

Any help would be appreciated, Thanks.

Here is our finished source code:

package RPS;

import java.util.Scanner;
import java.util.Random;
import java.io.Console;
public class Game 
{

public static void main (String[] args) 
{

System.out.println ("Credentials required.");

String username;
String password;

System.out.print ("Username: ");
username = Security.user();

Console console = System.console ();
password = new String (console.readPassword ("Password: ")); 

if ( username.equals (Security.jacobUser  ()) && password.equals (Security.jacobPswd  ()) || 
 username.equals (Security.tuckerUser ()) && password.equals (Security.tuckerPswd ()) || 
 username.equals (Security.austinUser ()) && password.equals (Security.austinPswd ())  )
{

Random randRoll = new Random();
Scanner input = new Scanner (System.in);   

int choiceArray[] = {1, 2, 3};

System.out.println ("Hello, Welcome to Rock-Paper-Scissors!");
System.out.println ("Here are your choices:");
System.out.printf ("%d for Rock \n", choiceArray[0]);
System.out.printf ("%d for Paper \n", choiceArray[1]);
System.out.printf ("%d for Scissors \n", choiceArray[2]);

int cChoice;
int pChoice;

int cScore = 0, pScore = 0, tie = 0, rounds = 0;


    do
        {

            System.out.println ("Please make a selection, or use 0 to end the game:");
            cChoice = randRoll.nextInt (3) + 1;
            pChoice = input.nextInt ();

            if (pChoice == choiceArray[0] || pChoice == choiceArray[1] || pChoice == choiceArray[2])
            {



            if (pChoice == cChoice)
                { 
                    System.out.println ("Tie Game!");
                    System.out.println ();
                    tie++;
                    rounds++;
                } 

            else
                {
                    if (cChoice==1 && pChoice==3)
                        {
                            System.out.println ("Computer picked Rock!");
                            System.out.println ("Rock beats Scissors!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        } 

                    if (cChoice==1 && pChoice==2)
                        {
                            System.out.println ("Computer picked Rock!");
                            System.out.println ("Paper beats Rock!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==2 && pChoice==3)
                        {
                            System.out.println ("Computer picked Paper!");
                            System.out.println ("Scissors beats Paper!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==2 && pChoice==1) 
                        { 
                            System.out.println ("Computer picked Paper!");
                            System.out.println ("Paper beats Rock!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        } 

                    if (cChoice==3 && pChoice==1)  
                        {
                            System.out.println ("Computer picked Scissors!");
                            System.out.println ("Rock beats Scissors!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==3 && pChoice==2) 
                        {
                            System.out.println ("Computer picked Scissors!");
                            System.out.println ("Scissors beats Paper!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        }
                }
            }
        }while (pChoice != 0); 

        System.out.println ("Here are the final standings.");
        System.out.println ("Rounds: " + rounds);
        System.out.println ("You\tComputer\tTies");
        System.out.println (" "+ pScore +"\t   " + cScore + "\t\t " + tie);
}
else
{
System.out.println ("Incorrect Credentials");
}
    }
} 

Security class:

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package RPS;

import java.util.Scanner;

/**
 *
 * @author Jacob, Tucker, Austin
 */
public class Security {

public static String user ()
{
    Scanner input = new Scanner (System.in);
    String username = input.nextLine ();
    return username;
}

public static String pswd ()
{
    Scanner input = new Scanner (System.in);
    String password = input.nextLine ();
    return password;
}

public static String jacobUser ()
{
    String username = "jacob";
    return username;
}

public static String jacobPswd ()
{
    String password = "password";
    return password;
}

public static String tuckerUser ()
{
    String username = "tucker";
    return username;
}

public static String tuckerPswd ()
{
    String password = "password";
    return password;
}

public static String austinUser ()
{
    String username = "austin";
    return username;
}

public static String austinPswd ()
{
    String password = "password";
    return password;
}
}

We are now getting compiling errors when using javac. Details are listed in the comments below.

Was it helpful?

Solution

Implementing a Do-While loop is pretty much always the best approach when handling repeatable user input

    do {

        System.out.println ("Please make a selection, or use 0 to end the game:");
        cChoice = randRoll.nextInt (3) + 1;
        pChoice = input.nextInt ();

        if (pChoice == choiceArray[0] || pChoice == choiceArray[1] || pChoice == choiceArray[2])
        {
            ...
            // All your normal game stuff
            ...
        }
    }while(pChoice != 0)

pChoice is initialized by the first pass through the loop

OTHER TIPS

Use whatever sort of loop you find easiest. But put a break; command inside. So something like

if (pChoice == 0) {
    break;
}

This will cause execution to escape from the loop.

You can initialize pchoice to a number that wont affect the game so you can use the while loop.

Another possibility is to have a boolean variable that is initialized before the loop that gets changed if pchoice == 0.

You do not need to test playChoice as part of the loop condition. It is only used to initialize, you can use an if statement to exit if they don't want to play.

You call game.done() to test for whether they are done, but you don't show what that method does. If game.done() tests the integer they enter as their choice along with 1, 2, or 3, then that will terminate the loop. Since your player enters that number before the game logic, you will also need an if statement to determine if they chose to end the game after they enter it but before the game logic. Something like if (!(game.done().equals(dontPlay))) so the game logic only executes if they entered another choice; that will skip all the game logic when they say they want to stop, and then drop out of the bottom of the loop.

Good luck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top