这是我第一次使用这个网站,所以不要对我太苛刻
我正在尝试做我自己的纸牌游戏,在游戏中,卡片被放置在一个6x9的网格布局中。所有其他我都能处理,但在游戏中有一个特点,我有困难,如果我能让它工作,我将完成。,这是我的最后一个项目,大约5天后就要到期了,
我试图使牌洗牌时,用户点击白色小丑卡。当那个白小丑被点击时,正面向上的扑克牌和其他的扑克牌也会被洗牌。我不想在网格中有任何重复的卡片
更明确地说,我将直观地展示我的问题,因为我想要做的是用语言来解释,而且要为它编写代码也很复杂。
当用户单击一个或多个正面向下的卡片时,它‘;如下所示:

当白色小丑被点击,它是洗牌,我不希望它洗牌。我想要其他的东西洗牌,而白小丑留在它的位置。

这是我的洗牌方法代码。下面我想要这个方法来洗牌JButtons,而不是白小丑。白小丑应该留下来
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;
}再说一遍,我不想看到重复的扑克牌,当我洗牌并点击按钮的时候。每当我尝试的时候,我都会这样做,我不知道如何修复它,这是我创建框架和其他东西的另一个代码:
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***我真的需要帮助。只有这个复杂的问题,一旦它解决了,我将整理我的代码,我将完成。
发布于 2015-12-25 22:58:53
您可以实现费舍-耶茨洗牌的修改版本(或为计算机使用而设计的现代版,该版本是理查德·德斯滕菲尔德于1964年引入的,唐纳德·E·克努特(DonaldE.Knuth)所推广):
在actionPerformed方法中,您将遍历所有按钮,然后检测到单击了“白小丑”。循环变量i包含白色小丑的索引,您不想更改该索引。可以将此值传递给修改后的混叠方法。
为了保持示例的简短性,我传递了一个字符串数组(而不是按钮),并在洗牌方法中确定了白小丑的索引:
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;
}
}https://stackoverflow.com/questions/34235775
复制相似问题