Solved

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

Posted on 2004-08-07
2
194 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countAbc challenge 9 49
wordmultiple challenge 12 89
Java asynchronous logging 4 32
GUI builder for Eclipse? 8 7
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 had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now