Solved

stucking in dealing the cards of my CardDeck Class which takes Object as a parameter

Posted on 2004-08-07
2
219 Views
Last Modified: 2010-03-31
Hello;

I tryign to continue in coding my game but have problems with my CardDeck class.
when I tried to code the Deal method in my CardDeck class, I couldn't use add and remove method from my LinkedList class to add a card to my new hand and then remove it from the deck( my add & remove methods are taking objects).


below is my program code with all classes.

///////////

/***
****** Card Clas
***/
public class Card implements Comparable{

  private int value;
  private int suit;
 
  public Card(int val, int s){
    value = val;
    suit = s;
  }
  public int getValue(){
    return value;
  }

  public int getSuit(){
    return suit;
  }

  public void setValue(int v){
    value = v;
  }

  public void setSuit(int s){
    suit = s;
  }
 
  public int compareTo (Object rhs) {
    Card card = (Card)rhs;
    if (this.suit < card.getsuit) return -1;
    if (this.suit > card.getsuit) return  1;
    if (this.value  < card.getvalue ) return -1;
    if (this.value  > card.getvalue ) return  1;
    return 0;
  }// end of CompareTo

  public String toString() {

      StringBuffer cardDescription = new StringBuffer();
     switch(value) {
         case 14 : cardDescription.append("Ace"); break;
         case 11 : cardDescription.append("Jack"); break;
         case 12 : cardDescription.append("Queen"); break;
         case 13 : cardDescription.append("King"); break;
         default :
             cardDescription.append( value + "" );
             break;
     }
     cardDescription.append(" of ");
     switch(suit) {
         case 1 : cardDescription.append("Clubs"); break;
         case 2 : cardDescription.append("Diamonds"); break;
         case 3 : cardDescription.append("Hearts"); break;
         case 4 : cardDescription.append("Spades"); break;
     }
     return cardDescription.toString();


  }//end of String
 
}

/***
****** Node Clas
***/
public class Node {


  private Object val;
  private  Node  next;
 
 
  // Constructors.
  public Node() {
   next = null;
   }
  public Node(Object v) {
   val = v; next = null;
    }
  public Node(Object v, Node n) {
   val = v; next = n;  
  }
 
 
  // Mutators.
  public void setData(Object v) {
   val = v;
    }
  public void setNext( Node  n) {
   next = n;
   }
  // Accessors.
  public int getData() {
   return val;
    }
  public Node getNext() {
   return next;
    }
}


/***
****** LinkedList Clas
***/
public class LinkedList {

      
  private Node head = null;
  private Node prev = null;
  private Node cur  = null;
 
 
  public boolean isEmpty() {
  return head==null;
 
 }
   
   
  public boolean contains(Object v) {
 
  boolean found = false;
  cur  = head;
  prev = null;
  while ((cur != null) && !found) {
   
    if (cur.getData().compareTo(v) == 0) //generic
    found = true;
    else {
      prev = cur;
      cur  = cur.getNext();
  }
  return found;
 
 }
   
   
  public int peek() {
 
  return cur.getData();
 
}
   
   public void insert(Object v)   {
  // Insert item (v) in list between previous (prev)
  //   and current (cur) nodes.
  // Pre-conditions : cur set to node immediately after
  //                    insertion point.
  //                  prev set to node immediately prior to cur.
  //                  If inserting at head of list,
  //                    prev == null and cur == head.
  //                  If inserting at tail of list,
  //                    prev set to last node and cur == null.
  // Post-conditions: item is in list.
  //                  cur set to the new node containing item.
  //                  prev set to immediate predecessor of item.
 
 
  cur = new Node(v, cur);   // Create, fill, & assign new node.
  if (prev == null) head = cur;  // Insert at head of list
  else prev.setNext(cur);        // or insert somewhere else.
}

   
   
 public void remove(<type> v) {
  // Pre-conditions: None.
  if (!contains(v))
    throw new NoSuchElementException();
  cur = cur.getNext()
  if (prev == null) head = cur;
  else prev.setNext(cur);
}

   
   
  public void print() {
  // Pre-conditions : None.
  // Post-conditions: All elements of list displayed.
  //                  cur is unchanged.
  Node n = head;
  while (n != null) {
    System.out.println(n.getData());
    n = n.getNext();
  }
}

   
   
  public void clear() {
 
  head =prev=cur=null;
   
 }
 
 
}//end of LinkedList


/***
****** CardDeck Clas
***/

public class CardDeck extends LinkedList {

      // instance variables
      private int numCards = 52;  //counter to keep track of the number of cards remaining in the deck
      
      
      private int idx;
      
      private Card[] deck;
      private LinkedList hand = new LinkedList();
      private Random generator = new Random();
            
      // constructor
      
      
      public void deal(Object v)
      {
            idx = generator.nextInt(numCards) + 1;
            deck.remove(idx);
//            hand.contains(idx)
            hand.insert(idx);
            
      }
      
      public void remove(Object v)
      {
            super(v);
      }
      public Object peek()
      {
            super();
      }
}


as you can see, the CardDeck class is a Linked list of the Card Class

any help please.
0
Comment
Question by:anosTekbot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 11744741
Card *is* an Object so you can add/remove them.
0
 
LVL 92

Expert Comment

by:objects
ID: 11750647
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question