gpt4 book ai didi

java - 需要帮助在内存游戏中随机化数字

转载 作者:太空宇宙 更新时间:2023-11-04 13:50:42 28 4
gpt4 key购买 nike

这将创建一个 3 行 4 列的 jframe。我正在尝试实现一个与字母匹配的内存游戏。截至目前,代码匹配,但字母不是随机放置的。我在网上找不到任何关于此的信息。我还想知道它们是否是我可以用来更改 GUI 背景的方法。

public class matchinggame implements ActionListener {
JPanel p;
JFrame f;
String[][] matchList = { {"a", "a"}, {"b", "b"},
{"c", "c" }, {"d", "d"}, {"e", "e"},
{"f", "f"}, {"g", "g" }};
JButton[][] buttons;
int i = 0;
boolean flipping = true;
int cardOne;
int secIndex;


public static void main(String[] args) {
//Schedule a job for the event-dispatching thread
//to create application and display its GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
matchinggame app = new matchinggame();
app.makeGUI();
}
});
}

public void dealCards(JPanel panel) {
buttons = new JButton[3][]; // array of buttons used to represent cards
for (int i= 0; i< 3*4; i++) { // initialize 3 rows with 4 columns each
if (i%4 == 0) buttons[i/4] = new JButton[4];
buttons[i/4][i%4] = new JButton("-Match-"); // show face down
buttons[i/4][i%4].addActionListener(this);
panel.add(buttons[i/4][i%4]);
}
}

public void updateMatchList(String a, String b, boolean add) {
int i,j;
String[][] courseList;
int oldLen = matchList.length;

if (add) { // add the new item to the list
courseList = new String[oldLen+1][];
courseList[0] = new String[2];
courseList[0][0] = new String(a); // new first course
courseList[0][1] = new String(b); // new first course num
for (int item=1; item<= oldLen; item++) {
courseList[item][0] = matchList[item-1][0];
courseList[item][1] = matchList[item-1][1];
}
matchList = courseList;
} else { // delete matching item
courseList = new String[oldLen-1][];
courseList[0] = new String[2];
courseList[0][0] = new String(a); // new first course
courseList[0][1] = new String(b); // new first course num
for (int item=0; item<= oldLen; item++) {
if (a != courseList[item][0]) { // no match so OK to copy over
courseList[item][0] = matchList[item][0];
courseList[item][1] = matchList[item][1];
}
}
matchList = courseList;
}
}

/**
* Creates the JFrame and its UI components.
*/
public void makeGUI() {
JFrame frame = new JFrame("CS435F08 - Java Match Game Starter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel(new GridLayout(3,4));
p.setPreferredSize(new Dimension(500, 300));
dealCards(p);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(p,BorderLayout.CENTER);

// Display the window.
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
int r,c;


if (i<2) { //find the card clicked on and flip it over

for (r=0; r< 3; r++) {
for (c=0; c< 4; c++) {
// if the card is not face down (showing "-Match-") don't flip it
if ((e.getSource()== buttons[r][c]) && buttons[r][c].getText().equals("-Match-")){
// flip the card face-up to show text from matchList
// looks up text based upon indexes
buttons[r][c].setText(matchList[(r*4+c)/2][(r*4+c)%2]);
i++; // increment number of cards flipped
if (i==1) cardOne = (r*4+c)/2; // save which pattern was shown first
else secIndex = (r*4+c)/2; // save the pattern shown second
return;
}
}
}
} else { // 2 cards already flipped, put all cards face down

for (r=0; r< 3; r++) {
for (c=0; c< 4; c++) {
if (cardOne == secIndex) { // first and second cards flipped match
if (!buttons[r][c].getText().equals("-Match-")) // don't change the face down cards
buttons[r][c].setText("*******"); // once matched, show the removed pattern
} else if ((!buttons[r][c].getText().equals("*******")) && (!buttons[r][c].getText().equals("-Match-"))) {
buttons[r][c].setText("-Match-"); // if 2 face up cards didn't match, flip face down again
}
}
i=0; // new turn, no cards flipped face up
}
}
}
}

