0% found this document useful (0 votes)
34 views17 pages

#Poker Machine

Uploaded by

kennyboy0212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views17 pages

#Poker Machine

Uploaded by

kennyboy0212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

#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 generates the deck of cards.


#It creates a 2D list using the lists suits/numbers, where for every number, it makes 4 different
cards with 4 different suits
#It returns the deck at the end
def generate_deck():
global suits
global numbers
global deck
for x in numbers:
count = 0
while count < 4:
card = []
card.append(x)
card.append(suits[count])
count += 1
deck.append(card)
return(deck)
generate_deck()

#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()

#The functions below is the initiation function


#It displays the rule if the player wishes
#It also allows the player to put in an amount of money
#It prints the cards the player has, the cards the computer has, and the community cards
#If the player puts in an invalid amount, they can choose to restart the program, else they game
ends
def initiation():
rule = input("This is a game of poker. You put a bet in and you play against the computer.
Type yes if you want to see the rules")
if rule == "yes":
print("All players are dealt 2 cards, followed by 5 cards in the middle. Players must make
the best possible 5 card poker hand using the 5 community cards and your own cards. The best
5 card poker hand wins the hand and receives the pot.")
print("Four of a kind > flush(5 of the same suit) > straight(5 consecutive numbers) > 3 of a
kind > pair")
print("If the player and comp has the same pair or the same four of a kind or the same 3 of
a kind, the bigger numbers win")
print("If both the player and comp dont have any of those, the one with the higher card
wins")
money = int(input("how much money do you want to bet"))
if money > 0:
print("your cards are" + str(playercard))
print("the computers cards are" + str(compcard))
print("the community cards are" + str(cardlist))
else:
restart = input("invalid sum, type yes if you want to start again")
if restart == "yes":
initiation()
else:
print("end of game")
initiation()

#All of the functions below are related to graphics


#The addtext function draws the suit and number of each card, by first checking the suit of the
card, then the number
#This is done by taking the values from the playercard list
#For every card, the number is written 50 pixels above the suit
def addtext1():
global playercard
global compcard
global cardlist
playercount = 0
x=0
y = 380
while playercount < 2:
txt = Text(str(playercard[0][x]))
txt.set_position(100, y)
txt.set_color(Color.blue)
txt.set_font("10pt Arial")
add(txt)
playercount += 1
x += 1
y += 50
def addtext2():
global playercard
global compcard
global cardlist
playercount = 0
x=0
y = 380
while playercount < 2:
txt = Text(str(playercard[1][x]))
txt.set_position(200, y)
txt.set_color(Color.blue)
txt.set_font("10pt Arial")
add(txt)
playercount += 1
x += 1
y += 50
def addtext3():
global compcard
playercount = 0
x=0
y = 80
while playercount < 2:
txt = Text(str(compcard[0][x]))
txt.set_position(100, y)
txt.set_color(Color.blue)
txt.set_font("10pt Arial")
add(txt)
playercount += 1
x += 1
y += 50
def addtext4():
global compcard
playercount = 0
x=0
y = 80
while playercount < 2:
txt = Text(str(compcard[1][x]))
txt.set_position(200, y)
txt.set_color(Color.blue)
txt.set_font("10pt Arial")
add(txt)
playercount += 1
x += 1
y += 50
def addtext5():
global compcard
playercount = 0
x=0
y = 220
while playercount < 2:
txt = Text(str(cardlist[2][x]))
txt.set_position(190, y)
txt.set_color(Color.blue)
txt.set_font("10pt Arial")
add(txt)
playercount += 1
x += 1
y += 50
def addtext6():
global compcard
playercount = 0
x=0
y = 220
while playercount < 2:
txt = Text(str(cardlist[1][x]))
txt.set_position(120, y)
txt.set_color(Color.blue)
txt.set_font("10pt Arial")
add(txt)
playercount += 1
x += 1
y += 50
def addtext7():
global compcard
playercount = 0
x=0
y = 220
while playercount < 2:
txt = Text(str(cardlist[0][x]))
txt.set_position(50, y)
txt.set_color(Color.blue)
txt.set_font("10pt Arial")
add(txt)
playercount += 1
x += 1
y += 50
def addtext8():
global compcard
playercount = 0
x=0
y = 220
while playercount < 2:
txt = Text(str(cardlist[3][x]))
txt.set_position(260, y)
txt.set_color(Color.blue)
txt.set_font("10pt Arial")
add(txt)
playercount += 1
x += 1
y += 50
def addtext9():
global compcard
playercount = 0
x=0
y = 220
while playercount < 2:
txt = Text(str(cardlist[4][x]))
txt.set_position(330, y)
txt.set_color(Color.blue)
txt.set_font("10pt Arial")
add(txt)
playercount += 1
x += 1
y += 50
#The function below draws the 5 community rectangle first, in a while loop
#Each community card is seperated by 70 pixels in between them
#Then it makes 2 player cards and 2 computer cards
def makecard():
count = 0
x = 50
while count<5:
card = Rectangle(50, 100)
card.set_position(x, 200)
card.set_color(Color.grey)
add(card)
count += 1
x += 70

playercard1 = Rectangle(50, 100)


playercard1.set_position(100, 350)
playercard1.set_color(Color.grey)
add(playercard1)

playercard2 = Rectangle(50, 100)


playercard2.set_position(200, 350)
playercard2.set_color(Color.grey)
add(playercard2)
compcard1 = Rectangle(50, 100)
compcard1.set_position(100, 50)
compcard1.set_color(Color.grey)
add(compcard1)

compcard2 = Rectangle(50, 100)


compcard2.set_position(200, 50)
compcard2.set_color(Color.grey)
add(compcard2)
makecard()
addtext1()
addtext2()
addtext3()
addtext4()
addtext5()
addtext6()
addtext7()
addtext8()
addtext9()

#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

if playerwon == True and compwon == False:


print("the player won")
print("the player had four of a kind")
elif playerwon == False and compwon == True:
print("the computer won")
print("the computer had four of a kind")
elif playerwon == compwon :
playerwon = False
compwon = False
flush()

if playerwon == True and compwon == False:


print("the player won")
print("the player had a flush")
elif playerwon == False and compwon == True:
print("the computer won")
print("the comp had a flush")
elif playerwon == compwon:
playerwon = False
compwon = False
straight()
if playerwon == True and compwon == False:
print("the player won")
print("the player had a straight")
elif playerwon == False and compwon == True:
print("the computer won")
print("the comp had a straight")
elif playerwon == compwon:
playerwon = False
compwon = False
threekind()
if playerthreeofakind == True and compthreeofakind == False:
playerwon = True
elif playerthreeofakind == False and compthreeofakind == True:
compwon = True
elif playerthreeofakind == True and compthreeofakind == True:
if playerthreecards > compthreecards:
playerwon = True
elif compthreecards>playerthreecards:
compwon = True
if playerwon == True and compwon == False:
print("the player won")
print("the player had three of a kind")
elif playerwon == False and compwon == True:
print("the computer won")
print("the comp had three of a kind")
elif playerwon == compwon:
pair()
if playertwoofakind == True and comptwoofakind == False:
playerwon = True
elif playertwoofakind == False and comptwoofakind == True:
compwon = True
elif playertwoofakind == True and comptwoofakind == True:
if playertwocards > comptwocards:
playerwon = True
elif playertwocards < comptwocards:
compwon = True

if playerwon == True and compwon == False:


print("the player won")
print("the player had a pair")

elif compwon == True and playerwon == False:


print("the computer won")
print("the comp had a pair")
elif playerwon == compwon:
playerwon = False
compwon = False
highcard()
if playerhighcard> comphighcard:
print("The player has won by highcard")
elif comphighcard> playerhighcard:
print("The computer has won by highcard")
else:
if playersecondcard > compsecondcard:
print("The player has won by highcard")
elif compsecondcard> playersecondcard:
print("The computer has won by highcard")

You might also like