• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to create method to count the color match between the computer and the user?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could I get your suggestion on the method that i use or should use?

 
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please start by explaining what you are trying to do.
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please start by explaining what you are trying to do.


I'm trying to do a game called Mastermind, so what I want to do is a method to find the number of exact matches. and a method to find the number of correct colors.
 
Campbell Ritchie
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We need lots more detail before we an tell whether youir code is correct or not.
 
Bartender
Posts: 5061
188
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Abdallah,

you start well, namely by checking, for every i, if guess[i] == solution[i]. What I would do next, is to set both elements to -1, so that it is easy to see later on that these elements have been processed.

However, changing the arrays that way means that you are also changing the original arrays! To prevent that, let's first make a copy of both input arrays and work with the copies. The easiest way to copy an array is to use:

Now, having these copies and having set all exact matches to -1, it is then relatively easy to check for the other matches:
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your advice. We didn't yet learn about the continue statement in java.
 
Piet Souris
Bartender
Posts: 5061
188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oke. Then drop the line with continue and do this:

The idea is that if the guess element == -1, then we have processed it before (it had an exact match and so we must not gonna count it again)
Do you know about the break statement?
The rason for that is, say:
if guess[] = [3, 1, 2] and solution[] = [1, 5, 1] and if we check the 1 from guess[], then we find immediately a matching 1 at location 0. Now, if we do not break the loop here, we would also find a match at location 2, so that we find 2 matches instead of just 1.
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Oke. Then drop the line with continue and do this:

The idea is that if the guess element == -1, then we have processed it before (it had an exact match and so we must not gonna count it again)
Do you know about the break statement?
The rason for that is, say:
if guess[] = [3, 1, 2] and solution[] = [1, 5, 1] and if we check the 1 from guess[], then we find immediately a matching 1 at location 0. Now, if we do not break the loop here, we would also find a match at location 2, so that we find 2 matches instead of just 1.


Thank you, I update my program but I'm just little confused on the logic for the exact matches and color count.
 
Piet Souris
Bartender
Posts: 5061
188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the idea is simple, but not so easy to explain. The main concern is: when a digit has already been checked, then we should not check that digit any further. How do we make this sure? By srtting a processed digit to -1, a value that is not being used, so we can easily see that it can be skipt.

In the next, I work with the arrays guess[] and solution[], and so it does not matter who is guessing and who provided the solution.

So, let guess[] = {1, 2, 3, 1} and solution[] = {1, 1, 1, 2}.

First, we do the check of exactMatches, and that is what you do as well, only I set matching elements to -1. So, after the exactCheck, my arrays will be as follows:
guess[] = {-1, 2, 3, 1}, solution[] = {-1, 1, 1, 2}, and exactMatch = 1.

The we do the check of colorMatches (albiet that we use digits). First, we look at the -1 from guess[]. Since that element is -1, we skip it. Then we look at the second element of guess[], being 2. We check that to every element of solution[], and we find one match. So, colorMatches will be 1. Then we check element 3 of guess, and we find no matches in solution[]. And finally, we look at the last element of guess[], being 1. We see a 1 in solution[] at index 1, so colorMatch can be increased to 2. Now, we must break this loop, since otherwise we would incorrectly also find a match with the 1 from solution[] at index 3.

We are now finished, but suppose that we have larger arrays. To prevent from matching a possible third 1 in guess[] from matchin the 1 in solution[] at index 1 (we already processed that element) I also set it to -1. So, I end up with these arrays:

quess[] = {-1, 2, 3, 1} and solution[] = {-1, -1, 1, -1}, with exactMatch = 1 and colorMatch = 2.

As you see, I change the arrays guess[] and solution[], and that may lead to nasty consequences, So, befor we start all of this, we make copies of both guess[] and solution[], and work with these copies.

Then, last but not least: having calculated the values for 'exactMatches' and 'colorMatches', the question is how to report these. A method in Java can only return one value!
So, let's create a

So, having all this to our disposal, I have this method:
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[quoted text deleted]

Thanks I created this method and it works :
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abdallah Taha,

It's not necessary to quote the entire previous post when the context is clear.  Only quote when you need context, and then only as much text as is needed.
 
This is my favorite show. And this is my favorite tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic