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
@SprocketN3 жыл бұрын
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.
@ExecutedBinary3 жыл бұрын
Thanks you! I'm really glad it was informative, I plan on making an update for python3 some day soon
@zuraqarchava30402 жыл бұрын
@@ExecutedBinary Hello, how to contact you on social media?
@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.
@frambuella2 жыл бұрын
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!
@xzex260910 ай бұрын
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 Жыл бұрын
One of the most helpful, informative, and succinct tutorials I've seen on KZbin.
@BaerenPole5 жыл бұрын
the hdd sound makes me so chill :)
@aquacube3 жыл бұрын
Yup, it was a fine sounding HDD 🙂
@jamesgebler57384 жыл бұрын
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!
@lamedev13424 жыл бұрын
I struggled with classes but you really make me understand them better
@ExecutedBinary3 жыл бұрын
Glad I could help!
@shubhampatel69082 жыл бұрын
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😅
@ant1fact5 жыл бұрын
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 Жыл бұрын
instead of writing a shuffling algorithm, you could use 'cards = random.shuffle(cards)'
@omerkaratas5875 Жыл бұрын
Perfect video to comprehand things about OPP. Thank you very much.
@diegodocarmoespindola96602 жыл бұрын
thank you, I enjoyed watching this, it's helping me to understand more about object oriented programming
@statuschannel85724 жыл бұрын
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
@cringelevelunlocked74184 жыл бұрын
His python is a bit old so probably it didn't had the "pass" in his version
@statuschannel85724 жыл бұрын
@@cringelevelunlocked7418 see the description he is using python 2.7 and you can use 'pass' in that version.
@manav1486 жыл бұрын
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")
@bobwalace50073 жыл бұрын
OK ok
@barrettsmith53775 жыл бұрын
Awesome video! Would defo watch more Python videos if you put them out!
@malabikasen24013 жыл бұрын
Thank you! This is a very nice video to get the concepts of OOP in
@aquacube4 жыл бұрын
Great for me getting my head around implementing oop, ty.
@yolamontalvan950210 ай бұрын
You’re an amazing teacher. But you only have 1 video.
@toniaalderbashi49644 жыл бұрын
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_love4 жыл бұрын
so, swapping in place?
@tribacioustee28466 жыл бұрын
Good video. Exactly what I was looking for.
@jetspray35 жыл бұрын
He started the class with an object argument, i think he is using python 2.
@stoneskull5 жыл бұрын
print statement gave it away
@caliapster87205 жыл бұрын
@@stoneskull XDDD
@Drytube4 жыл бұрын
The object parameter for the classes is also used in python 3.
@5staryzzz3 жыл бұрын
Cuz the Print statement lmao
@5staryzzz3 жыл бұрын
@@stoneskull Exactly
@secretsather3 жыл бұрын
random.shuffle is Fisher-Yates!
@appaesthetics12779 ай бұрын
Watched this 7 years later
@xzex260910 ай бұрын
very good example .why don't you use shuffle method and pass your list? this way It's predictable
@fun-fauna6 жыл бұрын
THIS IS AMAZING!!! PLEASE MAKE MORE VIDEOS!!!
@verdi07566 жыл бұрын
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
@luvasluvasluvas3 жыл бұрын
when i try to print the player's hand it shows
@Kyriesosa112 ай бұрын
in the shuffle function, why go from 51 to 0 instead of 0 to 51?
@TheLionwarrior876 жыл бұрын
No chill on the keys 😔
@barclayiversen3764 жыл бұрын
bruh.
@jennyjansson49973 жыл бұрын
i hate that af too. whyyy did he have to ruin such great tutorial
@schcharles3 жыл бұрын
Exactly what I thought
@harishpuvvada18776 жыл бұрын
Clear explanation. Thanks :)
@emzx1117 жыл бұрын
this is great! thanks for the clarity
@FelipeV34446 жыл бұрын
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.
@iangannon85393 жыл бұрын
14:50 nice...
@AftabBaloch_2 жыл бұрын
thanks
@samdavepollard6 жыл бұрын
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.
@samdavepollard6 жыл бұрын
Great stuff. Thanks, Eli.
@kidsmadeeasy94046 жыл бұрын
He's the man im learning from this
@itech404 жыл бұрын
Learn by making leetcode.com and other ! :D
@darrenjones36327 жыл бұрын
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?
@tamayo38616 жыл бұрын
Eli Byers you should be making more videos and commenting less!
@micahman336 жыл бұрын
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.
@mikey21766 жыл бұрын
Udemy brought me here too :) Thanks for the video. I like your explanations.
@yummyramen28216 жыл бұрын
@@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
@yummyramen28216 жыл бұрын
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 Жыл бұрын
Make videos bro I liked it.
@xxsurajbxx5 жыл бұрын
what was the build(self) method in the deck class?
@oliverludwig61483 жыл бұрын
how would i make an irregular deck of cards? meaning not all suits have the same number of cards in it.
@xjiren49364 жыл бұрын
Getting a traceback error, It's saying my deck is not defined. Not sure where I'm going wrong.
@Mune.T3 жыл бұрын
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.
@AndrewDangerously3 жыл бұрын
Really helpful, thanks!
@nimrakhan286810 ай бұрын
How can we add player names? What's the code for that?
@csbnikhil6 жыл бұрын
random.shuffle(self.cards) would've worked. 🤔
@shanemarchan6584 жыл бұрын
did that lol
@C0BALTWARRI0R193 жыл бұрын
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
@Yash421893 жыл бұрын
Dude you need to make more videos!
@dnvrdavid7 жыл бұрын
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?
@dnvrdavid7 жыл бұрын
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?
@1989TonyTu3 жыл бұрын
Can someone please explain to me what is happening at line 28?
@venumadhavrallapalli3 жыл бұрын
what ... u can pass a class object as an argument inside a function ??? u passed deck in draw method inside player class !!!
@NishantPatel57 жыл бұрын
Awesome video!! make more videos
@SarimFaruque3 жыл бұрын
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?
@ExecutedBinary3 жыл бұрын
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!
@bradleaman5 жыл бұрын
in the def show function i keep getting invalid syntax on print card.show(). any idea why?
@hyksoscorp78315 жыл бұрын
youre probably using python 3. he is using python 2 fix: print("{} of {}".format(self.value, self.suit))
@arshitchabukswar97417 жыл бұрын
If there were two or more players,there is a possibility that they draw the same card right? cause its random?
@kajekage9410 Жыл бұрын
This guy beats the hell out of his keyboard lol
@shanemarchan6584 жыл бұрын
11:39 instance of card class or deck class? trying to understand the show attachment u added
@srimanyadagiri24477 жыл бұрын
the show method is already present in the card class why do you define it again in the deck class?
@nikkster6266 жыл бұрын
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!
@nikkster6266 жыл бұрын
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!
@biohoo224 жыл бұрын
Why not just random choice without replacement?
@fr34kthc5 жыл бұрын
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()
@Drytube4 жыл бұрын
put 'print(card.show())'
@ruddha25 жыл бұрын
Thanks for this.
@benpritchard27167 жыл бұрын
How do you make it so 11:jack 12:queen 13:king 14:ace ? With a dictionary?
@manualvarado22127 жыл бұрын
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))
@mateuschwarz4 жыл бұрын
Define a global dictionary, or use named tuples
@manualvarado22127 жыл бұрын
Thank you so much!
@curtissnowden53826 жыл бұрын
Could someone help me with turning this method into C++ code?
@zanderox7 жыл бұрын
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?
@zanderox7 жыл бұрын
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_love4 жыл бұрын
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.
@ivantorres29373 жыл бұрын
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!!!!
@ExecutedBinary3 жыл бұрын
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!
@genroynoisis69804 жыл бұрын
thank you so much
@behbryan16 жыл бұрын
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!
@melfaraj46334 жыл бұрын
Need to know how to make the cards graphically for a realistic game
@atlantic_love4 жыл бұрын
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.
@lukaknez21717 жыл бұрын
what package are you using for printing Python?
@kevinvidzzz53937 жыл бұрын
good stuff
@grovest4life2282 жыл бұрын
awesome pawsome
@JoshPaulie3 жыл бұрын
Excuse me, just finding my jaw. It's somewhere on this floor...
@ExecutedBinary3 жыл бұрын
Hey Josh, I hope you found it!
@R0adx4 жыл бұрын
Hi, what syntax theme is this?
@georgioschristopoulos33037 жыл бұрын
Can you continue this project with an other video ?
@sexayboiee7 жыл бұрын
I really think you should make a part 2 and implement some card games like Texas holdem and Solitaire.
@georgioschristopoulos33037 жыл бұрын
Card games with GUI. Any card game would be nice.
@gloriaramlal80276 жыл бұрын
How about creating a game? What we've got is just cards :)
@atlantic_love4 жыл бұрын
@@gloriaramlal8027 Without the ability to create and manipulate a deck of cards, there isn't a single card game you can make. Baby steps.
@andrejjanecka26276 жыл бұрын
Hi, did you also implement Jokers?
@marioandresheviacavieres19233 жыл бұрын
thanks!!!
@leestons6 жыл бұрын
How would I calculate the value of a hand? For example, a Jack and a 4. Thanks!
@marvgarza4 жыл бұрын
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.
@ParzivalHomage4 жыл бұрын
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_leantech42943 жыл бұрын
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.
@flosalz874 жыл бұрын
Hey there, is there a way to reach you?
@samalextij4454 жыл бұрын
how would you get the program to draw more than 1 card
@manuelsoares43434 жыл бұрын
you run the draw method multiple times
@atlantic_love4 жыл бұрын
@@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.
@juggernautj8283 жыл бұрын
Thanks 15
@mailchibi884 жыл бұрын
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. :|
@willmoon44006 жыл бұрын
Is there any code to copy?
@adkoxderramoney29333 жыл бұрын
do you have instegram
@joeblowmha7 жыл бұрын
make more videos
@trinahaque85087 жыл бұрын
everything
@ross55547 жыл бұрын
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.
@BrianGarside6 жыл бұрын
Eli, could you post the script for this? Great example of OOP. Thanks!
@c4sper8774 жыл бұрын
Video description; always check the video description! 😜😏
@johnnycincocero3 жыл бұрын
I love poop!
@ExecutedBinary2 жыл бұрын
Thank you for getting this
@davidjohnhamm3 жыл бұрын
why is your computer full of ping pong balls?
@yogi5835 жыл бұрын
What kind of theme do you use, it looked like a good one.
@p.s.7585 жыл бұрын
Do you know it?
@chriss22956 жыл бұрын
Great content....horrible audio . Eek
@Ferventus2 жыл бұрын
Your code doesnt even function.
@ExecutedBinary2 жыл бұрын
This code is written in python 2. I will make a python 3 version at some point.
@chachacha88104 жыл бұрын
.shuffle()
@statixsc30133 жыл бұрын
was a good video but for the love of god saying right all the time had me mute u after 6:50
@anonymous.83933 жыл бұрын
self.name
@chrisnorthall8317 Жыл бұрын
had to stop listening, the keyboard bashing was driving me nuts.
@atlantic_love4 жыл бұрын
Try making a video on transforming a use case into a class model. otherwise, this is all pointless.