I already know that I shouldn't really use the global function and that how I judge who wins is also really inefficient so I would appreciate improvements for that. I watched a tutorial about Object Oriented Programming and this was my first attempt at using what i learned. I redid a game I did without OOP.
The Rules for the Game are :
Player 1 takes the top card from the deck.
Player 2 takes the next card from the deck. If both players have a card of the same colour, the player with the highest number wins.
If both players have cards with different colours, the winning colour is shown in the table.
Card | Card | Winner |
---|---|---|
Red | Black | Red |
Yellow | Red | Yellow |
Black | Yellow | Black |
- The winner of each round keeps both cards.
- The players keep playing until there are no cards left in the deck
- Players need to be validated
from random import shuffle
Ranks = [ "1", "2", "3", "4", "5", "6", "7","8", "9", "10"]
Colours = [ "Y", "R", "B"]
class Card() :
def __init__(self,rank,colour):
self.rank = rank
self.colour = colour
def get_card(self) :
return self.rank + self.colour
def get_move(self) :
return self.rank
def get_colour(self) :
return self.colour
class Deck() :
def __init__(self) :
self.deck = []
self.tempdeck = []
for Colour in Colours :
for Rank in Ranks :
self.tempdeck.append(str(Rank) + "-" + str(Colour))
for x in self.tempdeck :
s = x.split("-")
x = Card(s[0],s[1])
self.deck.append(x)
self.shuffle_deck()
def len(self) :
return len(self.deck)
def add_card(self,card) :
self.deck.append(card)
def remove_card(self,card) :
self.deck.remove(card)
def shuffle_deck(self) :
shuffle(self.deck)
class Hand(Deck):
def __init__(self,label) :
self.deck = []
self.label = label
self.Score = 0
def PrintCard(self) :
Cards = []
for x in self.deck :
Cards.append(x.get_card())
return str(Cards)
def WinnerDecider() :
if P1Hand.len() > P2Hand.len() :
print("Player 1 wins with, " + str(P1Hand.len() - P2Hand.len()) + "More Cards than Player 2" )
print("Your Cards were:" , P1Hand.PrintCard())
elif P1Hand.len() < P2Hand.len() :
print("Player 2 wins with, " + str(P2Hand.len() - P1Hand.len()) + " More Cards than Player 1" )
print("Your Cards were:" , P2Hand.PrintCard())
elif P1Hand.len() == P2Hand.len() :
print("Draw")
def RoundJudge() :
if P1Move.get_colour() == "R" :
if P2Move.get_colour() == "B" :
P1Hand.add_card(P1Move)
P1Hand.add_card(P2Move)
print("P1 Won Got these cards:" + P1Move.get_card() + "," + P2Move.get_card())
else :
P2Hand.add_card(P1Move)
P2Hand.add_card(P2Move)
print("P2 Won Got these cards:" + P1Move.get_card() + "," + P2Move.get_card())
if P1Move.get_colour() == "Y" :
if P2Move.get_colour() == "R" :
P1Hand.add_card(P1Move)
P1Hand.add_card(P2Move)
print("P1 Won Got these cards:" + P1Move.get_card() + "," + P2Move.get_card())
else :
P2Hand.add_card(P1Move)
P2Hand.add_card(P2Move)
print("P2 Won Got these cards:" + P1Move.get_card() + "," + P2Move.get_card())
if P1Move.get_colour() == "B" :
if P2Move.get_colour() == "Y" :
P1Hand.add_card(P1Move)
P1Hand.add_card(P2Move)
print("P1 Won Got these cards:" + P1Move.get_card() + "," + P2Move.get_card())
else :
P2Hand.add_card(P1Move)
P2Hand.add_card(P2Move)
print("P2 Won Got these cards:" + P1Move.get_card() + "," + P2Move.get_card())
def Game() :
global P1Move
global P2Move
while Deck.len() > 0 :
P1Choice = input(P1Name + " Press Enter to take your turn: ")
if P1Choice == "" :
P1Move = Deck.deck[0]
Deck.remove_card(P1Move)
print("You got: " , P1Move.get_card())
P2Choice = input(P2Name + " Press Enter to take your turn: ")
if P2Choice == "" :
P2Move = Deck.deck[0]
Deck.remove_card(P2Move)
print("You got: " , P2Move.get_card())
RoundJudge()
WinnerDecider()
def Start() :
global P1Hand
global P2Hand
global Deck
global P1Name
global P2Name
P1Name = input("Name: ")
P2Name = input("Name: ")
Deck = Deck()
P1Hand = Hand(P1Name)
P2Hand = Hand(P2Name)
Game()
def Login() :
User1 = input("Enter Username:")
Pass1 = input("Enter Password:")
User2 = input("Enter Username:")
Pass2 = input("Enter Password:")
if User1 == "" and Pass1 == "" and User2 == "" and Pass2 == "" :
Start()
else :
Login()
Login()