如何用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
悬赏问题
- ¥50 adb连接不到手机是怎么回事?
- ¥15 vs2022无法联网
- ¥15 TCP的客户端和服务器的互联
- ¥15 VB.NET操作免驱摄像头
- ¥15 笔记本上移动热点开关状态查询
- ¥85 类鸟群Boids——仿真鸟群避障的相关问题
- ¥15 CFEDEM自带算例错误,如何解决?
- ¥15 有没有会使用flac3d软件的家人
- ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
- ¥15 docker实践项目