如何用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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
- ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
- ¥15 gdf格式的脑电数据如何处理matlab
- ¥20 重新写的代码替换了之后运行hbuliderx就这样了
- ¥100 监控抖音用户作品更新可以微信公众号提醒
- ¥15 UE5 如何可以不渲染HDRIBackdrop背景
- ¥70 2048小游戏毕设项目
- ¥20 mysql架构,按照姓名分表
- ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
- ¥15 delphi webbrowser组件网页下拉菜单自动选择问题