在Java中使用tads(stack,Queue,Deque,List)的Solitaire Game - 由Stack实现的Classe

时间:2014-11-06 11:28:45

标签: java

我已经开始使用Java中的tads和软件模式起草Game Solitario,我已经开始制作Stack接口了

public interface Stack <E> {

// number of elements in the stack
     public int size ();

// does not contain elements?
     public boolean isEmpty ();

// returns next object to come out without removing
     public E peek () throws EmptyStackException;

// add new stack element
      public void push (E elem) throws FullStackException;

// pops or drop next element
     public E pop () throws EmptyStackException;

}

在这里我有一个calsss LotOfCard有一系列卡片并具有LIFO行为,你可以检查很多卡是否为空,在该批次中放置或添加新卡并从堆栈中移除卡片。

public class LotOfCards implements Stack <Card> {
     //
     private HashSet <Card> cards;//contains collections of Card

     //
     public LotOfCards() {
         this.cards= new HashSet <> ();
     }

     //
     public HashSet <Card> getCards () {
         return cards;
     }

     //
     public void setCards (HashSet <Card> cards) {
         this.cards= cards;
     }

     // return number of elements in the stack
     Override
     public int size() {
         return this.cards.size();
     }

     // does not contain elements?
     Override
     public boolean isEmpty () {
         return (this.cards.isEmpty () || this.cards == null) ;
     }

     // add new stack element
     Override
     public void push (Card element) throws FullStackException{
         this.cards.add (element);
     }

     // returns next object to come out without removing
     Override
     public Card peek () throws EmptyStackException{//Doubt here
         if (this.isEmpty ()) {
             throw new EmptyStackException ();
         }
         return (Card) null;
     }


     // pops or drop element
     Override
     Public Card pop () throws EmptyStackException{//Doubt here

         if (this.isEmpty ()) {
             throw new EmptyStackException ();
         }
         // returns the first letter is removed
         Card card = this.peek();

         // after the card is removed
         this.cards.remove (card); // Doubt

         // return card
         return card;

     }

我怀疑方法peek()和pop()中的类LotOfCard,即方法peek()返回顶部对象()和方法pop(),因为我已经删除了一个图表对象卡片的集合,我不知道该怎么做,感谢关于创建算法来开发这些方法的一些解释

1 个答案:

答案 0 :(得分:0)

@mvw,也许你有正确的数组可以很好地实现堆栈数据结构 我制作并测试了这个类LotOfCards,这很好,这是我对这个问题的回答

public class LotOfCards<Card> implements Stack<Card> {

        private Card[] array;
        private int top;
        private int capacity;//52 cards

        //
        public LotOfCards(int capacity) {
            if (capacity<= 0) {
                throw new IllegalArgumentException("The maximum stack size must be greater than 0.\n\n");
            } else if (capacity> 52) {
                throw new IllegalArgumentException("The maximum stack size should not be greater than 52.\n\n");
            }
            this.capacity = capacity;
            this.array= (Card[]) new Object[capacity];
            this.top = -1; // = 0
        }

        //return size of element in stack
        @Override
        public int size() {
            return (this.top + 1);
        }

        //stack is empty?
        @Override
        public boolean isEmpty() {
            return (this.top == -1);//==0
        }

        //stack is full
        public boolean isFull() {
            return this.capacity == this.size();
        }

        //add or push the new element in stack
        @Override
        public void push(Card element) throws FullStackException {
            if (isFull()) {
                throw new FullStackException();
            }
            this.array[++top] = element;
        }

        //return the first element, without pop or drop
        @Override
        public Card peek() throws EmptyStackException {
            if (this.isEmpty()) {
                throw new EmptyStackException();
            }
            return (Card) this.array[this.top];
        }

        //pop or drop the element on the top
        @Override
        public Card pop() throws EmptyStackException {
            //se for vazio
            if (this.isEmpty()) {
                throw new EmptyStackException();
            }
            return (Card) this.array[top--];

        }

        //for teste
        public void invertStack() throws FullStackException {
            LotOfCards<Card> stack = new LotOfCards<>(this.capacity);
            for (int i = this.top; i >= 0; i--) {
                stack.push(this.array[i]);
            }
            this.array = stack.array;
        }

        @Override
        public String toString() {
            StringBuilder str = new StringBuilder();
            for (Card element: this.array) {
                if (element != null) {
                    str.append(element);
                }
            }
            return str.toString();
        }
    }