Blackjack java card class

I am very new to Java and programming theory and desperately trying to improve my knowledge. This is the first program I've made without help and really would appreciate some feedback. I know there must be 1, better ways to do what I did. Any and all comments are appreciated, really want to get to grips with basic Java programming concepts.

A couple of quick observations: You repeat the code to get the numeric value of a card a few times - repeating code is never agood idea. You can put all that into a method public int getValue String card I can't do a complete into to OO here- there are lots aleady on the web, but for a card game you typically start with Card class with members rank Ace,2 etc and suit.

Then you have collection eg …. In that method you can use a switch to avoid all those nested if statements.

For a more "object oriented" approach you would create a Card class that contains the name of the card and its numeric value. You'll fund that tidies up and clarifies the code quite a lot. You repeat the code to get the numeric value of a card a few times - repeating code is never agood idea. What else would I need to add to get this functional? How do I avoid the 'cannot execute static reference from a non-static reference' error that always hits me?

That's great thank you! I am to make the program a total of 3 times, improving it each time. Given me a great foundation, thanks. I want to do this for the third time I write the program. Then you have collection eg ArrayList of 52 Cards that you can shuffle and deal from. You have a Player class which has a collection "hand" of Cards and methods to determine whnether that Player wants to twist or whatever.

YOu may have a specialised sub-class of Player for the Dealer because the Dealer is almost but not quite the same as an ordinary player. All this puts you in a position where you can code the top-level so its so clear that it needs no comments This one's marked "solved", so people may not realise there's new info. Better to start a new topic. How could I achieve this program using methods and OOP?

This is the biggest problem I want to fix. Congratulations, You Win! You Lose! Thanks for your time! Edited 7 Years Ago by speakon.For this project you will write a class named Blackjack that implements the game logic associated with a blackjack card game. The Blackjack class implements the BlackjackEngine Java interface whose definition you can find in the project's javadoc documentation. Unlike other class projects, for this project you are allowed to discuss or receive help from your classmates, but you may not exchange any code.

Coding Blackjack in Java

Every student must implement their own code. If you are assisted by a student add the name of the student to the top of the Blackjack. This project will give you practice using classes, ArrayList and other fundamental Java Constructs. The project's code distribution is available by checking out the project named Spring19Project1.

The code distribution provides you with the following:. If you have been to Vegas then you are familiar with the rules to play blackjack also known as However, what happens in Vegas stays in Vegas, therefore the rules for blackjack in this project are different. Note that the rules we will use are not exactly the same ones used in this online game. In our blackjack version we have a dealer person who shuffles and distribute cards and only one player.

blackjack java card class

The game's objective is for the player to beat the dealer by generating a hand of cards whose value is higher than the dealer's hand without exceeding a total value of One of the dealer's cards will be face down. If a value greater than 21 is generated the player wins.

Otherwise whoever player or dealer has the hand with the highest value will win the game. The following provides additional information about the game:. Game Video illustrates the functionality associated with the application.

Submit your project using the "Submit Project" option available by right clicking on the project folder. Please make sure you read the academic integrity section of the syllabus so you understand what is permissible in our programming projects. We want to remind you that we check your project against other students' projects and any case of academic dishonesty will be referred to the Office of Student Conduct.

Web Accessibility. Submission Submit your project using the "Submit Project" option available by right clicking on the project folder. Academic Integrity Please make sure you read the academic integrity section of the syllabus so you understand what is permissible in our programming projects.Stijn Rensen wrote: My first reaction is that you might want to consider a fourth class. Just like a deck holds more cards, a round of play includes more players with holdings.

Guillermo Ishi wrote: If you put all the cards in an arraylist you can do this - choose a random number between 0 and its size-1 51 to start with. Use that random number as an index into the arraylist.

That will be the card you are dealing. Remove the element with that index from the arraylist from from the arraylist. That gives you a new upper bound for the next random number. The arraylist elements can be a Card object with suit and value fields. The best ideas are the crazy ones.

If you have a crazy idea and it works, it's really valuable. Player 2: cards Forum: Java in General. Using my Hand, Deck, and Card class together. In the driver I have to ask the user, how many players there are going to be as well as how many cards in the hand. Here's what I have so far, I'll try to post only the necessary code to keep it short.

How would I go about creating hands for multiple players based on user input? Also, when I ask for how many cards are in a hand, and they input 15 cards a hand if there are to be 5 players? Since it can't that can't be done, where would I do the checking for that? I understood what I had to do when it was just the two classes Card and Deck but once the third class was introduced it just totally screwed my logic up.

I know this it's rather long, but a push in the right direction would be greatly appreciated. Stijn Rensen. My first reaction is that you might want to consider a fourth class. The criteria had only asked for the 3 classes though along with the driver.

What would the fourth class do exactly? Oke my understanding of the english language might restrict me here.

But i would consider the following: Class 1 the deck. As described Class 2 the card. As described Class 3 a game or a round of cards Class 4 a player. Stephan van Hulst. Saloon Keeper. Just take a step back.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. If nothing happens, download GitHub Desktop and try again. If nothing happens, download Xcode and try again. If nothing happens, download the GitHub extension for Visual Studio and try again.

Java program that simulates a single player card game of BlackJack, between the user and computer. I designed my classes with High Cohesion and Low Coupling in mind. I made the purpose of each of my classes very specific and created them to have low dependencies. Contains the main method that orchestrates and runs the game of Black Jack. Also contains a method that gives the user the option to either play again or quit. Jack, King, and Queens are represented by a value of 10 This means you are three times as more likely to draw a '10' than any other card.

Represents a standard deck of 52 cards. This class can shuffle, deal, and display the initial draw for each player. This class determines the status and score of each player based on the rules of the game. This class knows both the name of the dealer and player, and can declare either a winner or draw.

