0

I am trying to make a yahtzee scorer on picaxe, it is all okay other that the fact that there are so many different combinations. I am wandering if there is a way of testing whether 4 of my 5 variables are the same(no more and no less) without having to go through all the different combinations eg: if b1=b2 and b1=b3 and b1=b4 and b1!=b5 then ... if b1=b2 and b1=b3 and b1=b5 and b1!=b4 then ...

In summary is there a way athat i can see if only 4 out of the 5 variables are the same.

James Brennan
  • 635
  • 1
  • 6
  • 7

1 Answers1

0

Because you've told us this is for a Yahtzee scorer, I assume that the five variables we need to compare represent the throw of five dice, and so their values will only be between 1 and 6.

In that case a functional solution is to count how many of the variables are equal to a test value, and repeat this for test values between 1 and 6:

; define symbols for the two variables we will use
symbol same_test = b6
symbol same_count = b7

b1 = 3: b2 = 3: b3 = 3: b4 = 3: b5 = 1 ; test data
gosub test4same
if same_count = 4 then found_4_same ; do something
; else program flow continues here
end

found_4_same:
sertxd("found 4 the same")
end

test4same: ; test if any four of the variables are equal
same_count = 0
for same_test = 1 to 6
    if b1 = same_test then gosub found_one
    if b2 = same_test then gosub found_one
    if b3 = same_test then gosub found_one
    if b4 = same_test then gosub found_one
    if b5 = same_test then gosub found_one
    if same_count = 4 then exit ; 4 variables were equal to same_test
    same_count = 0
next
return

found_one:
inc same_count
return

gosub test4same will check whether four out of the five variables b1 to b5 are equal to the same number, for numbers between 1 and 6. If they are, the variable same_count will be 4 and the number the four variables are equal to will be in same_test.

Using the if ... then exit structure before resetting same_count back to zero was the most efficient way I could think of to tell whether we found four the same or not.

The code after the two symbol statements and before the label test4same is just to demonstrate that it works; replace this with your actual program.

In principle you could use the same technique over any range of values, but obviously it would get a bit slow if you needed to test all 256 possible values of a byte variable.

nekomatic
  • 5,988
  • 1
  • 20
  • 27