Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. deck_player1 = ['A', '4', '5', '6', 'Q', 'J', '8', '2', '7', 'J', 'J', '6', 'K', 'Q', '9', '2', '5', '9', '6', '8', 'A', '4', '2', '2', '7', '8']
  2. deck_player2 = ['10', '4', '6', '3', 'K', 'J', '10', 'A', '5', 'K', '10', '9', '9', '8', '5', 'A', '3', '4', 'K', '7', '3', 'Q', '10', '3', '7', 'Q']
  3.  
  4.  
  5. card_order = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
  6. score_dictionary = {'player1': 0, 'player2': 0}
  7.  
  8.  
  9. def find_winner(deck1, deck2, winning_order, score_dict):
  10.     flag = True
  11.     try:    # if both players still have cards to play
  12.         if winning_order.index(deck1[0]) < winning_order.index(deck2[0]): #battle
  13.             deck2.append(deck1.pop(0))
  14.             deck2.append(deck2.pop(0))
  15.             score_dict['player2'] += 1
  16.  
  17.         elif winning_order.index(deck1[0]) > winning_order.index(deck2[0]): #battle
  18.             deck1.append(deck1.pop(0))
  19.             deck1.append(deck2.pop(0))
  20.             score_dict['player1'] += 1
  21.  
  22.         elif winning_order.index(deck1[0]) == winning_order.index(deck2[0]):   #war
  23.             # Each player ses aside 3 cards as a deck of war
  24.             # Both players go back to battle mode using the 4th card to decide who wins the war
  25.             # the first person wins the battle wins the war
  26.             # the winner takes all the cards on the table
  27.             # wars can be chained if the next battle cant decide who wins
  28.             flag2 = True
  29.             # Initialise war decks as LISTS: cards set aside during "war" mode
  30.             war_deck1 = [deck1.pop(0)]
  31.             war_deck2 = [deck2.pop(0)]
  32.             while flag2 is True:
  33.                 try:    # both players have enough cards to play war
  34.                     for i in range(3):  # Set aside the next 3 cards
  35.                         war_deck1.append(deck1.pop(0))
  36.                         war_deck2.append(deck2.pop(0))
  37.                     if winning_order.index(deck1[0]) < winning_order.index(deck2[0]):   #assessing the 4th card
  38.                         score_dict['player2'] += 1  # player2 wins
  39.                         # add to deck2 the war decks and the cards from winning the battle
  40.                         deck2.extend(war_deck1)
  41.                         deck2.append(deck1.pop(0))
  42.                         deck2.extend(war_deck2)
  43.                         deck2.append(deck2.pop(0))
  44.                         flag2 = False
  45.                     elif winning_order.index(deck1[0]) > winning_order.index(deck2[0]):
  46.                         score_dict['player1'] += 1  # player1 wins
  47.                         # add to deck1 the war decks and the cards from winning the battle
  48.                         deck1.extend(war_deck1)
  49.                         deck1.append(deck1.pop(0))
  50.                         deck1.extend(war_deck2)
  51.                         deck1.append(deck2.pop(0))
  52.                         flag2 = False
  53.                     else:   # winning_order.index(deck1[0]) > winning_order.index(deck2[0]): wars chained
  54.                         war_deck1.append(deck1.pop(0))
  55.                         war_deck2.append(deck2.pop(0))
  56.  
  57.                 except IndexError:  # one of the players runs out of card and game ends with both players in the first place
  58.                     flag = False
  59.                     score_dict = 'PAT'
  60.                     return flag, score_dict
  61.  
  62.     except IndexError:   # one of the player runs out of card to play -> the other one wins
  63.         flag = False
  64.         return flag, score_dict
  65.  
  66.     return flag, score_dict
  67.  
  68.  
  69. flag_outside = True
  70. while flag_outside is True:
  71.     flag_outside, score_dictionary = find_winner(deck_player1, deck_player2, card_order, score_dictionary)
  72.  
  73. if score_dictionary != 'PAT':
  74.     if score_dictionary['player1'] > score_dictionary['player2']:
  75.         game_rounds = score_dictionary['player1'] + score_dictionary['player2']
  76.         print('1', game_rounds)
  77.     elif score_dictionary['player2'] > score_dictionary['player1']:
  78.         game_rounds = score_dictionary['player1'] + score_dictionary['player2']
  79.         print('2', game_rounds)
  80.     else:
  81.         print('PAT')
  82. else:
  83.     print('PAT')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement