• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RockPaperScissor

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
I am working on a java program RockPaperScissors
now I have a lot done but the one thing I can not figure out is how to add a quit option for the user to enter nor do I have the ability to display the result.
I just want to get this done please help, I dont want someone to write it for me just a proofread
these are the design requirements

The user/player will need to be presented a way to enter his/her item choice. Utilizing a simple JOptionPane GUI interface is required.
The user/player’s choice will need to be validated for entry errors. A separate user choice validation method that receives the user’s input selection as a parameter and determines if the user entry is a valid entry is needed. This method will return true if entry is valid and false if not valid. An invalid entry message and an opportunity to re-enter the choice should be repeatedly provided in the method that provides for user choice entry if a false is returned from the separate validation method.
If the user/player did not select “Quit”, then
The computer’s choice will need to be created by generating a random number between 1 and 3 and then associating the number with the game choice in string form. If the number is 1, then the computer has chosen rock, if 2, then paper, and
Page 1 of 3
if 3, then scissors. Don’t display the computer’s choice immediately after
creating it.
o Next the game action must determine who won the round based upon the rules of
the game (see table above). When the round winner is selected, output a brief message as to who won and the game choices that were made. In addition, increment the appropriate score: user or computer.
 When a “Quit” choice is selected by the user/player, then exit the game loop and output the final game results.
and this is my code
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Welcome to the ranch.

> the one thing I can not figure out is how to add a quit option for the user to enter nor do I have the ability to display the result.

Right now your program runs once and then stops.
So you have to change it so that after someone wins/loses it then plays the game again, and again, and again... until you say quit. You would do that with a loop.

Adding a quit option for the user should be done in the userChoice() method. (The place you take the user's input)
"Quit" should be a valid option as well as the existing three.

As each game is played, you should keep track of how many games have been won by the computer, and how many by the player (some more variables for you to keep track of)

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:
As each game is played, you should keep track of how many games have been won by the computer, and how many by the player (some more variables for you to keep track of)


He certainly could but not necessarily should; that's just a nice touch.

Another thing OP could do is to simplify the logic used to see who won. If you assign 0 to Rock, 1 to Paper, and 2 to Scissors, then you can calculate who won and reduce the number of if-else statements.

The expression (3 + play1 - play2) % 3 gives 0 if tied, 1 if play1 wins and 2 if play2 wins.

For example:

It works out nicely if you define an enum for the plays and use the built-in enum ordinal() method of each enum value.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

zap zodiberg wrote:The user/player will need to be presented a way to enter his/her item choice. Utilizing a simple JOptionPane GUI interface is required...


The only thing I can add to the excellent advice you've been given is that you should separate your game logic from the display/Input-output. Completely.

Right now, your program is a mish-mash of display (80%) and game (20%) logic; and one is distracting you from the other.

What's the most important aspect here? The game, or the display? Personally, I'd say the first, because without the game, there wouldn't be any display to worry about.

The trouble is that display - or input-output - logic is (and probably always will be) fiddly, verbose, and not very reusable; so if you start by writing that, you're likely to distract yourself from your main purpose, which is to write a program that plays the game.

It may be a bit late for this assignment; but next time you get one like it, concentrate on the game first, and leave the display logic until the end. And one of the markers of a good game class (or, as has already been suggested, enum) is that it shouldn't contain ANY classes starting with 'J' (JPanel, JFrame, JOptionPane, etc).

HIH

Winston
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm... someone's given a thumb up, but I'm not sure. The advice boils down to:

Beginner, don't be a Beginner!

And I doubt the educational validity of that.

Greetz,
Piet
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:
Beginner, don't be a Beginner!

And I doubt the educational validity of that.


Maybe we should leave that to the OP to judge.

I think it's good advice for beginners to defer thinking about UI concerns and concentrate on the main logic.

One way to do that is to expect the player's move to be provided on the command line, e.g. you would run the program as follows:

$ java RockPaperScissors rock
you: rock
computer: scissors
you won!

$ java RockPaperScissors paper
you: paper
computer: rock
you won!

$ java RockPaperScissors rock
you: rock
computer: paper
computer won!


BTW, not that I don't agree with the advice because I do, but in case you're wondering, no, that's not my thumb there
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Hmm... someone's given a thumb up, but I'm not sure. The advice boils down to:
Beginner, don't be a Beginner!


Well isn't that what most of our advice in this forum boils down to?

And I doubt the educational validity of that.


And in isolation I'd agree; but I believe I did also try to explain why the approach is flawed.

Perhaps not well enough...

Winston
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thumbs from me, this time.

Well, I guess we do our best and hope the best of it...

I don't know my limits. I'm doing this 'Reactive Programming Principles'
course at Coursera at the moment, and I know of one limit now, and that hurts.
Yesterday and this morning I had a huge struggle to get assignment 2 up
and running. Now, fortunately they at Coursera do not use a StyleChecker,
but if anyone would have had the nerves to start talking about structures
and separating logic from in and output

So, gents: go on, I'll disagree anyway

Greetz,
Piet
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Stefan Evans wrote:
As each game is played, you should keep track of how many games have been won by the computer, and how many by the player (some more variables for you to keep track of)


He certainly could but not necessarily should; that's just a nice touch.



Actually I read it as one of the requirements.

When the round winner is selected, output a brief message as to who won and the game choices that were made. In addition, increment the appropriate score: user or computer.

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:
Actually I read it as one of the requirements.

When the round winner is selected, output a brief message as to who won and the game choices that were made. In addition, increment the appropriate score: user or computer.


Ah, sorry, my mistake. You were correct, OP should do what you said.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic