0

The CPU and User is getting repeated cards. I've used the shuffle function, as well as pop. Is there a way to prevent useer and CPU from getting repeated cards.

Here is an example of the program compiled

Here are your cards: 1) The 10 of Clubs 2) The 4 of Diamonds 3) The 6 of Diamonds 4) The 7 of Clubs 5) The 10 of Clubs To play cards, simply type their number one at a time. When done, input blank 2 1 3 4

You played: The 4 of Diamonds The 10 of Clubs The 6 of Diamonds The 7 of Clubs

CPU played: The Jack of Spades The Jack of Spades

As you can see, the User was prompted repeated "random" cards and the CPU played repeated "random" cards.

        import random
        import math
        print("Gui-less poker sucks, but it sure is addicting probably")
        if 1:
            hcardss = [""]
            hcardsc = [""]
            hcardsh = [""]
            ccardss = [""]
            ccardsc = [""]
            ccardsh = [""]
            ingame = "true"
            while (ingame == "true"):
                undone = 5
                while (undone > 0):
                    card = random.randint(1,52)
                    # print(card)
                    temp = card / 13
                    temp2 = card / 4
                    temp = math.floor(temp)
                    temp2 = math.floor(temp2)
                    temp = temp + 1
                    # temp2 = temp2 + 1
                    #print(temp)
                    #print(temp2)
                    # undone -= 1
                    hcardss.append(temp)
                    hcardsc.append(temp2)
                    if (temp == 1):
                        temp3 = " of Spades"
                    elif (temp == 2):
                        temp3 = " of Diamonds"
                    elif (temp == 3):
                        temp3 = " of Clubs"
                    else:
                        temp3 = " of Hearts"
                    if (temp2 == 10):
                        temp4 = "Jack"
                    elif (temp2 == 11):
                        temp4 = "Queen"
                    elif (temp2 == 12):
                        temp4 = "King"
                    elif (temp2 == 13):
                        temp4 = "Ace"
                    else:
                        temp4 = str(temp2 + 1)
                    # print("Your card was the " + temp4 + temp3)
                    hcardsh.append("The " + temp4 + temp3)
                    undone -= 1
                undone = 5
                while (undone > 0):
                    # THIS ONE IS THE COMPUTER
                    card = random.randint(1,52)
                    # print(card)
                    temp = card / 13
                    temp2 = card / 4
                    temp = math.floor(temp)
                    temp2 = math.floor(temp2)
                    temp = temp + 1
                    # temp2 = temp2 + 1
                    #print(temp)
                    #print(temp2)
                    # undone -= 1
                    ccardss.append(temp)
                    ccardsc.append(temp2)
                    if (temp == 1):
                        temp3 = " of Spades"
                    elif (temp == 2):
                        temp3 = " of Diamonds"
                    elif (temp == 3):
                        temp3 = " of Clubs"
                    else:
                        temp3 = " of Hearts"
                    if (temp2 == 10):
                        temp4 = "Jack"
                    elif (temp2 == 11):
                        temp4 = "Queen"
                    elif (temp2 == 12):
                        temp4 = "King"
                    elif (temp2 == 13):
                        temp4 = "Ace"
                        temp4 = str(temp2 + 1)
                    # print("Your card was the " + temp4 + temp3)
                    ccardsh.append("The " + temp4 + temp3)
                    undone -= 1
                print()
                print()
                print()
                print("Here are your cards:")
                print("1) " + hcardsh[1])
                print("2) " + hcardsh[2])
                print("3) " + hcardsh[3])
                print("4) " + hcardsh[4])
                print("5) " + hcardsh[5])
                print("To play cards, simply type their number one at a time. When done, input blank")
                doneinput = "false"
                hplay = [""]
                while (doneinput == "false"):
                    latestinput = input("> ")
                    if (latestinput == ""):
                        doneinput = "true"
                    else:
                        if (int(latestinput) in hplay):
                            print("You already picked that one!")
                        else:
                            hplay.append(int(latestinput))
                # print("The cards you played are " + str(hplay))
                doneinput = "false"
                cplay = [""]
                while (doneinput == "false"):
                    latestinput = random.randint(1,5)
                    if (latestinput == ""):
                        doneinput = "true"
                    else:
                        if (int(latestinput) in cplay):
                            doneinput = "true"
                        else:
                            cplay.append(int(latestinput))
                #print("So you played " + str(hplay))
                #print("And the cpu played " + str(cplay))
                #print("So you played the " + hcardsh[hplay[1]] + hcardsh[hplay[2]]
                times = len(hplay)
                # times = times - 1
                hplayh = [""]
                cplayh = [""]
                sub = 1
                print()
                print()
                print("You played:")
                while (sub < times):
                    hplayh.append(hcardsh[hplay[sub]])
                    print(hcardsh[hplay[sub]])
                    sub += 1
                sub = 1
                times = len(cplay)
                print()
                print()
                print("CPU played:")
                while (sub < times):
                    cplayh.append(ccardsh[cplay[sub]])
                    print(ccardsh[cplay[sub]])
                    sub += 1
                #print(str(hplayh))
                #print(str(cplayh))


                ingame = "false"
  • In order to use `random.shuffle()` you should generate a list of the 52 cards, then apply the shuffle function on that list. Then you pick the cards using the `.pop()` function. – JohanL Jul 19 '17 at 18:43

