package csdn005;
import javax.swing.*;
import java.awt.*;
import java.util.Locale;
/**
* @author wangfei
*/
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";
// 定义按钮数组,按照WorldleGame的规则,需要5*5=25的数量
JButton[] jButtons = new JButton[25];
// 循环new 25个按钮,用于放置单个字母
for (int i = 0;i < jButtons.length;i++) {
jButtons[i] = new JButton();
// 同时将该按钮加到顶级容器(jPanel)里面
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
jPanel.add(jt);
// 定义匹配按钮
JButton submit = new JButton("匹配");
// 给匹配按钮添加点击事件
submit.addActionListener(e -> {
// 获取输入框的值
String text = jt.getText();
// 获取该值得长度
int length = text.length();
// 游戏规则是每个单词是由五个字母组成
if (length != 5) {
// 若长度不为5,那么直接结束
jt.setText("");
return;
}
// 将输入的值转化为大写
text = text.toUpperCase(Locale.ROOT);
// 循环1-5次结束
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("");
// over
break;
}
});
// 将按钮也加入容器
jPanel.add(submit);
// 600 指窗口左边距离电脑左边的距离
// 200 指窗口上边距离电脑上边的距离
// 280 指窗口的宽度
// 600 指窗口的高度
setBounds(600,200,280,500);
// 设置窗口显示
setVisible(true);
// 设置窗口的叉叉可以关闭窗口
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 将顶级容器加入窗口内
add(jPanel);
}
}
注释Wordle游戏
于 2022-06-05 17:07:59 首次发布