Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python card game 'war' dictionary problems

computer

Active Coder
I am a kid that is taking videos on how to code in python. I am making the card game war as a project and have been stuck on a problem for weeks. if i use a dictionary to show the card suits, or the face cards, it ignores if the cards are eaqual. i dont know where the problem is, so i'll post the entire thing. also, I am working in pycharm.

ps: if I use this,⬇︎ ,instead of the dictionary, the code runs without a flaw.
↳ unconverted_deck =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,1,2,3,4,5,6,7,8,9,10,11,12,13,14,1,2,3,4,5,6,7,8,9,10,11,12,13,14,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
here




Python:
import random
import time
test_list2 = []
test_list1 = []

unconverted_deck = {'2❤️' : 2,
                    '3❤️' : 3,
                    '4❤️' : 4,
                    '5❤️' : 5,
                    '6❤️' : 6,
                    '7❤️' : 7,
                    '8❤️' : 8,
                    '9❤️' : 9,
                    '10❤️' : 10,
                    'J❤️' : 11,
                    'Q❤️' : 12,
                    'K❤️' : 13,
                    'A❤️' : 15,
                    '2♠️' : 2,
                    '3♠️': 3,
                    '4♠️': 4,
                    '5♠️': 5,
                    '6♠️': 6,
                    '7♠️': 7,
                    '8♠️': 8,
                    '9♠️': 9,
                    '10♠️': 10,
                    'J♠️': 11,
                    'Q♠️': 12,
                    'K♠️️': 13,
                    'A♠️️': 15,
                    '2♦️': 2,
                    '3♦️': 3,
                    '4♦️': 4,
                    '5♦️': 5,
                    '6♦️': 6,
                    '7♦️': 7,
                    '8♦️': 8,
                    '9♦️': 9,
                    '10♦️': 10,
                    'J♦️': 11,
                    'Q♦️': 12,
                    'K♦️': 13,
                    'A♦️': 15,
                    '2︎♣️': 2,
                    '3♣️': 3,
                    '4♣️': 4,
                    '5♣️': 5,
                    '6♣️': 6,
                    '7♣️': 7,
                    '8♣️': 8,
                    '9♣️': 9,
                    '10♣️': 10,
                    'J♣️': 11,
                    'Q♣️': 12,
                    'K♣️': 13,
                    'A♣️': 15}
oedokn = list(unconverted_deck)
random.shuffle(oedokn)

computer_primary = oedokn[1::2]
player_primary = oedokn[0::2]
#random.shuffle(player_primary)
#random.shuffle(computer_primary)
player_secondary = []
computer_secondary = []
turns = 5
play_card_index = 0
# ---------------------------------
print(player_primary)
print(computer_primary)
comp_card_index = 0
play_war_at_risk = []
comp_war_at_risk = []

