Reputation: 61
I am working on a card war game, i tried to assign the number values to the letters on the deck (A-K-Q-J) and other numbers through a dictionary. but when i run the code sometimes an A loses to other inferior cards or a 10 loses to a 2. Is this the proper way to assign a value to the letters and numbers, or just assign it through a variable (A=14) at the beginning of the program.
cardsDict = {"A":14,"K":13,"Q":12,"J":11,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10}
from random import shuffle
Deck = []
FaceValues = ["A", "J", "Q", "K"]
def createDeck():
for i in range(4):
for card in range(2,11):
Deck.append(str(card))
for card in FaceValues:
Deck.append(card)
shuffle(Deck)
return Deck
deck = createDeck()
def dealDeck():
global hand1
global hand2
hand1 = Deck[:26]
hand2 = Deck[26:]
return hand1, hand2
hands = dealDeck()
def PlayWar(hand1, hand2):
round = 1
Player1 = input("Please enter your name: ")
Player2 = input("Please enter your name: ")
while len(hand1) > 0 and len(hand2) > 0:
Pause = input("Are you ready for the next round? if you are type yes, if you want to see the rules type no: ")
if Pause == "no":
print("Rules are:" + "\n" + "1. Deal out deck of 52 cards between two users." + "\n" + "2. Each player plays a card. Higher card wins. Winner takes both cards." "\n" "3. If players tie, then each player puts down three cards, and the third card competes. If a player has less than 3 cards, then they put down all of their cards and their final card competes against the other player's third card. Continue doing this until tie is broken. Winner takes all cards." + "\n" "4. Game is over when a player doesn't have any cards. The player with cards remaining is the winner.")
player1Card = hand1[0]
player2Card = hand2[0]
print("round:", round)
print(player1Card)
print(player2Card)
if player1Card > player2Card:
hand1.pop(0)
hand2.pop(0)
hand1.append(player1Card)
hand1.append(player2Card)
print(Player1 + " wins round " + str(round))
elif player2Card > player1Card:
hand1.pop(0)
hand2.pop(0)
hand2.append(player1Card)
hand2.append(player2Card)
print(Player2 + " wins round " + str(round))
elif player2Card == player1Card:
print("DRAW")
if len(hand1) < 4:
hand1 = []
print(Player1 + " looks you cant match the draw, you lost")
break
if len(hand2) < 4:
hand2 = []
print(Player2 + " looks you cant match the draw, you lost")
break
stash1 = hand1[0:4]
stash2 = hand2[0:4]
print(stash1)
print(stash2)
if hand1[4] > hand2[4]:
for card in hand1[0:4]:
hand1.remove(card)
for card in hand2[0:4]:
hand2.remove(card)
for card in stash1 + stash2:
hand1.append(card)
print(Player1 + " wins round: " + str(round))
elif hand2[4] > hand1[4]:
for card in hand1[0:4]:
hand1.remove(card)
for card in hand2[0:4]:
hand2.remove(card)
for card in stash1 + stash2:
hand2.append(card)
print(Player2 + " wins round: " + str(round))
elif hand1[4] == hand2[4]:
print("SUPERDRAW")
if len(hand1) < 8:
hand1 = []
print(Player1 + " looks you can not match the superdraw, you lost!!!")
break
if len(hand2) < 8:
hand2 = []
print(Player2 + " looks you can not match the superdraw, you lost!!!")
break
stash1 = hand1[0:8]
stash2 = hand2[0:8]
print(stash1)
print(stash2)
if hand1[8] > hand2[8]:
for card in hand1[0:8]:
hand1.remove(card)
for card in hand2[0:8]:
hand2.remove(card)
for card in stash1 + stash2:
hand1.append(card)
print(Player1 + " wins round: " + str(round))
elif hand2[8] > hand1[8]:
for card in hand1[0:8]:
hand1.remove(card)
for card in hand2[0:8]:
hand2.remove(card)
for card in stash1 + stash2:
hand2.append(card)
print(Player2 + " wins round: " + str(round))
if hand1 == []:
print("Congratulations " + Player2 + " , you won")
if hand2 == []:
print("Congratulations " + Player1 + " , you won")
round += 1
PlayWar(hand1, hand2)
Upvotes: 1
Views: 68
Reputation: 26
You aren't referencing your cardDict when seeing which card is greater.
Inputting the top line of this code fixed the issue when. I tested it out.
if cardsDict[player1Card] > cardsDict[player2Card]:
hand1.pop(0)
hand2.pop(0)
hand1.append(player1Card)
hand1.append(player2Card)
print(Player1 + " wins round " + str(round))
Upvotes: 1
Reputation: 31
cardsDict = {"A": 14, "K": 13, "Q": 12, "J": 11, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9,
"10": 10}
from random import shuffle
Deck = []
FaceValues = ["A", "J", "Q", "K"]
def createDeck():
for i in range(4):
for card in range(2, 11):
Deck.append(str(card))
for card in FaceValues:
Deck.append(card)
shuffle(Deck)
return Deck
deck = createDeck()
def dealDeck():
global hand1
global hand2
hand1 = Deck[:26]
hand2 = Deck[26:]
return hand1, hand2
hands = dealDeck()
def PlayWar(hand1, hand2):
round = 1
Player1 = input("Please enter your name: ")
Player2 = input("Please enter your name: ")
while len(hand1) > 0 and len(hand2) > 0:
Pause = input("Are you ready for the next round? if you are type yes, if you want to see the rules type no: ")
if Pause == "no":
print(
"Rules are:" + "\n" + "1. Deal out deck of 52 cards between two users." + "\n" + "2. Each player plays a card. Higher card wins. Winner takes both cards." "\n" "3. If players tie, then each player puts down three cards, and the third card competes. If a player has less than 3 cards, then they put down all of their cards and their final card competes against the other player's third card. Continue doing this until tie is broken. Winner takes all cards." + "\n" "4. Game is over when a player doesn't have any cards. The player with cards remaining is the winner.")
player1Card = hand1[0]
player2Card = hand2[0]
print("round:", round)
player1CardVal = cardsDict[player1Card]
player2CardVal = cardsDict[player2Card]
print("Player1 Card:" + player1Card + ",Card Value:" + str(player1CardVal))
print("Player2 Card:" + player2Card + ",Card Value:" + str(player2CardVal))
if player1CardVal > player2CardVal:
hand1.pop(0)
hand2.pop(0)
hand1.append(player1Card)
hand1.append(player2Card)
print(Player1 + " wins round " + str(round))
elif player2CardVal > player1CardVal:
hand1.pop(0)
hand2.pop(0)
hand2.append(player1Card)
hand2.append(player2Card)
print(Player2 + " wins round " + str(round))
elif player2CardVal == player1CardVal:
print("DRAW")
if len(hand1) < 4:
hand1 = []
print(Player1 + " looks you cant match the draw, you lost")
break
if len(hand2) < 4:
hand2 = []
print(Player2 + " looks you cant match the draw, you lost")
break
stash1 = hand1[0:4]
stash2 = hand2[0:4]
print(stash1)
print(stash2)
if hand1[4] > hand2[4]:
for card in hand1[0:4]:
hand1.remove(card)
for card in hand2[0:4]:
hand2.remove(card)
for card in stash1 + stash2:
hand1.append(card)
print(Player1 + " wins round: " + str(round))
elif hand2[4] > hand1[4]:
for card in hand1[0:4]:
hand1.remove(card)
for card in hand2[0:4]:
hand2.remove(card)
for card in stash1 + stash2:
hand2.append(card)
print(Player2 + " wins round: " + str(round))
elif hand1[4] == hand2[4]:
print("SUPERDRAW")
if len(hand1) < 8:
hand1 = []
print(Player1 + " looks you can not match the superdraw, you lost!!!")
break
if len(hand2) < 8:
hand2 = []
print(Player2 + " looks you can not match the superdraw, you lost!!!")
break
stash1 = hand1[0:8]
stash2 = hand2[0:8]
print(stash1)
print(stash2)
if hand1[8] > hand2[8]:
for card in hand1[0:8]:
hand1.remove(card)
for card in hand2[0:8]:
hand2.remove(card)
for card in stash1 + stash2:
hand1.append(card)
print(Player1 + " wins round: " + str(round))
elif hand2[8] > hand1[8]:
for card in hand1[0:8]:
hand1.remove(card)
for card in hand2[0:8]:
hand2.remove(card)
for card in stash1 + stash2:
hand2.append(card)
print(Player2 + " wins round: " + str(round))
if hand1 == []:
print("Congratulations " + Player2 + " , you won")
if hand2 == []:
print("Congratulations " + Player1 + " , you won")
round += 1
PlayWar(hand1, hand2)
Upvotes: 1