PDA

View Full Version : Question in logic



lw22
07-19-2009, 05:33 PM
Mainly the question is how can I compare 2 arrays to determine if a value exists in one array and if it is in the same position.

I will give you a brief overview of what this about those. Basically I am trying to make a game similar to the boardgame Mastermind. In the game a code is created of a sequence of 5 colors (there are 8 possible colors in the game and no one color can be used twice in the code). The object is for a person to try and guess the exact sequence of the colors in the code. They have 12 guesses to find the exact sequence. When one of the correct colors is guessed a white peg is placed on the gameboard to indicate a correct color was chosed. When the color is in the exact position in the guess as in the code a black peg is used instead. This website has a working verion of the game already: http://www.irt.org/games/js/mind/

As of now I get the color index of the code and guess sequence and store them in an array. Like I said before I am having trouble comparing the arrays. The problem I have having is when the same color is guessed in the guess sequence. I have the choice of disabling the same color to be guessed but that may take away from some of the strategic opertunities in the game. There should be only 1 peg for each color (so if there are multiple guesses and the color is in the code sequence there should be only 1 peg, either black or white).

I was thinking of making another array of colors in the guess sequence that are found in the actual sequence. Then if a guess color is in the above array that color would be ignored. However this won't work. (Assume blue is in the forth position of the code. If blue is also in the thrid and forth guess sequence a white peg will be chosen since the vba code will only reconize the blue in the thrid position. The correct result would be a black peg.)

This all may be very confusing (especially if you do not know the game). I do not have the time to go into more detail now. I have to take my friend to the train station. If needed I can explain more later. Also the coding isn't the best. I didn't take the time to declare my variables yet. I know this is an unusal request for this type of forum but I figured it wouldn't be bad to bounce some ideas around.

Zack Barresse
07-19-2009, 07:47 PM
Well, you can run a forumla on an array, i.e. Match(). The variable would have to be variant of course, and you'd need error handling. But then if you had another variable and looked at the same position (if a match was indeed found in the first) in thte second array, you'd have whether or not it was an exact match. If no exact match, use that second variable to check if the second array has the same color or not. If found, it's in the second array, just not in the same location as the first. You can then use a third array to populate with the results (if any).

Does that help? Took a quick look at the game, didn't look at your file.

Bob Phillips
07-20-2009, 01:59 AM
THis is just thinking out aloud, I haven't tried it, buy I would probably create a bit map for each possible colour aligned to the positions of that colour, and then do a simple AND with the bit map.

So for instance, if the colours to be guessed were yellow, red, blue, blue, my yellow bitmapo would be 1, my red would be 2 and my blue would be 4 + 8 =12.

Then when comparing each guess I would use its position to compare against the bitmap, so if they put blue in position 1 you would do

BlueBitMap And 1

that would return a 0 so you know it is not in position 1. You could then compare it against position 2, then 3, then 4 until it either matches or runs out of choices.

This way you can find the exact match, or a colour match.

Of course you could put all the bitmaps in an array and cycle through the array as well, you could make it as compact as you see fit.