【问题标题】:Random Array in JavaJava中的随机数组
【发布时间】:2014-02-04 01:57:43
【问题描述】:

我有这个卡片程序,可以在一个摆动窗口中打印出所有 52 张卡片。它包含 4 行,每行 14 张卡片,第一行是灰色的空点。

我正在尝试随机化它们的打印方式。到目前为止,每一行都以随机顺序打印 A 到国王和随机花色。我得到的错误是,一旦它具有像 [Ace, Four, Jack, etc] 这样的随机值,它在每一行中都遵循相同的模式。

如何随机化不同的行?

package project2;

import javax.swing.*;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.awt.*;

public class main
{
    public static final int PERIMETER_BEVEL = 20;  //space between panel border and perimeter cards
    public static final int LEFT_PERIMETER_BEVEL = 98;
    public static final int INTERIOR_BEVEL = 5;                 //space between cards
    public static final int CARD_HEIGHT = 97;
    public static final int CARD_WIDTH = 73;    

    public static final int PANEL_HEIGHT = (2*PERIMETER_BEVEL) + (4*CARD_HEIGHT) + (3*INTERIOR_BEVEL);
    public static final int PANEL_WIDTH = (2*PERIMETER_BEVEL) + (14*CARD_WIDTH) + (13*INTERIOR_BEVEL);

    public static final String   BACKGROUND_COLOR = "#64C866";  //window background color [hex]
    public static final String   CARD_FOLDER = "cardImages";    //default folder containing images

    public static final String[] RANKS = {  "jack","three","four","five","six","seven",
    "eight","nine","ten","two","queen","king","ace"
 };
    public static final String[] GRAY = {"gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray",
    "gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray",
    "gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray",
    "gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray"};
public static void main(String[] args)
{
    JFrame window = new JFrame("deck");
    JPanel panel = new JPanel() {
        public void paintComponent(Graphics g) {                     //find each rank of card in increasing
            super.paintComponent(g);                                 //order as specified in the array. All
            File[] files = new File(CARD_FOLDER).listFiles();        //ranks appear in the same suit order in

            //This randomizes the order the suits are put in.
            for(int i = 0; i < files.length; i++)
            {
                int r = (int)(Math.random()*(i+1));
                File swap = files[r];
                files[r] = files[i];
                files[i] = swap;
            }

            int counter = 0;

            //This displays the gray background cards. These cannot move but can be placed over with any other card.
            int grayCounter = 0;
            for(String rank : GRAY) {                               
                for(File filename : files) {                         
                    if(filename.getName().contains(rank)) {
                        new ImageIcon(filename.getPath()).paintIcon(this, g,
                            PERIMETER_BEVEL + (grayCounter/4) * (CARD_WIDTH + INTERIOR_BEVEL),
                            PERIMETER_BEVEL + (3-(grayCounter%4)) * (CARD_HEIGHT + INTERIOR_BEVEL));
                        grayCounter++;
                    }                                                
                }                                                    
            }  

            //This randomizes the ranks Ace through King.
            for(int i = 0; i < RANKS.length; i++)
            {
                int r = (int)(Math.random()*(i+1));
                String swap = RANKS[r];
                RANKS[r] = RANKS[i];
                RANKS[i] = swap;
            }
                                                                    //the filesystem so suits will automatically
            for(String rank : RANKS) {                               //be in order when printing in groups of four
                for(File filename : files) {                         //cards.
                    if(filename.getName().contains(rank)) {
                        new ImageIcon(filename.getPath()).paintIcon(this, g,
                            LEFT_PERIMETER_BEVEL + (counter/4) * (CARD_WIDTH + INTERIOR_BEVEL),
                            PERIMETER_BEVEL + (3-(counter%4)) * (CARD_HEIGHT + INTERIOR_BEVEL));
                        counter++;
                    }                                                //counter/4 keeps track of the correct column
                }                                                    //3-(counter%4) keeps track of the correct row
            }                                                        //in which to print the card image
        }
    };
    panel.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
    window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    window.setBackground(Color.decode(BACKGROUND_COLOR));
    window.add(panel);
    window.setVisible(true);    
    window.pack();

}
}

【问题讨论】:

  • 使用 ArrayList 并使用 Collections.shuffle() 可能会更好。
  • 你需要每一行从 Ace-King 开始吗?
  • 不要从paintXxx 方法中加载资源,这些资源会尽快返回。我只看到你将RANKS 随机化一次...

标签: java arrays swing random


【解决方案1】:

我相信你的问题是你只随机化 RANKS 一次,然后你对每一行都使用它。 那么这里的代码:

//This randomizes the ranks Ace through King.
        for(int i = 0; i < RANKS.length; i++)
        {
            int r = (int)(Math.random()*(i+1));
            String swap = RANKS[r];
            RANKS[r] = RANKS[i];
            RANKS[i] = swap;
        }
                                                                //the filesystem so suits will automatically
        for(String rank : RANKS) {                               //be in order when printing in groups of four
            for(File filename : files) {                         //cards.
                if(filename.getName().contains(rank)) {
                    new ImageIcon(filename.getPath()).paintIcon(this, g,
                        LEFT_PERIMETER_BEVEL + (counter/4) * (CARD_WIDTH + INTERIOR_BEVEL),
                        PERIMETER_BEVEL + (3-(counter%4)) * (CARD_HEIGHT + INTERIOR_BEVEL));
                    counter++;
                }                                                //counter/4 keeps track of the correct column
            }                                                    //3-(counter%4) keeps track of the correct row
        }                                                        //in which to print the card image

如果我正确理解您的代码,上面 sn-p 中的第二个 for 循环将为所有行绘制卡片,对吗?但是对于每一行,您都使用相同的 RANKS 列表。您需要在每次调用之间随机化 RANKS 以写入行。

正如我在 cmets 中提到的,对 RANKS 使用 ArrayList 并使用 Collections.shuffle 可能会更好,然后您可以使用类似以下的内容:

 for (int row = 0; row < 4; row++)
 {
     Collections.shuffle(RANKS);
     for(String rank : RANKS) {                             
            for(File filename : files) {                         
                if(filename.getName().contains(rank)) {
                    new ImageIcon(filename.getPath()).paintIcon(this, g,
                        LEFT_PERIMETER_BEVEL + (counter/4) * (CARD_WIDTH + INTERIOR_BEVEL),
                        PERIMETER_BEVEL + (row) * (CARD_HEIGHT + INTERIOR_BEVEL));
                    counter++;
                }                                               
            }                                                    
        }       
  }                                                 

注意:我实际上并没有运行该代码,因此您可能需要稍作调整。

正如@MadProgrammer 指出的那样,您不应该在您的绘画方法中加载所有这些,但这是一个不同的问题。

【讨论】:

    【解决方案2】:

    我会重新考虑你这样做的方式,将 Deck 与 Card 分离,并将控制器与 render 方法中的渲染器分离。 (仅限伪代码)

    类似这样的:

    public class CardView{
       String cardName, cardValue, [...];
       public void render(int x, int y){
          // Render this card, in the given (x,y) cordinates
       }
    }
    
    public class DeckView{
       ArrayList<CardView> cards;
    
       public void renderAll(){
          // Select random cars rows
          // Shuffle cols
          // For every card in array, call render inside the card with it's x and y
       }
    
       public ArrayList<CardView> getRandomCards(ArrayList<CardView> cards, int cardsToSelect){
          // Returns n random cards from the given list
       }
    
       public ArrayList<CardView> shuffleCards(ArrayList<CardView> cards){
          // Shuffle the given cards, in random order.
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 2014-05-13
      • 2021-06-26
      相关资源
      最近更新 更多