• 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

Mastermind Game - comparing two arrays

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so i have three arrays (filled with the following values
for sake of example):

String board[] = new String[4];

|R|B|G|O|

String useCode[] = new String[4];

|R|B|O|G|

String clues[] = new String[4];

| | | | |


I want to compare the board[] with useCode[], and store
the result in clues[]. If the corresponding elements (at index i)
in board[] and useCode[] are:

1) equal to each other: record a "B" in clues[]

2) not equal to each other (but the element at the current
index in board exists as an element in useCode[] (just
not in the same position as it is in board[])): record a
"W" in clues[]

3) not equal to each other (and the element at the current
index in board does not exist as an element in useCode[]
(in any other position): record a "o" in clues[]

I am fine with recognizing if the two elements are equal
to each other or not equal to each other, but for the two
cases (#2,#3) in which they are not equal to each other,
I do not know how to distinguish or recognize which case
it is.

Here is what I have so far:




If you haven't already picked up on it, I am developing a version of the game Mastermind. I have everything coded and working, except for this last obstacle that I have been stuck on for months! Please advise.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, since these values all appear to be single characters, I would seriously consider using chars instead of Strings. This would make comparisons a lot easier, and you could use switch/case statements. (Enums would also be a good choice.)

If you're still going to use Strings, I would take another look at this line...

Since these are Strings you're comparing, you should use the equals method instead of the simple value comparison (which compares references). This returns a boolean, so for "not equal" you could use the logical complement operator (!). That is, if( !(x.equals(y)) )...

When it comes to sorting out the other cases, I would be inclined to extract the char from the String (if you really want to use Strings) so that a switch/case would be used. You could do this with myString.charAt(0). Otherwise, I think you're stuck with a series of if statements using String's equals method.
 
Kevin Behr
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, I am stuck using Strings. I wanted to use character arrays but I made a sacrifice for what will ultimately lead to a more user-friendly application. For any situation in which I need to use characters, I extract (via a charAt) the String letter that I require, and convert it to a char. From your suggestion, it sounds easiest to copy the contents of the String arrays into character arrays. Ok. So supposing I do that...

Can you tell me more about the case statements idea? I have used them a bit, and know how to implement them...but in terms of using them to distinguish my two cases: i am still at a loss. I just can't understand the logic behind what I have to do...
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this help?
 
Kevin Behr
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That helps yes, from that I have created the following case statement:



Now, If I can get a tally of each color of each array (board[][] and useCode), then I should be able to compare the tallies - for every tally from each board that is not equal to each other, i know i will have to place an "o" in my clues[] array - but then i run into a similar problem if the tallies are equal - again, it could result in either a "B" or a "W"...ahhh! Is there a better way you know of for trying to distinguish the cases (using the case statement?)
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not entirely clear on how these tallies are working, but I sense a problem with the logic.

If I remember the game correctly, a black peg indicates a correct color in the correct position, and a white peg indicates a correct color in the wrong position.

It seems to me that "scoring" a user's guess would require two passes, along with some "bookkeeping":
  • First pass is for black pegs. Check each of the 4 guess colors to see if they match the actual colors in the same position. If so, then output a black peg. Also, "flag" these somehow (a boolean bookkeeping array?) so they don't get double counted later.
  • Second pass is for white pegs. Check each of the 4 guess colors that are not flagged from the first pass to see if they match any of the actual colors in different positions that are also not flagged. If so, then output a white peg, and flag these so they don't get double counted on another iteration.
  • Does that make sense?
     
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Does this help ?

     
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    See if you can write down the rules in "structured English" and then make code that looks as much like that as possible. Make the code sing out the logic. Are these the right conditions?

    If that's close, try to write it in exactly that many lines of Java. The predicates in the if clauses will turn into method calls. Maybe just run all the descriptive words together ... if characterMatchesSecretAtThisPosition() ...
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Some clarification on my earlier post...

    If I'm not mistaken, the outputs of 'B', 'W', or 'o' use a one-to-one correspondence between elements of the secret sequence and elements of the guess. (Although the output is not ordered.)

    For example, consider a secret sequence of { Black, Orange, Black, Black } and a guess of { Orange, Orange, Green, Green }. In this case, the guess has 1 correct color in the correct position (second), so we would return a single 'B'. But we would not return a 'W' for the guessed Orange in the first position, because the single Orange in the secret sequence has already been accounted for.

    This is why I think we need 2 separate "passes." First to establish all the 'B' outputs, and second to establish any 'W' outputs without re-using any 'B' elements.
     
    Stan James
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ah, the rules are trickier than I thought. Never played the hardware version either.
     
    author
    Posts: 9050
    21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    as an aside, it's a fun enhancement to play / program this game using 4, 5, 6, or 7 letter words.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic