如何用JAVA实现wordle游戏,最后要做出界面。简单提供提下思路也行。
我用了两天实现了简单的一个版本,你试试:步骤如下:
运行程序后,在框中依次输入"MOMMY","OTHER","ACTOR","MAJOR","FAVOR"。五个单词,点击【匹配】。这是我体验后的截图:
源码:
package csdn005;
import javax.swing.*;
import java.awt.*;
import java.util.Locale;
public class WorldleGame extends JFrame{
private int count = 0;
public static void main(String[] args) {
new WorldleGame();
}
public WorldleGame() {
super("WORDLE GAME");
JPanel jPanel = new JPanel();
//{"MOMMY","OTHER","ACTOR","MAJOR","FAVOR"}
String words = "FAVOR";
JButton[] jButtons = new JButton[25];
for (int i = 0;i < jButtons.length;i++) {
jButtons[i] = new JButton();
jPanel.add(jButtons[i]);
}
for (int i = 0;i < 25;i++) {
jButtons[i].setText(" ");
}
JTextField jt=new JTextField();
jt.setColumns(20);
jt.setFont(new Font("黑体", Font.PLAIN,20));
jPanel.add(jt);
JButton submit = new JButton("匹配");
submit.addActionListener(e -> {
String text = jt.getText();
int length = text.length();
if (length != 5) {
jt.setText("");
return;
}
text = text.toUpperCase(Locale.ROOT);
while(count < 6) {
if (count == 0) {
for (int i = 0;i < text.length();i++) {
String c = text.charAt(i) + "";
jButtons[i].setText(c + "");
if (c.equals(words.substring(i,i + 1))) {
jButtons[i].setBackground(Color.GREEN);
}else {
if (words.contains(c)) {
jButtons[i].setBackground(Color.YELLOW);
}
}
}
}
if (count == 1) {
for (int i = 0;i < text.length();i++) {
String c = text.charAt(i) + "";
jButtons[i + 5].setText(c + "");
if (c.equals(words.substring(i,i + 1))) {
jButtons[i + 5].setBackground(Color.GREEN);
}else {
if (words.contains(c)) {
jButtons[i + 5].setBackground(Color.YELLOW);
}
}
}
}
if (count == 2) {
for (int i = 0;i < text.length();i++) {
String c = text.charAt(i) + "";
jButtons[i + 10].setText(c + "");
if (c.equals(words.substring(i,i + 1))) {
jButtons[i + 10].setBackground(Color.GREEN);
}else {
if (words.contains(c)) {
jButtons[i + 10].setBackground(Color.YELLOW);
}
}
}
}
if (count == 3) {
for (int i = 0;i < text.length();i++) {
String c = text.charAt(i) + "";
jButtons[i + 15].setText(c + "");
if (c.equals(words.substring(i,i + 1))) {
jButtons[i + 15].setBackground(Color.GREEN);
}else {
if (words.contains(c)) {
jButtons[i + 15].setBackground(Color.YELLOW);
}
}
}
}
if (count == 4) {
for (int i = 0;i < text.length();i++) {
String c = text.charAt(i) + "";
jButtons[i + 20].setText(c + "");
if (c.equals(words.substring(i,i + 1))) {
jButtons[i + 20].setBackground(Color.GREEN);
}else {
if (words.contains(c)) {
jButtons[i + 20].setBackground(Color.YELLOW);
}
}
}
}
count++;
jt.setText("");
break;
}
});
jPanel.add(submit);
setBounds(600,200,280,500);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(jPanel);
}
}
是我邮的学生吗TAT