In: Computer Science
PYTHON:
Write the code to play a card game called Battle. Two players each have a card deck consisting of the following cards: two, three, four, … jack, queen, king, ace, in increasing order. One card deck could be represented by a list such as:
cardsPlayer1 = ["two", "three", "four"..."jack", "queen", "king", "ace"]
Both players have a card randomly selected. When a card is selected, remove it from the player’s deck. The player that plays the higher of the two cards wins a point for that round. If both cards have the same value, neither receives a point.
Use random.randint() with the current length of the list until all 13 cards have been played.
Display each card played in each round and the current scores. Print the final scores and pronounce the one with the highest score as the winner. Allow the user of the game to continue as long as the user wishes.
Ignore all notion of suits in this game.
The program is given below. The comments are provided for the better understanding of the logic.
import random
#Declare the points based on the increasing value of the cards.
points = {"two" : 2, "three" : 3, "four" : 4, "five" : 5, "six" : 6, "seven" : 7, "eight" : 8, "nine" : 9, "ten" : 10, "jack" : 11, "queen" : 12, "king" : 13, "ace" : 100}
#The gameCounter variable is used for tracking the games.
gameCounter = 1;
#This is an infinite loop. The exit consdition is taken care inside the loop.
while(True):
#Set the scores to 0.
scorePlayer1 = 0
scorePlayer2 = 0
#Set the card decks before the starting of each game.
cardsPlayer1 = ["two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"]
cardsPlayer2 = ["two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"]
print("Game: " + str(gameCounter))
#This iterates from 1 to 13. One each for 13 cards in the deck
for i in range(1, 14):
print("\nChance: " + str(i))
#Get input for player 1
input("\nPlayer 1 - Press Enter key to play a card: ")
currentCardIndexPlayer1 = random.randint(0, len(cardsPlayer1)-1)
print("Player 1 played a card.")
#Get input for player 2
input("\nPlayer 2 - Press Enter key to play a card: ")
currentCardIndexPlayer2 = random.randint(0, len(cardsPlayer2)-1)
print("Player 2 played a card.")
#Find the cards points from the points dictionary
player1CurrentCardPoints = points[cardsPlayer1[currentCardIndexPlayer1]]
player2CurrentCardPoints = points[cardsPlayer2[currentCardIndexPlayer2]]
#Check whose card has got higher points and increment the score for that player.
if(player1CurrentCardPoints > player2CurrentCardPoints):
scorePlayer1 = scorePlayer1 + 1
elif(player1CurrentCardPoints < player2CurrentCardPoints):
scorePlayer2 = scorePlayer2 + 1
#Print the cards played and scores.
print("\nPlayer 1 played: " + cardsPlayer1[currentCardIndexPlayer1])
print("Player 2 played: " + cardsPlayer2[currentCardIndexPlayer2])
print("\n######## Score: Player 1 - " + str(scorePlayer1) + ", Player 2 - " + str(scorePlayer2) + " ########")
#Remove the cards from the decks.
cardsPlayer1.pop(currentCardIndexPlayer1)
cardsPlayer2.pop(currentCardIndexPlayer2)
#Print Final Scores
print("\nFinal Scores:")
print("Player 1 - " + str(scorePlayer1) + ", Player 2 - " + str(scorePlayer2))
#Announce the winner
if(scorePlayer1 == scorePlayer2):
print("The scores are equal and both the player wins")
elif(scorePlayer1 > scorePlayer2):
print("**********Player 1 wins**********")
else:
print("**********Player 2 wins**********")
#Increment the gameCounter
gameCounter = gameCounter + 1
#Get the input from the user, if he wants to play the game again. If so continue. Otherwise break the loop and exit the game.
gameContinue = input("\nDo you want to continue the game(y/n): ")
gameContinue = gameContinue.strip().lower()
if(gameContinue != "y"):
break;
The screenshots of the code and output are provided below.