A
Annon
Guest
I'm making a blackjack program for CS2, but I'm having some problems with it. First we are supposed to ask how many decks we would like in the mix, generate them into a linked list, then shuffle.
The main problem I am having right now is dealing cards from the deck linked list to the hand linked list. Is there any way to directly move an object with out creating a new object, copying the data from the old to the new, and destroying the original data?
The way I am doing it now, Card.equals is either not being called, or is not working properly, and I'm not sure what the problem is.
Could any of you help?
Shoe.java
Hand.java
Card.java
cs2_Pgm02.java (just testing objects right now)
The main problem I am having right now is dealing cards from the deck linked list to the hand linked list. Is there any way to directly move an object with out creating a new object, copying the data from the old to the new, and destroying the original data?
The way I am doing it now, Card.equals is either not being called, or is not working properly, and I'm not sure what the problem is.
Could any of you help?
Shoe.java
Code:
import java.util.*;
public class Shoe{
final int DECK_SIZE = 52; //cards per deck
private List deck = new LinkedList();
private int shoeSize;
//Shoe Constructor, takes the number of decks you want, generates the cards
//into a linked list, then shuffles.
public Shoe(int deckNum){
shoeSize = deckNum * DECK_SIZE;
int CardValue = 1;
while(shoeSize > 0){
if (CardValue == 14)
CardValue -= 13;
Card card = new Card(CardValue);
deck.add(card);
shoeSize--;
CardValue++;
}
Collections.shuffle(deck);
}
//Returns the last card in the list, then removes it.
public Card deal(){
Card card2 = new Card(1);
card2.equals(deck.get(deck.size()-1));
deck.remove(deck.size()-1);
return card2;
}
//Returns the value of a given card (For Testing Purposes)
public int cardValue(){
Card card3 = new Card(1);
card3.equals(deck.get(5));
return card3.getValue();
}
//Returns the size of the shoe (For Testing Purposes)
public int Size(){
return deck.size();
}
}
Hand.java
Code:
import java.util.*;
public class Hand{
private List hand = new LinkedList();
private int hValue = 0;
public Hand(){
}
//Adds a card to the hand and increases the hands value
//Still need to add code to check if ace should be 1 or 11
public void addCard(Card c1){
hand.add(c1);
hValue += c1.getValue();
}
//Returns hand value
public int handValue(){
return hValue;
}
//Checks to see if there are duplicate cards.
//Need to code in a different way, because all face cards will show the
//same this way.
public boolean checkSplit(){
Card card = new Card(1);
Card card2 = new Card(1);
card.equals(hand.get(0));
card2.equals(hand.get(1));
if(card.getValue()==card2.getValue())
return true;
else
return false;
}
//Splits the hand into two
public Hand splitHand(){
Hand hand2 = new Hand();
Card card = new Card(1);
card.equals(hand.get(1));
hand2.addCard(card);
hand.remove(1);
return hand2;
}
}
Card.java
Code:
public class Card{
private int value;
private String name;
public Card(int val){
value = val;
if (value == 1)
name = "Ace";
if (value == 2)
name = "Two";
if (value == 3)
name = "Three";
if (value == 4)
name = "Four";
if (value == 5)
name = "Five";
if (value == 6)
name = "Six";
if (value == 7)
name = "Sevem";
if (value == 8)
name = "Eight";
if (value == 9)
name = "Nine";
if (value == 10)
name = "Ten";
if (value == 11)
name = "Jack";
if (value == 12)
name = "Queen";
if (value == 13)
name = "King";
}
//Returns the value of the card, all face cards return 10
public int getValue(){
if (value > 10)
return 10;
else
return value;
}
//Returns the name of the card
public String getName(){
return name;
}
//Sets the value of the card
public int setValue(int val){
value = val;
return value;
}
//Sets the value of one card equal to another.
//Need to code a different way, all face cards will return a 10.
//Currently, this is not working.
public void equals(Card c1){
value = c1.getValue();
}
}
cs2_Pgm02.java (just testing objects right now)
Code:
public class cs2_Pgm02{
public static void main(String [] args){
//Everything in here is just for testing my object functions, will all
//be scrapped later
//Tests the card constructor
Card c1 = new Card(5);
System.out.println(c1.getName()+""+c1.getValue() + "\n");
//Tests the shoe constructor, and should deal the last card.
//Currently, c2 is not being assigned the value of the shoe's card
Shoe shoe = new Shoe(2);
Card c2 = new Card(1);
c2.equals(shoe.deal());
System.out.println(shoe.Size()+"");
System.out.println(shoe.cardValue());
System.out.println(c2.getValue()+"\n");
//Tests hand creation and various functions of Hand
Hand hand = new Hand();
hand.addCard(shoe.deal());
System.out.println(hand.handValue()+"");
hand.addCard(shoe.deal());
System.out.println(hand.handValue()+"");
if (hand.checkSplit())
System.out.println("booyah!!!");
else
System.out.println("didn't work");
}
}