aspose file tools*
The moose likes Beginning Java and the fly likes Rock paper scissors project Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rock paper scissors project" Watch "Rock paper scissors project" New topic
Author

Rock paper scissors project

Katie Johns
Greenhorn

Joined: Nov 28, 2011
Posts: 6
Hi everyone,

I'm writing a Rock, Paper, Scissors program in Java. I am in my first Java class and I am still new at this
So far I have it so that the user enters a number that corresponds with their choice. I am supposed to error check the user input to make
sure it is either Rock, Paper or Scissors, and if not, they will be prompted to enter it again. I'm supposed to use a do/while loop to allow the
user to continue playing games until ready to quit, and what's scaring me the most, is I have to use a multi-dimensional array to store the user's choices and the computer's choices for each round, and output the game details for each round.

Can anyone PLEASE help me? I feel like this is overwhelming and I'm so lost.

Thanks,
Katie
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66



Okay... which parts do you understand and not understand?
1) get user entry from console
2) compare entry to valid possibilities
3) do/while loop
4) store info in array
5) output details for each round


If you break it down into its component pieces of logic, it's not that daunting. At least you can identify the parts you understand and which you need to work on.

Keith Rainey
OCPJP6
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Sure we can help. What specifically are you having trouble with? Are you getting errors or exceptions?


Steve
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Katie Johns wrote:So far I have it so that the user enters a number that corresponds with their choice.

There are a number of ways to ask a user for input and the next step will depend on how you've done it.
So show us what you have so far and we can then make some suggestions for how to continue.


Joanne
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Katie's a popular girl tonight
Katie Johns
Greenhorn

Joined: Nov 28, 2011
Posts: 6
Joanne Neal wrote:There are a number of ways to ask a user for input and the next step will depend on how you've done it.
So show us what you have so far and we can then make some suggestions for how to continue.


I use a JOptionPane input to get the user's input for Rock (0) Paper (1) or Scissors (2).

JOptionPane.showMessageDialog(null,"0:Rock\n1:Paper\n2:Scissors");
yourInput = JOptionPane.showInputDialog("Now tell me your choice!");
yourChoice = Integer.parseInt(yourInput);

Then for the computer choice I use
computerChoice = (int)((Math.random()*1000)+1);
computerChoice %= 3;

I guess my first question is instead of asking the using to input a number, I want to use the actual string of Rock, Paper, or Scissors.

Currently, I have an if/else statement that compares the users input of 0,1, or 2 with the random number of the computer to determine the winner.
I have been trying to fool around with making yourInput a string but so far I get errors that you can't have int data type and string to compare them.

Thanks!
Katie Johns
Greenhorn

Joined: Nov 28, 2011
Posts: 6
Keith Rainey wrote:

Okay... which parts do you understand and not understand?
1) get user entry from console
2) compare entry to valid possibilities
3) do/while loop
4) store info in array
5) output details for each round


If you break it down into its component pieces of logic, it's not that daunting. At least you can identify the parts you understand and which you need to work on.


I am new to arrays. From what I understand, an array has a set length defined on creation. My array has 100 elements(?). How would I store the input from the user and computer in an array and call that to output the details/
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

Katie Johns wrote:Currently, I have an if/else statement that compares the users input of 0,1, or 2 with the random number of the computer to determine the winner. I have been trying to fool around with making yourInput a string but so far I get errors that you can't have int data type and string to compare them.


You could use an int variable and load a value to is based on the string input. for instance: int userSelection; if (userInputStringValue=="rock" ) userSelection =0;

then compare the 'converted' input to your computer generated number...
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

Katie Johns wrote:I am new to arrays. From what I understand, an array has a set length defined on creation. My array has 100 elements(?). How would I store the input from the user and computer in an array and call that to output the details/


Yes, array length is determined when it is initialized. int[] myArray= new int[100]; will create an array of 100 ints. The size cannot be changed.

For your purpose, if you want to store both the human and computer values, you would need use a two dimensional array int[100][2] myArray; for example

Do you understand how to access the elements of arrays to set and retrieve values?
Katie Johns
Greenhorn

Joined: Nov 28, 2011
Posts: 6
Keith Rainey wrote:Do you understand how to access the elements of arrays to set and retrieve values?


Not entirely
I have used single arrays that are really small, everything was already set and I just retrieve the values from the position in the array. The two dimensional array is confusing to me. Will the user's input be stored into the array as the program runs?
Like as the user keeps playing after every game will it store the value in the array?
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

I have used single arrays that are really small, everything was already set and I just retrieve the values from the position in the array. The two dimensional array is confusing to me. Will the user's input be stored into the array as the program runs?
Like as the user keeps playing after every game will it store the value in the array?


Arrays are populated during the program's execution. As they are stored in memory, their content is lost when the program ends.

Both the user's input and the computer's random selection would be stored together if you are going to compare them later for your results.

A two dimensional array is an "array of arrays" -- that is, each element of the first dimension (100 elements in this example) contains another array of 2 elements. Consider it 100 slots, each slot is a container which holds two items: user selection and computer selection. They are stored together because you will want to compare each set later for your results.

Take a look at this tutorial on arrays ( multi-dimensional arrays are discussed down the page): Arrays Tutorial

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
And welcome to the Ranch
Katie Johns
Greenhorn

Joined: Nov 28, 2011
Posts: 6
I've got everything working with two one dimensional arrays. I can't figure out how to get a multidimensional array to work for printing out the results from the games the user played. :/
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

Katie Johns wrote:I can't figure out how to get a multidimensional array to work for printing out the results from the games the user played. :/

Someone (campbell...cough...cough...cough) is bound to point out at any moment that java does not have multi-dimensional arrays. It only has single-dimensional arrays. However, a single dimensional array can hold...arrays. They act similarly to mutli-dimensional ones, except that you can have a 'ragged array' where the sub-arrays can be of different lengths from each other.

Is your problem with setting up the array, with writing to it, or with reading from it?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Katie Johns
Greenhorn

Joined: Nov 28, 2011
Posts: 6
fred rosenberger wrote:Is your problem with setting up the array, with writing to it, or with reading from it?


Reading from it I guess. I currently have two arrays that have 100 empty elements, where the results of the winner are stored, whether it be the computer or the player. Then I have another array that has 100 empty elements, and at the end of the program, it prints what is stored in the results array. I am thinking there is a way I can store both the user input and the computer input in what I thought was a multidimensional array.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
I've taken the liberty of removing some of the quoted text, as it was becoming difficult to tell the new content from the quoted content.

Keith Rainey wrote:... if (userInputStringValue=="rock" ) ...

Small nitpick: You should never use "==" to compare strings; instead, use the equals method:

... if (userInputStringValue.equals("rock")) ...
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

you could also use a one dimensional array of Points with the x value being the user choice and the y value the computer choice. just a thought.


SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
fred rosenberger wrote: . . . Someone (campbell...cough...cough...cough) is bound to point out at any moment . . .
Sorry, I was really busy yesterday, so I didn’t have a chance to read these posts. Fred pointed it out better than I would have
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Katie Johns wrote:...what's scaring me the most, is I have to use a multi-dimensional array to store the user's choices and the computer's choices for each round...

Firstly: don't be scared; and don't be afraid to make mistakes.

All of us were where you are once, and most of us contributors got where we are by making thousands of them (I still make 'em, even after 35 years).
You'll learn more by trying something and getting it wrong than almost anything else.

However, a few tips for you:

1. Java arrays (and many other "indexable" things), are indexed from ZERO (0).
It's probably the hardest thing for beginners to remember, because we're so used to counting from 1, but if I create an array of something, say:
   int[] ints = new int[10];
the first element is ints[0] and the last element is ints[9].

2. All Java arrays have a length field that you can query. Therefore, from the above example:
System.out.println( ints.length );
will print out 10 (length itself is an int). This is very useful when you want to write loops for going through arrays.

3. Fred and Campbell are quite right in what they say about multi-dimensional arrays, but when it comes to creating and indexing them you can treat them just as though they were a matrix, so:
int[][] tenByTen = new int[10][10];
will create a new 10x10 matrix of ints; and:
tenByTen[0][7]
will return the value in the 8th column (indexing from 0 remember) of the first row.

It's only when you want to return portions of a matrix that you need to remember how Java makes them up. For example:
tenByTen[3]
returns you the 4th row in the matrix, and its type is int[]. Therefore:
tenByTen[5].length
will return you the length of the 6th row.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

I would add one other thing that almost ALWAYS confuses people - beginners and old-timers alike.

When you create an array of PRIMITIVES (ints, floats, etc), the array is automatically populated (assuming you don't stick anything in there) with "0".

However, if you create an array of OBJECTS, the array really is an array of REFERENCE TO objects...which are set to 'null'. If you read the forums enough, you'll see folks do this:



And then ask why they get a NullPointerException. the reason is that no actual MyClass objects have been created - you have created what I think of as an empty egg carton that can hold up to 10 eggs, but each spot is empty until you buy the egg.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
fred rosenberger wrote:... you have created what I think of as an empty egg carton that can hold up to 10 eggs, but each spot is empty until you buy the egg.


This is a great analogy! If I ever teach again I'm bringing in an empty egg carton to introduce arrays!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rock paper scissors project