Jump to content
  • entries
    2
  • comments
    23
  • views
    452

Deck


import random
global cards
global suit
global discards
global game
cards = list(range(1, 53))
discards = []
suit = ["Clubs", "Diamonds", "Hearts", "Spades"]
game = True
# Place card in Discard
def drawtodiscard():
    global suit
    top = (drawCard()).split()
    specifiedsuit = suit.index(top[2])
    discard(int(top[0])+(13*specifiedsuit))
def discard(card):
    global discards
    discards.append(card)
# Checks if the deck needs shuffling. (Every slot is empty, equal to -1)
def checkShuffle():
    global cards
    for i in cards:
        if i != -1:
            return False
    return True
# Draws a card from the deck, sets card slot empty
def drawCard():
    global cards
    global suit
    select = -1
    while select == -1:
        card = int(random.random()*52)
        select = cards[card]
    cards[card] = -1
    # Turns out 13 mod 13 is 0, not 13
    if select % 13 != 0:
        print(f"{select % 13} of {suit[int(select/13)]}")
        return(f"{select % 13} of {suit[int(select/13)]}")
    else:
        print(f"13 of {suit[int(card/13)]}")
        return(f"13 of {suit[int(card/13)]}")
# Shuffle the deck
def shuffle():
    global game
    global cards
    global discards
    if discards != []:
        for i in discards:
            cards[i-1] = i
        discards = []
    else:
        print("Discard is empty. Reseting Game.")
        game = 0
        return(True)
# Main loop
while (game):
    prompt = "\nActions:\n    -Input a positive integer to draw that many cards.\n    -Input the name of a card, formatted '(Number) of (Suit)', to discard it.\n    -Type 'Draw to Discard' to flip over top card of deck\n    -Type Save /path/to/save/file to save\n    -Type Import /path/to/save/file to load a deck\n    -Type Reset /path/to/save/file to reset deck and file\n\n"
    action = input(prompt).rstrip()
    # Determines the action by trying to convert it to an integer. If it fails, then keeps action as a string.
    draw = True
    try:
        action = int(action)
    except ValueError:
        draw = False
    if draw:
        for i in range(action):
            drawCard()
            if checkShuffle():
                if shuffle():
                    cards = list(range(1, 53))
                    discards = []
                    suit = ["Clubs", "Diamonds", "Hearts", "Spades"]
                    game = True
                    break
    elif action == "Draw to Discard":
        drawtodiscard()
    # Check Command or Discard. The commands - "if parselist[0] ==..." take files given to them and writes deck and discard to them, or reads deck and discard from them.
    elif action != "" and action.split()[-1] != action.split()[0]:
        parselist = action.split()
        if parselist[0] == "Save":
            try:
                save = open(parselist[1], "w")
                save.write(" ".join(map(str, cards)) + "\n" + " ".join(map(str, discards)))
                save.close()
            except:
                print("Error Opening File")
        elif parselist[0] == "Import":
            try:
                save = open(parselist[1], "r")
                files = save.read().rstrip().split("\n")
                cards = list(map(int, files[0].split(" ")))
                discards = list(map(int, files[1].split(" ")))
                save.close()
            except:
                print("Error Opening File")
        elif parselist[0] == "Reset":
            try:
                cards = list(range(1, 53))
                discards = []
                save = open(parselist[1], "w")
                save.write(" ".join(map(str, cards)) + "\n" + " ".join(map(str, discards)))
                save.close()
            except:
                print("Error Opening File")
        else:
        # Parse what should be a discard
            
            valid = True
            try:
                specifiedsuit = suit.index(parselist[2])
            except:
                valid = False
            if parselist[0].lower() == "a":
                parselist[0] = 1
            if parselist[0].lower() == "j":
                parselist[0] = 11
            if parselist[0].lower() == "q":
                parselist[0] = 12
            if parselist[0].lower() == "k":
                parselist[0] = 13
            try:
                num = int(action[0])
            except ValueError:
                valid = False
            if valid:
                discard(num+(13*specifiedsuit))
            else:
                print("Invalid Input")
    else:
        print("Invalid Input")

This is python code

 

I did give it to some friends to use in a project they're doing....

Edited by SpiritOfWrath

7 Comments


Recommended Comments

KaladinsSenseOfHumourSpren

Posted

2 minutes ago, SpiritOfWrath said:

@Ookla the Wallfacer

Nah

But I'm working on a new project, I'll place some of it here in a bit

Similar sort of thing, but more.

Interesting.

Through the Living Wrath

Posted

21 minutes ago, Ookla the Wallfacer said:

Interesting.

I put it in

×
×
  • Create New...