Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-07-2007, 11:36 PM
Member
 
Join Date: Nov 2007
Posts: 8
mandrake446 is on a distinguished road
Help On Coding problem
I'm having a problem coding a simple blackjack project. Here is what I have my questions are posted at the bottom of the code.

Note that in Blackjack the cards are valued as follows:
An Ace can count as either 1 or 11, depending on the hand;The cards from 2 through 9 are valued as indicated by rank;The 10, Jack, Queen, and King are all valued at 10.

1. Add a new class, called DealerHand, to the project. This class simulates a dealer’s hand in the game of Blackjack. Implement class DealerHand according to the following declaration:
public class DealerHand {
protected int numberOfPoints;
//points in the hand
public DealerHand(){ ... }
public void dealCards(CardDeck deck){ ... }
public int handTotal() { ... }
}

In your implementation, you must follow these guidelines:

– Constructor of the class must initialize field numberOfPoints with 0;
– Method dealCards should call the method topCard() of class CardDeck
in a loop, adding the value of each resulting card (see above rules) to the current
value of numberOfPoints. Use the object returned by method topCard() to
call method displayCard of class PlayingCard. The loop terminates when
value of the field numberOfPoints reaches 17 or higher;
– For simplicity, always count Ace as 11 points for the dealer's hand;
– Method handTotal() returns the value of field numberOfPoints

2. Add a new class, called PlayerHand, to the project. Implement class
PlayerHand according to the following declaration:

public class PlayerHand extends DealerHand {
public void dealCards(CardDeck deck){ ... }
}

In your implementation, you must follow these guidelines:

– Method dealCards first calls the method topCard() of class CardDeck twice, to deal the first two “cards”, and adds the value of each resulting card to the
value of numberOfPoints. Use the object returned by method topCard()
to call method displayCard of class PlayingCard. Then, in a loop, the
following steps are repeated:

a. Show a confirm dialog box containing the question: “Another Card?”,
and wait for user response.

b. The user responds by clicking either “yes” or “no” button; this input is
accepted by the program.

c. If the user answers “yes”, call method topCard() of class CardDeck
again, and add the value of the card to numberOfPoints; display a
picture of each card on canvas. Note that rank of an ace is counted as
either 1 or 11 depending on the hand.

d. If the user answers “no”, terminate the loop. The loop is also terminated as
soon as field numberOfPoints reaches 21 or higher.

3. Add another class, called GameApp, to the project cards. This class should contain only the method public static void main(String[] args), implemented according to the following guidelines (note: for ease of implementation, a player's hand is dealt first, before the dealer's hand):

– First, create an object deck of type CardDeck, and invoke method shuffle
on this object;

– Next, create an object of type PlayerHand, and call method dealCards on
this object, passing to it the above object deck as parameter value; when method dealCards returns, call the method handTotal to display the value of
player's hand;

– Next, create an object of type DealerHand, and call method dealCards on
this object, again passing to it the above object deck as parameter value; when method dealCards returns, call the method handTotal to display the value of dealer's hand;

– Display a message box stating who won: the dealer or the player.


I have done part of it and i'm having some serious problems


My CardDeck code:

