Blackjack game java code


















This will make the game a lot less fun. The way I would handle this is to actually simulate all thirteen possible ranks with the numbers 2—13 in your random loop. Your Card class would then have a blackjackValue method that you can use for scoring purposes. Another option would be to build an array with all the possible scores and select a random element.

You'd put the value ten in this array four times, and the nine values all go in there once. This is slightly slower and more complex, but it gets you closer to a real shuffling algorithm. If you're confident your Card will never be used for anything but Blackjack, you could just call your method points , but in my experience you should never assume your classes won't be reused for something you didn't foresee.

Of course there's a deeper issue with your scheme, which is that in a real game the deck has a "memory" of which cards have already been played. This is the effect that allows card counters to gain a statistical advantage in Blackjack. I think it's reasonable to ignore this effect for a simple simulated game, but if you wanted a next step you might look up shuffling algorithms: they're not too hard to implement. Real Vegas casinos typically use something like six decks and reshuffle when they're roughly halfway through.

That's enough cards that your random-card approach is going to be pretty close to real most of the time. Going with the other answers, you need to create a cards class member of decks or similar to put the card information in. You need to make sure you have at least 52 unique cards Ace through to King, one of each suit to create a deck. The reason for this is being able to reuse the code, and for Blackjack's rules about Aces: they can be a 1 or an 11, depending on the player.

Currently, they could be dealt two "11"'s, and they would instantly bust. Definitely not fun. If they are dealt an Ace and a Nine, and they hit and get a Ten, they need to treat the first Ace as just a 1, not as an Sign up to join this community.

The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Text-based BlackJack game Ask Question. Asked 7 years, 2 months ago. Active 7 years, 2 months ago. Viewed 21k times. Is it a good structure? Is it easy to read? The JavaDoc; what could I improve? I am not including the package or imports for space reasons This code has a recursive call back to main.

Main: In this game I have chosen to make it as simple as I can whilst still functional. Let's play! And user goes always first. It returns the winner. She will hit until her hand is It will then draw and display the initial hand of two cards. Improve this question. Pimgd Gemtastic Gemtastic 1 1 gold badge 10 10 silver badges 21 21 bronze badges. Add a comment. Active Oldest Votes. Does BlackJackGame represent a single hand or a set of hands? Why does BlackJackGame need to be prompted to ask for a new game Future Features You have Card s.

Blackjack in casino's has betting, of course. That's the point of playing blackjack. There is good breakdown of the code into classes and methods, good utilization of OO paradigms like data abstraction, proper use of features like enums.

However, best practice convention is for a class to provide a String representation by overriding toString method and let the caller decide what to do with that String maybe print to stderr?

I would go with 2nd option since it allows caller to get whatever they wish from Face instance variable. However, you should know that Java has built-in feature to shuffle collections like List and such. I have no idea if you uploaded your latest code, but this one here doesn't seem to work. Actually that's a reason to close this topic:. Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability.

We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review. I made this review for you anyways, but keep this in mind for future questions. I uploaded a working version of your code below. Maybe you saw the waring java gave you, that your scanner is not closed at all. In this example it doesn't make a difference, but it's good practice. Since there are Face. Actually your can make all of your private fields final. Good practice. Since msot of your fields are final, you may remove the getters and make the fields prublic instead.

I fixed this in my example. Just don't. Instead return a String, which can be printed by whoever called your method. You hold your deck in an array. Because of this you need to "move" the cards in the deck after getting the top most.

You may better use a Collection type to hold them. Since order is important you man use an implementation of List. Then the whole method dealNextCard would reduce to:. This also would remove the need to track the number of cards left since you can call isEmpty on any Collection.

Code Revisions 1 Stars 10 Forks Embed What would you like to do? Embed Embed this gist in your website. Share Copy sharable link for this gist.

Learn more about clone URLs. Download ZIP. A game of blackjack in Java. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters. Copy link. I like it.

Sign up for free to join this conversation on GitHub. Answered by JamesCherrill 4, in a post from 9 Years Ago. Yes, something along those lines is OK, at least for now Then you have collection eg … Jump to Post. Would the skeleton look something along these lines? Two last questions: How would you deal with user input? I want to do this for the third time I write the program Thanks for your help. That's absolutely great, thank you. You've helped me a lot!

Thanks again, guess I will see you for my 3rd attempt in a couple of days, haha! Glad to help. Finished my second program, is it OK to post the code here or do I need to make a new topic? Facebook Like. Twitter Tweet. Be a part of the DaniWeb community. Sign Up — It's Free! Reply to this Topic.



0コメント

  • 1000 / 1000