Results 1 to 3 of 3

Thread: Rock Paper Scissors

  1. #1
    Join Date
    Sep 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rock Paper Scissors

    sorry about the other post
    can anyone help me out with this
    thanks

    i am trying to get it to play again and not print out the scores until the player is done playing


    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class boobitty
    {
    	public static void main (String[] args)
    	{
    	int Computer=0,Player=0,tie=0,compic,pscore;
    	String str="Y"; 
    	Random generate= new Random();
    	Scanner scan=new Scanner(System.in);
    	while (str=="Y");
    		{
    		compic=generate.nextInt(3)+1;
    		System.out.println ("Enter 1 for Rock, 2 for Paper, and 3 for Scissors");
    		pscore=scan.nextInt();
    		if (compic==pscore)
    		{
    		 System.out.println("Tie Game");
    		 tie++;
    		 System.out.println ("Play Again? y/n");
    		 str = scan.nextLine();
    		}
    		 else
    			{
    				switch (pscore)
    				{
    					case 1:
    					{	
    						  if (compic==2)
    						  {
    							 System.out.println ("Paper beats Rock");
    							 System.out.println ("Computer wins");
    							 Computer++;
    							 System.out.println ("Play Again? y/n");
    							 str = scan.nextLine();
    							}
    						  else
    						  {  
    								  System.out.println ("Rock beats Scissors");
    								  System.out.println ("Player wins!");
    								  Player++;
    								  System.out.println ("Play Again? y/n?");
    							 	  str = scan.nextLine();
    							}
    						 break;
    						}
    						case 2:
    						{
    						 if (compic==1)
    						 {
    							 System.out.println ("Paper beats Rock");
    							 System.out.println ("Player wins!");
    							 Player++;
    							 System.out.println ("Play Again? y/n?");
    							 	  str = scan.nextLine();
    				       }
    						 else
    						 {  
    								  System.out.println ("Scissors beat Paper");
    								  System.out.println ("Computer wins!");
    								  Computer++;
    								  System.out.println ("Play Again? y/n?");
    							     str = scan.nextLine();
    						}
    						 break;
    						}
    						case 3:
    						{
    						 if (compic==1)
    						 {
    							 System.out.println ("Rock beats Scissors");
    							 System.out.println ("Computer wins");
    							 Computer++;
    							 System.out.println ("Play Again? y/n?");
    							     str = scan.nextLine();
    						}
    						 else
    						 {  
    							System.out.println ("Scissors beat Paper");
    								  System.out.println ("Player wins");
    								  Player++;
    								  System.out.println ("Play Again? y/n");
    							     str = scan.nextLine();
    							}	  
    						 break;
    						 }
    					default:
    					{
    					System.out.println("Enter 1 2 or 3");
    					System.out.println ("Would you like to play again? y/n");
    					str=scan.nextLine();
    					break;
    					}
    				}
    				
    			}	
    	 }
    	 
    	 
    	 
    	 if (str=="Y")
    	 {
    	 		System.out.println ("Play Again? y/n");
    					 str = scan.nextLine();
    	  }
    	  else
    	  { 
    	  	 System.out.println ("Scores:");
    			System.out.println ("Ties= "+tie+" Wins= "+Player+" Losses= "+Computer);	
    
    	  
    	  }
    	 
    	    
    	 
    	 
    			
    	}
    }

    this is what i get from it
    Enter 1 for Rock, 2 for Paper, and 3 for Scissors
    2
    Scissors beat Paper
    Computer wins!
    Play Again? y/n?
    Scores:
    Ties= 0 Wins= 0 Losses= 1
    Last edited by Snookerman; 08-27-2009 at 09:54 AM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't like the way you've done this.
    Code:
    import java.util.Random;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    
    class JankenPlayer {
    	int score = 0;
    	boolean isAI;
    
    	Random rnd = new Random();
    
    	public JankenPlayer(boolean ai) {
    		isAI = ai;
    	}
    
    	public int go() {
    		if(isAI) {
    			int a = rnd.nextInt(3);
    			System.out.println("Computer chose " + Janken.choices[a] + ".");
    			return a;
    		} else {
    			System.out.print("\nIt's your turn.  [R]ock, [P]aper or [S]cissors? ");
    			BufferedReader stdin = new BufferedReader(
    				new InputStreamReader(
    					System.in
    				)
    			);
    
    			String line = "";
    			int choice = -1;
    
    			while(choice == -1) {
    				try {
    					line = stdin.readLine();
    				} catch(IOException e) {
    					System.out.println("Error: could not read from stdin.");
    					System.exit(2);
    				}
    				if(line.length() == 0) line = " ";
    				switch(line.charAt(0)) {
    					case 'r':
    					case 'R':
    						choice = 0;
    						break;
    					case 'p':
    					case 'P':
    						choice = 1;
    						break;
    					case 's':
    					case 'S':
    						choice = 2;
    						break;
    					default:
    						System.out.print("Invalid choice.  Please choose one of [R/P/S]: ");
    				}
    			}
    			return choice;
    		}
    	}
    }
    
    class Janken {
    	int maxPoints = 3;
    	JankenPlayer human = new JankenPlayer(false),
    		computer = new JankenPlayer(true);
    
    	public static final int ROCK = 0, PAPER = 1, SCISSORS = 2;
    
    	public static final String[] choices = {"rock", "paper", "scissors"};
    
    	public void play(int towin) {
    		maxPoints = towin;
    		while(human.score < maxPoints && computer.score < maxPoints)
    			takeTurn();
    		if(human.score < computer.score)
    			System.out.println("\n\n\n*************************\n***** COMPUTER WINS *****\n*************************");
    		else
    			System.out.println("\n\n\n************************\n****** HUMAN WINS ******\n************************");
    			
    	}
    
    	public void takeTurn() {
    		int s1 = human.go(), s2 = computer.go();
    		if(s1 == s2)
    			System.out.println("Draw.  Computer " + computer.score + ", human " + human.score + ".");
    		else if(s1 == s2 - 1 || (s1 == Janken.SCISSORS && s2 == Janken.ROCK))
    			System.out.println("Computer scores.  Computer " + (++computer.score) + ", human " + human.score + ".");
    		else if(s1 == s2 + 1 || (s1 == Janken.ROCK && s2 == Janken.SCISSORS))
    			System.out.println("Human scores.  Computer " + computer.score + ", human " + (++human.score) + ".");
    	}
    
    	static void printUsage() {
    		System.out.println("Usage: java Janken [#]\n"
    				+ "\twhere # is the maximum\n"
    				+ "\tnumber of points.  Defaults\n"
    				+ "\tto 2.");
    		System.exit(1);
    	}
    
    	public static void main(String[] args) {
    		int gameLen = 2;
    		if(args.length > 1) printUsage();
    		else if(args.length == 1)
    			try {
    				gameLen = Integer.parseInt(args[0]);
    			} catch(NumberFormatException e) {
    				printUsage();
    			}
    		Janken s = new Janken();
    		s.play(gameLen);
    	}
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Sep 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •