#Poker Machine
#Poker Machine
#https://www.w3schools.com/python/python_howto_remove_duplicates.asp
import random
suits = ["Spade", "Heart", "Diamond", "Clubs"]
numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13]
#Empty lists that we are going to populate later.
deck = []
playercard = []
compcard = []
cardlist = []
#The function below returns a card, by randomly picking card from the deck
#This function would be called many times to generate new cards in other functions
#Everytime a card is generated, the card is removed from the deck to avoid duplicates
def generate_card():
global deck
card = random.choice(deck)
deck.remove(card)
return(card)
#The three functions below generate cards from the player, cards, and also the community
cards
#It distributes 2 cards each to player and computer, and then 5 cards for the community cards
#The cards are put into a empty list for each function, and these lists could be used later to find
the patterns
#The lists are returned
def generate_playercard():
global playercard
count = 0
while count < 2:
playercard.append(generate_card())
count += 1
return(playercard)
def generate_compcard():
global compcard
count = 0
compcard = []
while count < 2:
compcard.append(generate_card())
count += 1
return(compcard)
def communitycards():
global cardlist
count = 0
while count < 5:
cardlist.append(generate_card())
count += 1
return(cardlist)
generate_playercard()
generate_compcard()
communitycards()
#In poker, we check cards by combining the community cards and the player cards
#We join to list to make a totalplayercard list that we can use to check for patterns
totalplayercard = cardlist + playercard
totalcompcard = cardlist + compcard
#Below are variables to check for each pattern, and their corresponding values. These are all
global values.
playerwon = False
compwon = False
playerfourofakind = False
compfourofakind = False
playerfourcards = 0
compfourcards = 0
playerthreeofakind = False
compthreeofakind = False
playerthreecards = 0
compthreecards = 0
playertwoofakind = False
comptwoofakind = False
playertwocards = 0
comptwocards = 0
#The function below checks if the player or the computer has 4 of a kind
#It takes the total card list, and take the first index of each sublist to create a list with only
numbers
#For each number in the list, we check if it reappears 4 more times
#If it does reapepar 4 more times, playerfourofakind becomes true, playerfourcards is the value
that repeated 4 times
def fourkind():
global playerwon
global compwon
def checkplayer(list1):
global playerfourofakind
global playerfourcards
count = 0
numlist = []
x=0
while count < 7:
numlist.append(int(list1[x][0]))
x += 1
count += 1
for x in numlist:
count = 0
for y in numlist:
if y == x:
count += 1
if count == 4:
playerfourcards = x
playerfourofakind = True
checkplayer(totalplayercard)
def checkcomp(list2):
global compfourofakind
global compfourcards
count = 0
numlist = []
x=0
while count < 7:
numlist.append(int(list2[x][0]))
x += 1
count += 1
for x in numlist:
count = 0
for y in numlist:
if y == x:
count += 1
if count == 4:
compfourcards = x
compfourofakind = True
checkcomp(totalcompcard)
#The function below checks if there are 5 of the same suit
#It first checks the player, then the computer
#For each player, it first makes a suits list by adding the second index in each sublist of the
totalplayercards
#Then, if the suit reappears 5 more times in the for loop, flush becomes True, playerwon also
becomes True
def flush():
def checkplayer():
flush = False
global totalplayercard
global playerwon
count = 0
suitlist = []
x=0
while count < 7:
suitlist.append(totalplayercard[x][1])
x += 1
count += 1
for x in suitlist:
yes = 0
for y in suitlist:
if y == x:
yes += 1
if yes == 5:
flush = True
if flush == True:
playerwon = True
def checkcomp():
flush = False
global totalcompcard
global compwon
count = 0
suitlist = []
x=0
while count < 7:
suitlist.append(totalcompcard[x][1])
x += 1
count += 1
for x in suitlist:
yes = 0
for y in suitlist:
if y == x:
yes += 1
if yes == 5:
flush = True
if flush == True:
compwon = True
checkcomp()
checkplayer()
#The function belows check if there are 5 consecutive numbers in a list
#It first makes the number list as usual
#We sorted the list first
#Then, we removed any duplicates from the list(source:
#https://www.w3schools.com/python/python_howto_remove_duplicates.asp)
#Then, we checked if the number before the current index is 1 less than the current number
#If it isn't, then it would be removed from the list
#At the end, if the remaining list is longer than 4 items, then we know we have a consecutive list
of 5 numbers
def straight():
def checkplayer():
straight = False
global totalplayercard
global playerwon
count = 0
numlist = []
x=0
while count < 7:
numlist.append(int(totalplayercard[x][0]))
x += 1
count += 1
numlist.sort()
numlist = list(dict.fromkeys(numlist))
for x in numlist[1:]:
index = numlist.index(x)
if x - numlist[index-1] != 1:
numlist.remove(x)
if len(numlist) > 4:
straight = True
if straight == True:
playerwon = True
def checkcomp():
straight = False
global totalcompcard
global compwon
count = 0
numlist = []
x=0
while count < 7:
numlist.append(int(totalcompcard[x][0]))
x += 1
count += 1
numlist.sort()
numlist = list(dict.fromkeys(numlist))
for x in numlist[1:]:
index = numlist.index(x)
if x - numlist[index-1] != 1:
numlist.remove(x)
if len(numlist) > 4:
straight = True
if straight == True:
compwon = True
checkplayer()
checkcomp()
#The function below checks if the player or the computer has 3 of a kind
#It takes the total card list, and take the first index of each sublist to create a list with only
numbers
#For each number in the list, we check if it reappears 3 more times
#If it does reapepar 3 more times, playerthreeofakind becomes true, playerthreecards is the
value that repeated 3 times
def threekind():
global playerwon
global compwon
def checkplayer():
global playerthreeofakind
global playerthreecards
global totalplayercard
count = 0
numlist = []
x=0
while count < 7:
numlist.append(int(totalplayercard[x][0]))
x += 1
count += 1
for x in numlist:
count = 0
for y in numlist:
if y == x:
count += 1
if count == 3:
playerthreecards = x
playerthreeofakind = True
def checkcomp():
global compthreeofakind
global compthreecards
global totalcompcard
count = 0
numlist = []
x=0
while count < 7:
numlist.append(int(totalcompcard[x][0]))
x += 1
count += 1
for x in numlist:
count = 0
for y in numlist:
if y == x:
count += 1
if count == 3:
compthreecards = x
compthreeofakind = True
checkplayer()
checkcomp()
#The function below checks if the player or the computer has 2 of a kind
#It takes the total card list, and take the first index of each sublist to create a list with only
numbers
#For each number in the list, we check if it reappears 2 more times
#If it does reapepar 2 more times, playertwoofakind becomes true, playertwocards is the value
that repeated 2 times
def pair():
global playerwon
global compwon
global totalplayercard
def checkplayer():
global playertwoofakind
global playertwocards
count = 0
numlist = []
x=0
while count < 7:
numlist.append(int(totalplayercard[x][0]))
x += 1
count += 1
for x in numlist:
count = 0
for y in numlist:
if y == x:
count += 1
if count == 2:
playertwocards = x
playertwoofakind = True
def checkcomp():
global comptwoofakind
global comptwocards
global totalcompcard
count = 0
numlist = []
x=0
while count < 7:
numlist.append(int(totalcompcard[x][0]))
x += 1
count += 1
for x in numlist:
count = 0
for y in numlist:
if y == x:
count += 1
if count == 2:
comptwocards = x
comptwoofakind = True
checkplayer()
checkcomp()
#The function retrieves the highest card the player has, and the second highest
#It first makes the number list based on the playercards
#Then it sort the list
#The first index in the list would the player's second highest value
#The second index in the list would the player's highest value
#We return these values at the end
def highcard():
global playercard
playerhighcard = 0
comphighcard = 0
playersecondcard = 0
compsecondcard = 0
def checkplayer():
global playercard
global playerhighcard
global comphighcard
global playersecondcard
global compsecondcard
count = 0
numlist = []
x=0
for count in range(2):
numlist.append(int(playercard[x][0]))
x += 1
count += 1
numlist.sort()
playerhighcard = numlist[1]
playersecondcard = numlist[0]
def checkcomp():
global compcard
global playerhighcard
global comphighcard
global playersecondcard
global compsecondcard
count = 0
numlist = []
x=0
for count in range(2):
numlist.append(int(compcard[x][0]))
x += 1
count += 1
numlist.sort()
comphighcard = numlist[1]
compsecondcard = numlist[0]
checkplayer()
checkcomp()
#The whole section below utilizes the above function to check if the player or computer won
#It calls each function, and check if the player or the computer has each pattern
#If only one of them has the pattern, then that person has won
#If both of them has it, we check who has the higher fourkind/threekind/pair etc
#If both of them don't have the pattern, then we check the next pattern by calling the next
function
#It cycles through each if loop until the end, where we check for high cards, or even down to the
second highest card
#if in any case, playerwon or compwon is true, we print(x has won by x methods)
fourkind()
if playerfourofakind == True and compfourofakind == False:
playerwon = True
elif compfourofakind == True and playerfourofakind == False:
compwon = True
elif playerfourofakind == True and compfourofakind == True:
if playerfourcards > compfourcards:
playerwon = True
elif playerfourcards < compfourcards:
compwon = True