Pergunta

Para uma classe de programação Estou criando um programa de blackjack para a primeira lição de casa. O professor nos deu uma aula de cartão de amostra, que inclui o método para adicioná-los a uma plataforma. Por seu deck, ela usa um ArrayList, que você pode facilmente Knuth shuffle com o método Collections.shuffle ().

Esse método não funciona para Pilhas que (obviamente), mas acho que uma estrutura Stack seria melhor trabalhar para este programa, porque você pode pop e cartões de empurrar para dentro e fora do baralho.

Foi útil?

Solução

java.util.ArrayList<E> e java.util.stack<E> implementar o java.util.List<E> interface, e Collections.shuffle() leva um java.util.List<?> como parâmetro. Você deve ser capaz de passar um Stack em Collections.shuffle(), a menos que você estiver usando uma implementação de pilha diferente que não implementa java.util.list<E>. Se você for, eu o aconselharia a mudança para uma implementação de pilha diferente.

Outras dicas

I guess it's much easier to do stack operations on an ArrayList.

A stack is a list, so you can call Collections.shuffle() on your stack.

That said, Stack is an old class, like Vector and kind of outmoded. Nowadays you would use a Dequeue (a double ended queue which works as either a queue or a stack) rather then a stack but, Dequeues are not lists, so they can't be shuffled.

Also, you can always put your cards in a List, shuffle them, and then add all of them to a Dequeue

There is no reason why a stack structure should not be random access as well (java.util.Stack does, although that has problems of its own). Other than that, you can pop the elements of the stack into an ArrayList, shuffle and then push them back on to your stack.

No, Fisher-Yates shuffle relies on random access to the dataset. You need some Collection which allows get(int index). If you need a stack just use a list. push and pop just call get(0) and add(0). This is better than implementing some custom stack class. Use what you have, don't invent new classes.

Adam's answer is best for a stack. For card games, what I usually use is a simple arraylist and remove random elements. No shuffling required.

Just shuffle before/as you put the cards onto the stack. Since a properly implemented Knuth shuffle does not allow replacement of cards in the part of the deck already traversed you can simply place them onto the stack as you go along...

Since java will not let you treat a stack as a random access list just copy from the stack into an ArrayList to do the shuffling phase (an extra 52 element ArrayList knocking around is no big deal)

the Collections.shuffle() method does that for you you dont have to explicitly.

"If the specified list does not implement the RandomAccess interface and is large, this implementation of shuffle() dumps the specified list into an array before shuffling it, and dumps the shuffled array back into the list. This avoids the quadratic behavior that would result from shuffling a "sequential access" list in place."

this is what the java documentation says about Collections.shuffle() method implementation so passing a java.util.Stack (an implementation of java.util.List interface) should work...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top