The question is to make the game of war using Python. The information on how the cards will be represented and how the game will be played along with 2 sample runs is below.
Representation of the deck of cards:
The deck of cards will be simulated with integers of values from 1
to 13, where 1 represents the Ace, 11 the Jack, 12 the Queen, and
13 the King – these integers also represent the face values of the
cards. Each card should be displayed as a pair (tuple) of the suit
and its value, e.g., (Spades : 10), (Clubs : Jack), (Hearts : 5),
(Diamonds : Ace), etc. In addition, your program will store two
data structures called suits and values. The suits data structure
is to store the names of the suits (Clubs, Spades, Diamonds,
Hearts) and their
corresponding integer mapping. You can use either a dictionary or a
2D list for suits. For example, suits can represent information
like this:
suits = {1: 'suitname1', 2: 'suitname2', ...}
or
suits = [[1, 'suitname1'], [2, 'suitname2'],...]
Similarly, values data structure will store the face values
(Ace, 1, 2, .. King) and their corresponding integer mapping.
Again, you can use either a dictionary or a 2D list for
values.
For example, values can store information like this:
values = {1: 'Ace' , ... 13: 'King'}
or
values = [[1, 'Ace'],......,[13, 'King']]
The Game of Life N Death:
This is a 2 player game and the goal of this game is to collect ‘totLen’, (a user defined number) number of cards, first. The user can decide how many cards (num_cards) will be dealt for each hand and how many cards (totLen) should be collected to win. If the user enters values such that, totLen < num_cards, then your program must ask the user for valid numbers. To begin with, both players will be dealt hands of num_cards number of cards each, where each card is worth its face value. The players will play the top cards in their hands and the player having the card with higher face value takes both cards (this is one round). If both cards have the same value, they will be ignored. The game ends when one of the hands reaches totLen number of cards, (or the total number of round played exceeds 50). At the end, the player with the totLen (or the maximum number) of cards will be declared as the winner. Your program should print the winner and the total number of rounds played.
Note that your program should:
• Display current hands of both players.
• Invalid user inputs should be met with a repeated prompt.
Sample Run #1:
Select which game you want to play, enter 1 or 2.
1. Game of War
2. Game of Life N Death
> 2
How many cards you want to deal? 4
What is the size of the winning hand? 7
Player 1's hand:
(Diamonds:6)(Hearts:9)(Hearts:4)(Spades:3)
Player 2's hand:
(Hearts:Ace)(Diamonds:10)(Diamonds:King)(Diamonds:Ace)
Start>>
Player 1: Diamonds 6
Player 2: Hearts Ace
P1's hand
(Hearts:9)(Hearts:4)(Spades:3)(Diamonds:6)(Hearts:Ace)
P2's hand
(Diamonds:10)(Diamonds:King)(Diamonds:Ace)
Player 1: Hearts 9
Player 2: Diamonds 10
P1's hand
(Hearts:4)(Spades:3)(Diamonds:6)(Hearts:Ace)
P2's hand
(Diamonds:King)(Diamonds:Ace)(Hearts:9)(Diamonds:10)
Player 1: Hearts 4
Player 2: Diamonds King
P1's hand
(Spades:3)(Diamonds:6)(Hearts:Ace)
P2's hand
(Diamonds:Ace)(Hearts:9)(Diamonds:10)(Hearts:4)(Diamonds:King)
Player 1: Spades 3
Player 2: Diamonds Ace
P1's hand
(Diamonds:6)(Hearts:Ace)(Spades:3)(Diamonds:Ace)
P2's hand
(Hearts:9)(Diamonds:10)(Hearts:4)(Diamonds:King)
Player 1: Diamonds 6
Player 2: Hearts 9
P1's hand
(Hearts:Ace)(Spades:3)(Diamonds:Ace)
P2's hand
(Diamonds:10)(Hearts:4)(Diamonds:King)(Diamonds:6)(Hearts:9)
Player 1: Hearts Ace
Player 2: Diamonds 10
P1's hand
(Spades:3)(Diamonds:Ace)
P2's hand
(Hearts:4)(Diamonds:King)(Diamonds:6)(Hearts:9)(Hearts:Ace)(Diamonds:10)
Player 1: Spades 3
Player 2: Hearts 4
P1's hand
(Diamonds:Ace)
P2's hand
(Diamonds:King)(Diamonds:6)(Hearts:9)(Hearts:Ace)(Diamonds:10)(Spades:3)(Hearts:4)
The winner of Life N Death game is Player 2. Total round played: 7
Sample Run #2:
Select which game you want to play, enter 1 or 2.
1. Game of War
2. Game of Life N Death
> 2
How many cards you want to deal? 10
What is the size of the winning hand? 8
Invalid input!
How many cards you want to deal? 5
What is the size of the winning hand? 7
…
The winner of Life N Death game is ...
below is required code. let me know if you have nay problem or doubt. thank you.
=================================================================
import random
# class to represent card
class Card:
# dict of suits
suits = {1: "Clubs", 2: "Diamonds", 3: "Hearts", 4: "Spades"}
# duct of face values
values = {1: "Ace", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9",
10: "10", 11: "Jack", 12: "Queen", 13: "King"}
def __init__(self, suit: int, value: int):
self.suit = self.suits[suit]
self.value = self.values[value]
def __repr__(self):
return "(" + self.suit + ":" + self.value + ")"
def get_suit(self):
return self.suit
# returns a key associated with value to compare value of card
def get_value(self):
key_list = list(self.values.keys())
value_list = list(self.values.values())
return key_list[value_list.index(self.value)]
def get_face_value(self):
return self.value
# class to represent deck of cards
class Deck:
def __init__(self):
# create deck of 52 cards
self.deck = []
for i in range(4):
for j in range(13):
# add 1 as dict keys start from 1
self.deck.append(Card(i + 1, j + 1))
# shuffle the deck
self.shuffle()
# arrange cards in random order
def shuffle(self):
random.shuffle(self.deck)
# draw a card from deck
def draw(self):
# give top card from deck
return self.deck.pop(0)
# class to play The Game of War
class GameOfWar:
# hand of players
player1 = []
player2 = []
def __init__(self, number_of_cards: int):
# create a new deck and deal number of cards to both player
deck = Deck()
# give cards to each player
for i in range(number_of_cards):
self.player1.append(deck.draw())
self.player2.append(deck.draw())
# play the game
self.play()
def play(self):
score1 = 0 # score of player 1
score2 = 0 # score of player 2
# show hand of player
print("Player 1's hand: ", end="")
for c in self.player1:
print(c, end="")
print() # print new hand in new line
print("Player 2's hand: ", end="")
for c in self.player2:
print(c, end="")
print() # new line
print("Start>>")
# start the game
while len(self.player1) > 0:
# get top card from each player's hand
card1 = self.player1.pop(0)
card2 = self.player2.pop(0)
# compute score for this card
if card1.get_value() > card2.get_value():
# player 1 has high value card
score1 += 1
if card1.get_value() < card2.get_value():
# player 2 has high value card
score2 += 1
# print cards for player
print("Player 1: " + card1.get_suit() + " " + card1.get_face_value())
print("Player 2: " + card2.get_suit() + " " + card2.get_face_value())
# print current score
print("Scores>> Player 1: " + str(score1) + ", Player 2: " + str(score2))
# print the winner
if score1 > score2:
print("The winner of this round is Player 1 with " + str(score1 - score2) + " points.")
elif score1 < score2:
print("The winner of this round is Player 2 with " + str(score2 - score1) + " points.")
else:
print("This round is Draw!")
# class to play Game of Life N Death
class GameOfLifeNDeath:
# hand of players
player1 = []
player2 = []
def __init__(self, number_of_cards: int, totLen: int):
# create a new deck and deal number of cards to both player
deck = Deck()
# create winning condition
self.totLen = totLen
self.round = 0 # number of round played so far
# give cards to each player
for i in range(number_of_cards):
self.player1.append(deck.draw())
self.player2.append(deck.draw())
# play the game
self.play()
def play(self):
# show hand of player
print("Player 1's hand:")
for c in self.player1:
print(c, end="")
print() # print new hand in new line
print("Player 2's hand:")
for c in self.player2:
print(c, end="")
print() # new line
print("Start>>")
# start the game
while self.round < 50: # play game till 50 rounds
self.round += 1
# get top card from each player's hand
try:
card1 = self.player1.pop(0)
card2 = self.player2.pop(0)
except IndexError:
break
# compare both card's face value
if card1.get_value() > card2.get_value():
# player 1 has high value card
# add both cards at end of player 1's hand
self.player1.append(card1)
self.player1.append(card2)
if card1.get_value() < card2.get_value():
# player 2 has high value card
# add both cards at end of player 2's hand
self.player2.append(card1)
self.player2.append(card2)
# print cards for player
print("Player 1: " + card1.get_suit() + " " + card1.get_face_value())
print("Player 2: " + card2.get_suit() + " " + card2.get_face_value())
# show hand of player
print("Player 1's hand:")
for c in self.player1:
print(c, end="")
print() # print new hand in new line
print("Player 2's hand:")
for c in self.player2:
print(c, end="")
print() # new line
# check winning condition
if (len(self.player1) >= self.totLen) or (len(self.player2) >= self.totLen):
break
# print the winner
if len(self.player1) > len(self.player2):
print("The winner of Life N Death game is Player 1")
elif len(self.player1) < len(self.player2):
print("The winner of Life N Death game is Player 2")
else:
print("This round is Draw!")
# main function
def main():
game = 0
while not (game == 1 or game == 2):
# let user select a game to play
game = input("Select which game you want to play, enter 1 or 2.\n"
"1. Game of War\n2. Game of Life N Death\n")
if not game.isdigit():
print("Enter a number")
else:
game = int(game)
if game == 1:
# prompt user for number of cards till get a valid input
cards = 0
while True:
num_of_cards = input("How many cards you want to deal? ")
# check for valid input
if num_of_cards.isdigit():
cards = int(num_of_cards)
# maximum 26 cards can be given to each player
if (cards > 0) and (cards < 27):
break
else:
print("Invalid input!")
else:
print("Invalid input!")
# play a round of game
GameOfWar(cards)
elif game == 2:
# prompt user for number of cards till get a valid input
cards = 0
while True:
num_of_cards = input("How many cards you want to deal? ")
# check for valid input
if num_of_cards.isdigit():
cards = int(num_of_cards)
# maximum 26 cards can be given to each player
if (cards > 0) and (cards < 27):
break
else:
print("Invalid input!")
else:
print("Invalid input!")
totLen = 0
while True:
totLen = input("What is the size of the winning hand? ")
# check for valid input
if totLen.isdigit():
totLen = int(totLen)
# totLen should be higher than initial card to win the game
if totLen > cards:
break
else:
print("Invalid input!")
else:
print("Invalid input!")
# play a round of game
GameOfLifeNDeath(cards, totLen)
else:
print("enter 1 or 2")
if __name__ == '__main__':
main()









Run: Game of War "C:\Users\ASUS FX505\AppData\Local\Programs\Python Python37\python.exe" "C:/Users/ASUS FX505/PycharmProjects/MyProject/Game of War.py" Select which game you want to play, enter 1 or 2. 1. Game of War 2. Game of Life N Death + → IR 7 Di How many cards you want to deal? 4 What is the size of the winning hand? 7 Player l's hand: (Spades: 10) (Clubs: King) (Hearts:6) (Spades:8) Player 2's hand: (Clubs:7) (Spades: Queen) (Clubs:5) (Spades:5) Start >> Player 1: Spades 10 Player 2: Clubs 7 Player l's hand: (Clubs: King) (Hearts:6) (Spades: 8) (Spades: 10) (Clubs:7) Player 2's hand: (Spades: Queen) (Clubs:5) (Spades:5) Player 1: Clubs King Player 2: Spades Queen Player 1's hand: (Hearts:6) (Spades:8) (Spades: 10) (Clubs:7) (Clubs: King) (Spades: Queen) Player 2's hand: (Clubs:5) (Spades:5) Player 1: Hearts 6 Player 2: Clubs 5 Player 1's hand: (Spades:8) (Spades: 10) (Clubs:7) (Clubs: King) (Spades: Queen) (Hearts:6) (Clubs:5) Player 2's hand: (Spades:5) The winner of Life N Death game is Player 1 Process finished with exit code o 4: Run E 6: TODO Terminal Python Console
fa Game of War.py X import random # class to represent card -class Card: # dict of suits suits = {1: "Clubs", 2: "Diamonds", 3: "Hearts", 4: "Spades"} # duct of face values values = {1: "Ace", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "10", 11: "Jack", 12: "Queen", 13: "King"} def __init__(self, suit: int, value: int): self.suit = self.suits[suit] self.value = self.values [value] 16 of def _repr_(self): return "(" + self.suit + ":" + self.value + ")" def get_suit(self): return self.suit # returns a key associated with value to compare value of card def get_value(self): key_list = list(self.values.keys()) value_list = list(self.values.values()) return key_list[value_list.index(self.value)] def get_face_value(self): return self.value # class to represent deck of cards class Deck: definit (self).
We were unable to transcribe this image
ta Game of War.py self.player1.append(deck.draw()) self.player2.append(deck.draw()) # play the game self.play() def play(self): score1 = 0 # score of player 1 score2 = 0 # score of player 2 # show hand of player print("Player l's hand: ", end="") for c in self.player1: print(c, end="") print() # print new hand in new line print("Player 2's hand: ", end="") for c in self.player2: print(s, end="") print() # new Line print("Start>>") # start the game while len(self.player1) > 0: # get top card from each player's hand card1 = self.player1.pop() card2 = self.player2.pop() # compute score for this card if card1.get_value() > card2.get_value(): # player 1 has high value card score1 += 1 card1.get_value() < card2.get_value(): # player 2 has high value card score2 += 1 # print cards for player print("Player 1: " + card1.get_suit() + " " + card1.get_face_value()) print("Player 2: " + card2.get_suit() + " " + card2.get_face_value()) if
We were unable to transcribe this image
We were unable to transcribe this image
163 165 172 176 Game of War.py X 101 print("Player 2: + caraz.get_suit + - + caraz.get_race_value ] 162 # show hand of player print("Player l's hand:") 164 for c in self.player1: print(c, end="") 166 print() # print new hand in new Line print("Player 2's hand:") for c in self.player2: print(c, end="") 170 print() # new Line 171 # check winning condition if (len(self.player1) >= self.totlen) or (len(self.player2) >= self.totlen): 173 break 174 # print the winner 175 if len(self.player1) > len(self.player2): print("The winner of Life N Death game is Player 1") 177 elif len(self.player1) < len(self.player2): 178 print("The winner of Life N Death game is Player 2") 179 else: 180 print("This round is Draw!") 181 182 183 # main function 184 def main(): 185 game = 0 186 while not (game == 1 or game == 2): # Let user select a game to play game = input("Select which game you want to play, enter 1 or 2.\n" 189 "1. Game of War\n2. Game of Life N Death\n") if not game.isdigit(): print("Enter a number") 192 else: 193 game = int(game) 194 if game == 1: 187 188 190 191
te Game of War.py X 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 game = int(game) if game == 1: # prompt user for number of cards till get a valid input cards = 0 while True: num_of_cards = input("How many cards you want to deal? ") # check for valid input if num_of_cards.isdigit(): cards = int(num_of_cards) # maximum 26 cards can be given to each player if (cards > 0) and (cards < 27): break else: print("Invalid input!") else: print("Invalid input!") # play a round of game GameOfWar(cards) elif game == 2: # prompt user for number of cards till get a valid input cards = 0 while True: num_of_cards = input("How many cards you want to deal? ") # check for valid input if num_of_cards.isdigit(): cards = int(num_of_cards) # maximum 26 cards can be given to each player if (cards > 0) and (cards < 27): break else: print("Invalid input!") else: print("Invalid input!") 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
e Game of War.py X 219 220 # maximum 26 cards can be given to each player if (cards > 0) and (cards < 27): break else: print("Invalid input!") else: print("Invalid input!") totLen = 0 while True: totlen = input("What is the size of the winning hand? ") # check for valid input if totLen.isdigit(): totlen = int(tot Len) # totLen should be higher than initial card to win the game if totLen > cards: break else: print("Invalid input!") else: print("Invalid input!") # play a round of game GameOfLifeNDeath(cards, totLen) else: print("enter 1 or 2") 242 243 244 245 | 246 247 if __name__ == '__main__': main()
The question is to make the game of war using Python. The information on how the...
Program 4: C++ The Game of War The game of war is a card game played by children and budding computer scientists. From Wikipedia: The objective of the game is to win all cards [Source: Wikipedia]. There are different interpretations on how to play The Game of War, so we will specify our SMU rules below: 1) 52 cards are shuffled and split evenly amongst two players (26 each) a. The 26 cards are placed into a “to play” pile...
Write in Java! Do NOT write two different programs for
Deck and Card, it should be only one program not 2 separate
ones!!!!!!
!!!!!!!!!!!!!!!Use at least one array defined in your
code and two array lists defined by the operation of your
code!!!!!!!!!!!!!!!!!!!!!
The array should be 52 elements and contain a representation of
a standard deck of cards, in new deck order. (This is the order of
a deck of cards new from the box.)
The 2 Array lists...
I am having problem
understanding this problem. please explain it explicitly. its a
discrete computer science problem. thanks
Exercises 27-32 concern a 5-card hand from a standard 52-card deck. A standard deck has 13 cards from each of 4 suits (clubs, diamonds, hearts, spades). The 13 cards have face value 2 through10, jack, queen, king, or ace Each face value is a "kind" of card. The jack, queen, and king are "face cards. 27. How many hands contain 4 queens?...
Instructions for Question: We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 11. ii) One of 9 different suits. Hence, there are 99 cards in the deck with 11 ranks for each of the 9 different suits, and none of...
War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War: Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...
This has to be done in Haskell only. Write a program that: Given 2 poker hands (5 cards) in list of tuples form (ex. hand1 = [(1,0),(2,0),(3,0),(4,0),(5,0)]). Each tuple represents a card where first number of a tuple is its value (1=Ace, 2=2,...,10=10, 11=Jack, 12=Queen, 13=King) and second number its suit (0=Clubs, 1=Diamonds, 2=Hearts, 3=Spades) so (1,0) will be Ace of Clubs, or (5,2) will be 5 of Hearts, etc. Rule website: https://www.fgbradleys.com/et_poker.asp These hands are generated by an end...
4. A group of students are playing a card game. The game uses a well-shuffled deck of 56 playing cards. 52 of the cards are exactly as described in your textbook. However, there are also 4 Jokers. This means that there are 56 cards: 14 are hearts, 14 are diamonds, 14 are clubs, and 14 are spades. A hand of cards consists of Eight of the cards. Find the number of different hands that contain: a. At least 6 Diamonds....
This activity needs to be completed in the Python language. This program will simulate part of the game of Poker. This is a common gambling game comparing five-card hands against each other with the value of a hand related to its probability of occurring. This program will simply be evaluating and comparing hands, and will not worry about the details of dealing and betting. Here follow the interesting combinations of cards, organized from most common to least common: Pair --...
Discrete Mathematics: Counting Principles
a. How many hands consists of a pair of aces?
b. How many hands contain all face cards?
c. How many hands contain at least one face card?
Concern a hand consisting of 1 card drawn from a standard 52-card deck with flowers on the back and 1 card drawn from a standard 52-card deck with birds on the back. A standard deck has 13 cards from each of 4 suits (clubs, diamonds, hearts, spades). The...
A deck of cards contains 52 cards. They are divided into four suits: spades, diamonds, clubs and hearts. Each suit has 13 cards: ace through 10, and three picture cards: Jack, Queen, and King. Two suits are red in color: hearts and diamonds. Two suits are black in color: clubs and spades.Use this information to compute the probabilities asked for below and leave them in fraction form. All events are in the context that three cards are dealt from a...