这是我第一次使用这个网站,所以不要对我这么认真
我试图制作自己的纸牌游戏,而在游戏中,这些卡片被放置在6 x 9网格布局中。我可以处理的其他所有内容,但游戏中有一个我遇到麻烦的功能,如果我能让它工作,我就会完成。 这是我最后的项目,大约需要5天才能完成
当用户点击White Joker卡时,我试图让卡片洗牌。 当点击那个白色小丑时,面朝下的牌和其他面朝下的扑克牌也会被洗牌。 我不希望网格中出现任何重复的卡片
为了更明确,我会在视觉上展示我的问题,因为我试图做的事情很难用文字解释,而且制作代码也很复杂。
当用户点击一张或多张面朝下的卡时,它会'看起来像这样:
当点击白色小丑时,它被洗牌,我不想让它洗牌。当白色小丑留在它的位置时,我希望其他一切都可以洗牌。
这是我的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;
}
再一次,当我洗牌并点击按钮时,我不想看到重复的扑克牌。我每次尝试都会让这种情况发生,我不会知道如何修复它所以这是我的其他代码创建框架和东西:
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***
我真的需要帮助。这是一个复杂的问题,一旦它解决了,我就会整理我的代码,我就会完成。
答案 0 :(得分:0)
你可以实现一个修改版的Fisher–Yates shuffle(或modern version,专为计算机使用而设计,由Richard Durstenfeld于1964年推出并由Donald E. Knuth推广):
在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;
}
}