Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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']
- 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']
- card_order = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
- score_dictionary = {'player1': 0, 'player2': 0}
- def find_winner(deck1, deck2, winning_order, score_dict):
- flag = True
- try: # if both players still have cards to play
- if winning_order.index(deck1[0]) < winning_order.index(deck2[0]): #battle
- deck2.append(deck1.pop(0))
- deck2.append(deck2.pop(0))
- score_dict['player2'] += 1
- elif winning_order.index(deck1[0]) > winning_order.index(deck2[0]): #battle
- deck1.append(deck1.pop(0))
- deck1.append(deck2.pop(0))
- score_dict['player1'] += 1
- elif winning_order.index(deck1[0]) == winning_order.index(deck2[0]): #war
- # Each player ses aside 3 cards as a deck of war
- # Both players go back to battle mode using the 4th card to decide who wins the war
- # the first person wins the battle wins the war
- # the winner takes all the cards on the table
- # wars can be chained if the next battle cant decide who wins
- flag2 = True
- # Initialise war decks as LISTS: cards set aside during "war" mode
- war_deck1 = [deck1.pop(0)]
- war_deck2 = [deck2.pop(0)]
- while flag2 is True:
- try: # both players have enough cards to play war
- for i in range(3): # Set aside the next 3 cards
- war_deck1.append(deck1.pop(0))
- war_deck2.append(deck2.pop(0))
- if winning_order.index(deck1[0]) < winning_order.index(deck2[0]): #assessing the 4th card
- score_dict['player2'] += 1 # player2 wins
- # add to deck2 the war decks and the cards from winning the battle
- deck2.extend(war_deck1)
- deck2.append(deck1.pop(0))
- deck2.extend(war_deck2)
- deck2.append(deck2.pop(0))
- flag2 = False
- elif winning_order.index(deck1[0]) > winning_order.index(deck2[0]):
- score_dict['player1'] += 1 # player1 wins
- # add to deck1 the war decks and the cards from winning the battle
- deck1.extend(war_deck1)
- deck1.append(deck1.pop(0))
- deck1.extend(war_deck2)
- deck1.append(deck2.pop(0))
- flag2 = False
- else: # winning_order.index(deck1[0]) > winning_order.index(deck2[0]): wars chained
- war_deck1.append(deck1.pop(0))
- war_deck2.append(deck2.pop(0))
- except IndexError: # one of the players runs out of card and game ends with both players in the first place
- flag = False
- score_dict = 'PAT'
- return flag, score_dict
- except IndexError: # one of the player runs out of card to play -> the other one wins
- flag = False
- return flag, score_dict
- return flag, score_dict
- flag_outside = True
- while flag_outside is True:
- flag_outside, score_dictionary = find_winner(deck_player1, deck_player2, card_order, score_dictionary)
- if score_dictionary != 'PAT':
- if score_dictionary['player1'] > score_dictionary['player2']:
- game_rounds = score_dictionary['player1'] + score_dictionary['player2']
- print('1', game_rounds)
- elif score_dictionary['player2'] > score_dictionary['player1']:
- game_rounds = score_dictionary['player1'] + score_dictionary['player2']
- print('2', game_rounds)
- else:
- print('PAT')
- else:
- print('PAT')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement