Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
War game
#1
Trying to finish my game of war card game. I am currently on the final step and need to have it keep score.

Can someone please guide me on how to compare the position of 2 items from the same list? The further the item is to the left of the list or 0 is the one that gets the point. (or should at least).

Here is the code I currently have.

import random

def main():

    totalCards = 52
    playerOne = 0
    playerTwo = 0
    pOneScore = 0
    pTwoScore = 0

    deck = []
    suit = []
    totalList = []

    deck = ["Ace","King","Queen","Jack",2,3,4,5,6,7,8,9,10]
    suit = ["of spades","of hearts","of clubs","of diamonds"]

    for num in deck:
        for suitType in suit:
            totalList.append(str(num) + ' ' + suitType)

    print("Welcome to war. Press any button to deal a hand.")

    while (totalCards > 0):
    
        input()
        print("Cards are being delt!")
        print()
        
        dealCard(totalList, playerOne, playerTwo)
        print()

        totalCards -= 2
        
        print(totalCards, "cards remain in the deck.")

        print("Player one's score: ", pOneScore)
        print("Player two's score: ", pTwoScore)

    print("The game of war has ended!")
    print("The final score is: ")

def dealCard(totalList, playerOne, playerTwo):

    playerOneSuit = ""
    playerTwoSuit = ""
    
    playerOne = random.choice(totalList)

    print("Player one: ",playerOne, playerOneSuit)
    totalList.remove(playerOne)
    
    playerTwo = random.choice(totalList)
    
    print("Player two: ",playerTwo, playerTwoSuit)
    totalList.remove(playerTwo)

    return totalList, playerOne, playerTwo

main()
Reply
#2
Write a compare function. Then input the two cards for the parameters. Write an if function either in compare or before executing it. The if statements will determine if it is one of the string values, and if so give it an int value. Ex.
if card1 == 'Ace':
    card1 = 1
elif card1 == 'Queen':
    card1 = 12
#etc.'
Reply
#3
Wouldn't that cause me to have a million if statements?

Isn't there a way I could compare the position in the list?

Ok so I tried making this using a bunch of if statements. For some reason it always does the first if statement. Anyone know why?

import random

def main():

    totalCards = 52
    playerOne = 0
    playerTwo = 0
    pOneScore = 0
    pTwoScore = 0

    deck = []
    suit = []
    totalList = []

    deck = ["Ace","King","Queen","Jack",2,3,4,5,6,7,8,9,10]
    suit = ["of spades","of hearts","of clubs","of diamonds"]

    for num in deck:
        for suitType in suit:
            totalList.append(str(num) + ' ' + suitType)

    print("Welcome to war. Press any button to deal a hand.")

    while (totalCards > 0):
        
        input()
        print("Cards are being delt!")
        print()
        
        dealCard(totalList, playerOne, playerTwo)
        print()

        score(playerOne, playerTwo, pOneScore, pTwoScore)

        totalCards -= 2
        
        print(totalCards, "cards remain in the deck.")

        print("Player one's score: ", pOneScore)
        print("Player two's score: ", pTwoScore)

    print("The game of war has ended!")
    print("The final score is: ")

def dealCard(totalList, playerOne, playerTwo):

    playerOneSuit = ""
    playerTwoSuit = ""
    
    playerOne = random.choice(totalList)

    print("Player one: ",playerOne, playerOneSuit)
    totalList.remove(playerOne)
    
    playerTwo = random.choice(totalList)
    
    print("Player two: ",playerTwo, playerTwoSuit)
    totalList.remove(playerTwo)

    return totalList, playerOne, playerTwo

