• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Rock, Paper, Scissors Java game  RSS feed

 
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!
I'm still learning basics of Java and I made that game to practice, would appreciate if anyone more experienced could spot me things that I'm doing wrong in there, things that make no sense, bad practices, etc...  Next thing a want to do is a basic GUI using Java Swing. Thanks!!

 
Saloon Keeper
Posts: 2137
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think the Rock paper scissors game has been discussed in our forum in the past. You can search and get good explanations such as using enums instead of Strings.
 
Ansamana Sankarray
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a quick fix on the line 38, as if the player input was 0 it was playing "scissors" instead of printing wrong selection and asking to input a number from 1 to 3 again.

while(playerChoice > 3)

changed to:

while(playerChoice < 1 || playerChoice > 3)
 
salvin francis
Saloon Keeper
Posts: 2137
80
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of points on the above code:
1. Your indentations are not correct
2. It's unclear why "playGame" and "printResult" are static and "convertToString" is not static.
3. Use braces around if conditions

A small nitpick:
Do not display user messages in all caps
 
Sheriff
Posts: 5672
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What salvin francis hinted at in line (2), I will say more plainly: Don't create static methods or variables unless a) the variable is a constant, or b) you are creating a utility class.  There may be other reasons to create static methods and variables, but for a beginner this is a good rule.

So why were they created as static in the first place?  Probably because your IDE suggested it.  The typical thing that happens to beginners is they create a method, then try to call that method directly from the main() method, and the IDE complains.  The trick is to call your starting method from an instance of your class.  Typically all you need in main() is one line, in your case:
This is shorthand for:
...but without creating the unnecessary variable in the middle.  Now you can remove the static modifier from the two methods.
 
Knute Snortum
Sheriff
Posts: 5672
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing that you wrote this:
because the IDE suggested it.  In this case, you are correct to not close a resource opened to System.in, but this is not the case for other resources.  I want to make sure you don't blindly supress a warning that may be important in other circumstances.
 
Knute Snortum
Sheriff
Posts: 5672
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method playGame() is too long.  I would suggest that it have just the main while loop and method calls inside, so
and
To make this work correctly, pull the Scanner declaration and initialization out of the method playGame() and make it an instance variable (declared outside of any method, usually at the top), so:
You can exactact two other methods from playGame() and then you will have a nice, readable, compact method.
 
Ansamana Sankarray
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Salvin Francis and Knute Snortum for your replays!! Really helpful

I will take your suggestions, make modifications and keep them in mind for the future!

About using enums like Salvin said I still don't know how to use them, didn't get to that topic yet.

About using static methods, yes, it was suggested by Eclipse, I'm using notepad++ now.

 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!