• 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

Rock-Paper-Scissors Game (Critique Wanted)

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to Java and so far I have read the first 5 chapters of Java Software Solution (Edition 7).
Today I have been working on one of the Chapter 5 question and would love some feedback on my code.

Is it readable, with enough comments?
Is there any way I could make it better with the tools I know right now.

Question:
Design and implement an application that plays the Rock-Paper-
Scissors game against the computer. When played between two
people, each person picks one of three options (usually shown by
a hand gesture) at the same time, and a winner is determined. In
the game, Rock beats Scissors, Scissors beats Paper, and Paper
beats Rock. The program should randomly choose one of the
three options (without revealing it), then prompt for the user’s
selection. At that point, the program reveals both choices and
prints a statement indicating if the user won, the computer won,
or if it was a tie. Continue playing until the user chooses to stop,
then print the number of user wins, losses, and ties.

Answer:
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works, and that's a big deal. It catches bad input and handles it, that's a big deal. It's formatted and commented, that's a big deal.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the next step is to get your code out of main().

MainIsAPain
 
Julie Anne Moore
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:So the next step is to get your code out of main().

MainIsAPain



Thanks Knute Snortum.

Right now I am following the textbook that will be used in my first year java course this fall, and they use the main method.
But is the following code better with less code in the main method.
 
Julie Anne Moore
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this better for reducing my code in the main method?

 
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a start. I'd create a class "ComputerPlayer" and a class "HumanPlayer". The ComputerPlayer class would make the computer's selection, and the HumanPlayer would take care of getting the Human Player's choice. I might also create a class that "referees" the competition, determining wins and draws.
 
Julie Anne Moore
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Schuetz wrote:That's a start. I'd create a class "ComputerPlayer" and a class "HumanPlayer". The ComputerPlayer class would make the computer's selection, and the HumanPlayer would take care of getting the Human Player's choice. I might also create a class that "referees" the competition, determining wins and draws.



Would I have to redefine my ArrayList<String>, choices, in all each class to be able to use it?
 
Julie Anne Moore
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Schuetz wrote:That's a start. I'd create a class "ComputerPlayer" and a class "HumanPlayer". The ComputerPlayer class would make the computer's selection, and the HumanPlayer would take care of getting the Human Player's choice. I might also create a class that "referees" the competition, determining wins and draws.



Thanks Brian Schuetz, that does make the program clearer to read.

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Schuetz wrote:That's a start. I'd create a class . . .

Surely a Player class with two instances, computer and human? You will have to create some method for each object to take an input.

Suggestions: You can have a special class with three instances, called ROCK PAPER and SCISSORS.
You can find out about nearly random numbers in various places. Note this discussion.
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic