• Post Reply Bookmark Topic Watch Topic
  • New Topic

Rock-Paper-Scissors Game (Critique Wanted)  RSS feed

 
Julie Anne Moore
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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:
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Is this better for reducing my code in the main method?

 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.

 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!