Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
纸牌游戏使用tads(堆栈、队列、Deque、列表)在Java-Classe中由堆栈实现_Java - Fatal编程技术网

纸牌游戏使用tads(堆栈、队列、Deque、列表)在Java-Classe中由堆栈实现

纸牌游戏使用tads(堆栈、队列、Deque、列表)在Java-Classe中由堆栈实现,java,Java,我已经开始用Java中的TAD和软件模式起草游戏Solitario,并且我已经开始制作堆栈接口 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

我已经开始用Java中的TAD和软件模式起草游戏Solitario,并且我已经开始制作堆栈接口

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,它有一组卡片,有后进先出的行为,你可以检查是否有很多卡片是空的,把或者添加新的卡片放到这一组,然后从堆栈中移除卡片

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方法返回top对象和pop方法,因为我一直在删除卡片集合中的一个图表对象,我不知道该怎么做,感谢关于创建算法来开发这些方法的一些解释,也许你对数组的看法是正确的,可以实现堆栈数据结构 我制作并测试了很多卡片,很好,这是我对这个问题的回答

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();
        }
    }

一套没有秩序。如果你想订购卡片,不要用套装。@mvw不,我不想你写实现peek和pop的代码,我只想知道,如何制作算法,你可以用你的话解释我,而不是你给我做的代码,我需要的代码do@immibis,classe LotOfCards有一组卡片,我想使用HashSet或ArrayList,是吗?哈希集不会记录插入元素的顺序。数组可以很好地实现堆栈数据结构,但是classe LotOfCards有一个卡片集合