Python OOP - Deck of Cards

  Рет қаралды 157,520

Executed Binary

Executed Binary

Күн бұрын

Card, Deck, and Player classes written in Python 2.7.
You can use this as a starting point to write any card game you can think of!
The code can be found at:
github.com/eli...

Пікірлер: 156
@SprocketN
@SprocketN 3 жыл бұрын
Though this is an older version of Python than I use. It is the first video that explained classes in a way that I could understand enough to create my own class for a tile based game I’m writing, while learning Python. I have a lot yet to learn, but you have really helped. Thank you for taking the time to create the video and sharing it with us.
@ExecutedBinary
@ExecutedBinary 3 жыл бұрын
Thanks you! I'm really glad it was informative, I plan on making an update for python3 some day soon
@zuraqarchava3040
@zuraqarchava3040 2 жыл бұрын
@@ExecutedBinary Hello, how to contact you on social media?
@MenyeMC
@MenyeMC Жыл бұрын
I genuinly believe this is the best thing I have ever seen on youtube. I have been looking for an OOP example to help me wrap my head around it but all channels keep saying the same generic stuff. Thanks for this video.
@frambuella
@frambuella 2 жыл бұрын
Yooo! I just started learning oop in python last week and this is such a great way to learn about classes and objects using a deck of cards! Thanks for the video!
@xzex2609
@xzex2609 10 ай бұрын
if you want to have a good example of implementing somethin with OOP I suggest you to see JImShappedCoding developing a mine sweeper game in tkinter.
@jeffreykrenitsky6190
@jeffreykrenitsky6190 Жыл бұрын
One of the most helpful, informative, and succinct tutorials I've seen on KZbin.
@BaerenPole
@BaerenPole 5 жыл бұрын
the hdd sound makes me so chill :)
@aquacube
@aquacube 3 жыл бұрын
Yup, it was a fine sounding HDD 🙂
@jamesgebler5738
@jamesgebler5738 4 жыл бұрын
Thank you so much for this video! This is incredibly helpful. I was trying to understand how to create a deck of cards myself, and this solved my problem!
@lamedev1342
@lamedev1342 4 жыл бұрын
I struggled with classes but you really make me understand them better
@ExecutedBinary
@ExecutedBinary 3 жыл бұрын
Glad I could help!
@shubhampatel6908
@shubhampatel6908 2 жыл бұрын
I started learning OOP recently and this project was really awesome. It also cleared my OOP concepts. Soon I will create its python 3 version and send it to you if you wanna add it in your source code repository. Syntax was almost same in python 3 , so I guessn there will be no need😅
@ant1fact
@ant1fact 5 жыл бұрын
Great video. Instead of "for i in range(len(self.cards)-1, 0 , -1):" you could also use "for card in reversed(self.cards):"
@dylanboyd6147
@dylanboyd6147 Жыл бұрын
instead of writing a shuffling algorithm, you could use 'cards = random.shuffle(cards)'
@omerkaratas5875
@omerkaratas5875 Жыл бұрын
Perfect video to comprehand things about OPP. Thank you very much.
@diegodocarmoespindola9660
@diegodocarmoespindola9660 2 жыл бұрын
thank you, I enjoyed watching this, it's helping me to understand more about object oriented programming
@statuschannel8572
@statuschannel8572 4 жыл бұрын
2:37 you don't have to comment the other 2 classes just type "pass" inside of it python will know you don't like to execute those classes for now
@cringelevelunlocked7418
@cringelevelunlocked7418 4 жыл бұрын
His python is a bit old so probably it didn't had the "pass" in his version
@statuschannel8572
@statuschannel8572 4 жыл бұрын
@@cringelevelunlocked7418 see the description he is using python 2.7 and you can use 'pass' in that version.
@manav148
@manav148 6 жыл бұрын
Nice video! Some nit's : - The card value should be an independent dictionary. ex: CARD_VALUE = {1: A, 2 : 2 ......, 11: J, 12 : Q} - Suit should be a separate object: ex class SUIT: def __init_(self, n, s, c): self.name = n self.symbol = s self.color = c s = SUIT ("Diamonds", "◆", "Red")
@bobwalace5007
@bobwalace5007 3 жыл бұрын
OK ok
@barrettsmith5377
@barrettsmith5377 5 жыл бұрын
Awesome video! Would defo watch more Python videos if you put them out!
@malabikasen2401
@malabikasen2401 3 жыл бұрын
Thank you! This is a very nice video to get the concepts of OOP in
@aquacube
@aquacube 4 жыл бұрын
Great for me getting my head around implementing oop, ty.
@yolamontalvan9502
@yolamontalvan9502 10 ай бұрын
You’re an amazing teacher. But you only have 1 video.
@toniaalderbashi4964
@toniaalderbashi4964 4 жыл бұрын
This is a better algorithm for shuffling. def shuffle(self): """Shuffles the deck post: randomizes the order of cards in self""" n = self.size() cards = self.cards for i,card in enumerate(cards): pos = randrange(i,n) cards[i] = cards[pos] cards[pos] = card
@atlantic_love
@atlantic_love 4 жыл бұрын
so, swapping in place?
@tribacioustee2846
@tribacioustee2846 6 жыл бұрын
Good video. Exactly what I was looking for.
@jetspray3
@jetspray3 5 жыл бұрын
He started the class with an object argument, i think he is using python 2.
@stoneskull
@stoneskull 5 жыл бұрын
print statement gave it away
@caliapster8720
@caliapster8720 5 жыл бұрын
@@stoneskull XDDD
@Drytube
@Drytube 4 жыл бұрын
The object parameter for the classes is also used in python 3.
@5staryzzz
@5staryzzz 3 жыл бұрын
Cuz the Print statement lmao
@5staryzzz
@5staryzzz 3 жыл бұрын
@@stoneskull Exactly
@secretsather
@secretsather 3 жыл бұрын
random.shuffle is Fisher-Yates!
@appaesthetics1277
@appaesthetics1277 9 ай бұрын
Watched this 7 years later
@xzex2609
@xzex2609 10 ай бұрын
very good example .why don't you use shuffle method and pass your list? this way It's predictable
@fun-fauna
@fun-fauna 6 жыл бұрын
THIS IS AMAZING!!! PLEASE MAKE MORE VIDEOS!!!
@verdi0756
@verdi0756 6 жыл бұрын
Hello! What i dont really understand is how does the program know which show function are we refering to, the one we created in the Card class, or the otherone in the Deck class. Im a little confused with OOP yet hehe
@luvasluvasluvas
@luvasluvasluvas 3 жыл бұрын
when i try to print the player's hand it shows
@Kyriesosa11
@Kyriesosa11 2 ай бұрын
in the shuffle function, why go from 51 to 0 instead of 0 to 51?
@TheLionwarrior87
@TheLionwarrior87 6 жыл бұрын
No chill on the keys 😔
@barclayiversen376
@barclayiversen376 4 жыл бұрын
bruh.
@jennyjansson4997
@jennyjansson4997 3 жыл бұрын
i hate that af too. whyyy did he have to ruin such great tutorial
@schcharles
@schcharles 3 жыл бұрын
Exactly what I thought
@harishpuvvada1877
@harishpuvvada1877 6 жыл бұрын
Clear explanation. Thanks :)
@emzx111
@emzx111 7 жыл бұрын
this is great! thanks for the clarity
@FelipeV3444
@FelipeV3444 6 жыл бұрын
I had tried something like this before (in java tho, I know, I know, it's shitty) but I got stuck when it comes assigning a rank to no limit hold'em hands (high card, three of a kind, straight,...) let alone taking kickers into account... so that was kind of what I was looking for, but I was shuffling like crap and off course python is much better to use in general, so pretty good vid! Any ideas, maybe future content on how to actually code the game of texas hold'em, even without UI? That'd be awesome.
@iangannon8539
@iangannon8539 3 жыл бұрын
14:50 nice...
@AftabBaloch_
@AftabBaloch_ 2 жыл бұрын
thanks
@samdavepollard
@samdavepollard 6 жыл бұрын
Very nice. Thanks for sharing your knowledge. I've done similar in C# but only recently getting into Python so your vid was a must watch. In the deck's draw method should we not check first that we're not trying to draw from an empty deck? Thanks again.
@samdavepollard
@samdavepollard 6 жыл бұрын
Great stuff. Thanks, Eli.
@kidsmadeeasy9404
@kidsmadeeasy9404 6 жыл бұрын
He's the man im learning from this
@itech40
@itech40 4 жыл бұрын
Learn by making leetcode.com and other ! :D
@darrenjones3632
@darrenjones3632 7 жыл бұрын
So I am doing a Python course on Udemy and we were just introduced to OOP essentially and now a milestone project has us building a blackjack game. Should I really be able to come up with something like this on my own? Seems I am pretty far off from using my own reasoning to invent a whole deck of cards... Maybe I am not trying hard enough. Or maybe I need to go practice a lot more OOP before trying something this big?
@tamayo3861
@tamayo3861 6 жыл бұрын
Eli Byers you should be making more videos and commenting less!
@micahman33
@micahman33 6 жыл бұрын
felt the same way. I as getting frustrating trying it on my own so I was looking for a good explanation someplace of what should be objects and what shouldnt.
@mikey2176
@mikey2176 6 жыл бұрын
Udemy brought me here too :) Thanks for the video. I like your explanations.
@yummyramen2821
@yummyramen2821 6 жыл бұрын
@@ExecutedBinary do u think a 22 yr old like me is too late to learn to code ? I want to learn how to make web applications and make it as my career
@yummyramen2821
@yummyramen2821 6 жыл бұрын
I always doubt because most computer science dudes that i asked that even after a decade on the field its hard to become one
@sarvanandgaikwad3048
@sarvanandgaikwad3048 Жыл бұрын
Make videos bro I liked it.
@xxsurajbxx
@xxsurajbxx 5 жыл бұрын
what was the build(self) method in the deck class?
@oliverludwig6148
@oliverludwig6148 3 жыл бұрын
how would i make an irregular deck of cards? meaning not all suits have the same number of cards in it.
@xjiren4936
@xjiren4936 4 жыл бұрын
Getting a traceback error, It's saying my deck is not defined. Not sure where I'm going wrong.
@Mune.T
@Mune.T 3 жыл бұрын
What do i do when i want to have 2 times the same card in my deck? e.g. a deck with 8 ace of spades and 2 jokers.
@AndrewDangerously
@AndrewDangerously 3 жыл бұрын
Really helpful, thanks!
@nimrakhan2868
@nimrakhan2868 10 ай бұрын
How can we add player names? What's the code for that?
@csbnikhil
@csbnikhil 6 жыл бұрын
random.shuffle(self.cards) would've worked. 🤔
@shanemarchan658
@shanemarchan658 4 жыл бұрын
did that lol
@C0BALTWARRI0R19
@C0BALTWARRI0R19 3 жыл бұрын
Its my understanding that the fisher yates shuffle is the best way to ensure a mathematically random shuffle. Not sure how random.shuffle does it, but maybe OP didnt feel like it would be entirely truely random. IDK
@Yash42189
@Yash42189 3 жыл бұрын
Dude you need to make more videos!
@dnvrdavid
@dnvrdavid 7 жыл бұрын
Great tutorial You seem to have thought about adding a GUI. Have you actually done it? If you don't have one to share, can you advise us on how to do it. I love Python, but I might convert my code to Java in order to add the GUI. What would you do?
@dnvrdavid
@dnvrdavid 7 жыл бұрын
Thanks for the link! It is timely. I've been working with PyQt5 (converted this from PyQt4: github.com/eladj/Whist and want to adapt it to play bridge), but it wasn't easy. I might switch to wxPython based on your link, to add a gui to my own bridge game.... I'm looking forward to your next videos! One more question: You seem to do the OO design in your head and then just code it. For those of us who struggle with OO, do you have suggestions on how to design or plan before writing code (defining requirements, objects, attributes, methods)? e.g. How did you know you would need 3 classes here?
@1989TonyTu
@1989TonyTu 3 жыл бұрын
Can someone please explain to me what is happening at line 28?
@venumadhavrallapalli
@venumadhavrallapalli 3 жыл бұрын
what ... u can pass a class object as an argument inside a function ??? u passed deck in draw method inside player class !!!
@NishantPatel5
@NishantPatel5 7 жыл бұрын
Awesome video!! make more videos
@SarimFaruque
@SarimFaruque 3 жыл бұрын
I like this code, but I also want to divide the deck by half via a child class for each player to use, and still keep the properties of shuffling and drawing a card. Any way I could do that?
@ExecutedBinary
@ExecutedBinary 3 жыл бұрын
I would create a constructor for the Deck class that takes an array of cards, so you can initialize one with a partial set of cards instead of generating them. Then when create a split method that splits the cards and initializes two new deck instances with each half, and returns them. Keep at it!
@bradleaman
@bradleaman 5 жыл бұрын
in the def show function i keep getting invalid syntax on print card.show(). any idea why?
@hyksoscorp7831
@hyksoscorp7831 5 жыл бұрын
youre probably using python 3. he is using python 2 fix: print("{} of {}".format(self.value, self.suit))
@arshitchabukswar9741
@arshitchabukswar9741 7 жыл бұрын
If there were two or more players,there is a possibility that they draw the same card right? cause its random?
@kajekage9410
@kajekage9410 Жыл бұрын
This guy beats the hell out of his keyboard lol
@shanemarchan658
@shanemarchan658 4 жыл бұрын
11:39 instance of card class or deck class? trying to understand the show attachment u added
@srimanyadagiri2447
@srimanyadagiri2447 7 жыл бұрын
the show method is already present in the card class why do you define it again in the deck class?
@nikkster626
@nikkster626 6 жыл бұрын
I'm struggling with understanding this.. I get that you called the cards show method in the deck show method, but I don't see how the class Deck knew to use the method from the class Cards. What if the show method had been something else completely, say in the class Player? Does this mean you need to make sure all methods have very different names? Do you know any resources where I can find more info on this? Thanks!
@nikkster626
@nikkster626 6 жыл бұрын
That makes sense.. I guess I'm confused because I tried, for example, to take a method to draw a card in the Deck class, and use that method to place that card in the player's hand (in the Hand class).. however I keep getting errors saying Hand does not have a ___ method or attribute or what not.. maybe I am making an error in my formatting, not sure. Thank you!
@biohoo22
@biohoo22 4 жыл бұрын
Why not just random choice without replacement?
@fr34kthc
@fr34kthc 5 жыл бұрын
I am running an syntax error, any idea what might be the reason ? # Display all cards in the deck def show(self): for card in self.cards: print card.show()
@Drytube
@Drytube 4 жыл бұрын
put 'print(card.show())'
@ruddha2
@ruddha2 5 жыл бұрын
Thanks for this.
@benpritchard2716
@benpritchard2716 7 жыл бұрын
How do you make it so 11:jack 12:queen 13:king 14:ace ? With a dictionary?
@manualvarado2212
@manualvarado2212 7 жыл бұрын
You could do something like this: def build_deck(self): for suit in ["hearts", "diamonds", "clubs", "spades"]: for value in "A,2,3,4,5,6,7,8,9,10,J,Q,K".split(",") #or directly a list ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]: self.cards.append(Card(suit, value))
@mateuschwarz
@mateuschwarz 4 жыл бұрын
Define a global dictionary, or use named tuples
@manualvarado2212
@manualvarado2212 7 жыл бұрын
Thank you so much!
@curtissnowden5382
@curtissnowden5382 6 жыл бұрын
Could someone help me with turning this method into C++ code?
@zanderox
@zanderox 7 жыл бұрын
How would you change the values of the cards past 10 to jack Queen king instead of 11,12,13? Also ace instead of 1?
@zanderox
@zanderox 7 жыл бұрын
Ah, I just changed the build to this ''' def build(self): for s in ["Spades, Clubs, Diamonds, Hearts"]: for v in ["A",2,3,4,5,6,7,8,9,10,"J","Q","K"]: print "{} of {}".format(v, s) ''' Instead of using for v in range (1,14)
@atlantic_love
@atlantic_love 4 жыл бұрын
In your card class you have 3 variables: suit, value and image_name (if you're wanting to easily blit the cards to a gui.....use a list containing the names of the faces, and then take any value about 10 and subtract 11 from it to get the element number which you use to get the name of the card face.
@ivantorres2937
@ivantorres2937 3 жыл бұрын
Thank you so much for this tutorial, it's very helpful!! I'm noob in programming, could someone explainme why can't I see the information using the "show()" method of "Card" class? --> print(deck.cards[2].show()) it return "None" Thanks!!!!
@ExecutedBinary
@ExecutedBinary 3 жыл бұрын
This is probably because your Cards show method prints a string instead of returning one. So when you try and print the return value of show you get the implicit None return value. You can either just call show and not print it, or you can have show return a string instead of printing it inside. Keep it up!
@genroynoisis6980
@genroynoisis6980 4 жыл бұрын
thank you so much
@behbryan1
@behbryan1 6 жыл бұрын
Thank you Eli for the informative video! Just wondering how I could store the values of the cards in the player's hand so I can do indexing and sum them up. Regular indexing does not work!
@melfaraj4633
@melfaraj4633 4 жыл бұрын
Need to know how to make the cards graphically for a realistic game
@atlantic_love
@atlantic_love 4 жыл бұрын
download a card pack. Google playing cards download. Or, you can take pictures of actual playing cards, and crop and resize the images, and then copy those images into your program folder.
@lukaknez2171
@lukaknez2171 7 жыл бұрын
what package are you using for printing Python?
@kevinvidzzz5393
@kevinvidzzz5393 7 жыл бұрын
good stuff
@grovest4life228
@grovest4life228 2 жыл бұрын
awesome pawsome
@JoshPaulie
@JoshPaulie 3 жыл бұрын
Excuse me, just finding my jaw. It's somewhere on this floor...
@ExecutedBinary
@ExecutedBinary 3 жыл бұрын
Hey Josh, I hope you found it!
@R0adx
@R0adx 4 жыл бұрын
Hi, what syntax theme is this?
@georgioschristopoulos3303
@georgioschristopoulos3303 7 жыл бұрын
Can you continue this project with an other video ?
@sexayboiee
@sexayboiee 7 жыл бұрын
I really think you should make a part 2 and implement some card games like Texas holdem and Solitaire.
@georgioschristopoulos3303
@georgioschristopoulos3303 7 жыл бұрын
Card games with GUI. Any card game would be nice.
@gloriaramlal8027
@gloriaramlal8027 6 жыл бұрын
How about creating a game? What we've got is just cards :)
@atlantic_love
@atlantic_love 4 жыл бұрын
@@gloriaramlal8027 Without the ability to create and manipulate a deck of cards, there isn't a single card game you can make. Baby steps.
@andrejjanecka2627
@andrejjanecka2627 6 жыл бұрын
Hi, did you also implement Jokers?
@marioandresheviacavieres1923
@marioandresheviacavieres1923 3 жыл бұрын
thanks!!!
@leestons
@leestons 6 жыл бұрын
How would I calculate the value of a hand? For example, a Jack and a 4. Thanks!
@marvgarza
@marvgarza 4 жыл бұрын
Hey brother I got a question for you if you have the time... My question is... Have you ever done Baccarat Simulation at all I mean the rules the values and all the data I'm a complete newbie. Show me if you have or if you haven't please create it and show how you would go about it. Thank You. It's my dream to see full simulation of this game in widest parameters.
@ParzivalHomage
@ParzivalHomage 4 жыл бұрын
does anybody know why did he inherit 'object' in classes(deck,card and player) ? and how did he use variable of one class or function, in another ? when I try and d it, it doesnot work. I usually have do mention the name of the object where the variable exists.(example: )
@carlospimentel_leantech4294
@carlospimentel_leantech4294 3 жыл бұрын
I also got myself wondering why to explicitly inherit from object. As far as I am aware in Python 3.x there is no need for explicitly do that since it is already inheriting by default. You can refer to the book "Fluent Python" and it talks about that.
@flosalz87
@flosalz87 4 жыл бұрын
Hey there, is there a way to reach you?
@samalextij445
@samalextij445 4 жыл бұрын
how would you get the program to draw more than 1 card
@manuelsoares4343
@manuelsoares4343 4 жыл бұрын
you run the draw method multiple times
@atlantic_love
@atlantic_love 4 жыл бұрын
@@manuelsoares4343 Create a deal() method that accepts an argument representing the number of cards to draw. Then, use a loop to draw the cards and place those cards in a list. Finally, return the list. Or, as Manual Soares said, you can just invoke the method X number of times. But that doesn't scale well, it's a time consuming to code and creates unnecessary clutter.
@juggernautj828
@juggernautj828 3 жыл бұрын
Thanks 15
@mailchibi88
@mailchibi88 4 жыл бұрын
While this video was informative, its hard to keep up if you're changing variable names without warning. I had to keep backtracking just to fix these issues. Furthermore if you want people to follow along maybe consider slowing down a bit. Beginners like myself do not necessarily have the same programming logic as you when they're first going through this exercise. Looking back at the code I'm able to see how everything works but it can be exceedingly frusterating to keep backtracking to figure out where an error has been made. :|
@willmoon4400
@willmoon4400 6 жыл бұрын
Is there any code to copy?
@adkoxderramoney2933
@adkoxderramoney2933 3 жыл бұрын
do you have instegram
@joeblowmha
@joeblowmha 7 жыл бұрын
make more videos
@trinahaque8508
@trinahaque8508 7 жыл бұрын
everything
@ross5554
@ross5554 7 жыл бұрын
Eli Byers I have been looking for a game of life tutorial in Python, but couldn't find any. Could you make it? and if it is using OOP, better than better. THANKS.
@BrianGarside
@BrianGarside 6 жыл бұрын
Eli, could you post the script for this? Great example of OOP. Thanks!
@c4sper877
@c4sper877 4 жыл бұрын
Video description; always check the video description! 😜😏
@johnnycincocero
@johnnycincocero 3 жыл бұрын
I love poop!
@ExecutedBinary
@ExecutedBinary 2 жыл бұрын
Thank you for getting this
@davidjohnhamm
@davidjohnhamm 3 жыл бұрын
why is your computer full of ping pong balls?
@yogi583
@yogi583 5 жыл бұрын
What kind of theme do you use, it looked like a good one.
@p.s.758
@p.s.758 5 жыл бұрын
Do you know it?
@chriss2295
@chriss2295 6 жыл бұрын
Great content....horrible audio . Eek
@Ferventus
@Ferventus 2 жыл бұрын
Your code doesnt even function.
@ExecutedBinary
@ExecutedBinary 2 жыл бұрын
This code is written in python 2. I will make a python 3 version at some point.
@chachacha8810
@chachacha8810 4 жыл бұрын
.shuffle()
@statixsc3013
@statixsc3013 3 жыл бұрын
was a good video but for the love of god saying right all the time had me mute u after 6:50
@anonymous.8393
@anonymous.8393 3 жыл бұрын
self.name
@chrisnorthall8317
@chrisnorthall8317 Жыл бұрын
had to stop listening, the keyboard bashing was driving me nuts.
@atlantic_love
@atlantic_love 4 жыл бұрын
Try making a video on transforming a use case into a class model. otherwise, this is all pointless.
@zuraqarchava3040
@zuraqarchava3040 2 жыл бұрын
Hello, how to contact you on social media?
Python Object Oriented Programming (OOP) - For Beginners
53:06
Tech With Tim
Рет қаралды 3,3 МЛН
Create Blackjack in Python | Beginner Friendly Tutorial
24:43
Code Coach
Рет қаралды 59 М.
When you discover a family secret
00:59
im_siowei
Рет қаралды 33 МЛН
❌Разве такое возможно? #story
01:00
Кэри Найс
Рет қаралды 6 МЛН
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 59 М.
Python Object Oriented Programming in 10 minutes 🐍
10:04
Bro Code
Рет қаралды 384 М.
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
5 Tips For Object-Oriented Programming Done Well - In Python
16:08
Python Resume Projects - You Can Finish in a Weekend
10:38
Tech With Tim
Рет қаралды 1,5 МЛН
Top 18 Most Useful Python Modules
10:50
Tech With Tim
Рет қаралды 929 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 302 М.
Object Oriented Programming (OOP) in Python
46:37
Traversy Media
Рет қаралды 280 М.
Building the Card Game "War" in Python Part 1
33:49
Keyfaze
Рет қаралды 3,1 М.
Python OOP Tutorial 1: Classes and Instances
15:24
Corey Schafer
Рет қаралды 4,4 МЛН