• 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

Code Review / Mastermind Game

 
Greenhorn
Posts: 14
Suse Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all,

So I just started learning Java not too long ago and have been struggling with creating useful objects. A lot of the material in the book I'm reading covers technical details of the Java language itself without much emphasis on designing classes. I was hoping that someone would be willing to take a look at a class I made and point out some of the things I've done wrong so I can work on improving them.

The object I tried to create was a Mastermind game. The game consists of one player creating a unique four element code of six colours and the other playing attempting to guess it. After each guess the code maker indicates whether the code breaker has guesses a correct colour or a correct colour and position. I know that the code is likely ugly but the only way it's going to get prettier is if I practice and recognize my errors. Feel free to criticize my naming conventions, class design, method creation and what-have-you.

 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two very quick observations:

1) Your fields CORRECT_COLOUR AND CORRECT__POSITION are capitalised, which is the convention for compile-time constants. They need to be static as well as final.

2) Well done for adding comments. Take a look at Javadoc style comments though, that is standard.

oracle.com/technetwork/java/javase/documentation/index-137868.html
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see a lot of promise here, but there's also some needless complexity, I think. Both your guesses and evaluations arrays are two-dimensional, but every access to them indexes the first dimension with attemptNumber, which only keeps track of the current guess number. Since you never go back and look at prior guesses or evaluations, saving all that history in the those arrays ends up serving no purpose. You could replace both of those two-dimensional arrays with one-dimensional arrays, and drop the use of attemptNumber as an index variable in the dimension you no longer have. (Line 84 is the only place I see one of those arrays indexed by anything other than attemptNumber, where evaluations is indexed by a hard-coded "1." What I am suggesting would let you drop that hard-coded index as well.)

Good early effort.
 
Greg Hatt
Greenhorn
Posts: 14
Suse Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My rationale for the two dimensional array is so that the class could be used to print out a game board which would show all previous attempts and evaluations. If you look at an image of the game on the Wikipedia article you can see what I mean.

The idea being that an actual mastermind game stores all previous guesses and evaluations.
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case, I would consider creating a Board or a Guess object, and perhaps maintain a stack of those.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic