Home > Blockchain >  "Wild Symbol" for Slots Program?
"Wild Symbol" for Slots Program?

Time:08-24

Be gentle, I'm brand new to programming! For my first project, I'm working on a 3 reel slot machine program. Just got to the point of making a "Wild Symbol" out of the integer 1. Is there a simpler way to code it because it feels inelegant? Thank you!

#3 wheel slot machine code
ri1 = random.randint(1, 20)
ri2 = random.randint(1, 20)
ri3 = random.randint(1, 20)
print(ri1, ri2, ri3)

if ri1 == ri2 == ri3:
    print("Win")
elif ri1 == 1 and ri2 == ri3:
    print("Win")
elif ri2 == 1 and ri1 == ri3:
    print("Win")
elif ri1 == ri2 and ri3 == 1:
    print("Win")
elif ri1 == 1 and ri2 == 1:
    print("Win")
elif ri1 == 1 and ri3 == 1:
    print("Win")
elif ri2 == 1 and ri3 == 1:
    print("Win")
elif ri1 == 20 and ri2 == 20 and ri3 == 20:
    print("Jackpot")
else:
    print("Play Again")

CodePudding user response:

The usual way to check for duplicates is to build a set of the values. For this specific case, we can build the set, remove 1 if it appears, and then see if there's at most one unique value. (If there are no remaining values, then all of the inputs were 1, which is also a win.)

results = {ri1, ri2, ri3}
results.discard(1)
if results == {20}:
    print("Jackpot")
elif len(x) <= 1:
    print("Win")
else:
    print("Play again")

CodePudding user response:

Using sets you can achieve your goal. Since sets remove duplicates you can easily create a few checks to see how many duplicates there were by adding all 3 numbers to a set and seeing how many are left. Then you can check if 20, or a 1 is one of those numbers to determine a prize.

s = set([ri1, ri2, ri3])

# if the length of the set is 1 then you know all the numbers were the same
if len(s) == 1:   
    if 20 in s: # if 20 is that number then they got the jackpot.
        print("Jackpot")
    else:
        print("Win")  # otherwise they win

# if the length is only 2 then you know at least two of the number 
# were equal and if one of those numbers was a 1 then its a win.
elif len(s) == 2 and 1 in s:   
    print("Win")
else:
    print("Play Again")        # otherwise play again
  • Related