• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with Blackjack game  RSS feed

 
Nick Arauza
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"o Hi There", please check your private messages for an important administrative matter.
 
Nick Arauza
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, but I still need help with this matter.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Henry
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it's ">="?
 
Nick Arauza
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!