fork download
  1. class War():
  2. def __init__(self):
  3. # player decks
  4. self.p1 = []
  5. self.p2 = []
  6.  
  7. # Challenge Inputs
  8. input1a = "5 1 13 10 11 3 2 10 4 12 5 11 10 5 7 6 6 11 9 6 33 13 6 1 8 1"
  9. input1b = "9 12 8 3 11 10 1 4 2 4 7 9 13 8 2 13 7 4 2 8 9 12 3 12 7 5"
  10. self.input1a = [int(i) for i in input1a.split(' ')]
  11. self.input1b = [int(i) for i in input1b.split(' ')]
  12.  
  13. input2a = "3 11 6 12 2 13 5 7 10 3 10 4 12 11 1 13 12 2 1 7 10 6 12 5 8 1"
  14. input2b = "9 10 7 9 5 2 6 1 11 11 7 9 3 4 8 3 4 8 8 4 6 9 13 2 13 5"
  15. self.input2a = [int(i) for i in input2a.split(' ')]
  16. self.input2b = [int(i) for i in input2b.split(' ')]
  17.  
  18. input3 = "1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13"
  19. self.input3 = [int(i) for i in input3.split(' ')]
  20.  
  21. def compare(self, card1, card2):
  22. #print("Compare: ", card1, ", ", card2)
  23. if card1 > card2: # player1 wins
  24. self.p1 = self.p1 + [card1, card2]
  25. return 1
  26. elif card2 > card1: #player2 wins
  27. self.p2 = self.p2 + [card2, card1]
  28. return 2
  29. else: # draw
  30. return(self.war(card1, card2))
  31.  
  32. def play(self):
  33. while self.p1 and self.p2:
  34. card1 = self.p1.pop(0)
  35. card2 = self.p2.pop(0)
  36. self.compare(card1, card2)
  37.  
  38. if not self.p1 and self.p2:
  39. return 2 # p2 wins
  40. elif not self.p2 and self.p1:
  41. return 1 # p1 wins
  42. else:
  43. return 0 # no one wins
  44.  
  45. def war(self, wp1, wp2):
  46. # determine size of war
  47. warNum = 3
  48. if len(self.p1) > 0 and len(self.p2) > 0: # incase player out of cards (i.e. someone lost)
  49. if len(self.p1) < 4 or len(self.p2) < 4: # incase player has fewer cards than necessary
  50. warNum = min(len(self.p1), len(self.p2)) - 1
  51. else: # if somebody is out of cards at this stage they lose
  52. if len(self.p1) != 0:
  53. return 1
  54. elif len(self.p2) != 0:
  55. return 2
  56. else:
  57. return 0
  58.  
  59. # format card1 and card2 into list, where
  60. # wp1 and wp2 are the war 'decks'
  61. if isinstance(wp1, int) and isinstance(wp2, int):
  62. wp1 = [wp1]
  63. wp2 = [wp2]
  64.  
  65. # draw wager
  66. for i in range(warNum - 1):
  67. wp1.insert(0, self.p1.pop(0))
  68. wp2.insert(0, self.p2.pop(0))
  69.  
  70. # determine who wins war
  71. card1 = self.p1.pop(0)
  72. card2 = self.p2.pop(0)
  73. result = self.compare(card1, card2)
  74.  
  75. # winner take all
  76. if result == 1:
  77. self.p1 += [card1, card2]
  78. self.p1 += [card for tupl in zip(wp1, wp2) for card in tupl]
  79. return 1
  80.  
  81. elif result == 2:
  82. self.p2 += [card2, card1]
  83. self.p2 += [card for tupl in zip(wp2, wp1) for card in tupl]
  84. return 2
  85.  
  86. else: # draw state
  87. return 0
  88.  
  89. def runChallenge(self):
  90. print("Challenge 1:")
  91. self.p1 = self.input1a
  92. self.p2 = self.input1b
  93. print(self.play())
  94.  
  95. self.p1 = self.input2a
  96. self.p2 = self.input2b
  97. print(self.play())
  98.  
  99. self.p1 = self.input3
  100. self.p2 = self.input3[:]
  101. print(self.play())
  102. w = War()
  103. w.runChallenge()
Success #stdin #stdout 0.01s 28384KB
stdin
Standard input is empty
stdout
Challenge 1:
1
2
0