Can anyone help me with a Java program?

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
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");
		
	}
}
 
Yeah, when ever main calls getValue, it will return a 10 for jack queen and king. I still have to code the ace.

There is much left to do, but my program is worthless if I can't deal the cards, so this is where I'm stuck at now.

EDIT: heh, you're quick with edits :)
 
is this what you mean?


objectType copy = new objectType();

copy = oldone;

oldone.resetData();

that way copy would be the same as the old object and the old object would be reset.



also when you have alot of if statements all determined by an Int value...try and use a switch statement...

switch(num){

case 0:
statment;
break;

case 1:

...

}
 
equals

public boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

* It is reflexive: for any non-null reference value x, x.equals(x) should return true.
* It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
* It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
* It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
* For any non-null reference value x, x.equals(null) should return false.

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.

the method equals is similiar to "==" not "=".

I sugest you download the Java API Documentation ;)

BTW if you don't like using switch you can use if(){}else if(){} ect. It's better programming practice than just using a load of if statements and is less bug prone (In my experience) than switch.
 
I'd just use an array of Strings.
Code:
class Card
{
     private static final String names[]={"Ace","Two","Three"};
     //complete the list
     private int value;
     private String name;
     public Card(int value)
     {
         this.value=value;
         name=names[--value];        
     } 
}
looks alot cleaner to me
[edit]
You should probably do a bounds check though ;)
[edit2]
You can drop the name member var completely and just write a toString function. Then you can just write
Code:
//in your card class you have to add the method
public String toString()
{
    return names[--value];
}

//in some file
Card card=new Card(5);
System.out.println(card);
 
Back
Top