Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Repeating a game

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Repeating a game

    I am looking to repeat this game, and I'm unsure as to how to go about it. Does anyone want to give me suggestions? I am unsure as to how to make the appropriate loop to do so.

     
    import java.util.Random;
    import java.util.Scanner;
     
     
    class FunGame
    {
    	/*@param this is the main method
    	 * 
    	 */
        public static void main(String [] args)
        {
            Random ran = new Random();
            Scanner s = new Scanner(System.in);
            /*@ return this is returning a string to be used later on in the game.
             * 
             */
            String a = "Computer chose Rock";
            String b = "Computer chose Paper";
            String c = "Computer chose Scissors";
    		String e = "Computer chose Lizard";
    		String f = "Computer chose Spock";
    		// Initialize variables
            int hs = 0;
            int cs = 0;
            int d = 0;
     
     
     
            // Created string to use for the game
            String choice, rock, paper, scissors, lizard, spock;
            int comp;
            /* Created a while loop that prints out the appropriate current score and 
             * basic score for each game played.
             */
            while(hs < 5 && cs < 5)
            {
    			comp = ran.nextInt(5);
    			/* For this assignment, I am using shorthand p to venture new ways
    			 * into learning how to implement different things into Java.
    			 */
     
    			p("\nRock, Paper, Scissors, Lizard, Spock . . . Go!\n");
    	    	p("Current score: \nYou = " + hs + "\nComputer = " + cs + "\nDraws = " + d + "\n\n");
    			choice = s.nextLine();
                /* In my example, rock is 0, paper 1, scissors 2 , lizard 3, and spock 4
                 * The if statements are self explanatory so I will include the logic in one piece.
                 * For the first if statement, if the user inputs rock, the computer will randomly 
                 * select an option, and based on that option, the output is printed notifying
                 * the user if they won or not.  For example, if the user selects rock and the random
                 * selection is = 0, which is rock, there is a tie.  This follows through all parts of the game
                 * and is consistent in each section.  IF the random selection is = 1 which is paper, the user loses.
                 */
    			if(choice.equals ("rock"))
    			{
                   	if(comp == 0)
                    {
                        p(a);
                        p("Rock and rock? Looks like we have a tie!");
                        d = d+1;
                    }
                    if(comp == 1)
                    {
                        p(b);
                        p("Paper covers rock, Want to try again?!");
                        cs = cs + 1;
                    }
                    if(comp == 2)
                    {
    					p(c);
                        p("Rock crushes scissors like Cartmen crushes Cheesy Poofs, You win!");
                        hs = hs+1;
                    }
    				if(comp == 3)
    				{
    					p(e);
    					p("Rock crushes the lizard! You win!");
    					hs = hs+1;
    				}
    				if(comp == 4)
    				{
    					p(f);
    					p("Spock vaporizes rocks. Womp Womp, try again!");
    					cs = cs+1;
    				}
     
     
                }
     
    			// If statement consistent with my previous statement comments
    			if(choice.equals ("paper"))
                {
                   	if(comp == 0)
                    {
                        p(a);
                        p("Paper closes on rock and no longer lets it see, you win!");
                        hs = hs + 1;
                    }
                    if(comp == 1)
                    {
                        p(b);
                        p("Paper and paper?  Try again!");
                        d = d+1;
                    }
                    if(comp == 2)
                    {
                        p(c);
                        p("Paper gets cut by scissors. You Lose!");
                        cs = cs + 1;
                    }
                    if(comp == 3)
                    {
                        p(e);
                        p("Lizard eats paper. You lose!");
                        cs = cs+1;
                    }
                    if(comp == 4)
                    {
                        p(f);
                        p("Paper beats spock! You win");
                        hs = hs + 1;
     
                    }
     
    			}
     
    			// If statements consistent with previous comments
    			if(choice.equals ("scissors"))
                {
                   	if(comp == 0)
                    {
                        p(a);
                        p("Scissors get smashed by rocks. Try again!");
                        cs = cs + 1;
                    }
                    if(comp == 1)
                    {
                        p(b);
                        p("Two scissors?  This isn't a sword fight! Tie!");
                        d = d+1;
                    }
                    if(comp == 2)
                    {
                        p(c);
                        p("Scissors demolish paper. You Win!");
                        hs = hs+1;
                    }
    				if(comp == 3)
    				{
    					p(e);
    					p("Scissors and lizards?  Things don't end well for the lizard.  You win! ");
    					hs = hs+1;
    				}
    				if(comp == 4)
    				{
    					p(f);
    					p("Spock crushes scissors! You Lose!");
    					cs = cs+1;
     
    				}
     
                }	
    			if(choice.equals ("lizard"))
                {
                   	if(comp == 0)
                    {
                        p(a);
                        p("Rock crushes your poor lizard. You lose!");
                        cs = cs + 1;
                    }
                    if(comp == 1)
                    {
                        p(b);
                        p("Lizard eats paper! You win!");
                        hs = hs + 1;
                    }
                    if(comp == 2)
                    {
                        p(c);
                        p("Scissors and lizards?! You lose, just think about that one!");
                        cs = cs+1;
                    }
    				if(comp == 3)
                    {
                        p(e);
                        p("Lizard and lizard = friendship. Draw.");
                        d = d + 1;
                    }
                    if(comp == 4)
                    {
                        p(f);
                        p("Lizard beats spock! You win!");
                        hs = hs + 1;
                    }
     
                }
                if(choice.equals ("spock"))
                {
                   	if(comp == 0)
                    {
                        p(a);
                        p("Spock vaporizes rock. You win!");
                        hs = hs + 1;
                    }
                    if(comp == 1)
                    {
                        p(b);
                        p("Spock loses to paper. You lose!");
                        cs = cs+1;
                    }
                    if(comp == 2)
                    {
                        p(c);
                        p("Spock > scissors. You win!");
                        hs = hs+1;
                    }
    				if(comp == 3)
                    {
                        p(e);
                        p("Spock is killed by a lizard. You lose!");
                        cs = cs+1;
                    }
                    if(comp == 4)
                    {
                        p(f);
                        p("Two spocks? Tie!");
                        d = d+1;
                    }
     
            	}
        	}
            // If statement saying if the computer score is 5, if you'd like do try again.
        	if(cs == 5)
        	{
        		p("\nThe compter has beat you.");
        		p("Try again?  yes or no?");
     
        	}
        	if(hs == 5)
        	{
        		p("You win! You managed to beat the computer!");
        	}
     
        }
        /* @param this describes a method parameter that I used for the string
         * 
         */
        public static void p(String a)
        {
            System.out.println(a);
        }
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Repeating a game

    pretty simple:
    repeat = true;
    do{
      // game here
      repeat = askUser();
    } while(repeat);

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Repeating a game

    So exactly how can I implement that? I'm really bad with repeating. Although it makes sense to me in some way, I'm not sure exactly how to use this.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Repeating a game

    Quote Originally Posted by kjdeutin View Post
    So exactly how can I implement that? I'm really bad with repeating. Although it makes sense to me in some way, I'm not sure exactly how to use this.
    Try working through the tutorial on the use of the loop and ask any questions you have left

Similar Threads

  1. [SOLVED] Repeating infinite loop in prime number finder
    By dontpanic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 6th, 2012, 01:24 PM
  2. Non-Repeating Integers
    By Tecness2 in forum Java Theory & Questions
    Replies: 5
    Last Post: June 7th, 2012, 09:38 AM
  3. [SOLVED] Java- Non repeating random questions.
    By arkaneraven in forum Java Theory & Questions
    Replies: 4
    Last Post: June 15th, 2011, 09:55 AM
  4. Repeating different parts of a program
    By swiftxjames in forum Loops & Control Statements
    Replies: 3
    Last Post: November 17th, 2010, 04:29 PM
  5. Repeating program issue
    By Bill_H in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2009, 10:58 AM