7 Replies - 1178 Views - Last Post: 18 October 2012 - 01:52 PM Rate Topic: -----

#1 Yao4958  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 12

How do I make a counter for a rock paper scissors game?

Posted 18 October 2012 - 12:26 PM

I am making a rock paper scissors game in java for class. I've got the majority of the program done. All I need to do is have the program count how many times the user and the computer have won. It has to be displayed after the user wants to stop playing, not after every round. Can someone help me to do this? I don't know where to start.

   import javax.swing.*;
   import java.util.*;
   public class Minder06
	// Rock, Paper, Scissors
   {
      public static void main(String[] args)
      {
         String[] ddList = {"Rock", "Paper", "Scissors"}; // contents of your drop down list
         Object selectedValue;  //stores which selection the user chose
         int code;
         int seed;
         String printMessage, str;
			char oppMove, yourMove, y;
			int win, lose, tie;
			int userCount, compCount;
			
         Random rnd = new Random();
   
	   	boolean cont = true;
			
			do
			{ //start loop
						
         selectedValue = JOptionPane.showInputDialog(
                    null,
                    "Make Your Move",  // your instructions on what to do
                    "Rock Paper Scissors",                 // the title of the input dialog box
            		  JOptionPane.QUESTION_MESSAGE,  // the icon of the input dialog box
            		  null,                   // must have
            		  ddList,                 //  this is the String array above that lists the contents of your drop down list
                    ddList[0]);             //  which item in the drop down list will be displayed as default
         
			seed = rnd.nextInt(3);  // get their move
			if (seed == 0)
				oppMove = 'r';
			else if (seed == 1)
				oppMove = 'p';
			else
				oppMove = 's';
				
			if (ddList[0].equals(selectedValue))  // get your move
            yourMove = 'r';
         else if (ddList[1].equals(selectedValue))
				yourMove = 'p';
			else
       		yourMove = 's';
      	      	
               	
      	//score
        
		  	if (yourMove == 'r')  // you chose rock
				if (oppMove == 'r')
					printMessage = "You both chose rock.  \n\nTie game.";
				else if (oppMove == 'p')
					printMessage = "You chose rock.  \nYour opponent chose paper.  \n\nYou LOSE!"; 
					
				else
					printMessage = "You chose rock.  \nYour opponent chose scissors.  \n\nYou WIN!";
				
				
				else if (yourMove == 'p')  // you chose paper
				if (oppMove == 'r')
					printMessage = "You chose paper.  \nYour opponent chose rock.  \n\nYou WIN!";
					
				else if (oppMove == 'p')
					printMessage = "You both chose paper.  \n\nTie game.";
				else
					printMessage = "You chose paper.  \nYour opponent chose scissors.  \n\nYou LOSE!";
					

				else  // you chose scissors
				if (oppMove == 'r')							
					printMessage = "You chose scissors.  \nYour opponent chose rock.  \n\nYou LOSE!";
				
				else if (oppMove == 'p')
					printMessage = "You chose scissors.  \nYour opponent chose paper.  \n\nYou WIN!";
					
				else
					printMessage = "You both chose scissors. \n\nTie game.";											
					
  
         JOptionPane.showMessageDialog(null, printMessage, "Results", 
            JOptionPane.INFORMATION_MESSAGE);
         
			str = JOptionPane.showInputDialog("Do you want to play again? y/n"); //asks user if they want to play again
			
			if (str.charAt(0) == 'y' || str.charAt(0) == 'y')
				cont = true;
				
			else
				cont = false;	
				
  	
			}
			while (cont);
			 //end loop
	
			
	
			  System.exit(0);
      
      }
   }



Is This A Good Question/Topic? 0
  • +

Replies To: How do I make a counter for a rock paper scissors game?

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: How do I make a counter for a rock paper scissors game?

Posted 18 October 2012 - 12:33 PM

I normally stay out of the java area but this post just begs the question:

You can code the entire game, but you have no idea where to start to add a couple ints to count the number of times the human and computer win?

That really makes me think you didn't write the code you provided. It makes no sense that you grasp arrays, bools, ints, loops and everything else that is in this application but you can't add a couple more ints to the existing ones for these counters. I suspect you found this code someplace on the net and turned it in to your teacher as your own work; and now you're stuck because you have to add on to it but don't understand the code you plagiarized.
Was This Post Helpful? 1
  • +
  • -

#3 Yao4958  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 12

Re: How do I make a counter for a rock paper scissors game?

Posted 18 October 2012 - 12:37 PM

No, she sent everyone in the class this program. The assignment is to add a loop for the game and to add a counter to it. We haven't went over how to make a counter in class and I couldn't find it in our book.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: How do I make a counter for a rock paper scissors game?

Posted 18 October 2012 - 12:55 PM

Assumption: The concepts of doing this were discussed or this assignment wouldn't have been assigned. I doubt the curriculum includes deliberately setting students up for fairly through lack of covering the needed material.

You may not have been told step-by-step and your hand held through the exact typing of the code; but I'm sure the idea of how to assign a value to an int was covered. Does int x = 5; or x = x + 1; look at all familiar?

If not, then this assignment is doing exactly what it was intended to do: Weed out those that need some additional face-time with the teacher. Don't let your ego get in the way of your education. Go get some face-time with the teach and admit you are lost. 10 minutes will probably clear it right up for you.
Was This Post Helpful? 0
  • +
  • -

#5 Yao4958  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 12

Re: How do I make a counter for a rock paper scissors game?

Posted 18 October 2012 - 01:22 PM

I remember seeing the x = x + 1 but not the int x = 5
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: How do I make a counter for a rock paper scissors game?

Posted 18 October 2012 - 01:31 PM

Then

1) Go back and re-read how to assign a value to a variable.

2) Think. Is it possible that what you do say you know could be applied here? Hmmm... x=x+1... and you want to increase a counter when the human wins another game...

This post has been edited by tlhIn`toq: 18 October 2012 - 01:50 PM

Was This Post Helpful? 0
  • +
  • -

#7 Yao4958  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 12

Re: How do I make a counter for a rock paper scissors game?

Posted 18 October 2012 - 01:45 PM

So would I make the variables int userWin, compWin? And then as either one wins I would put in userWin = userWin + 1?
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: How do I make a counter for a rock paper scissors game?

Posted 18 October 2012 - 01:52 PM

Sounds like a good plan.

Of course you look at line 15 of the code your teacher gave you, I think you'll see they planned that out for you. Seems most of what you need to do has already been given to you.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1