Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mastermind game

Status
Not open for further replies.

streetrcr

Programmer
Mar 24, 2006
1
AU
hi, im new here and looking for some help with a mastermind game i have to program for school. this is the first thing i have programmed and so plz keep the solutions simple.

if anyone knows how to play mastermind, they will know that there is a correct colour wrong location pin (the white one) i need to write some code which will scan through my array of colours and tell me how many near matches i have, the problem is i cannot get it to work. logically it is correct and works when desk checked, but for some reason i do not get the correct answer.

plz help
thanks
mitch

my code (this is just the procedure which does not work, it is not the whole game)

Code:
 function countNearMatches: integer;

                var
                   i, j : integer;
                   solutionCopy : ColourArray;
                   guessCopy : ColourArray;

                begin
                        countNearMatches := 0;

                        guessCopy := guess;
                        solutionCopy := solution;
                        for i := 0 to CODELENGTH-1 do
                        begin
                                for j := 0 to CODELENGTH-1 do
                                begin
                                        if (guessCopy [i] = solutionCopy [j])
                                        and (i <> j)
                                        and (guessCopy[i] <> NULL)
                                        and (solutionCopy[j] <> NULL) then
                                        begin
                                                countNearMatches := countNearMatches + 1;
                                                guessCopy[i] := NULL;
                                                SolutionCopy[j] := NULL;
                                        end;
                                end;
                        end;
                end;
 
I haven't spent long looking at this, so I might have missed the point, but I think it's this:

Did you intend to write a recursive function (a function that calls itself)? When you run the statement
countNearMatches := countNearMatches + 1;
what actually happens is that you don't increment a local copy of the answer-so-far. What happens is you call the function again, add one to whatever result you get, and assign that to the current output of this function call (does that make sense?).

I suspect in your case the whole thing just crashed, because there's no reason why it shouldn't keep calling itself for ever. Recursion is occasionally useful, but it can be messy...

What you need to do, if you don't want recursion, is to make a local variable to hold the answer as you calculate it. You can increment this or do maths with it as much as you want. Then, at the end of the function, assign the value of your local variable to the function name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top