How to use only one instance of a class in python

I can already say that this question will not be liked, and will probably answer very quickly. I'd like a preface to this, letting you know that I have a study of this, but can't figure out what to do.

So, I have a python script that creates a card game. Card game - 3. A game that, as far as I know, my family knows how to play.

My script so far:

import math
import random
from itertools import product

def start_game():
    print ("Game started")
    deck = Deck()
    deck.current_turn = random.randint(1,2)
    print ("Player " + str(deck.current_turn) + " will go first.")
    Round_Start()
def Round_Start():
    deck = Deck()
    p1down = Player1Down()
    p2down = Player2Down()
    p1up = Player1Up()
    p2up = Player2Up()
    if p1down.hidden == True:
        print("P1: " + " - ".join(map(str,p1up.cards)))
        print("P1: #/# - #/# - #/#")
    else:
        print("P1: " + " - ".join(map(str,p1up.cards)))
        print("P1: " + " - ".join(map(str,p1down.cards)))
    if p2down.hidden == True:
        print("P2: " + " - ".join(map(str,p2up.cards)))
        print("P2: #/# - #/# - #/#")
    else:
        print("P2: " + " - ".join(map(str,p2up.cards)))
        print("P2: " + " - ".join(map(str,p2down.cards)))
    Give_Turn()
def P1Turn():
    print("It is now Player 1 turn.")
def P2Turn():
    print("It is now Player 2 turn.")
def Give_Turn():
    deck = Deck()
    print(deck.current_turn)
    if deck.current_turn == 2:
        print("It is now Player 1 turn.")
        P1Turn()
    elif deck.current_turn == 1:
        print("It is now Player 2 turn.")
        P2Turn()
class Player1Down(object):
    def __init__(self):
        deck = Deck()
        self.cards = deck.Deal(3)
        self.hidden = True
class Player2Down(object):
    def __init__(self):
        deck = Deck()
        self.cards = deck.Deal(3)
        self.hidden = True
class Player1Up(object):
    def __init__(self):
        deck = Deck()
        self.cards = deck.Deal(3)
class Player2Up(object):
    def __init__(self):
        deck = Deck()
        self.cards = deck.Deal(3)
class Deck(object):
    current_turn = 0
    def __init__(self, ranks=None, suits=None):

        if ranks is None:
            ranks = range(2,15)
        if suits is None:
            suits = ["H","D","C","S"]
        self.deck = []
        for r in ranks:
            for s in suits:
                self.deck.append(Card(r,s))
    def Deal(self, n):
        return random.sample(self.deck,n)

class Card(object):
    FACES = {11: 'J', 12: 'Q', 13: 'K', 14: 'A'}
    def __init__(self, rank, suit):
        self.suit = suit
        self.rank = rank

    def __str__(self):
        value = self.FACES.get(self.rank, self.rank)
        return "{0}/{1}".format(value, self.suit)

    def __lt__(self, other):
        return self.rank < other.rank
if __name__ == '__main__':
    start_game()

Now some of the script is a direct copy to copy from other users, this was the only way to get things to work up to this point.

My problem is that

deck.current_turn

saves the reset to 0. I believe this is due to the fact that I have several instances of the Deck () class. But I do not know how to fix it.

My conclusion from the current script:

Game started
Player 2 will go first.
P1: 7/H - 9/H - J/H
P1: #/# - #/# - #/#
P2: 5/H - 3/S - 10/H
P2: #/# - #/# - #/#
0

Stack Exchange, , .

+4
1

, , , - Singleton Borg.

Singleton:

class Deck(object):
    _deck = None
    def __new__(cls, *a, **k):
        if not cls._deck:
            cls._deck = object.__new__(cls, *a, **k)
        return cls._deck
    # and the rest as you have it above

:

class Deck(object):
    _dict = {}
    def __init__(self, ranks=None, suits=None):
        self.__dict__ = self._dict
        # and the rest as you have it, inc. the rest of __init__

. , Borg, , http://www.aleax.it/Python/5ep.html.

+1

All Articles