import java.util.Random;
import java.util.Scanner;
public class CardDeck
{
public PlayingCard[] deck;
public static final int DECK_SIZE = 52;
private int numCardsLeft;

public CardDeck(){
deck = new PlayingCard[DECK_SIZE];
numCardsLeft = DECK_SIZE;
int i;
for (i = 0; i < 13; i++)
deck[i] = new PlayingCard(PlayingCard.Spades, i+1, true);
for(i = 13; i < 26; i++)
deck[i] = new PlayingCard(PlayingCard.Clubs, i-12, true);
for(i = 26; i < 39; i++)
deck[i] = new PlayingCard(PlayingCard.Hearts, i-25, true);
for(i = 39; i < 52; i++)
deck[i] = new PlayingCard(PlayingCard.Diamonds, i-38, true);
}

public void shuffle(){
PlayingCard temp;
Random randomGenerator = new Random();
for(int k = 0; k < 100; k++){
int i = randomGenerator.nextInt(DECK_SIZE);
int j = randomGenerator.nextInt(DECK_SIZE);
temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}

public void wholeDeck(){
for(PlayingCard card: deck){
if(card != null)
card.describeCard();
}
}

public PlayingCard topCard(){
PlayingCard temp = deck[0];
this.removeCard(0);
return temp;
}

public PlayingCard anyCard() {
PlayingCard temp;
Random randomGenerator = new Random();
int number = randomGenerator.nextInt(numCardsLeft);
temp = deck[number];
removeCard(number);
return temp;
}

private void removeCard(int index) {
if(index >= 0 && index < numCardsLeft){
for(int i = index; i < numCardsLeft-1; i++)
deck [i] = deck[i+1];
deck[numCardsLeft-1] = null;
numCardsLeft--;
}
}
}

My DealerHand code :

public class DealerHand
{
protected int numberOfPoints; //Points in the hand
public DealerHand()
{
numberOfPoints = 0;
}
public void dealCards(CardDeck deck)
{

while(numberOfPoints < 17){

switch(CardDeck.topCard()){
case Ace:
numberOfPoints = 11;
break;
case King:
numberOfPoints = 10;
break;
case Queen:
numberOfPoints = 10;
break;
case Jack:
numberOfPoints = 10;
break;
case 10:
numberOfPoints = 10;
break;
case 9:
numberOfPoints = 9;
break;
case 8:
numberOfPoints = 8;
break;
case 7:
numberOfPoints = 7;
break;
case 6:
numberOfPoints = 6;
break;
case 5:
numberOfPoints = 5;
break;
case 4:
numberOfPoints = 4;
break;
case 3:
numberOfPoints = 3;
break;
case 2:
numberOfPoints = 2;
break;
default:
numberOfPoints = 0;
break;
}
}
}
public int handTotal()
{
return numberOfPoints;
}
}

My GameApp code:

public class GameApp
{
public static void main (String[] args)
{
CardDeck deck = new CardDeck();
deck.shuffle();

PlayerHand bobsHand = new PlayerHand();
bobsHand.dealCards(deck);

DealerHand shady = new DealerHand();
shady.dealCards(deck);

if(dealersHand >= playersHand){
System.out.println("Dealter wins");}
else
System.out.println("Player wins");
}
}

I do not currently have anything written in class PlayerHand because it relies heavily on Dealerhand.

What I cant figure out is....

1. If i'm going about doing deal cards right by my instructions. <-- step 1
2. how to write a dialog box (i only know how to do regular lines of code) <---step 2 and 3
3. what it means when it says "when method dealCards returns, call the method handTotal to display the value of the players hand" <--- step 3

If you can post a explanation of any code you actually post i would apprciate it so that i can finish the project, and actually understand how to do it.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-07-2007, 11:45 PM
Member
 
Join Date: Nov 2007
Posts: 8
mandrake446 is on a distinguished road
Nvm on number 2 i figured it out
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-08-2007, 12:31 AM
Member
 
Join Date: Nov 2007
Posts: 8
mandrake446 is on a distinguished road
my updated DealerHand (easier to read)
mport java.util.Random;
import java.util.Scanner;
public class DealerHand
{
protected int numberOfPoints; //Points in the hand
public DealerHand()
{
numberOfPoints = 0;
}
public void dealCards(CardDeck deck)
{

while(numberOfPoints < 17){
PlayingCard pc = deck.topCard();
int rank = pc.getRank();
switch(rank){
case 1: //Ace
numberOfPoints += 11;
break;
case 13: //King
case 12: //Queen
case 11: //Jack
numberOfPoints += 10;
break;
default: //the rest of cards
numberOfPoints += rank;
break;
}
}
}
public int handTotal()
{
return numberOfPoints;
}
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-08-2007, 08:01 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
If i'm going about doing deal cards right by my instructions
As far as I can tell with casual inspection, yes it looks okay.
If I had the PlayingCard class I could run it and maybe learn more.
what it means when it says "when method dealCards returns, call the method handTotal to display the value of the players hand"
Like this:
Code:
PlayerHand bobsHand = new PlayerHand(); bobsHand.dealCards(deck); int bobsPoints = bobsHand.handTotal(); System.out.println("bobsPoints = " + bobsPoints);); DealerHand shady = new DealerHand(); shady.dealCards(deck); int dealerPoints = shady.handTotal(); System.out.println("dealerPoints = " + dealerPoints); if(dealerPoints >= bobsPoints){ System.out.println("Dealter wins");} else System.out.println("Player wins");
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Coding an FTP server in java Zucheto Networking 3 06-22-2008 05:24 AM
Error in my coding one198 New To Java 2 10-13-2007 06:07 AM
Cannot solve the coding problem of my assignment elimmom New To Java 3 08-13-2007 12:33 PM
Problem in my coding one198 New To Java 9 08-09-2007 11:07 AM
Help with program coding cachi AWT / Swing 1 07-31-2007 08:16 AM


All times are GMT +3. The time now is 07:45 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org