最佳答案

我将 matchList 更改为一维数组。这样,我就可以在 shuffleCards 方法中随机播放文本。

这是 GUI。

Match Game GUI

我修复了您的 Action 监听器的一些问题。

这是格式化的代码。

package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MatchingGame implements ActionListener {
JPanel p;
JFrame f;

String[] matchList = { "a", "a", "b", "b", "c", "c", "d", "d", "e", "e",
"f", "f", "g", "g" };
String[] shuffledList;

JButton[][] buttons;

boolean flipping = true;

int i = 0;
int cardOne;
int secIndex;

public static void main(String[] args) {
// Schedule a job for the event-dispatching thread
// to create application and display its GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
MatchingGame app = new MatchingGame();
app.makeGUI();
}
});
}

public void dealCards(JPanel panel) {
buttons = new JButton[3][]; // array of buttons used to represent cards
shuffleCards();
for (int i = 0; i < 3 * 4; i++) { // initialize 3 rows with 4 columns
// each
if (i % 4 == 0)
buttons[i / 4] = new JButton[4];
buttons[i / 4][i % 4] = new JButton("-Match-"); // show face down
buttons[i / 4][i % 4].addActionListener(this);
panel.add(buttons[i / 4][i % 4]);
}
}

public void shuffleCards() {
List<String> list = Arrays.asList(matchList);
Collections.shuffle(list);
shuffledList = list.toArray(new String[list.size()]);
}

public void updateMatchList(String a, String b, boolean add) {
String[] courseList;
int oldLen = matchList.length;

if (add) { // add the new item to the list
courseList = new String[oldLen + 2];
courseList[0] = new String(a); // new first course
courseList[1] = new String(b); // new first course num
for (int item = 2; item <= oldLen; item += 2) {
courseList[item] = matchList[item - 2];
courseList[item + 1] = matchList[item - 1];
}
matchList = courseList;
} else { // delete matching item
courseList = new String[oldLen - 2];
int matchItem = 0;
for (int item = 0; item <= oldLen; item += 2) {
if (a != matchList[item]) { // no match so OK to copy over
courseList[item] = matchList[matchItem];
courseList[item + 1] = matchList[matchItem + 1];
matchItem += 2;
}
}
matchList = courseList;
}

}

/**
* Creates the JFrame and its UI components.
*/
public void makeGUI() {
JFrame frame = new JFrame("CS435F08 - Java Match Game Starter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel(new GridLayout(3, 4));
p.setPreferredSize(new Dimension(500, 300));
dealCards(p);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(p, BorderLayout.CENTER);

// Display the window.
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
int r, c;

if (i < 2) { // find the card clicked on and flip it over

for (r = 0; r < 3; r++) {
for (c = 0; c < 4; c++) {
// if the card is not face down (showing "-Match-") don't
// flip it
if ((e.getSource() == buttons[r][c])
&& buttons[r][c].getText().equals("-Match-")) {
// flip the card face-up to show text from matchList
// looks up text based upon indexes
buttons[r][c].setText(shuffledList[(r * 4 + c)]);
i++; // increment number of cards flipped
if (i == 1)
cardOne = (r * 4 + c); // save which pattern was
// shown first
else
secIndex = (r * 4 + c); // save the pattern
// shown second
return;
}
}
}
} else { // 2 cards already flipped, put all cards face down

for (r = 0; r < 3; r++) {
for (c = 0; c < 4; c++) {
// first and second cards flipped
if (shuffledList[cardOne].equals(shuffledList[secIndex])) {
// match
// don't change the face down cards
if (!buttons[r][c].getText().equals("-Match-"))
// once matched, show the removed pattern
buttons[r][c].setText("*******");
} else if ((!buttons[r][c].getText().equals("*******"))
&& (!buttons[r][c].getText().equals("-Match-"))) {
// if 2 face up cards didn't match, flip face down again
buttons[r][c].setText("-Match-");
}
}
i = 0; // new turn, no cards flipped face up
}
}
}
}

关于java - 需要帮助在内存游戏中随机化数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30309246/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com