如何在 GUI 中 Shuffle/Deal 一副牌
How to Shuffle/Deal a deck of cards in GUI
大家好,我目前正在制作 GUI。这将是一个名为 war 的纸牌游戏,我想做的是在用户和计算机之间随机分配牌组。我已经制作了三个阵列,一个用于您的计算机和整个平台,并且我制作了一个 for 循环,其中没有任何内容,因为我尝试的一切都不起作用。任何帮助将不胜感激
public class WarMultiScreen {
static int[] fullDeck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52 };
static int[] playerDeck; // the players deck
static int[] opponentDeck; // the computers deck
static String tMade = new String();
static int turnsMade = 0;
static int warsDeclared = 0;
private JFrame frmWarcardGame;
JPanel panelScreen1;
JPanel panelScreen2;
Random rand = new Random();
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WarMultiScreen window = new WarMultiScreen();
window.frmWarcardGame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public WarMultiScreen() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmWarcardGame = new JFrame();
frmWarcardGame.setTitle("War (Card Game)");
frmWarcardGame.setBounds(100, 100, 450, 300);
frmWarcardGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmWarcardGame.getContentPane().setLayout(new CardLayout(0, 0));
panelScreen1 = new JPanel();
panelScreen1.setBackground(new Color(124, 252, 0));
frmWarcardGame.getContentPane().add(panelScreen1,
"name_180601670832760");
panelScreen1.setLayout(null);
panelScreen2 = new JPanel();
panelScreen2.setBackground(Color.BLACK);
frmWarcardGame.getContentPane().add(panelScreen2,
"name_180620303895729");
// Loop for shuffling deck
for (int i = 0; i <= fullDeck.length; i++) {
}</i>
看看这个问题来洗牌你的数组:
Random shuffling of an array
洗牌后,遍历数组并将牌发给玩家数组。像这样:
for(int i = 0; i < cards.size(); i++){
if(i % 2 == 0){
userDeck.add(i);
} else{
computerDeck.add(i)
}
}
大家好,我目前正在制作 GUI。这将是一个名为 war 的纸牌游戏,我想做的是在用户和计算机之间随机分配牌组。我已经制作了三个阵列,一个用于您的计算机和整个平台,并且我制作了一个 for 循环,其中没有任何内容,因为我尝试的一切都不起作用。任何帮助将不胜感激
public class WarMultiScreen {
static int[] fullDeck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52 };
static int[] playerDeck; // the players deck
static int[] opponentDeck; // the computers deck
static String tMade = new String();
static int turnsMade = 0;
static int warsDeclared = 0;
private JFrame frmWarcardGame;
JPanel panelScreen1;
JPanel panelScreen2;
Random rand = new Random();
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WarMultiScreen window = new WarMultiScreen();
window.frmWarcardGame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public WarMultiScreen() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmWarcardGame = new JFrame();
frmWarcardGame.setTitle("War (Card Game)");
frmWarcardGame.setBounds(100, 100, 450, 300);
frmWarcardGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmWarcardGame.getContentPane().setLayout(new CardLayout(0, 0));
panelScreen1 = new JPanel();
panelScreen1.setBackground(new Color(124, 252, 0));
frmWarcardGame.getContentPane().add(panelScreen1,
"name_180601670832760");
panelScreen1.setLayout(null);
panelScreen2 = new JPanel();
panelScreen2.setBackground(Color.BLACK);
frmWarcardGame.getContentPane().add(panelScreen2,
"name_180620303895729");
// Loop for shuffling deck
for (int i = 0; i <= fullDeck.length; i++) {
}</i>
看看这个问题来洗牌你的数组: Random shuffling of an array
洗牌后,遍历数组并将牌发给玩家数组。像这样:
for(int i = 0; i < cards.size(); i++){
if(i % 2 == 0){
userDeck.add(i);
} else{
computerDeck.add(i)
}
}