def score(playerOne, playerTwo, pOneScore, pTwoScore):
    
    if playerOne == ("Ace of spades") or ("Ace of clubs") or ("Ace of hearts") or ("Ace of diamonds"):
        pOne = 1
    elif PlayerOne == ("King of spades") or ("King of clubs") or ("King of hearts") or ("King of diamonds"):
        pOne = 2
    elif PlayerOne == ("Queen of spades") or ("Queen of clubs") or ("Queen of hearts") or ("Queen of diamonds"):
        pOne = 3
    elif PlayerOne == ("Jack of spades") or ("Jack of clubs") or ("Jack of hearts") or ("Jack of diamonds"):
        pOne = 4
    elif PlayerOne == ("2 of spades") or ("2 of clubs") or ("2 of hearts") or ("2 of diamonds"):
        pOne = 5
    elif PlayerOne == ("3 of spades") or ("3 of clubs") or ("3 of hearts") or ("3 of diamonds"):
        pOne = 6
    elif playerOne == ("4 of spades") or ("4 of clubs") or ("4 of hearts") or ("4 of diamonds"):
        pOne = 7
    elif PlayerOne == ("5 of spades") or ("5 of clubs") or ("5 of hearts") or ("5 of diamonds"):
        pOne = 8
    elif PlayerOne == ("6 of spades") or ("6 of clubs") or ("6 of hearts") or ("6 of diamonds"):
        pOne = 9
    elif PlayerOne == ("7 of spades") or ("7 of clubs") or ("7 of hearts") or ("7 of diamonds"):
        pOne = 10
    elif PlayerOne == ("8 of spades") or ("8 of clubs") or ("8 of hearts") or ("8 of diamonds"):
        pOne = 11
    elif PlayerOne == ("9 of spades") or ("9 of clubs") or ("9 of hearts") or ("9 of diamonds"):
        pOne = 12
    elif PlayerOne == ("10 of spades") or ("10 of clubs") or ("10 of hearts") or ("10 of diamonds"):
        pOne = 13

    #debug
    print("DEBUG", pOne)

    if playerTwo == ("Ace of spades") or ("Ace of clubs") or ("Ace of hearts") or ("Ace of diamonds"):
        pTwo = 1
    elif playerTwo == ("King of spades") or ("King of clubs") or ("King of hearts") or ("King of diamonds"):
        pTwo = 2
    elif playerTwo == ("Queen of spades") or ("Queen of clubs") or ("Queen of hearts") or ("Queen of diamonds"):
        pTwo = 3
    elif playerTwo == ("Jack of spades") or ("Jack of clubs") or ("Jack of hearts") or ("Jack of diamonds"):
        pTwo = 4
    elif playerTwo == ("2 of spades") or ("2 of clubs") or ("2 of hearts") or ("2 of diamonds"):
        pTwo = 5
    elif playerTwo == ("3 of spades") or ("3 of clubs") or ("3 of hearts") or ("3 of diamonds"):
        pTwo = 6
    elif playerTwo == ("4 of spades") or ("4 of clubs") or ("4 of hearts") or ("4 of diamonds"):
        pTwo = 7
    elif playerTwo == ("5 of spades") or ("5 of clubs") or ("5 of hearts") or ("5 of diamonds"):
        pTwo = 8
    elif playerTwo == ("6 of spades") or ("6 of clubs") or ("6 of hearts") or ("6 of diamonds"):
        pTwo = 9
    elif playerTwo == ("7 of spades") or ("7 of clubs") or ("7 of hearts") or ("7 of diamonds"):
        pTwo = 10
    elif playerTwo == ("8 of spades") or ("8 of clubs") or ("8 of hearts") or ("8 of diamonds"):
        pTwo = 11
    elif playerTwo == ("9 of spades") or ("9 of clubs") or ("9 of hearts") or ("9 of diamonds"):
        pTwo = 12
    elif playerTwo == ("10 of spades") or ("10 of clubs") or ("10 of hearts") or ("10 of diamonds"):
        pTwo = 13

    #debug
    print("DEBUG", pTwo)

    if pOne > pTwo:
        print("Player 2 wins the hand!")
        pTwoScore += 1

    elif pOne < pTwo:
        print("Player 1 wins the hand!")
        pOneScore += 1

    elif pOne == pTwo:
        print("It's a tie!")

    return playerOne, playerTwo, pOneScore, pTwoScore

main()
Reply
#4
I meant something more like this
import random
 
def main():
 
    totalCards = 52
    playerOne = 0
    playerTwo = 0
    pOneScore = 0
    pTwoScore = 0
 
    deck = []
    suit = []
    totalList = []
 
    deck = ["Ace","King","Queen","Jack",2,3,4,5,6,7,8,9,10]
    suit = ["of spades","of hearts","of clubs","of diamonds"]
 
    for num in deck:
        for suitType in suit:
            totalList.append(str(num) + ' ' + suitType)
 
    print("Welcome to war. Press any button to deal a hand.")
 
    while (totalCards > 0):
         
        input()
        print("Cards are being delt!")
        print()
        theOutput = dealCard(totalList, playerOne, playerTwo)
        totalList = theOutput[0]
        playerOne = theOutput[1]
        playerTwo = theOutput)[2]
        print()
 
        score(playerOne, playerTwo, pOneScore, pTwoScore)
            
        totalCards -= 2
         
        print(totalCards, "cards remain in the deck.")
 
        print("Player one's score: ", pOneScore)
        print("Player two's score: ", pTwoScore)
 
    print("The game of war has ended!")
    print("The final score is: ")
 
def dealCard(totalList, playerOne, playerTwo):
 
    playerOneSuit = ""
    playerTwoSuit = ""
     
    playerOne = random.choice(totalList)
 
    print("Player one: ",playerOne, playerOneSuit)
    totalList.remove(playerOne)
     
    playerTwo = random.choice(totalList)
     
    print("Player two: ",playerTwo, playerTwoSuit)
    totalList.remove(playerTwo)
 
    return totalList, playerOne, playerTwo
 
