Card.java
import java.util.*;
public class Card implements Comparable
{
private int rank;
private int face;
private final int ACE = 1;
private final int KING = 13;
private final int QUEEN = 12;
private final int JACK = 11;
private final int TEN = 10;
private final int NINE = 9;
private final int EIGHT = 8;
private final int SEVEN = 7;
private final int SIX = 6;
private final int FIVE = 5;
private final int FOUR = 4;
private final int THREE = 3;
private final int TWO = 2;
private final int HEARTS;
private final int DIAMONDS;
private final int CLUBS;
private final int SPADES;
public GetValue()
{
int TEN =10 |
}
public String toString() {
String[] r = {"", "SPADES", "CLUBES", "DIAMONDS", "HEARTS"};
String[] c = {"", "","ACE", "TWO", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
return str[c] + " of " + color[r];
}
}
Blackjack.java
//Blackjack.java
import java.util.Scanner;
public class Blackjack
{
static Deck deck = new Deck();
static Hand player = new Hand();
static Hand dealer = new Hand();
public static void main(String[] args)
{
Blackjack game = new Blackjack();
deal();
if (finishPlayersHand()) return;
if (finishDealersHand()) return;
wrapUpScoring();
}
static void deal()
{
player.hit(deck.draw());
dealer.hit(deck.draw());
player.hit(deck.draw());
dealer.hit(deck.draw());
System.out.println("Dealer's up card = " + dealer.lastCard() + '\n');
System.out.println("Player's hand is: ");
System.out.println(player);
}
static boolean finishPlayerHand()
{ //returns true if player busts; otherwise
// player is still in game
Scanner input = new Scanner(System.in);
System.out.println("Do you want a hit (y/n): ");
String reply = input.next();
while (reply.equalsIsIgnoreCase("y"));
{
player.hit(deck.draw());
System.out.println(player.lastCard());
if (player.getScore() > 21)
{
System.out.println("You busted!");
}
return true;
}
static boolean finishDealerHand()
{
while
{
dealer.hit(deck.draw());
System.out.println(dealer.lastCard());
if (dealer.getScore() > 21)
{
System.out.println("Dealer busted!");
}
return true;
}
}
static void wrapUpScoring()
{
}
}
Hand.java
//Hand.java
import java.util.*; //List interface & ArrayList class
class Hand
{ //use the List interface and the ArrayList implementation to hold cards;
private List<Card> cards = new ArrayList<Card>;
private int Score = 0;
private int num11ptAces = 0; //num of aces valued at 11pts. in the hand
void hit(Card c)
{ //mutator; modifies the hand and the score
cards.add(c);
if (c.getValue() == Card.ACE && (score +11) <=21)
{
score += 11;
++ num11ptAces;
} else score += c.getValue();
}
Card lastCard()
{ //returns most recently dealt card
if(cards.size() > 0) return cards.get(cards.size() - 1);
return null;
}
int getScore()
{ //returns score in this hand
while (score > 21 && num11ptAce > 0)
{
score -= 10;
-- num11ptAce;
}
return score;
}
public String toString()
{ //returns a 'print string' for the hand
String r = "";
for (Card c : cards)
r += c + "\n";
return r;
}
}
Deck.java
//Deck.java
import java.util.*;
class Deck
{
private int numCards = 52;
private Card[] deck = new Card[numCards];
Deck()
{
fill();
shuffle();
}
private void fill()
{
for (int i = 0; i < deck.length; ++i)
deck[i] = new Card((i / 13) + 1, (i % 13) + 1);
}
private void shuffle()
{
Collections.shuffle(Arrays.asList(deck));
}
Card draw()
{
if (numCards < 0)
return null;
return deck[--numCards];
}
}
Thank you in advance for your time and help.

New Topic/Question
Reply
MultiQuote









|