Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # INSERT CONSTANTS AND FUNCTIONS USED ACROSS ALL QUESTIONS HERE
- #outputs a list of the card suites of a pile in order
- def suitelst(bpile):
- out=[]
- if type(bpile) is list:
- for i in bpile:
- out.append(i[1])
- if type(bpile) is str:
- out.append(bpile[1])
- return out
- return out
- #outputs a list of the card value of a pile in order
- def vallst(pile):
- outp=[]
- for i in pile:
- j=i[0]
- if j=="0":
- outp.append("10")
- if j.isdigit():
- outp.append(i[0])
- elif j in "AJQK":
- if j=="A":
- j=1
- elif j=="J":
- j=11
- elif j=="Q":
- j=12
- elif j=="K":
- j=13
- outp.append(str(j))
- return outp
- # build pile play types
- INVALID_PLAY = 0
- VALID_NONFINAL_PLAY = 1
- VALID_FINAL_PLAY = 2
- def comp10001bo_match_discard(play_card, discard_pile, player_no, to_player_no,
- from_hand=True):
- #This function checks if a valid play can be made onto a discard pile
- #this if block checks for valid moves given an empty discard pile, namely
- #whether a it's a personal discard pile, if it's an Ace and if it doesn't
- #originate from the hand
- if not discard_pile:
- if player_no != to_player_no:
- return 0
- elif play_card[0]=="A":
- return 0
- elif not from_hand:
- return 0
- else:
- return 2
- #dcheck is one of three conditionals needed to validate a non-turn-ending
- #play, it checks if the discard pile is not empty
- dcheck=False
- if discard_pile:
- dcheck=True
- #checks if the discard pile is valid by checking if consequtive cards
- #have alternating suites
- suites=suitelst(discard_pile)
- vals= vallst(discard_pile)
- for i in range(1, len(suites)):
- if suites[i] in "SC":
- if suites[i-1] in "SC":
- return 0
- if suites[i] in "HD":
- if suites[i-1] in "HD":
- return 0
- #spass is the second conditional which validates a non-urn-nding move
- spass=False
- #checks if there is actually a card being played
- if not play_card:
- return 0
- #These two if blocks test whether the playing card and the top card of
- #the discard pile are of the same suite colour
- if (play_card[1]in"SC"):
- if (discard_pile[-1][1]in"HD"):
- spass=True
- if (play_card[1]in"HD"):
- if (discard_pile[-1][1]in"SC"):
- spass=True
- #vpass is the third conditional to validate a non-turn-ending play
- vpass=False
- #These three if blocks test whether the playing card is either and Ace or
- #numerically adjacent to the top build pile card
- if play_card:
- if play_card[0]== "A":
- return 0
- if abs(int(vallst(play_card)[0])-int(vallst(discard_pile[-1])[0]))==1:
- vpass=True
- if abs(int(vallst(play_card)[0])-int(vallst(discard_pile[-1])[0]))==11:
- vpass=True
- #given the three conditionals dcheck vpass and spass are all true the
- #conditions for a non-turn-ending play are met and the function outputs
- #1
- if dcheck and vpass and spass:
- return 1
- #these two blocks sort any remaining play circumstances into their
- #respective outputs
- if from_hand:
- return 2
- else:
- return 0
- # automatically run each of the examples from the question
- if __name__ == "__main__":
- tests = (
- # can start own discard pile with any card from hand (FINAL play)
- (VALID_FINAL_PLAY, '4S', [], 2, 2),
- # can't start the discard pile of another player
- (INVALID_PLAY, '4S', [], 2, 0),
- # can't start a discard stack from the stockpile/build pile
- (INVALID_PLAY, '4S', [], 2, 2, False),
- # can play a black 4 on a red 3 (to own discard pile; NON-FINAL)
- (VALID_NONFINAL_PLAY, '4S', ['3H'], 2, 2),
- # can play a black 4 on a red 3 (to another
- # player's discard pile; NON-FINAL)
- (VALID_NONFINAL_PLAY, '4S', ['3H'], 2, 3),
- # can play a black 4 on a red 3 (from discard/stockpile to
- # own discard pile; NON-FINAL)
- (VALID_NONFINAL_PLAY, '4S', ['3H'], 2, 3, False),
- # can't play an Ace on a discard pile
- (INVALID_PLAY, 'AH', ['KS'], 2, 3),
- # can play a red 2 on a black King (to another
- # player's discard pile; NON-FINAL
- (VALID_NONFINAL_PLAY, '2H', ['KS'], 2, 3),
- # can (illegally) play 2 on Q (from hand to another player's
- # discard pile; FINAL)
- (VALID_FINAL_PLAY, '2H', ['QS'], 2, 3),
- # can't play 2 on Q (from stockpile/build pile to another player's
- # discard pile; FINAL)
- (INVALID_PLAY, '2H', ['QS'], 2, 3, False),
- # can (illegally) play red 4 on red 3 (from hand to another player's
- # discard pile; FINAL)
- (VALID_FINAL_PLAY, '4H', ['3H'], 2, 3),
- # can't (illegally) play red 4 on red 3 (from source other
- # than hand to own discard pile; INVALID)
- (INVALID_PLAY, '4H', ['3H'], 2, 2, False),
- )
- for retval, *args in tests:
- if comp10001bo_match_discard(*args) == retval:
- result = "passed"
- else:
- result = "failed"
- print("Testing comp10001bo_match_discard{} ... {}".format(
- repr(tuple(args)), result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement