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

New Topic/Question
Reply



MultiQuote





|