Card game 'war' dictionary issues

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. I am only a 7’th grader and dont expect you to read the whole thing. thanks!

ps: if I use this,:arrow_down:︎ ,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





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(' ')
    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```
1 Like

When unconverted_deck is a dict, oedokn is a list of the keys, i.e. the names of the cards, which you then shuffle.

2 cards will never compare equal because their names are unique - there’s only one ace of spades, for example.

thanks. are you saying that It will be hard, if not imposible, to add the card suites(spades, clubs…)? thank you for your advice, I will take it to heart. I am only in 7’th grade and this is new to me.bye!

No, I’m not saying that.

As I understand it, this line:

player_primary[play_card_index] > computer_primary[comp_card_index]

should compare the ranks of the cards.

If player_primary and computer_primary actually contain the names of the cards, then you’ll need to lookup their ranks:

unconverted_deck[player_primary[play_card_index]] > unconverted_deck[computer_primary[comp_card_index]]

and similarly for the other places.

ohh! Now I understand! Thank you for taking the time to help me out! I have been on 5 different forums and have not gotten any advice as good as that. I will try it out to the best of my abilites. Once again, thank you!

This appears not to have worked. When I come across an instance of war (the cards are equal), both of the cards are randomly given to either the player or computer. there is no pattern that I can detect. I think that mabey, it is comparing if all parts of the card is the same (including the keys). If that is the problem, what do you think I should do about it?

Check the logic. Add print statements to describe what it’s doing (what the values are, what decision it’s taking, etc). Actually, I’d write to a log file because that’s easier to read if there’s a lot of output.

Then, when it goes wrong, use the log file to follow the program’s logic.

To save time, you could skip bit where it shuffles and fix the cards that are given to the player and the computer to test specific paths through the program.

ok. thanks again, and I will make a note, but I am still young and I was up till 3 in the morning trying out various different ways to solve many different problems. I will do that first thing in the morning, but now, all I want to do is sleep. see you next time :+1: :sleeping:

I’m a little lost as to what problems you’re having and your code quite long, so it would take me more time than I have, to nail it down.

I’ve written a Blackjack app and as such, I may be able to help you with some aspect of your code; lmk if you think I can help.

most of the problems I have are just cosmetics, and I am also trying to do a seperate project that is really more of a exersize so that I remember some ways to check my code and try out new things. I also have to do homework for school, and other extracurricular activities. the only real problem is that the dictionary does not reconize that the cards are equal.
thanks for helping me out!

You are very welcome.

A dictionary will only (so far as I know) store data; it cares not what the data is or if the data is the same or not (keys aside, which have to be unique). So I’m not understanding what you mean by “the dictionary does not reconize that the cards are equal”: well, no it won’t and it’s not designed to (so far as I know).

Maybe a simple example of what you’re trying to do, what you expect, and what you get, would help me to help you.


to add…

I’ve had a quick look at your code: you don’t need random.shuffle() to pick a ‘random’ card

As an example:

from random import choice

rank = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suit = ['S', 'C', 'D', 'H']

picked = []
cards = []

# pick two cards
for pick in range(2):
    c_rank = choice(rank)
    c_suit = choice(suit)
    picked.append((c_rank,c_suit))

for hand in picked:
    for card in hand:
        cards.append(card)
        if len(cards) > 3:
            if cards[1] == cards[3]:
                print(f"The cards are from the same suit: {cards[1]} and {cards[3]}")
            else:
                print(f"The cards are from different suits: {cards[1]} and {cards[3]}")
            if cards[0] == cards[2]:
                print(f"The cards are the same value: {cards[0]} and {cards[2]}")
            else:
                print(f"The cards are a different value: {cards[0]} and {cards[2]}")
            if cards[1] == cards[3] and cards[0] == cards[2]:
                print(f"The cards are identical: {cards}")

Now, you won’t have to run that too many times before you get two cards that are identical, but it is as 'random` as it can be, so there’s that.

Not sure if this is of any help, but it may be.


to add something else…

Why not do the symbols, on the fly?

Again, as an example:

from random import choice

suits = {
    'S' : '\u2660',
    'C' : '\u2663',
    'D' : '\u2666',
    'H' : '\u2665'
}

rank = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suit = ['S', 'C', 'D', 'H']

# pick a card and display it
c_rank = choice(rank)
c_suit = choice(suit)

print(f"{c_rank} {suits[c_suit]}")