3 Answers3

2

Don't use randint() to pick cards at all. Construct a "deck", which is just a list of cards, use shuffle() to randomize it, and pop() to deal a card. Also, I'd recommend representing cards with numbers rather than strings. Strings are for humans. Numbers will make the rest of your code simpler and faster. Just translate to strings for the user when needed.

Something like:

theDeck = [];

def shuffle():
    theDeck = range(52)
    random.shuffle(theDeck)

def dealCard():
    return theDeck.pop()

def nameOfCard(c):
    return [ "Deuce", "Trey", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" ][c >> 2] + \
        " of " + [ "Clubs", "Diamonds", "Hearts", "Spades" ][c & 3];
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
0

Add played cards to list. Check if new random card already in this list and regenerate new card if so until it's not in list. Don't forget to check list have all the cards to avoid endless loop. P.S. Don't use this method, generating all list at once and shuffle it much more effective.

CrazyElf
  • 763
  • 2
  • 6
  • 17
0

You would have to keep a list of all the cards that have been in play, and then check if the most recently generated card is in that list, and if it is, re-generate it.

generated_cards = []
...
card = random.randint(1,52)
while card in generated_cards:
    card = random.randint(1,52)
generated_cards.append(card)  # once the card is a new, unique one,
                              # add it to the generated_cards list 

An easier way of handling this might be to generate the list of cards, shuffle them, and them pop them off the list one by one.

deck_of_cards = [x for x in range(0, 52)]
random.shuffle(deck_of_cards)
...
card = deck_of_cards.pop()  # this removes the item from the list 
                            # and puts it in the card variable
Cameron
  • 127
  • 10
  • 2
    First method is terribly inefficient. Picking from a shuffled list is simpler and faster. – Lee Daniel Crocker Jul 19 '17 at 18:53
  • I'm aware it's a possibly infinitely long operation but it's the answer to the question that was asked. The correct way to handle all of this would be OOP but again, that seems to me to be outside the scope of the question. – Cameron Jul 19 '17 at 18:55
  • 1
    I try not to be too literal about the OP's exact questions. After all, if he's asking for help, why would I think he knows exactly what question to ask? I'm more inclined to try to figure out what problem he's trying to solve. In your answer you did both. Also, there's a better version of your idea called Floyd's algorithm that's O(log n) and guaranteed finite. – Lee Daniel Crocker Jul 19 '17 at 19:51
  • That makes a lot of sense. Thanks for the (implied) advice, still getting my bearings as a question answerer! [This](https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm) Floyd's algorithm? I know you could also just increment the randint % 52 until you got a unique card, but a O(log n) solution sounds really interesting. – Cameron Jul 19 '17 at 22:50