Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 88
Java Loop 6 59
web services creation SOAP vs REST 5 37
runtime exception 2 27
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

813 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