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 ?
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...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
This game is meant for two or more players. In the game, each player starts out...
This game is meant for two or more players. In the game, each player starts out with 50 points, as each player takes a turn rolling the dice; the amount generated by the dice is subtracted from the player’s points. The first player with exactly one point remaining wins. If a player’s remaining points minus the amount generated by the dice results in a value less than one, then the amount should be added to the player’s points. (As an...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
There are two players, each holding a box. At the beginning of the game, each box...
There are two players, each holding a box. At the beginning of the game, each box contains one dollar. Player 1 is offered the choice between stopping the game and continuing. If he chooses to stop,then each player receives the money in his own box and the game ends.If Player 1 chooses to continue, then a dollar is removed from his box and two dollars are added to Player 2’s box. Then Player 2 must choose between stopping the game...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT