Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1.  
  2. # INSERT CONSTANTS AND FUNCTIONS USED ACROSS ALL QUESTIONS HERE
  3.  
  4. #outputs a list of the card suites of a pile in order
  5. def suitelst(bpile):
  6. out=[]
  7. if type(bpile) is list:
  8. for i in bpile:
  9. out.append(i[1])
  10. if type(bpile) is str:
  11. out.append(bpile[1])
  12. return out
  13. return out
  14.  
  15.  
  16. #outputs a list of the card value of a pile in order
  17. def vallst(pile):
  18. outp=[]
  19.  
  20. for i in pile:
  21. j=i[0]
  22.  
  23. if j=="0":
  24. outp.append("10")
  25.  
  26.  
  27. if j.isdigit():
  28. outp.append(i[0])
  29.  
  30. elif j in "AJQK":
  31. if j=="A":
  32. j=1
  33. elif j=="J":
  34. j=11
  35. elif j=="Q":
  36. j=12
  37. elif j=="K":
  38. j=13
  39. outp.append(str(j))
  40.  
  41.  
  42. return outp
  43.  
  44. # build pile play types
  45. INVALID_PLAY = 0
  46. VALID_NONFINAL_PLAY = 1
  47. VALID_FINAL_PLAY = 2
  48.  
  49.  
  50.  
  51.  
  52. def comp10001bo_match_discard(play_card, discard_pile, player_no, to_player_no,
  53. from_hand=True):
  54. #This function checks if a valid play can be made onto a discard pile
  55.  
  56.  
  57. #this if block checks for valid moves given an empty discard pile, namely
  58. #whether a it's a personal discard pile, if it's an Ace and if it doesn't
  59. #originate from the hand
  60. if not discard_pile:
  61. if player_no != to_player_no:
  62. return 0
  63. elif play_card[0]=="A":
  64. return 0
  65. elif not from_hand:
  66. return 0
  67. else:
  68. return 2
  69.  
  70.  
  71. #dcheck is one of three conditionals needed to validate a non-turn-ending
  72. #play, it checks if the discard pile is not empty
  73. dcheck=False
  74. if discard_pile:
  75. dcheck=True
  76.  
  77. #checks if the discard pile is valid by checking if consequtive cards
  78. #have alternating suites
  79. suites=suitelst(discard_pile)
  80. vals= vallst(discard_pile)
  81. for i in range(1, len(suites)):
  82. if suites[i] in "SC":
  83. if suites[i-1] in "SC":
  84. return 0
  85. if suites[i] in "HD":
  86. if suites[i-1] in "HD":
  87. return 0
  88.  
  89.  
  90.  
  91.  
  92. #spass is the second conditional which validates a non-urn-nding move
  93. spass=False
  94.  
  95. #checks if there is actually a card being played
  96. if not play_card:
  97. return 0
  98.  
  99. #These two if blocks test whether the playing card and the top card of
  100. #the discard pile are of the same suite colour
  101. if (play_card[1]in"SC"):
  102. if (discard_pile[-1][1]in"HD"):
  103. spass=True
  104.  
  105. if (play_card[1]in"HD"):
  106. if (discard_pile[-1][1]in"SC"):
  107. spass=True
  108.  
  109. #vpass is the third conditional to validate a non-turn-ending play
  110. vpass=False
  111. #These three if blocks test whether the playing card is either and Ace or
  112. #numerically adjacent to the top build pile card
  113. if play_card:
  114. if play_card[0]== "A":
  115. return 0
  116.  
  117. if abs(int(vallst(play_card)[0])-int(vallst(discard_pile[-1])[0]))==1:
  118. vpass=True
  119. if abs(int(vallst(play_card)[0])-int(vallst(discard_pile[-1])[0]))==11:
  120. vpass=True
  121.  
  122.  
  123.  
  124. #given the three conditionals dcheck vpass and spass are all true the
  125. #conditions for a non-turn-ending play are met and the function outputs
  126. #1
  127. if dcheck and vpass and spass:
  128. return 1
  129.  
  130. #these two blocks sort any remaining play circumstances into their
  131. #respective outputs
  132. if from_hand:
  133. return 2
  134. else:
  135. return 0
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. # automatically run each of the examples from the question
  153. if __name__ == "__main__":
  154. tests = (
  155. # can start own discard pile with any card from hand (FINAL play)
  156. (VALID_FINAL_PLAY, '4S', [], 2, 2),
  157.  
  158. # can't start the discard pile of another player
  159. (INVALID_PLAY, '4S', [], 2, 0),
  160.  
  161. # can't start a discard stack from the stockpile/build pile
  162. (INVALID_PLAY, '4S', [], 2, 2, False),
  163.  
  164. # can play a black 4 on a red 3 (to own discard pile; NON-FINAL)
  165. (VALID_NONFINAL_PLAY, '4S', ['3H'], 2, 2),
  166.  
  167. # can play a black 4 on a red 3 (to another
  168. # player's discard pile; NON-FINAL)
  169. (VALID_NONFINAL_PLAY, '4S', ['3H'], 2, 3),
  170.  
  171. # can play a black 4 on a red 3 (from discard/stockpile to
  172. # own discard pile; NON-FINAL)
  173. (VALID_NONFINAL_PLAY, '4S', ['3H'], 2, 3, False),
  174.  
  175. # can't play an Ace on a discard pile
  176. (INVALID_PLAY, 'AH', ['KS'], 2, 3),
  177.  
  178. # can play a red 2 on a black King (to another
  179. # player's discard pile; NON-FINAL
  180. (VALID_NONFINAL_PLAY, '2H', ['KS'], 2, 3),
  181.  
  182. # can (illegally) play 2 on Q (from hand to another player's
  183. # discard pile; FINAL)
  184. (VALID_FINAL_PLAY, '2H', ['QS'], 2, 3),
  185.  
  186. # can't play 2 on Q (from stockpile/build pile to another player's
  187. # discard pile; FINAL)
  188. (INVALID_PLAY, '2H', ['QS'], 2, 3, False),
  189.  
  190. # can (illegally) play red 4 on red 3 (from hand to another player's
  191. # discard pile; FINAL)
  192. (VALID_FINAL_PLAY, '4H', ['3H'], 2, 3),
  193.  
  194. # can't (illegally) play red 4 on red 3 (from source other
  195. # than hand to own discard pile; INVALID)
  196. (INVALID_PLAY, '4H', ['3H'], 2, 2, False),
  197.  
  198. )
  199.  
  200. for retval, *args in tests:
  201. if comp10001bo_match_discard(*args) == retval:
  202. result = "passed"
  203. else:
  204. result = "failed"
  205. print("Testing comp10001bo_match_discard{} ... {}".format(
  206. repr(tuple(args)), result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement