5 Replies - 3942 Views - Last Post: 02 August 2009 - 03:31 PM Rate Topic: -----

#1 AUAN  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 30-July 09

Rock/Paper/Scissors program

Posted 02 August 2009 - 05:49 AM

Hey all.

I am writing a Rock/Paper/Scissors program in which a user versus a CPU opponent.

I have coded the core of the application, and I am trying to do now is when the 10 rounds of the game have finished that it displays the overall results
eg: CPU:2 User:4


/** A program that plays Rock-Paper-Scissors with the user. */
public class RPS{

	
   String cpuattack;
   String userinput;
   int wins2;
   int wins1;
   int cpuwins;
   int userwins;


   
	/** Play 10 rounds of RPS */
	public void playRPS(){
	int count = 0;
	while (count < 10) {
		System.out.println("------------------------------------------");
		this.playRound();
		count = count + 1;
	}
	System.out.println("------------------------------------------");
	System.out.println("Game Over");
	}

	
	/** Play one round of RPS and print out the choices and result */
	public void playRound()
	{
			  
		// Decide the Computer's choice
	 
		double random = (10.0 * Math.random()) + 1;

		if (random < 3.333)
		{	
			 cpuattack = "Paper";
		}
		else if (random >= 3.333 && random <= 6.667)
		{
			cpuattack = "Rock";
		}
		else if (random > 6.667)
		{
			cpuattack = "Scissors";  
		}
		
		// Record the User's Attack

		Scanner scan = new Scanner(System.in);
		System.out.println();
		System.out.print("Attack with Rock, Paper, or Scissors: ");
		userinput = scan.next(); 
		System.out.println();
		
		
		
		// Print the Round
		
		System.out.println("------------------------------------------");
		System.out.println();
		System.out.println("The CPU attacked with: " +cpuattack+ "!");
		System.out.println();
		System.out.println("You attacked with: " + userinput + "!");
		System.out.println();
		
		// Print the Round Score
		
		if(cpuattack.equals(userinput))
		{
			System.out.println("Round Draw!");
		}
		else if(cpuattack.equals("Paper") && userinput.equals("Rock"))
		{
			System.out.println("CPU Wins!");
			wins1 = wins1 + 1;
		}
		else if(cpuattack.equals("Paper") && userinput.equals("Scissors"))
		{
			System.out.println("User Wins!");
			wins2 = wins2 + 1;
		}
		else if(cpuattack.equals("Rock") && userinput.equals("Paper"))
		{
			System.out.println("User Wins!");
			wins2 = wins2 + 1;
		}
		else if(cpuattack.equals("Rock") && userinput.equals("Scissors"))
		{
			System.out.println("CPU Wins!");
			wins1 = wins1 + 1;
		}
		else if(cpuattack.equals("Scissors") && userinput.equals("Rock"))
		{
			System.out.println("User Wins!");
			wins2 = wins2 + 1;
		}
		else if(cpuattack.equals("Scissors") && userinput.equals("Paper"))
		{
			System.out.println("CPU Wins!");
			wins1 = wins1 + 1;
		}   
	   
		// Print Game Score
		
		cpuwins = wins1;
		userwins = wins2;
		
	   return cpuwins;
	   return userwins;
	   
	   System.out.println("CPU: " + cpuwins + " ");
	   System.out.println("User: " + userwins + " ");
	   
	   
	}
}




I have added in "return" to call the final value for win1 and win2, but I have got'n abit confused with how to actually display it all at the end of the playRound method as you can tell with me adding cpuwins = wins1; and
userwins = wins2;.

Also instead of using " wins1 = wins1 + 1;" is it possible to do "wins1++"?

Any help appreciated.

Cheers.

This post has been edited by AUAN: 02 August 2009 - 06:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Rock/Paper/Scissors program

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Rock/Paper/Scissors program

Posted 02 August 2009 - 06:23 AM

The two return statements should be removed. The method is not expected to return anything.
Why save the wins in wins1 and wins2 respectively when you could just save them in userwins and cpuwins from the start?

And yes, you can use wins1++; instead of wins1=wins1+1;

This post has been edited by Gloin: 02 August 2009 - 06:24 AM

Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Rock/Paper/Scissors program

Posted 02 August 2009 - 10:35 AM

Correct.

You can just have something like this...

if ( /* computer wins */ )
{
    System.out.println( /* message  */ );
    cpuwins++;
}

// likewise for the other case.


Also, like Gloin said, remove those 2 return statements. They aren't supposed to be there.
Was This Post Helpful? 0
  • +
  • -

#4 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: Rock/Paper/Scissors program

Posted 02 August 2009 - 10:59 AM

Just a quick question: How do you control users input? What i mean is: user can just type in for example paper with small "P", the equals will not ignore capital meaning they will be different.

I would suggest either using equalsIgnoreCase or turn everything into uppercase or lowercase :)
Was This Post Helpful? 0
  • +
  • -

#5 AUAN  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 30-July 09

Re: Rock/Paper/Scissors program

Posted 02 August 2009 - 12:53 PM

Thank you all for the help. I found that the print overall score part was going to work better within the playRPS method anyway.

View PostAtspulgs, on 2 Aug, 2009 - 09:59 AM, said:

Just a quick question: How do you control users input? What i mean is: user can just type in for example paper with small "P", the equals will not ignore capital meaning they will be different.

I would suggest either using equalsIgnoreCase or turn everything into uppercase or lowercase :)


I don't to be honest.I found this: http://www.java-samp...?tutorialid=224 which looks useful.
I will look into it today. Cheers.

This post has been edited by AUAN: 02 August 2009 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#6 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Rock/Paper/Scissors program

Posted 02 August 2009 - 03:31 PM

I must still say that considering she can't be more than like 3 years old, she's quite decent at Java. And people, she started when she was like 1 years old.

This post has been edited by Gloin: 02 August 2009 - 03:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1