Question

In: Computer Science

PYTHON: Write the code to play a card game called Battle. Two players each have a...

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.

Solutions

Expert Solution

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.


Related Solutions

Design and implement a Python program which will allow two players to play the game of...
Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid! X | O | X | O -------------- O | O | X | O -------------- X | X | O | X -------------- X | X | O | X The rules for this game is the same as the classic, 3x3, game – Each cell can hold one of the following three strings: "X", "O", or "...
I am using C++ Write a program that allows two players to play a game of...
I am using C++ Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row...
Two players A and B play a game of dice . They roll a pair of...
Two players A and B play a game of dice . They roll a pair of dice alternately . The player who rolls 7 first wins . If A starts then find the probability of B winning the game ?
USING JAVA... We want to play a simple card game with 2 players option. The deck...
USING JAVA... We want to play a simple card game with 2 players option. The deck of cards contains 52 cards with 13 cards each in the 4 suits: clubs, diamonds, hearts and spades. Each player begins with 26 cards and one of the players starts the game by putting their first card on the table. Players take turns by putting the top card from their hand, until the current card placed on the table matches the suit of the...
Create a java program. War is a card game for two players. A standard deck of...
Create a java program. War is a card game for two players. A standard deck of 52 cards is dealt so that both players have 26 cards. During each round of play (or "battle"), both players play a card from the top of their hand face up. The player who plays the card of the higher rank wins both cards and places them at the bottom of his stack of cards. If both cards played are of the same rank,...
Write Java code for each of the following problem a. Two players, a and b, try...
Write Java code for each of the following problem a. Two players, a and b, try to guess the cost of an item. Whoever gets closest to the price without going over is the winner. Return the value of the best bid. If both players guessed too high, return -1. example: closestGuess(97, 91, 100) → 97 closestGuess(3, 51, 50) → 3 closestGuess(12, 11, 10) → -1 b. Given a non-empty string, return true if at least half of the characters...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
a video-game developer as received reports of players who are cheating. all game players each have...
a video-game developer as received reports of players who are cheating. all game players each have five capabilities that are ranked on a scale of 1 to 10 points, with 10 total points available for balance. players can move these points between capabilities at any time. the programming logic is as follows: o a player asks to move points from one capability to another. o the source capability must have enough points to allow the move. o the destination capability...
Two players play a game where they start with a row of n piles of varied...
Two players play a game where they start with a row of n piles of varied amounts of money. The players take turns and in each turn a player can pocket either the money in the first pile or the last pile in the row of piles that remains. Design an efficient algorithm (using dynamic programming), which on any given sequence of amounts, determines the maximum amount of money that player 1 can win. If n is even, prove that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT