如何用JAVA实现wordle游戏,最后要做出界面。简单提供提下思路也行。
2条回答 默认 最新
- 小飞LOVE霞 2022-05-27 17:27关注
我用了两天实现了简单的一个版本,你试试:步骤如下:
运行程序后,在框中依次输入"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); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 7无用 1
悬赏问题
- ¥15 Attention is all you need 的代码运行
- ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
- ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
- ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
- ¥30 关于<main>标签页面跳转的问题
- ¥80 部署运行web自动化项目
- ¥15 腾讯云如何建立同一个项目中物模型之间的联系
- ¥30 VMware 云桌面水印如何添加
- ¥15 用ns3仿真出5G核心网网元
- ¥15 matlab答疑 关于海上风电的爬坡事件检测