Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

837 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