A Parent class that represents an arbitrary player's current hand. This class knows how to add cards, retrieve the number of cards in hand, and can print out the information. A child class of PlayersHand that represents the human player of the game. This class prompts the user for their name, and keeps track of their hand. A child class of PlayersHand that represents the dealer of the game. The dealer is the computer and was created using a simple Artificial Intelligence algorithm that makes strategic decisions based on the user's performance and the rules of the game.

To minimize code duplication, I created a Parent class, called PlayersHand, which contains all the methods that are needed for an arbitrary player's hand, such as adding and accessing cards. The child classes of PlayersHand, Dealer and Player representing the dealer and human player of the game, respectivelyinherit the state and behavior of PlayersHand, but also have added additional information that are specific to the Player and Dealer.

For instance, the Dealer implements an Artificial Intelligence algorithm, while the Player uses a Scanner object in its constructor that prompts the human player to enter their name. In the children classes, I also overrided the parent's toString method, to make the toString method more useful to the children.

Although I did not need to use the super reference in the children's constructors since the parent's constructor is parameterless, I wanted to explicitly call the super keyword to make it clear that the child class is inheriting the parent's state and behavior.

I first wrote the problem as one, 'top', sentence: simulate a single player game of BlackJack, between the user and computer. Then, I imagined playing one full game of BlackJack, and wrote down all the necessary steps that happen during the game from beginning e.

Asking the player for name, shuffling deck to end e. Next, I took each, individual step, and did my 'first refinement' by dividing the step into a series of smaller task and listing them in the order they would happen. Then I broke this down further, my 'second refinement', and added variable names. I then translated the algorithm into Java code.

blackjack java card class

For my testing strategyI used JUnit and my own written test classes to test for bugs. For my JUnit test classes, I tested for equality in objects and truth or false values for boolean methods. To make sure I accounted for all errors, I tried all the different possibilities for various methods.I was bored at university one night so coded blackjack in Java. Here are some of the more interesting aspects of coding up the game. So the question is how to manage and keep track of them. There are only two things we need to know about a card:.

The easiest way to store these are just as integers with 0, 1, 2, 3 meaning spades, clubs, hearts, diamonds and using numbers 2 — 14 for values 2 through to 11 for jack and 14 for the aces.

JavaFX Game: BlackJack

We want a way of putting all valid cards together and doing stuff like shuffling them and drawing cards from the top, so it makes sense to make a Deck class that will handle these events. The deck is an array list of cards array lists are like lists but it is a bit more easy to dynamically add, remove and reference things. The empty deck is used for shuffling, you will see that in a minute. We iterate through all suits, 0 — 3 and make all of our new cards in the nested loop where we iterate through values remember, 11 — 14 are the face cards.

The other function we need is to shuffle these cards. I can think of a better solution now but here is what I went with at the time:. We re-add in order all the cards into the deck. We then make our main deck to be the emptyDeck. This looks a bit clunky but any method would be fairly similar to this. To pick cards from this deck we can just skim the Card in last position and then remove it from the deck. Aces can be 1 or 11 so you have to take this into account:.

Whilst we do this we note if any of these cards were aces and keep a tally of how many.

blackjack java card class

After running through all cards and adding up the final sum of scores, if the sum is over 21, the algorithm runs through all the aces.

It sets each in turn to 1 instead of 11 by taking off 10 from the sum until either the sum goes below 22, or there are no more aces.

Return the sum.There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors.

Sanne Gloeric wrote: Thank you. I wrote the java code in English. Please don't laugh at my very-beginner simple code or don't laugh to hard I'm trying to learn java on my own. The code does work. If I would make one method to generate a card, should I use an other method to determine the value and one to determine if it's a ace or not? Or do I make it more difficult than I should by using multiple methods for a simple game?

Forum: Beginning Java. Making a simple blackjack game. Sanne Gloeric. I'm trying to make a very simple blackjack game to play against the computer. I now made it, without using GUI in one class. It's quite big and now I want to try to make it possible to split. However, I would like to make methods like "drawcard". Now I use a switch for this and call the "drawcard" method in the main class. But, if I place my switch drawcard in an method in an other class, I can only return 1 variable, and I want to return more the name of the card, the points you get by it and if it's a ace or not.

Coding Blackjack in Java

I just started with learning Java so sorry if I don't use the correct terms. I can copy the code I have now, but then I have to translate some names of variables and text. Could I just copy the Javacode in the message? Could anybody give me some advise if I should use more classes or not? Maybe I just have to learn more. I like With a proper OO design, you should only have to return one thing. You should be able to create a 'card' class that would hold the rank A-K and a suit.

I would NOT have it return a value, since the value is not a property of the card, but of the rules of the game of blackjack. Stephan van Hulst.

Saloon Keeper.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Can someone please please help. I have created a card class and Deck class but I just dont know how to create the Hand class. Similarly for suit. Of course you will most probably need other methods, but I am sure you can figure them out yourself. Learn more. How to create a hand class for BlackJack java [closed] Ask Question. Asked 7 years, 5 months ago.

Active 7 years, 5 months ago. Viewed 5k times. This is my Card class below. ArrayList; import java. Collections; import java. I have tried nothing because honestly I dnt know what to do. Then try and come back when you have. I just need to know what logic should be in the hand class. Please use your own brain. Stack Overflow is not a site where you can make requests to implement code for you.

I don't get the problem. You have your card class. Each hand has 2 cards. What's the issue? Active Oldest Votes. Create Class, which will contain for example ArrayList hand. Thank you for that, I just needed the basic logic to get me started. I appreciate that. The Overflow Blog.

thoughts on “Blackjack java card class”

Leave a Reply

Your email address will not be published. Required fields are marked *