fork download
  1. class Player(object):
  2. def __init__(self, cardString):
  3. # Splits the deck string into a list
  4. self.deckList = [int(n) for n in cardString.split()]
  5. self.collection = []
  6.  
  7. def draw(self):
  8. # Returns the first card of the deck while also removing it
  9. return self.deckList.pop(0)
  10.  
  11. def addToCollection(self, cardStack):
  12. # Collection is a form of staging a deck until we're ready to add it to the bottom of the deck
  13. self.collection.extend(cardStack)
  14.  
  15. def collect(self):
  16. self.deckList.extend(self.collection)
  17. self.collection = []
  18.  
  19. def cardsRemaining(self):
  20. return len(self.deckList)
  21.  
  22. # Used to grab the next 1-4 cards when performing a war
  23. def split(self, amount):
  24. deckCopy = self.deckList
  25. self.deckList = deckCopy[amount:]
  26.  
  27. return deckCopy[:amount]
  28.  
  29. def main(deckOne, deckTwo):
  30. playerOne = Player(deckOne)
  31. playerTwo = Player(deckTwo)
  32.  
  33. while (True):
  34. battle(playerOne, playerTwo)
  35.  
  36. if (playerOne.cardsRemaining() == 0 and playerTwo.cardsRemaining() == 0):
  37. print('0')
  38. break
  39. elif(playerOne.cardsRemaining() == 0):
  40. print('2')
  41. break
  42. elif(playerTwo.cardsRemaining() == 0):
  43. print('1')
  44. break
  45.  
  46. def battle(playerOne, playerTwo):
  47. victor = None
  48.  
  49. if (playerOne.deckList[0] > playerTwo.deckList[0]):
  50. playerOne.addToCollection([playerOne.draw(), playerTwo.draw()])
  51. victor = playerOne
  52. elif(playerTwo.deckList[0] > playerOne.deckList[0]):
  53. playerTwo.addToCollection([playerTwo.draw(), playerOne.draw()])
  54. victor = playerTwo
  55. else:
  56. # Upon a tie, we draw the two cards we were comparing and hold onto them for the time being
  57. # We then start a war
  58. tieDeck = [playerOne.draw(), playerTwo.draw()]
  59. victor = war(playerOne, playerTwo)
  60.  
  61. # If there wasn't a victor in the war, we simply return
  62. if (victor == None):
  63. return
  64.  
  65. # If there is a victor, we add to the cards we were sitting onto to the victor
  66. victor.addToCollection(tieDeck)
  67.  
  68. # Moves the victor's collection of cards to the bottom of their deck
  69. victor.collect()
  70.  
  71. def war(playerOne, playerTwo):
  72. index = None
  73.  
  74. # We iterate to see how many cards can we pull from both deck, once we find the amount, we break
  75. for x in range(4,0,-1):
  76. if (playerOne.cardsRemaining() >= x and playerTwo.cardsRemaining() >= x):
  77. index = x
  78. break;
  79.  
  80.  
  81. # If index wasn't defined, that means that either one or both of the players don't have enough cards
  82. if (index == None):
  83. # If both don't have enough, we return None to signify no victor
  84. if (playerOne.cardsRemaining() == 0 and playerTwo.cardsRemaining() == 0):
  85. return None
  86. # Otherwise we return the winner
  87. return playerTwo if (playerOne.cardsRemaining() == 0) else playerOne
  88.  
  89. # We split the most cards we can from each deck and sit on them for now
  90. playerOneSplit = playerOne.split(index)
  91. playerTwoSplit = playerTwo.split(index)
  92.  
  93. # We assign the cards we'll be checking for the war
  94. playerOneDrawnCard = playerOneSplit[len(playerOneSplit) - 1]
  95. playerTwoDrawnCard = playerTwoSplit[len(playerTwoSplit) - 1]
  96.  
  97. victor = None
  98.  
  99. # If the new face up cards still result in a tie, we recursively start a new war
  100. if (playerOneDrawnCard == playerTwoDrawnCard):
  101. victor = war(playerOne, playerTwo)
  102. else:
  103. # Otherwise, we decide the victor
  104. victor = playerOne if playerOneDrawnCard > playerTwoDrawnCard else playerTwo
  105.  
  106. # If we recursively retrieve a None as the victor, we continue the return
  107. if (victor == None):
  108. return None
  109. elif (victor == playerOne):
  110. playerOne.addToCollection(playerOneSplit + playerTwoSplit)
  111. else:
  112. playerTwo.addToCollection(playerTwoSplit + playerOneSplit)
  113.  
  114. return victor
  115.  
  116. if __name__ == "__main__":
  117. # Challlenge 1
  118. deckOne = "5 1 13 10 11 3 2 10 4 12 5 11 10 5 7 6 6 11 9 6 3 13 6 1 8 1"
  119. deckTwo = "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"
  120. print("\nChallenge One")
  121. print("-------------")
  122. main(deckOne, deckTwo)
  123.  
  124. print("\nChallenge Two")
  125. print("-------------")
  126. # Challlenge 2
  127. deckOne = "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"
  128. deckTwo = "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"
  129. main(deckOne, deckTwo)
  130.  
  131. print("\nChallenge Three")
  132. print("-------------")
  133. # Challlenge 3
  134. deckOne = "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"
  135. deckTwo = "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"
  136. main(deckOne, deckTwo)
  137.  
Success #stdin #stdout 0.02s 28384KB
stdin
Standard input is empty
stdout
Challenge One
-------------
2

Challenge Two
-------------
2

Challenge Three
-------------
0