Consolidation of Permutation combination code

ajetrumpet

Banned
Local time
Today, 16:28
Joined
Jun 22, 2007
Messages
5,638
Folks,

I have about 1200 lines of code in a module to play a game I have created in Access. The code is checking to see weather a user has colored the right number of pictures the right color(s). Initially, this code was not in the game, but what I found without it, was that if a user was correct with their answer, but colored the pictures in the wrong order from what the voice instructions' order was, it would tell the user that the answer was incorrect. I know I need this code, but I am looking for some kind of looping consolidation here, if someone cares to take a crack at it. Thanks!

PlayType is the number of random figures being colored with randomly chosen colors:
Code:
Select Case PlayType 'DETERMINE IF THE ANSWER IS CORRECT USING 
                        THE 'CORRECTSELECT' VARIABLE HERE
   Case Is = 1
      If SelectedPict1 = Figure1 And SelectedColor1 = FigColor1 Then
         CorrectSelect = True
      Else
         CorrectSelect = False
      End If
   Case Is = 2
      If (SelectedPict1 = Figure1 And SelectedColor1 = FigColor1 _
      And SelectedPict2 = Figure2 And SelectedColor2 = FigColor2) Or _
         (SelectedPict1 = Figure2 And SelectedColor1 = FigColor2 _
         And SelectedPict2 = Figure1 And SelectedColor2 = FigColor1) Then
            CorrectSelect = True
      Else
            CorrectSelect = False
      End If
   Case Is = 3
      If (SelectedPict1 = Figure1 And SelectedColor1 = FigColor1 _
      And SelectedPict2 = Figure2 And SelectedColor2 = FigColor2 _
      And SelectedPict3 = Figure3 And SelectedColor3 = FigColor3) Or _
         (SelectedPict1 = Figure1 And SelectedColor1 = FigColor1 _
         And SelectedPict2 = Figure3 And SelectedColor2 = FigColor3 _
         And SelectedPict3 = Figure2 And SelectedColor3 = FigColor2) Or _
            (SelectedPict1 = Figure3 And SelectedColor1 = FigColor3 _
            And SelectedPict2 = Figure2 And SelectedColor2 = FigColor2 _
            And SelectedPict3 = Figure1 And SelectedColor3 = FigColor1) Or _
               (SelectedPict1 = Figure2 And SelectedColor1 = FigColor2 _
               And SelectedPict2 = Figure1 And SelectedColor2 = FigColor1 _
               And SelectedPict3 = Figure3 And SelectedColor3 = FigColor3) Or _
                  (SelectedPict1 = Figure2 And SelectedColor1 = FigColor2 _
                  And SelectedPict2 = Figure3 And SelectedColor2 = FigColor3 _
                  And SelectedPict3 = Figure1 And SelectedColor3 = FigColor1) Or _
                     (SelectedPict1 = Figure3 And SelectedColor1 = FigColor3 _
                     And SelectedPict2 = Figure1 And SelectedColor2 = FigColor1 _
                     And SelectedPict3 = Figure2 And SelectedColor3 = FigColor2) Then

         CorrectSelect = True
      Else
         CorrectSelect = False
      End If
End Select
 
Last edited:
Hi Adam,

One thought is that your permutation model is based on a factorial. So recursively speaking, the number of permutations can be represented as:

Factorial(PlayType) = PlayType * Factorial(PlayType - 1)

The tough part though is that you need to interrogate each element of your permutations. So far it escapes me whether this could be achieved recursively or not. But maybe this gives you some ideas...

Hope this helps.

Regards,
John
 
Hey John -

Where does factorial() come from? If it's a function of your making, please post it.

Bob
 
Hi Bob,

Not of my making, but mathematically speaking, it's pretty straightforward.

Here's a link to a factorial function put together by Marshall Barton.

Regards,
John
 
is this a mastermind type thing, adam?

i am not sure exactly what you are trying to do, or how you are doing it. Perhaps you could expand on what the game actually is/does

Firstly - I know you wrote some code to extract potential permutations explicitly - at the time, i came up with a recursive solution which did the same thing, but i am not sure whether this is relevant

Secondly - pattern matching - you are obviously laboriously checking your solution in some way. One way of representing a pattern is by using separate bits of a byte/word, which lends itself to boolean arithmetic - again not sure if this is an easier way of resolving your particular issue.


Factorial function - this is just identifying the number of possible permutations of items. Calculating combinations can be done recursively or directly

the formula for p objects taken form a population of n

is given by the generic calculation

n * n-1 * n-2 etc
(not sure how to express this in terms of n,p
but eg if you want to select 3 items form 7 then you have 7*6*5 = 210 combinations - if the order is not significant then you have 7*6*5/1*2*3 = 210/6 = 35 - incidentally these calculations all form the basis of the binomial expansion.)


now this can be written factorially as

n!
------
(n-p)!


if the order is not significant then it becomes

n!
-----------
(n-p)! * p!


I think this is correct
 
no Dave, this is not a mastermind thing. The company I currently work for is using this game in their applications. I'm not sure what new applications are worth in lines of code, but this particular one I am working on has thousands of lines. All I am trying to do is eliminate the need for some of it, and this problem takes way more lines than should be necessary, that's all.
 
Adam,

i was trying to understand exactly what you were trying to achieve with the code segment - it isnt really clear to be honest
 

Users who are viewing this thread

Back
Top Bottom