How to program blackjack in python


















For example, your clear method doesn't reset points. That leads to a couple problems:. You'll need to remember to change the string in both the constructor and use-sites if you ever refactor.

IDE refactoring won't be able to help you with that. Then, the user can call populate instead of passing a string. You could also, either instead of populate, or along with it, have a "pseudo-constructor" class method:.

You're having each doing multiple jobs based on a passed string. Trying to anticipate future needs and creating new features ahead of time can be helpful, but also remember: in many cases, you aren't going to need those features. Can you ever, within the context of a blackjack deck of cards, see ever needing to add a card to multiple positions or really, would you ever add a card to a blackjack deck after the deck is formed?

I think normal indexing of a list would suffice here. Just like before, messing around with strings to dispatch behavior is messy and can lead to errors.

This method can utilize the. Your current implementation leaves a "," at the end of the string. Take a look:. Does the exact same thing yours does, but in one line. This function can also be written in one line. Since you're checking one boolean expression, and returning a value either way, the while True: loop isn't necessary.

There is also an easier way to return a boolean. Since you are evaluating a boolean expression, and returning boolean values, you can simply return the expression. Since it returns a boolean value, that value will be returned. While it may seem like a longer line, it reduces line count, which is almost always a good thing.

There is a bug in this method. It allows the user to bet 0 and start the game. Take a look at the first line in the method:.

Here's an example:. This says that the parameters amount should be an integer when passed, and that the method returns an integer. While this may be a trivial example, it helps as you write bigger and more expandable programs. It also helps people reading your code understand what types the method accepts.

Note: There is a subtle difference between the adjustment of scores for the player and the dealer. In the former case, the value of the first card is adjusted, whereas, in the latter, the value of the second card is adjusted. The reason for adjusting the value of the second card is that, had we adjusted the first one, we would have revealed the identity of the hidden card as an Ace.

One final thing that needs to be done here is checking whether the player already has a Blackjack. If he does, the player wins and the game ends. This prevents a quick fall-through of all the game events. The clear function is responsible for clearing the terminal giving a clean aesthetic for the game. The player decides whether to hit or stand until the score exceeds 21 or the player decides to stand. There is no limit to the number of cards dealt to the player, just on the score.

Every time the player decides to hit, a new card is dealt from the deck and the score is updated. As mentioned before, an Ace can be counted as 1 or The player stands when he is satisfied with the current score. The Dealer won. See you soon Himan10 Here is something you can add.

Wat is uw bankrekeningnummer? Uw bankgegevens zijn niet gevonden in onze database. Wilt U zo vriendelijk zijn ze opnieuw in te toetsen? U zult uw inzet terugkrijgen wanneer U nu stopt' self. Mogen wij U een aangenaam verstrooiing toewensen' pass elif self. Hi Himan10, This is my code. Have fun. Greetings, Emiel. How would we do this with a dictionary instead of a list? I like it. Looks good. Sign up for free to join this conversation on GitHub.

Already have an account? Sign in to comment. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window.

Hey, I pretty much told the OP to drop his class, so I don't know how you can call me nurturing after that! Unless it's "tough love".. Show 1 more comment. Some info that might be helpful, though: Regarding the generation of numbers between , ace, king, queen, and jack: it might be helpful to assign each card a numeric index.

Blair Conrad Blair Conrad k 25 25 gold badges silver badges bronze badges. Hi, thanks for your answer, but im really having trouble with the "Hit" and "Stand" stuff, i just can't seem to find anything that would help, can you please explain some more, or link me to somewhere? Please, you'll make me blush. Add a comment. John Fouhy John Fouhy One of my users pointed out that he could: Enter a negative number as a bet. Deliberately lose.

Which is of course equivalent to adding a positive number to the total. Sometimes, you can win by losing Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.



0コメント

  • 1000 / 1000