• Post Reply Bookmark Topic Watch Topic
  • New Topic

No clue what's wrong with this code  RSS feed

 
Bill Balabanis
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to create a mastermind game and am having serious difficulties why it is not working. Could you maybe pick out any logical flaws I've made? Forgive my poor formatting.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Balabanis wrote:Could you maybe pick out any logical flaws I've made?

Did you consider that random is not the same as unique? When you generate your hidden numbers, it's entirely possible that you'll generate a duplicate or two. Does your code account for repeats in the hidden values? If you don't want to have repeats, then a better approach is to specify the numbers you want to hide, then shuffle them. The java.util.Collections class has a shuffle() method.

Forgive my poor formatting.

Instead of apologizing for your poor formatting and posting it like that anyway, why not just format your code properly before you post it? If you're not using an IDE that can autoformat your code for you, you can always use one of the online Java code prettifiers available for free.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and Welcome to the Ranch!
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure the code you wrote for counting black and white pins is not correct although it's pretty convoluted so it's difficult to pinpoint what exactly is wrong with it.  It reminds me of what C.A.R. Hoare wrote: “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

Try to describe the process for counting white and black pins in plain English sentences, as you would do it by hand. If you can do that in a clear and logical way, then it's easier to translate what needs to be done to Java.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Counting pins in plain english:
Go through each pair of code/guess slots, IF the slots are identical THEN increment the qtyColorAndPosition counter, ELSE add the unmatched guess to a guess list and the unmatched code to a code list. After all slots have been compared, sort both unmatched lists, then go through the unmatched lists looking for color matches and increment the qtyColorOnly counter.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again.
Please move all that code out of the main method. The main method is intended for starting the application, not for the whole application.
Also I don't like doing arithmetic with Math#random. You will find a discussion here which describes what I think is a much better way to get “random” numbers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!