while winner := ' ':
    play_win_round = False
    comp_win_round = False
    play_len = len(player_primary) + len(player_secondary)
    comp_len = len(computer_primary) + len(computer_secondary)
    #print('play len = ' + str(play_len))
    #print('comp len = ' + str(comp_len))
    print(' ')
    print(' ')
    ready = input("Ready?:  ")
    try:
        if ready.lower() == "":
            print("your card is: " + str(player_primary[play_card_index]))
            print("your opponent's is: " + str(computer_primary[comp_card_index]))
    # ------------------------------------
            if player_primary[play_card_index] > computer_primary[comp_card_index]:
                player_secondary.append(player_primary[play_card_index])
                player_secondary.append(computer_primary[comp_card_index])
                player_primary.remove(player_primary[play_card_index])
                computer_primary.remove(computer_primary[comp_card_index])
                print('player discard: '+str(player_secondary))
                play_win_round = True
                print('computer discard: [?,?,?...]')
            elif player_primary[play_card_index] < computer_primary[comp_card_index]:
                computer_secondary.append(player_primary[play_card_index])
                computer_secondary.append(computer_primary[comp_card_index])
                player_primary.remove(player_primary[play_card_index])
                computer_primary.remove(computer_primary[comp_card_index])
                print('player discard: '+str(player_secondary))
                print('computer discard: [?,?,?...]')
                comp_win_round = True
            elif play_win_round or comp_win_round == False:
                while len(comp_war_at_risk) < 4:
                    play_war_at_risk.append(player_primary[play_card_index])
                    player_primary.remove(player_primary[play_card_index])
                    comp_war_at_risk.append(computer_primary[comp_card_index])
                    computer_primary.remove(computer_primary[comp_card_index])
                # ------------------------------------
                print('player cards at risk: '+str(play_war_at_risk))
                print('computer cards at risk: [?,?,?...]'
                print(' ')
                input("⚔️ Ready for war⚔️?:  ")
                print(' ')
                print("your card is: " + str(player_primary[play_card_index]))
                print("your opponent's is: " + str(computer_primary[comp_card_index]))
                # ------------------------------------
                if player_primary[play_card_index] > computer_primary[comp_card_index]:
                    player_secondary.append(player_primary[play_card_index])
                    player_secondary.append(computer_primary[comp_card_index])
                    player_primary.remove(player_primary[play_card_index])

                    # ------------------------------------------
                    player_secondary.append(play_war_at_risk[0])
                    player_secondary.append(play_war_at_risk[1])
                    player_secondary.append(play_war_at_risk[2])
                    player_secondary.append(play_war_at_risk[3])
                    # -------------------------------------------
                    player_secondary.append(comp_war_at_risk[0])
                    player_secondary.append(comp_war_at_risk[1])
                    player_secondary.append(comp_war_at_risk[2])
                    player_secondary.append(comp_war_at_risk[3])
                    # -------------------------------------------
                    play_war_at_risk.clear()
                    comp_war_at_risk.clear()
                    # -------------------------------------------
                    computer_primary.remove(computer_primary[comp_card_index])
                    print('player discard: ' + str(player_secondary))
                    print('computer discard: [?,?,?...]')
                    # -------------------------------------------
                elif player_primary[play_card_index] < computer_primary[comp_card_index]:
                    computer_secondary.append(player_primary[play_card_index])
                    computer_secondary.append(computer_primary[comp_card_index])
                    player_primary.remove(player_primary[play_card_index])
                    computer_primary.remove(computer_primary[comp_card_index])
                    # --------------------------------------------
                    computer_secondary.append(play_war_at_risk[0])
                    computer_secondary.append(play_war_at_risk[1])
                    computer_secondary.append(play_war_at_risk[2])
                    computer_secondary.append(play_war_at_risk[3])
                    # -------------------------------------------
                    computer_secondary.append(comp_war_at_risk[0])
                    computer_secondary.append(comp_war_at_risk[1])
                    computer_secondary.append(comp_war_at_risk[2])
                    computer_secondary.append(comp_war_at_risk[3])
                    # -------------------------------------------
                    play_war_at_risk.clear()
                    comp_war_at_risk.clear()
                    print('player discard: ' + str(player_secondary))
                    print('computer discard: [?,?,?...]')
                elif player_primary[play_card_index] == computer_primary[comp_card_index]:
                    while len(comp_war_at_risk) < 4:
                        play_war_at_risk.append(player_primary[play_card_index])
                        player_primary.remove(player_primary[play_card_index])
                        comp_war_at_risk.append(computer_primary[comp_card_index])
                        computer_primary.remove(computer_primary[comp_card_index])
                    print(play_war_at_risk)
                    print(comp_war_at_risk)
                    print('your at risk = ' + str(play_war_at_risk))
                    print('COMP at risk = '+ str(comp_war_at_risk))
                    print(' ')
                    print(' ')
                    ready = input("⚔️ Ready for war⚔️?:  ")
    except IndexError:
        for u in computer_secondary:
            computer_primary.append(u)
            random.shuffle(computer_primary)
        for i in player_secondary:
            player_primary.append(i)
            random.shuffle(player_primary)
        computer_secondary.clear()
        player_secondary.clear()
        print('    new round ')
        print('    good luck')
        print('    👍🏻👍🏻👍🏻👍🏻👍🏻')
        print('    🤘🏻😁🤘🏻😁🤘🏻 ')
        print(' ')
        print(' ')
        print(' ')
        print(' ')
        print(' ')
        turns=turns+1
    if play_len == 0:
        for e in player_secondary:
            player_primary.append(e)
        print('end of the game!')
        print('the winner is...')
        print('💣🧨💣🧨')
        print(5)
        time.sleep(1)
        print(4)
        time.sleep(1)
        print(3)
        time.sleep(1)
        print(2)
        time.sleep(1)
        print(1)
        time.sleep(1)
        print('💥💥 you 💥💥')
        break
    elif comp_len == 0:
        for e in computer_secondary:
            computer_primary.append(e)
        print('end of the game!')
        print('the winner is...')
        print('💣🧨💣🧨')
        print(5)
        time.sleep(1)
        print(4)
        time.sleep(1)
        print(3)
        time.sleep(1)
        print(2)
        time.sleep(1)
        print(1)
        time.sleep(1)
        print('💥💥 computer 💥💥')
        break
 
Solution
Here is a working example of using classes. Not all the Player attributes are used. They are just for display examples.

Python:
# Do the imports
import random as rnd
from tabulate import tabulate
from sys import platform
import subprocess
from time import sleep
def clear():
    subprocess.call('cls' if platform == 'win32' else 'clear')

class Player:
    ''' Create the player class
        Set some class variables
        Return a string representation
    '''
    def __init__(self, player):
        self.player = player
        self.card = None
        self.cards = 0
        self.win = 0
        self.loss = 0
    def __repr__(self):
        return f'{self.player}: Cards:{self.cards} wins: {self.win} loss: {self.loss}'

class Card:
    '''...

Buy us a coffee!

Back
Top Bottom