0% found this document useful (0 votes)
22 views3 pages

Untitled Document

Unti

Uploaded by

hgsrdtyv4t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Untitled Document

Unti

Uploaded by

hgsrdtyv4t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

restricted = ["@", "!

", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "/", ]
numPlayers = int(input("How many players will be playing? (No spaces, characters, or
alphabets) "))
if (numPlayers < 0):
numPlayers = int(input("Negative players cannot play this game. Please enter another
number. "))
if (numPlayers > 4):
numPlayers = int(input("More than 4 players cannot play this game. Please enter another
number. "))
if (numPlayers == 0):
numPlayers = int(input("0 players cannot play this game. Please enter another number. "))
if (numPlayers == 1):
numPlayers = int(input("You cannot play by yourself. Please enter another number. "))

red = "\033[31m"
black = "\033[30m"
import random
cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "K", "Q", "J"]
suits = [(red + "♥" + black), (black + "♠" + black), (black + "♣" + black), (red + "♦" + black)]
class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value
if self.value == "K":
self.value = 13
elif self.value == "Q":
self.value = 12
elif self.value == "J":
self.value = 11
elif self.value == "A":
self.value = 14
else:
self.value = value
def __repr__(self):
if self.value == 1:
return("A" + str(self.suit))
else:
return(str(self.suit) + str(self.value))
# Deck contains all logic for game
class Deck:
def __init__(self):
self.deck_Of_Cards = []
# creates deck in a list with string in each index
def createDeck(self):
for s in suits:
for c in cards:
self.deck_Of_Cards.append(Card(s, c))
# gives 3 cards to each player on default
def deal(self, num_cards = 2, num_players = numPlayers):
self.players = [[]for i in range (num_players)]
random.shuffle(self.deck_Of_Cards)
for i in range(num_cards):
for x in range(num_players):
if(len(self.deck_Of_Cards)) != 0:
self.players[x].append(self.deck_Of_Cards.pop())
if(len(self.deck_Of_Cards) == 0):
break

# if the user specfies hand by passing in value for player


def show_decks(self, player = "All"):
if (player != "All"):
print("Player " + str((player + 1)) + "'s deck " + str(self.players[player]))
else:
for i in range(len(self.players)):
print("Player " + str((i + 1)) + "'s deck consists of " + str(self.players[i]))
def __repr__(self):
return str(self.deck_Of_Cards)
def declareWinner(self):
if len(self.players[0]) > len(self.players[1]):
print("Player 1 wins the game")
if len(self.players[0]) < len(self.players[1]):
print("Player 1 wins the game")
def gameLogic(self):
self.middlePile = []
while True:
for i in range(len(self.players)):
if len(self.players[i]) == 52:
print("Player " + str((i + 1)) + " has no cards no left. Game Over!")
self.declareWinner()
return
player1hand = self.players[0].pop(0)
player2hand = self.players[1].pop(0)
print("Player 1 draws: " + str(player1hand))
print("Player 2 draws: " + str(player2hand))
self.middlePile.append(player1hand)
self.middlePile.append(player2hand)
if player1hand.value > player2hand.value:
print("Player 1 wins the round")
self.players[0].extend(self.middlePile)
self.middlePile.clear()
if player1hand.value < player2hand.value:
print("Player 2 wins the round")
self.players[1].extend(self.middlePile)
self.middlePile.clear()
if player1hand.value == player2hand.value:
print("War")
if len(self.players[0]) < 4 or len(self.players[1]) < 4:
print("Not enough cards to war")
self.declareWinner()
return
player1war = [self.players[0].pop(0) for i in range(3)]
player2war = [self.players[1].pop(0) for i in range(3)]
self.middlePile.extend(player1war + player2war)
player1final = self.players[0].pop(0)
player2final = self.players[1].pop(0)
self.middlePile.append(player1final)
self.middlePile.append(player1final)
print("Player 1's War Cards " + str(player1war) + str([player1final]))
print("Player 2's War Cards " + str(player2war) + str([player2final]))
if player1final.value > player2final.value:
print("Player 1 wins the war")
self.players[0].extend(self.middlePile)
self.middlePile.clear()
if player1final.value < player2final.value:
print("Player 2 wins the war")
self.players[1].extend(self.middlePile)
self.middlePile.clear()
D1 = Deck()
D1.createDeck()
D1.deal(26, numPlayers)
D1.gameLogic()

You might also like