Java Rock Paper Scissors Game

Want to see if there are any improvements I should make

Page 1 of 1

4 Replies - 12747 Views - Last Post: 10 March 2010 - 08:53 PM Rate Topic: -----

#1 secretformula  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 10-December 09

Java Rock Paper Scissors Game

Posted 10 March 2010 - 02:53 PM

For my AP Computer science class we have to make a rock paper scissors game. I was wondering if there were any improvements anybody thinks I should make. Thanks in advance.
import java.util.Scanner;
public class labSection14 
{
	public static void main(String[] args)
	{
		String userInput;
		int userChoice = -1, cpuChoice, timesWon = 0, timesLost = 0, timesTied = 0;
		boolean debug = false;
		// debug = true;
		
		Scanner kb = new Scanner(System.in);
		while( userChoice >= -1)
		{
			cpuChoice = (int)(Math.random() * 3);
			
			System.out.print("Enter your choice (Rock, Paper, or Scissors): ");
			userInput = kb.next();
			
			// Start Choice Assignments
			if( userInput.equalsIgnoreCase("rock") || userInput.equalsIgnoreCase("r") )
				userChoice = 0;
			else if( userInput.equalsIgnoreCase("paper") || userInput.equalsIgnoreCase("p") )
				userChoice = 1;
			else if( userInput.equalsIgnoreCase("scissors") || userInput.equalsIgnoreCase("s") )
				userChoice = 2;
			else if( userInput.equalsIgnoreCase("debug") && debug == true)
				userChoice = 3;
			else
			{
				System.out.println("Invalid choice"); 
				break;
			}
			
			// Start Choice Winner Determination
			if( userChoice == 3) // Backdoor :)/>
				System.out.println("You won! You are: "+ ++timesWon +":"+ timesLost +":"+ timesTied);
			else if( userChoice == 0 && cpuChoice == 0) // Rock v Rock
				System.out.println("You tied! You are: "+ ++timesWon +":"+ timesLost +":"+ ++timesTied);
			else if( userChoice == 0 && cpuChoice == 1) // Rock v Paper
				System.out.println("You lost! You are: "+ ++timesWon +":"+ ++timesLost +":"+ timesTied);
			else if( userChoice == 0 && cpuChoice == 2) // Rock v Scissors
				System.out.println("You won! You are: "+ ++timesWon +":"+ timesLost +":"+ timesTied);
			else if( userChoice == 1 && cpuChoice == 0) // Paper v Rock
				System.out.println("You won! You are: "+ ++timesWon +":"+ timesLost +":"+ timesTied);
			else if( userChoice == 1 && cpuChoice == 1) // Paper v Paper
				System.out.println("You tied! You are: "+ timesWon +":"+ timesLost +":"+ ++timesTied);
			else if( userChoice == 1 && cpuChoice == 2) // Paper v Scissors
				System.out.println("You lost! You are: "+ timesWon +":"+ ++timesLost +":"+ timesTied);
			else if( userChoice == 2 && cpuChoice == 0) // Scissors v Rock
				System.out.println("You lost! You are: "+ timesWon +":"+ ++timesLost +":"+ timesTied);
			else if( userChoice == 2 && cpuChoice == 1) // Scissors v Paper
				System.out.println("You won! You are: "+ ++timesWon +":"+ timesLost +":"+ timesTied);
			else if( userChoice == 2 && cpuChoice == 2) // Scissors v Scissors
				System.out.println("You tied! You are: "+ timesWon +":"+ timesLost +":"+ ++timesTied);
			else // Internal Error
			{
				System.err.println("Internal Error"); 
				break;
			}
			
			if(debug == true)
			{
				System.out.println("RAW INFO::userChoice=="+ userChoice +"::cpuChoice==="+ cpuChoice);
			}
			
			System.out.print("Play again(y/n)? ");
			userInput = kb.next();
			
			if( userInput.equalsIgnoreCase("y") || userInput.equalsIgnoreCase("yes") )
			{
				continue;
			}
			else
			{
				break;
			}
		}
		kb.close();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Rock Paper Scissors Game

#2 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2632
  • View blog
  • Posts: 15,652
  • Joined: 24-September 08

Re: Java Rock Paper Scissors Game

Posted 10 March 2010 - 03:07 PM

Well, does it work? As far as cleaning it up, with what I've seen so far, I don't think you can do much at that level to clean it up. No offense, I'm just saying. Most professors don't like when the student codes GUIs while the rest of the class is only on Hello, world! (that was a metaphor). They think you're cheating. So, does it have any problems or what?
Was This Post Helpful? 1
  • +
  • -

#3 secretformula  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 10-December 09

Re: Java Rock Paper Scissors Game

Posted 10 March 2010 - 03:14 PM

No it doesn't have any problems lol. I guess you are right was just trying to see if there was something new I could learn, but you are right.
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2632
  • View blog
  • Posts: 15,652
  • Joined: 24-September 08

Re: Java Rock Paper Scissors Game

Posted 10 March 2010 - 03:23 PM

Alrighty, looks fine to me for now ^___^
Was This Post Helpful? 0
  • +
  • -

#5 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Java Rock Paper Scissors Game

Posted 10 March 2010 - 08:53 PM

View PostNeoTifa, on 10 March 2010 - 01:07 PM, said:

Well, does it work? As far as cleaning it up, with what I've seen so far, I don't think you can do much at that level to clean it up. No offense, I'm just saying. Most professors don't like when the student codes GUIs while the rest of the class is only on Hello, world! (that was a metaphor). They think you're cheating. So, does it have any problems or what?

I guess it's good that my teacher knows that I'm ahead. His mentality: as long as it does what I want it to do, you can add stuff to it. Though it's true, most teachers generally don't like it when you start doing tons of stuff that's out of class in class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1