hi i'm having trouble with this rock paper scissors program looping correctly. Ok so first of all that game is a simple rock paper scissors of the user against the cpu. The program begins by asking the user what choice he/she would like from a dropdown menu rock. paper or scissors. it then generates and random number for the cpu that selects rock, paper, scissors and compares it to the users choice. it then reports to the user who won, Users wins, the computer wins, draws, and rounds the user has played. now the trick is this the only way the user wins or loses is by having an ADVANTAGE of two games that means if the user wins one and loses the next then wins again the user has not won as the loss nullfies the first win. so it has to be two straight wins. I have all this logic down and this works perfectly but my problem is that i can't get my program to run continously untill a winner/loser is declared as it always asks between games if you would like to play again. I really just want to ask that once there has been a winner! not while it's in the process!! oh and one more thing when i get it to ask the question when a winner/loser has been declared i want to set all the values back to what they were because it's a new game. I simply have it breaking after the user or computer wins to cover this up. heres the code.
import javax.swing.*;
import java.util.Random;//for my random function
public class P1 {
public static void main(String[] args) {
int computerChoice;
String computerPick;
ImageIcon myIcon = new ImageIcon("icon.jpg"); //overloaded constructor;//Rock Paper Scissors Image
JOptionPane.showMessageDialog(
null, "Welcome to my Rock, Paper, Scissors game!\n John Acosta Java CIS 2235\n Chapter 4 pg 153\n press ok to play!",
"Rock Paper Scissors",
JOptionPane.WARNING_MESSAGE, myIcon);
//Asking User to choose Rock, paper or Scissors
int computerwins = 1; //initializing computer wins to 1 so counter can start at 1 for that first game
int humanwins = 1;
int ties = 1;
int compwinsadv = 0;//these variables set to 0 will be working behind the scenes for the two round advantage aspect of the game.
int humanwinsadv = 0;
int tiesadv = 0;
int subtract= -1; //this variable will be used to subtract 1 from my numbers due to the fact that my numbers would be one off
//because i initialized computerwins,humanwins, and ties at 1.
String answer = "Yes";//Initializing answer to "Yes"
// beginning of my do while loop asking if you want to play again after every round.
while(answer=="Yes"){
Object[] possibilities = {"Rock", "Paper", "Scissors"};
String s = (String)JOptionPane.showInputDialog( //dialog box asking to choose, rock, paper, or scissors.
null,
"Choose one!:\n",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"Rock"
);
Random generator = new Random(); //setting generator to random function
computerChoice=generator.nextInt(3);//setting variable computerChoice to generate 0, 1, 2
//switch statment that allows the computer choice to randomly choose either Rock, Paper, 0r Scissors.
switch (computerChoice)
{
case 0:
{
computerPick = "Rock";
break;
}
case 1:
{
computerPick = "Paper";
break;
}
case 2:
{
computerPick = "Scissors";
break;
}
default:
{
computerPick = "won't happen";
}
}
//Beginning of nested If else statements
//if what the user chose equals what the computer chose do this
if (s==computerPick)
{
Icon icon = new ImageIcon("draw.jpg");
String stats = "CPU Games Won: "+ (computerwins+subtract)//subtracting 1 because neither the comp or user won
+ "\nGames You've Won: " + (humanwins+subtract)
+ "\nDraws: "+ (ties++)//only ties gets incremented because it was a tie
+ "\nTotal Rounds: "+ (computerwins+subtract+humanwins+subtract+ties+subtract) //making subtractions so rounds comes out to what it shou
+"\nThe computer also chose "+ s //should be.
+"\nit's a Draw.";
JOptionPane.showMessageDialog(
null,
stats, "Draw!",
JOptionPane.INFORMATION_MESSAGE,
icon
);
//this is the logic for winning by two rounds
tiesadv++; //everytime the statement is true then increment 1 to tiesadv
if(humanwinsadv>0){//and if humanwinsadv is greater than 0 then bring it back down essentially to 0
--humanwinsadv;//because it gets nullified because there was a tie
}
else if(compwinsadv>0){//same goes for compwinsadv
--compwinsadv; //this logic is used through all these nested if else statements
}
}
else if (s=="Rock"&&computerPick=="Scissors"){
Icon winicon = new ImageIcon("Youwin.png");
String stats = "CPU Games Won: "+ (computerwins+subtract)
+ "\nGames You've Won: " + (humanwins++)
+ "\nDraws: "+ (ties+subtract)
+ "\nTotal Rounds: "+ (computerwins+subtract+humanwins+subtract+ties+subtract)
+"\nComputer picked scissors and rock demolishes the crap out of scissors.\n"
+"You Win!";
JOptionPane.showMessageDialog(
null,
stats, "You Win!",
JOptionPane.INFORMATION_MESSAGE,
winicon
);
humanwinsadv++;
if(compwinsadv>0){
--compwinsadv;
}
else if (tiesadv>0){
--tiesadv;
}
}
else if (s=="Rock"&&computerPick=="Paper"){
Icon licon = new ImageIcon("lose.jpg");
String stats = "CPU Games Won: "+ (computerwins++)
+ "\nGames You've Won: " + (humanwins+subtract)
+ "\nDraws: "+ (ties+subtract)
+ "\nTotal Rounds: "+ (computerwins+subtract+humanwins+subtract+ties+subtract)
+"\nBad luck! Computer chose paper\n In real life paper would never beat rock but in this game it does.\n"
+"You Lose!";
JOptionPane.showMessageDialog(
null,
stats, "You Lose!",
JOptionPane.INFORMATION_MESSAGE,
licon
);
compwinsadv++;
if(humanwinsadv>0){
--humanwinsadv;
}
else if (tiesadv>0) {
--tiesadv;
}
}
else if (s=="Paper"&&computerPick=="Scissors"){
Icon loicon = new ImageIcon("lose.jpg");
String stats = "CPU Games Won: "+ (computerwins++)
+ "\nGames You've Won: " + (humanwins+subtract)
+ "\nDraws: "+ (ties+subtract)
+ "\nTotal Rounds: "+ (computerwins+subtract+humanwins+subtract+ties+subtract)
+"\nComputer chose Scissors\n and Scissors dices paper.\n"
+"You Lose!";
JOptionPane.showMessageDialog(
null,
stats, "You Lose!",
JOptionPane.INFORMATION_MESSAGE,
loicon
);
compwinsadv++;
if(humanwinsadv>0){
--humanwinsadv;
}
else if(tiesadv>0){
--tiesadv;
}
}
else if (s=="Paper"&&computerPick=="Rock"){
Icon wicon = new ImageIcon("Youwin.png");
String stats = "CPU Games Won: "+(computerwins+subtract)
+ "\nGames You've Won: " + (humanwins++)
+ "\nDraws: "+ (ties+subtract)
+ "\nTotal Rounds: "+ (computerwins+subtract+humanwins+subtract+ties+subtract)
+"\nThe computer chose rock\n You lucky bastard, paper would never beat rock in reality but whatever.\n"
+"You Win!";
JOptionPane.showMessageDialog(
null,
stats, "You Win!",
JOptionPane.INFORMATION_MESSAGE,
wicon
);
humanwinsadv++;
if(compwinsadv>0){
--compwinsadv;
}
else if(tiesadv>0){
--tiesadv;
}
}
else if (s=="Scissors"&&computerPick=="Paper"){
Icon wiicon = new ImageIcon("Youwin.png");
String stats = "CPU Games Won: "+ (computerwins+subtract)
+ "\nGames You've Won: " + (humanwins++)
+ "\nDraws: "+ (ties+subtract)
+ "\nTotal Rounds: "+ (computerwins+subtract+humanwins+subtract+ties+subtract)
+"\nThe computer chose paper and Scissor dices paper.\n"
+"You Win!";
JOptionPane.showMessageDialog(
null,
stats, "You Win!",
JOptionPane.INFORMATION_MESSAGE,
wiicon);
humanwinsadv++;
if(compwinsadv>0){
--compwinsadv;
}
else if (tiesadv>0){
--tiesadv;
}
}
else if (s=="Scissors"&&computerPick=="Rock"){
Icon rockicon = new ImageIcon("lose.jpg");
String stats = "CPU Games Won: "+ (computerwins++)
+ "\nGames You've Won: " + (humanwins+subtract)
+ "\nDraws: "+ (ties+subtract)
+ "\nTotal Rounds: "+ (computerwins+subtract+humanwins+subtract+ties+subtract)
+"\nComputer picked rock and rock demolishes the crap out of scissors.\n"
+"You Lose";
JOptionPane.showMessageDialog(
null,
stats, "You Lose!",
JOptionPane.INFORMATION_MESSAGE,
rockicon
);
compwinsadv++;
if(humanwinsadv>0) {
--humanwinsadv;
}
else if(tiesadv>0) {
--tiesadv;
}
}
//this is where the logic from the compwinsadv, tiesadv, and humanwinsadv variables meet up.
if (compwinsadv>=2){//if compwinsadv is greater or equal to 2 then you have lost the game because you've lost by two rounds
Icon gameovericon = new ImageIcon("gameover.png");
String stats = "Sorry you lost by a two game advantage!\n The game is over and the loop is going to end\n"
+"Below Are The Results\n"
+"CPU Games Won: "+ (computerwins+subtract)
+ "\nGames You Won: " + (humanwins+subtract)
+ "\nDraws: "+ (ties+subtract)
+ "\nTotal Rounds: "+ (computerwins+subtract+humanwins+subtract+ties+subtract);
JOptionPane.showMessageDialog(
null,
stats, "You Lost Game Over!",
JOptionPane.INFORMATION_MESSAGE,
gameovericon
);
break; //break code if this happens since the loop has to end
}
//same logic for this one except this is in case if you win.
else if(humanwinsadv>=2){
Icon winnericon = new ImageIcon("winner.jpg");
String stats = "Congrats You won by a two game advantage!\n The game is over and the loop is going to end\n"
+"Below Are The Results\n"
+"CPU Games Won: "+ (computerwins+subtract)
+ "\nGames You Won: " + (humanwins+subtract)
+ "\nDraws: "+ (ties+subtract)
+ "\nTotal Rounds: "+ (computerwins+subtract+humanwins+subtract+ties+subtract);
JOptionPane.showMessageDialog(
null,
stats, "You Win!",
JOptionPane.INFORMATION_MESSAGE,
winnericon
);
break;
}
//dialog box asking if you want to keep playing yes or no
Object[] results = {"Yes", "No"};
answer = (String)JOptionPane.showInputDialog(
null,
"Would You Like To Play Again?\n",
"Yes or No?",
JOptionPane.PLAIN_MESSAGE,
null,
results,
"Yes");
//while the answer is yes the loop will continue to run unless you win by two rounds
}
//if user chooses no then give them a goodbye message with their stats.
if(answer=="No"){
//custom title, custom icon
JOptionPane.showMessageDialog(
null,
"Goodbye press ok to exit!\n"
+"\nBelow Are your stats\n"
+"\n/////CPU Games Won: "+ (computerwins+subtract)
+"////"
+ "\n////Games You Won: " + (humanwins+subtract)
+"/////"
+ "\n////////Draws: "+ (ties+subtract)
+"///////////////////"
+ "\n////Total Rounds: "+ (computerwins+subtract+humanwins+subtract+ties+subtract)
+"//////////"
);
}
}
}