GetDotted Domains

Viewing Thread:
"Java Mastermind"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Sun 01/05/05 at 20:42
Regular
Posts: 4,279
Right. I'm creating a java mastermind game for college and I'm stuck.

Not so much the code, but the algorithm.

You have to guess 4 colours in the correct order (choose between 8 colours) to win the game.

After every guess it needs to display pico and fermi points.

Pico - correct colour, correct position
Fermi - correct colour, wrong position

Pico points are dead easy, however I'm really smashing monitors over the Fermi points.

Any ideas?
Wed 11/05/05 at 11:32
Regular
Posts: 4,279
Well, thanks for the advice, but as it was in last Friday it is no longer needed. =P

I tried the vector arrays, failed and then gave up.

If statements all the way =P

Thanks again.
Tue 10/05/05 at 20:14
Regular
"Devil in disguise"
Posts: 3,151
Some of you have some really strange ideas about coding. :)
Tue 10/05/05 at 19:57
Regular
"bing bang bong"
Posts: 3,040
VenomByte wrote:
> Use vectors, not arrays. It will make it much easier to separate the
> pico points first.


This is very good advice. Vectors are pretty much always preferable to arrays.

Think about the problem in structured English. This is what you want to do:


For each element in the players guess:
.....if that element is the same as the corresponding element
.....of the solution, remove that element from the guess and add
.....a picopoint

For each element in the players guess:
.....if that element is present in the solution, add a fermipoint




And now to apply the above comments to Venombyte's code:

int pico = 0;
int fermi = 0;

//FOR EACH ELEMENT IN THE PLAYERS GUESS
for (int i=0; i {
.....//if that element is the same as the corresponding
.....//element of the solution...
.....if ( guessVector.elementAt(i).equals(answerVector.elementAt(i)) )
.....{
..........//remove that element from the guess
..........//and add a picopoint
..........guessVector.remove(i);
..........answerVector.remove(i)
..........pico++;
..........i--;
.....}
}

boolean found = false;

//FOR EACH ELEMENT IN THE PLAYERS GUESS
for (int i=0; i {
.....//if that element is present in the solution, add a fermipoint
.....for (int j=0; j .....{
..........if (guessVector.elementAt(i).equals(answerVector.elementAt(j)) )
..........{
...............fermi++;
...............found = true;
..........}
.....}
}

Hope this (a) formats correctly and (b) makes sense.
Tue 10/05/05 at 17:18
Regular
"Bad Wolf; England"
Posts: 920
Don't forget the catchphrase:

You've started, so you'll finish.
Wed 04/05/05 at 13:07
Regular
"smile, it's free"
Posts: 6,460
Use vectors, not arrays. It will make it much easier to separate the pico points first.

First check for all your Pico points. Every time you get one, remove the entry from both your guess and answer array.

Then loop through the arrays checking for matches in the 'wrong place', and stopping the inner loop every time you get a match (in case of multiple entries for the same colour).


int pico = 0;
int fermi = 0;

for (int i=0; i {
if ( guessVector.elementAt(i).equals(answerVector.elementAt(i)) )
{
guessVector.remove(i);
answerVector.remove(i)
pico++;
i--; //better decrement since you've removed an element
}
}

//all pico points will now have been removed, so if you find a match when
//looping, it must be a fermi point

for (int i=0; i {
boolean found = false;
for (int j=0; j {
if (guessVector.elementAt(i).equals(answerVector.elementAt(j)) )
{
fermi++;
found = true;
//setting found to true makes sure you only get max of ONE fermi
// point when running the inner loop for a given guess value
}
}
}

NOTE: You will also need to cast the vector content according to your chosen data type
Mon 02/05/05 at 15:24
Posts: 15,443
Use a nested loop. In the inner one, go through the guessArray, comparing if the current value in the answerArray is equal to the one selected in guessArray. If so, compare the position of the answerArray to that of the guessArray; if equal, add a point to the Pico variable; if not, 1 to the fermi.

Use the outer loop to cycle through the colours. Having said that, duplicates where a colour appears twice, one in the correct position, one not, would be an issue. Ah well, you can think up some additional checks for it.
Mon 02/05/05 at 11:08
Regular
Posts: 4,279
Right, for the simplicity of not having to scrap the rest of the code I have created the arrays using.

String[] answerarray = {colour1 +colour2 +colour3 +colour4};
String[] guessarray = {select1 +select2 +select3 +select4};

Now the next part I don't understand at all. I've done for loops to go through arrays before, however I really hate arrays and everything I learn about them goes in one ear and out the other.

Sorry, how would I do this?
Mon 02/05/05 at 04:04
Regular
"Devil in disguise"
Posts: 3,151
Ok, your problem is you're trying micromanage the problem too much and give everything a variable name. Its a much easier problem if you try to model the collections of things you have instead.

Simplest way is to have 2 arrays, one that contains the 4 colours selected at random, the other containing the 4 colours the user selected. Create a function that takes a colour as a parameter and loops through the array containing the randomly selected colours. At each position in the array it checks if the current colour matches the one passed to the function. If it does then the function returns the position of the colour. If it gets the end of the array without finding the colour then it returns -1.

The main part is to then have a loop that goes through your array of user selected colours. And at each position in the array you call your function, passing the current colour. Your function returns either -1 (the colour isn't in there) or the position of the colour. If the return value is -1, then the colour isn't in there at all so increase neither score, If the position returned is equal to the current position in the array then you're increasing the Pico score. If not (and its not -1) then you're increasing the Fermi score.

Hm, is that clear? Reading t back, doesn't seem like a good explanation. Explaining stuff was never my forte..
Mon 02/05/05 at 01:13
Regular
Posts: 4,279
picocount = 0;
fermicount = 0;

if (select1 == colour1) {picocount++;}
if (select2 == colour2) {picocount++;}
if (select3 == colour3) {picocount++;}
if (select4 == colour4) {picocount++;}



select = user chosen colour

colour = computer generated colour (answer)
Sun 01/05/05 at 23:46
Regular
"Devil in disguise"
Posts: 3,151
Damarus wrote:
> Pico points are dead easy, however I'm really smashing monitors over
> the Fermi points.

Maybe you could show us how you're doing Pico points? Theres plenty enough solutions but it would be easier to help if we knew how you've represented your data and how you do your pico points calculation.

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Impressive control panel
I have to say that I'm impressed with the features available having logged on... Loads of info - excellent.
Phil
Everybody thinks I am an IT genius...
Nothing but admiration. I have been complimented on the church site that I manage through you and everybody thinks I am an IT genius. Your support is unquestionably outstanding.
Brian

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.