Need help with python! I am trying to create a script for a small-straight dice in Yahtzee where I have 5 dice and I will need to have a 4 sequential dice in the 5 dice that will return a true if this condition is satisfied. I already figured out the random module in order to randomly roll 5 dices. I just need a script where the function tests the dice if it would satisfy small-straight or not.
I would create a bitmask of which of the 6 numbers were rolled. Then I'd have a target bitmask (0b1111 or 0xF) that represented the lowest small straight. I'd shift it left by 0, 1, and 2 bits, and for each of those shifts, and it with the rolled bits and see if the result is the shifted target bitmask. Another way (which is kind of a variant of the first one) is to assign each rolled number a different prime number (1->2, 2->3, 3->5, 4->7, 5->11, 6->13). Then calculate the product of the primes for the rolled dice, and the products of the primes for the small straights, and see if the rolled product is divisible by any of the products for the straights. A more straightforward way is to create a list of booleans that says whether that number was rolled. Start a counter at 0, iterate through the list, and when you hit a True increment the counter, and when you hit a False reset it to 0. If it ever gets to 4, return True.
Join our real-time social learning platform and learn together with your friends!