I am writing a simple card game (Similar to Snap). I've got it working, without problems, but I feel that there ought to be a more elegant solution.
Given a set of win conditions:
Y beats R
R beats B
B beats Y
etc
I want to compare the two player's cards and assign both cards to the winner. Caveat: I'm teaching at secondary school level (no OOP) and want to be able to discuss the resulting code with students.
I've left the final condition as an elif, as I wanted to go back and add extra cards to the list of options
The if - elif chain works without problems; I was wondering if there was a more elegant solution.
#I have code that randomly selects from a list, but this is the basic
#idea:
p1=input("enter r,y or b")
p2=input("enter r,y or b")
stack1=[]
stack2=[]
if p1=="r" and p2=="b":
stack1.extend([p1,p2])
elif p1=="y" and p2=="r":
stack1.extend([p1,p2])
elif p1 =="b" and p2 =="y":
stack1.extend([p1,p2])
elif p2 =="r" and p1 =="b":
stack2.extend([p1,p2])
elif p2 =="y" and p1 =="r":
stack2.extend([p1,p2])
elif p2 =="b" and p1 =="y":
stack2.extend([p1,p2])
print(stack1)
print(stack2)
I've excerpted the code from the remainder - the cards are all randomly generated, so no user input is actually required.