首页 文章

当我单击网格中的按钮时,卡片在gridlayout中随机播放

提问于
浏览
0

这是我第一次使用这个网站,所以不要狠狠地对我说

我麻烦了,如果能让它上班,我会完蛋的 . This is for my final project and it's due in about 5 days

当用户点击White Joker卡时,我正试图让卡片洗牌 . 当点击那个白色小丑时,面朝下的牌和其他面朝下的扑克牌也会被洗牌 . I don't want any duplicate cards in the grid

更明确一点,我会在视觉上展示我的问题,因为我想要做的事情很难用文字解释,也很难为它编写代码 .

当用户点击一张或多张面朝下的牌时,它';看起来像这样:
When cards are clicked Image

当点击白色小丑时,它被洗牌了,我不知道 .
When White Joker is clicked Image

这是我的shuffle方法代码 . 下面我想要这个方法来洗牌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;
}

再次, I do not want to see duplicate poker cards when I shuffle the cards and click on the buttons. 我每次尝试时都会发生这种情况,我不知道如何修复它,所以这里是我创建框架和其他内容的其他代码:

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;

/**
*
* @author elngo
*/
public class GameFrame extends JFrame {

private final int FRAME_WIDTH = 900;
private final int FRAME_HEIGHT = 730;
private int _attempts = 0;

private GridLayout _buttonMatrix;
private ImageIcon _image;
private ImageIcon _faceDownImage;
private JButton[] _button;
private ActionListener _listener;
private JPanel _gridPanel;


private JOptionPane _introduction; //this pops up BEFORE the gamem starts
private JOptionPane _endGameResult; //this ONLY pops up when the game ENDS

//using the Cards class
private Cards _cards;
private String[] _pokerDeck;

private JButton _whiteJoker;

final int GRID_ROWS = 6;
final int GRID_COLUMNS = 9;

//Constructor
/**
 * 
 */
public GameFrame()
{ 
    frameComponents();
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
}

/**
 * 
 */
private void frameComponents()
{
    _cards = new Cards(); //invokes Cards class
    String[] faceDown = _cards.getFaceDown();
    _pokerDeck = _cards.getPokerDeck();


    //makes a matrix of JButtons
    _button = new JButton[_pokerDeck.length];
    _gridPanel = new JPanel(_buttonMatrix = new GridLayout(GRID_ROWS, GRID_COLUMNS));


    _listener = new ClickListener();

    //places FACE DOWN cards in the 6x9 grid first
    for (int i = 0; i<faceDown.length; i++)
    {
       _faceDownImage = new ImageIcon(faceDown[i]);

       _gridPanel.add(_button[i] = new JButton(_faceDownImage)); //adds to grid

       _button[i].addActionListener(_listener);
    }
    add(_gridPanel);
    //shuffle poker cards

    //comment this randomizer out UNTIL I find a way to make WhiteJoker work
    //_cards.shuffleDeck(_pokerDeck);
}

public class ClickListener implements ActionListener{


@Override
/**
 * 
 */
public void actionPerformed(ActionEvent event)
{

  for (int i=0; i<_button.length; i++)
  {
      if (event.getSource() == _button[i])
      {
          _image = new ImageIcon(_pokerDeck[i]);

          _button[i].setIcon(_image);

          _attempts++;
          System.out.println("Attempts: " + _attempts); //delete later


          //***THE WHITE JOKER SHUFFLE PROBLEM STARTS HERE***
          if (_pokerDeck[i] == "WJ.png") //if White Joker clicked
          {
            //FIX THISSSSSSSSSSSSSSSsssSSSSSSSSSssssSs  
            System.out.println("White Joker found"); //delete later

            //save off Joker Spot so iterate through _buttonMatrix
            String whiteJoker = _pokerDeck[i];

            for (int j = 0; j<_button.length; j++)
            {
                if (_button[j] != null)
                {
                  //***THE SHUFFLE METHOD I SHOWED IS USED BELOW***
                  _cards.whiteJokerShuffle(_button); 
                }

            }
            _gridPanel.removeAll();
            for (JButton button : _button)
            {
                _gridPanel.add(button);
            }
            _gridPanel.revalidate();
            _gridPanel.repaint();
          }
         //***PROBLEM STOPS HERE***

我真的需要帮助 . 只是这一个复杂的问题,一旦它解决了,我将整理我的代码,我将完成 .

1 回答

  • 0

    您可以实现Fisher–Yates shuffle(或modern version,专为计算机使用而设计的修改版本,由Richard Durstenfeld于1964年推出并由Donald E. Knuth推广):

    • 当白色小丑被洗牌时,你将它放在同一个位置 .

    • 当其他一张牌要洗牌时,你要确保它不与White Joker交换 .

    actionPerformed 方法中,您遍历所有按钮,然后检测是否单击了White Joker . 循环变量 i 包含White Joker的索引,您不想更改它 . 该值可以传递给修改后的shuffle方法 .

    为了使下面的例子简短,我传递了一个字符串数组(而不是按钮)并确定了shuffle方法中White Joker的索引:

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

相关问题