简体   繁体   中英

How can I better check for a pair using a data set of card numbers and their suits?

I have recently taken it upon my self to create a program that plays DJ Wild the poker game. I haven't ran into many bumps but I am not very familiar with time complexity which I know that many programs can run into. This is making me cautious about how many and how long my if statements are. Thus a question occurred, can I simplify the following if statement that uses the count method.

`

#imports

import random
import itertools


#declaration of the variables
ante = 0
bonus = 0
balance = 200
cards = []
hands0 = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
hands1 = ["Spade", "Club", "Diamond", "Heart"]

#initializing the card deck
carddeck = list(itertools.product(['A','2','3','4','5','6','7','8','9','10','J','Q','K'],["Spade", "Club", "Diamond", "Heart"]))


#shuffling the deck
random.shuffle(carddeck)


#drawing n number of cards from the shuffled deck
def user(n):
    for i in range(n):
        print("Player:", carddeck[i][0], carddeck[i][1])
        cards.append(carddeck[i][0])
        cards.append(carddeck[i][1])
        carddeck.remove(carddeck[i])
        

user(5)
#print(cards)
if cards.count('2') == 2 or \
   cards.count('3') == 2 or \
   cards.count('4') == 2 or \
   cards.count('5') == 2 or \
   cards.count('6') == 2 or \
   cards.count('7') == 2 or \
   cards.count('8') == 2 or \
   cards.count('9') == 2 or \
   cards.count('10') == 2 or \
   cards.count('J') == 2 or \
   cards.count('Q') == 2 or \
   cards.count('K') == 2 or \
   cards.count('A') == 2:   
    print("You have a pair")
else:
    print("You don't have a pair")

`

I have tried using the line breaks with all the \ implemented but I can't help but think that there is a more simplistic way to check for pairs using the list data for the cards created and dealt to the player.

  • initialize boolean to false
  • loop the hands0 array
  • check value of array
  • if its true set your boolean to true and break your for loop
  • then you can check against your boolean

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM