Forums Register Login

Need help with Blackjack game

+Pie Number of slices to send: Send
Hello all

I've noticed that this game is done quite often by people and now it's one of my assignments. It was split into two parts, the first assignment being to create a deck of cards as an array and assign an image of each card to the corresponding suit and rank, then methods that shuffle and deal these cards.

The second assignment is the actual Blackjack game, and now I' am completely stuck. In my program I need to have a separate class for the player and dealer but I'm stuck on the dealer class. I have to initialize field numberOfPoints to 0 with a constructor, then the method deal should call the method dealCard() of class CardDeck in a loop, adding the value of each resulting card to the current value of numberOfPoints. Use the object returned by method dealCard() to call method drawCard of class PlayingCard, in order to display a picture of the card on screen. The loop terminates when value of the field numberOfPoints reaches 17 or higher. Also for simplicity, Im always to count Ace as 11 points for the dealer's hand. Lastly method handTotal() returns the value of field numberOfPoints.



Now I'm not sure how to go about implementing the loop, as it says that the cards drawn should add up, but the array of cards contained actual file images of the cards themselves so how do they have a value?

Thanks in advance.
+Pie Number of slices to send: Send
"o Hi There", please check your private messages for an important administrative matter.
+Pie Number of slices to send: Send
thanks, but I still need help with this matter.
+Pie Number of slices to send: Send


Well, you designed the CardDeck class -- so how the loop works depends on how the CardDeck class works.

Henry
+Pie Number of slices to send: Send
Are you sure it's ">="?
+Pie Number of slices to send: Send
Alright I changed my code up quite a bit, I think I' am heading in the right direction, but I get an error message saying
"non static variable rank cannot be referenced from a static context." This occurs when the value of rank is checked. My teacher suggested I use if else statements to assign points to the dealer based on the rank of a card. I'm not sure how to get around this error.

Here is what I have:


also, before this can work, I'm not sure how to call the method dealCard() from the dealerhand class. dealCard() is in class CardDeck and looks like this:



I'm a little confused right now, so I dont know if I have given enough info for you guys to help me out, if so just let me know. Thanks again.
+Pie Number of slices to send: Send
 

I get an error message saying "non static variable rank cannot be referenced from a static context." This occurs when the value of rank is checked. My teacher suggested I use if else statements to assign points to the dealer based on the rank of a card. I'm not sure how to get around this error.



Well, you have not shown the actual class (just methods), so this is speculation... When the compiler complains that non-static variables can't be accessed from static content.... It means exactly that. The variable "rank" is not a static variable. To access it, you need to specify an instance. Furthermore, if you don't specify an instance, meaning just use the variable name, then it will assume that the instance is "this", which means that it can't be called from a static method.

Now, I can guess what the issue is...



I am guessing that PlayingCard is *not* an instance, but the actual name of the class itself. If rank is not a static variable, then you can't access it via it's class name. You need an instance, because each instance of PlayingCard has a copy of the rank variable. (The compiler doesn't know which one you are referring to)

Henry

You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2148 times.
Similar Threads
Error
no such method error
blackjack java
Java program help
objects, objects, objects...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:02:28.