2013-08-27 42 views
6

我正在尝试创建一个“shuffleDeck()”方法。如何“洗牌”数组?

我想要做的是创建一个方法,该方法需要一个数组参数(这将是纸牌)洗牌,然后返回混洗数组列表。

这是代码:

class Card 
{ 
    int value; 
    String suit; 
    String name; 

    public String toString() 
    { 
     return (name + " of " + suit); 
    } 
} 

public class PickACard 
{ 
    public static void main(String[] args) 
    { 
     Card[] deck = buildDeck(); 
     // display Deck(deck); 

     int chosen = (int)(Math.random()* deck.length); 
     Card picked = deck[chosen]; 

     System.out.println("You picked a " + picked + " out of the deck."); 
     System.out.println("In Blackjack your card is worth " + picked.value + " points."); 

    } 

    public static Card[] buildDeck() 
    { 
     String[] suits = {"clubs", "diamonds", "hearts", "spades" }; 
     String[] names = {"ZERO", "ONE", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "Jack", "Queen", "King", "Ace" }; 

     int i = 0; 
     Card[] deck = new Card[52]; 

     for (String s: suits) 
     { 
      for (int v = 2; v<=14; v++) 
      { 
       Card c = new Card(); 
       c.suit = s; 
       c.name = names[v]; 
       if (v == 14) 
        c.value = 11; 
       else if (v>10) 
        c.value = 10; 
       else 
        c.value = v; 

       deck[i] = c; 
       i++; 
      } 
     } 
     return deck; 
    } 

    public static String[] shuffleDeck(Card[] deck) 
    { 
     /** I have attempted to get two index numbers, and swap them. 
     I tried to figure out how to loop this so it kind of simulates "shuffling". 
     */ 
    } 

    public static void displayDeck(Card[] deck) 
    { 
     for (Card c: deck) 
     { 
      System.out.println(c.value + "\t" + c); 
     } 
    } 
} 
+4

将数组转换为'List'并调用'Collections.shuffle(list)'。 –

+0

是的,就是这样! – LastFreeNickname

+1

您发布的代码毫无意义,因为它缺少最重要的部分 - 您尝试的洗牌实施。我已经投票结束了,因为你根本没有尝试过。 –

回答

10

如何:

List<Card> list = Arrays.asList(deck); 
Collections.shuffle(list); 
+0

'Card []'怎么办? –

+1

但是你错过了你的结果:哪一个变量包含混洗内容? ;) – UR6LAD

+0

我错了:Arrays.asList创建一个基于数组的列表,因此当集合交换此列表中的任何元素时,它将以数组交换它们。 – UR6LAD

0

如果这是一所学校的项目(因为我认为这是),你可能不会被允许使用的内置功能,如类别::洗牌()。如果是这种情况,那么你必须尝试模拟随机性(在编程中可能会令人惊讶地很难)。

创建随机感最常用的方法是使用RNG (random number generator)。 至于你说

我试图得到两个索引号,并交换他们。

正确。洗牌的一种方法是每次选择一张牌并随机选择另一张牌以交换位置。

  • 你知道这个套牌总是有52张牌。
  • 你有一个随机发生器 来选择一个随机索引。
  • 你有一个编程语言 循环结构。

使用这些工具,您可以轻松实现自己的洗牌功能。

2

一种方法是将数组转换成列表,并使用java.util.Collections.shuffle(array)洗牌它:

Card[] deck = ...; 
List<Card> list = Arrays.asList(deck); 
Collections.shuffle(list); 

如果你仍然需要一个数组,而不是一个列表,你可以添加:

list.toArray(deck); 

Here is a TIO (Try-it-online) link to see the array to list conversion and shuffling in action.

的TIO的代码复制如下面参照ence:

import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 

class M{ 
    public static void main(String[] a){ 
    // Original array 
    Integer[] array = new Integer[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
    System.out.println("before: " + Arrays.toString(array)); 

    // Convert array to list 
    List<Integer> list = Arrays.asList(array); 
    // And shuffle that list 
    Collections.shuffle(list); 
    System.out.println("after as list: " + list); 

    // (Optional) then convert the list back to an array, 
    // and save it in its initial variable (`array` in this case) 
    list.toArray(array); 
    System.out.println("after as array: " + Arrays.toString(array)); 
    } 
}