def score(playerOne, playerTwo, pOneScore, pTwoScore):
    playerOne = playerOne.split()
    playerTwo = playerTwo.split()
    try:
        int(playerOne[0])
        pOne = playerOne + 3
    except:
        if playerOne == ("Ace of spades") or ("Ace of clubs") or ("Ace of hearts") or ("Ace of diamonds"):
            pOne = 1
        elif PlayerOne == ("King of spades") or ("King of clubs") or ("King of hearts") or ("King of diamonds"):
            pOne = 2
        elif PlayerOne == ("Queen of spades") or ("Queen of clubs") or ("Queen of hearts") or ("Queen of diamonds"):
            pOne = 3
        elif PlayerOne == ("Jack of spades") or ("Jack of clubs") or ("Jack of hearts") or ("Jack of diamonds"):
            pOne =4
        
    try:
        int(playerTwo[0])
        pOne = playerOne + 3
    except:
        if playerTwo == ("Ace of spades") or ("Ace of clubs") or ("Ace of hearts") or ("Ace of diamonds"):
            pTwo = 1
        elif playerTwo == ("King of spades") or ("King of clubs") or ("King of hearts") or ("King of diamonds"):
            pTwo = 2
        elif playerTwo == ("Queen of spades") or ("Queen of clubs") or ("Queen of hearts") or ("Queen of diamonds"):
            pTwo = 3
        elif playerTwo == ("Jack of spades") or ("Jack of clubs") or ("Jack of hearts") or ("Jack of diamonds"):
            pTwo = 4
 
    #debug
    print("DEBUG", pTwo)
 
    if pOne > pTwo:
        print("Player 2 wins the hand!")
        pTwoScore += 1
 
    elif pOne < pTwo:
        print("Player 1 wins the hand!")
        pOneScore += 1
 
    elif pOne == pTwo:
        print("It's a tie!")
 
    return playerOne, playerTwo, pOneScore, pTwoScore
 
main()
May be a few syntax errors, sorry.

Also nothing is storing the returns from "score"
Reply
#5
Can anyone please tell me why it's always resulting in a tie no matter what I do?
Reply
#6
Sorry I had quite a few errors, this one works though. They were both equal to one because it was something to do with the if statement
import random, time
  
def main():
  
    totalCards = 52
    playerOne = 0
    playerTwo = 0
    pOneScore = 0
    pTwoScore = 0
  
    deck = []
    suit = []
    totalList = []
  
    deck = ["Ace","King","Queen","Jack",2,3,4,5,6,7,8,9,10]
    suit = ["of spades","of hearts","of clubs","of diamonds"]
  
    for num in deck:
        for suitType in suit:
            totalList.append(str(num) + ' ' + suitType)
  
    print("Welcome to war. Press any button to deal a hand.")
  
    while (totalCards > 0):
          
        input()
        print("Cards are being delt!")
        print()
        theOutput = dealCard(totalList, playerOne, playerTwo)
        totalList = theOutput[0]
        playerOne = theOutput[1]
        playerTwo = theOutput[2]
        print()
        time.sleep(2)
        scoreTup = score(playerOne, playerTwo, pOneScore, pTwoScore)
        pOneScore = scoreTup[0]
        pTwoScore = scoreTup[1]
             
        totalCards -= 2
          
        print(totalCards, "cards remain in the deck.")
  
        print("Player one's score: ", pOneScore)
        print("Player two's score: ", pTwoScore)
  
    print("The game of war has ended!")
    if pOneScore > pTwoScore:
        winner = 'Player One Wins'
    elif pOneScore < pTwoScore:
        winner = 'Player Two Wins'
    else:
        winner = 'It was a tie'
    print(f"The final score is: {pOneScore} to {pTwoScore}, {winner}")
  
def dealCard(totalList, playerOne, playerTwo):
  
    playerOneSuit = ""
    playerTwoSuit = ""
      
    playerOne = random.choice(totalList)
  
    print("Player one: ",playerOne, playerOneSuit)
    totalList.remove(playerOne)

    time.sleep(2)
    playerTwo = random.choice(totalList)
      
    print("Player two: ",playerTwo, playerTwoSuit)
    totalList.remove(playerTwo)
  
    return totalList, playerOne, playerTwo
  
def score(playerOne, playerTwo, pOneScore, pTwoScore):
    pOne = 0
    pTwo = 0
    playerOne = playerOne.split()
    playerTwo = playerTwo.split()
    try:
        int(playerOne[0])
        pOne = int(playerOne) + 3
    except:
        if playerOne[0] == "Ace":
            pOne = 1
        elif playerOne[0] == "King":
            pOne = 2
        elif playerOne[0] == "Queen":
            pOne = 3
        elif playerOne[0] == "Jack":
            pOne =4
         
    try:
        int(playerTwo[0])
        pOne = int(playerOne) + 3
    except:
        if playerTwo[0] == "Ace":
            pTwo = 1
        elif playerTwo[0] == "King":
            pTwo = 2
        elif playerTwo[0] == "Queen":
            pTwo = 3
        elif playerTwo[0] == "Jack":
            pTwo = 4
  
    #debug
    if pOne > pTwo:
        print("Player 2 wins the hand!")
        pTwoScore += 1
  
    elif pOne < pTwo:
        print("Player 1 wins the hand!")
        pOneScore += 1
  
    elif pOne == pTwo:
        print("It's a tie!")
  
    return pOneScore, pTwoScore
  
main()

Also some time.sleep would add suspense
player 1 got - bla
wait 2 sec
player 2 got - bla

Sorry I just tested and it still won't work let me check again

Found the error
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020