大家好,我目前正在制作一个GUI。这将是一个叫做战争的纸牌游戏我要做的是在玩家和电脑之间随机分配牌组。我做了三个数组一个是给你的电脑,一个是完整的数组我做了一个没有任何内容的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>
看这个问题来洗牌数组:数组的随机洗牌
洗牌后,遍历数组并将牌分发给玩家数组。像这样:
for(int i = 0; i < cards.size(); i++){
if(i % 2 == 0){
userDeck.add(i);
} else{
computerDeck.add(i)
}
}