aspose file tools*
The moose likes Beginning Java and the fly likes Mastermind Game - comparing two arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Mastermind Game - comparing two arrays" Watch "Mastermind Game - comparing two arrays" New topic
Author

Mastermind Game - comparing two arrays

Kevin Behr
Greenhorn

Joined: Jun 29, 2006
Posts: 23
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Kevin Behr
Greenhorn

Joined: Jun 29, 2006
Posts: 23
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

Joined: Aug 31, 2004
Posts: 11343

Does this help?
Kevin Behr
Greenhorn

Joined: Jun 29, 2006
Posts: 23
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

Joined: Aug 31, 2004
Posts: 11343

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?
    vidya Sreedharan
    Greenhorn

    Joined: Jun 27, 2007
    Posts: 4
    Does this help ?

    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    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() ...


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    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)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Ah, the rules are trickier than I thought. Never played the hardware version either.
    Bert Bates
    author
    Sheriff

    Joined: Oct 14, 2002
    Posts: 8829
        
        5
    as an aside, it's a fun enhancement to play / program this game using 4, 5, 6, or 7 letter words.


    Spot false dilemmas now, ask me how!
    (If you're not on the edge, you're taking up too much room.)
     
     
    subject: Mastermind Game - comparing two arrays