Java 当我点击网格中的按钮时,卡片在网格布局中洗牌

Java 当我点击网格中的按钮时,卡片在网格布局中洗牌,java,swing,user-interface,jbutton,grid-layout,Java,Swing,User Interface,Jbutton,Grid Layout,这是我第一次使用这个网站,所以不要对我太苛刻 我正在尝试制作自己的纸牌游戏,在游戏中,纸牌被放置在一个6 x 9的网格布局中。其他我都能处理,但游戏中有一个功能我遇到了麻烦,如果我能让它工作,我就完成了这是我的最后一个项目,大约5天后到期 当用户点击白色小丑卡时,我试图使卡洗牌。 当点击白色小丑时,正面朝上的牌和其他正面朝下的牌也会被洗牌我不希望网格中有任何重复的卡片 更明确地说,我将以直观的方式展示我的问题,因为我试图做的事情很复杂,很难用文字来解释,也很难为它编写代码 当用户点击一张或多张面

这是我第一次使用这个网站,所以不要对我太苛刻

我正在尝试制作自己的纸牌游戏,在游戏中,纸牌被放置在一个6 x 9的网格布局中。其他我都能处理,但游戏中有一个功能我遇到了麻烦,如果我能让它工作,我就完成了这是我的最后一个项目,大约5天后到期

当用户点击白色小丑卡时,我试图使卡洗牌。 当点击白色小丑时,正面朝上的牌和其他正面朝下的牌也会被洗牌我不希望网格中有任何重复的卡片

更明确地说,我将以直观的方式展示我的问题,因为我试图做的事情很复杂,很难用文字来解释,也很难为它编写代码

当用户点击一张或多张面朝下的卡片时,它是“;看起来像这样:

当点击白色小丑时,它被洗牌了,我不想让它洗牌。我希望其他的一切都能在白小丑呆在原地的时候慢慢来。

这是我的洗牌方法代码。下面我想用这个方法来洗牌JButtons,但不是White Joker方法。白人小丑应该留下来

public JButton[] whiteJokerShuffle(JButton[] button)
{
    //shuffles using fisher yates shuffle BUT ONLY White Joker does not     
    //shuffle
    //FIX THISSSSSSSSSSSSSSSSSSSSSSS
    Random rand = new Random();
    int randomCard;
    JButton randomValue;

    for (int i = 0; i<button.length; i++)
    {
        randomCard = rand.nextInt(button.length);
        //can't find a way to check for White Joker and make it stay

        randomValue = button[randomCard];

        button[randomCard] = button[i];
        button[i] = randomValue;

    }
    return button;
}
public JButton[]白笑话(JButton[]按钮)
{
//使用fisher-yates shuffle进行洗牌,但只有白色小丑不会
//洗牌
//修复此SSSSSSSSSS
Random rand=新的Random();
int随机卡;
JButton随机值;

对于(int i=0;i,您可以实现的(或,设计用于计算机的,由Richard Durstenfeld于1964年引入,由Donald E.Knuth推广的)的修改版本:

  • 当白小丑被洗牌时,你把它放在同一个位置上
  • 当另一张牌被洗牌时,你要确保它没有与白小丑交换
  • actionPerformed
    方法中,迭代所有按钮,然后检测是否单击了白色小丑。循环变量
    i
    包含白色小丑的索引,您不想更改该索引。此值可以传递给修改后的shuffle方法

    为了简化下面的示例,我传递了一个字符串数组(而不是按钮),并确定了shuffle方法中白色小丑的索引:

    import java.util.*;
    
    public class GameFrame {
        private static final String WHITE_JOKER_CODE = "*W";
    
        public static void main(String[] arguments) {
            String[] originalCards = {"5H", "5C", "6S", WHITE_JOKER_CODE, "7S", "KD"};
            System.out.println("Original cards: " + Arrays.toString(originalCards));
    
            String[] shuffledCards = new GameFrame().whiteJokerShuffle(originalCards);
    
            System.out.println("Shuffled cards: " + Arrays.toString(shuffledCards));
        }
    
        // Uses the modern version of the Fisher–Yates shuffle, designed for computer use,
        // as introduced by Richard Durstenfeld in 1964 and popularized by Donald E. Knuth.
        // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
        public String[] whiteJokerShuffle(String[] cards)
        {
            int whiteJokerIndex = Arrays.asList(cards).indexOf(WHITE_JOKER_CODE);
            Random randomNumbers = new Random();
    
            for (int cardIndex = cards.length - 1; cardIndex > 0; cardIndex--)
            {
                if (cardIndex != whiteJokerIndex) {
                    // The upper bound is normally one higher than cardIndex, but it is
                    // lowered by one when the white joker is in the range (to "hide" it).
                    boolean hideJoker = cardIndex > whiteJokerIndex;
                    int upperBound = cardIndex + (hideJoker ? 0 : 1);
                    int swapIndex = randomNumbers.nextInt(upperBound);
    
                    if (swapIndex == whiteJokerIndex) {
                        swapIndex++;
                    }
    
                    // Swap the cards on indices swapIndex and cardIndex.
                    String swapCard = cards[swapIndex];
                    cards[swapIndex] = cards[cardIndex];
                    cards[cardIndex] = swapCard;
                }
            }
    
            return cards;
        }
    }
    
    一些一般提示:(1)不要在Java变量名中使用下划线(
    \uu
    ),除非它们是常量。(2)不要硬编码帧的大小。我的屏幕没有730像素的高度。让布局管理器计算大小。“找不到检查白色小丑并使其保持不变的方法”您可以访问代表卡片的按钮,为什么您不能从按钮中读取它们显示的卡片?
    import java.util.*;
    
    public class GameFrame {
        private static final String WHITE_JOKER_CODE = "*W";
    
        public static void main(String[] arguments) {
            String[] originalCards = {"5H", "5C", "6S", WHITE_JOKER_CODE, "7S", "KD"};
            System.out.println("Original cards: " + Arrays.toString(originalCards));
    
            String[] shuffledCards = new GameFrame().whiteJokerShuffle(originalCards);
    
            System.out.println("Shuffled cards: " + Arrays.toString(shuffledCards));
        }
    
        // Uses the modern version of the Fisher–Yates shuffle, designed for computer use,
        // as introduced by Richard Durstenfeld in 1964 and popularized by Donald E. Knuth.
        // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
        public String[] whiteJokerShuffle(String[] cards)
        {
            int whiteJokerIndex = Arrays.asList(cards).indexOf(WHITE_JOKER_CODE);
            Random randomNumbers = new Random();
    
            for (int cardIndex = cards.length - 1; cardIndex > 0; cardIndex--)
            {
                if (cardIndex != whiteJokerIndex) {
                    // The upper bound is normally one higher than cardIndex, but it is
                    // lowered by one when the white joker is in the range (to "hide" it).
                    boolean hideJoker = cardIndex > whiteJokerIndex;
                    int upperBound = cardIndex + (hideJoker ? 0 : 1);
                    int swapIndex = randomNumbers.nextInt(upperBound);
    
                    if (swapIndex == whiteJokerIndex) {
                        swapIndex++;
                    }
    
                    // Swap the cards on indices swapIndex and cardIndex.
                    String swapCard = cards[swapIndex];
                    cards[swapIndex] = cards[cardIndex];
                    cards[cardIndex] = swapCard;
                }
            }
    
            return cards;
        }
    }