-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordleGUI.java
More file actions
147 lines (127 loc) · 4.21 KB
/
WordleGUI.java
File metadata and controls
147 lines (127 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package wordle_gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class WordleGUI extends JFrame {
private JTextField[][] textRows;
private int currRow;
private State state;
private JPanel panel;
public WordleGUI() {
setTitle("Tiles GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 700);
// Create a panel to hold the components
JPanel mainPanel = new JPanel(new BorderLayout());
panel = new JPanel();
panel.setLayout(new GridLayout(6, 5));
mainPanel.add(panel, BorderLayout.CENTER);
// Create the input fields
textRows = new JTextField[6][5];
createInputField();
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
submitButtonClicked();
}
});
mainPanel.add(submitButton, BorderLayout.SOUTH);
add(mainPanel);
currRow = 0;
state = new State();
}
private void submitButtonClicked() {
int total = 0;
for(int i = 0; i <=4; i++) {
int correct = state.checkCharacter(textRows[currRow][i].getText(), i);
if(correct == 1) {
textRows[currRow][i].setBackground(Color.GREEN);
total++;
}
else if(correct == 2) {
textRows[currRow][i].setBackground(Color.YELLOW);
}
textRows[currRow][i].setEditable(false);
}
if(currRow == 5 && total != 5) {
JOptionPane.showMessageDialog(this, "Better Luck Next Time", "MESSAGE", JOptionPane.PLAIN_MESSAGE);
state.gameOver(false);
resetGame();
return;
}
else if(total == 5) {
JOptionPane.showMessageDialog(this, "Great Job", "MESSAGE", JOptionPane.PLAIN_MESSAGE);
state.gameOver(false);
resetGame();
return;
}
textRows[currRow+1][0].requestFocus();
currRow += 1;
}
private void createInputField() {
for(int i = 0; i <=5; i++) {
for(int j = 0; j<= 4; j++) {
JTextField inputField = new JTextField();
inputField.setHorizontalAlignment(JTextField.CENTER);
inputField.setFont(new Font("Ariel", Font.BOLD, 35));
inputField.addKeyListener(new TileKeyListener(i, j));
panel.add(inputField);
textRows[i][j] = inputField;
}
}
}
public void resetGame() {
resetInputFields();
currRow = 0;
state = new State();
}
public void resetInputFields() {
for(int i = 0; i <= 5; i++) {
for(int j = 0; j <= 4; j++) {
textRows[i][j].setEditable(true);
textRows[i][j].setText("");
textRows[i][j].setBackground(Color.white);
}
}
}
private class TileKeyListener implements KeyListener {
private int tileRow;
private int tileColumn;
public TileKeyListener(int tileRow, int tileColumn) {
this.tileRow = tileRow;
this.tileColumn = tileColumn;
}
@Override
public void keyReleased(KeyEvent e) {
int pos = textRows[tileRow][tileColumn].getCaretPosition();
textRows[tileRow][tileColumn].setText(textRows[tileRow][tileColumn].getText().toUpperCase());
textRows[tileRow][tileColumn].setCaretPosition(pos);
char c = e.getKeyChar();
// validate keys: only allow letters for our guesses
if (Character.isLetter(c)) {
// Move focus to the next tile
if (tileColumn < 4) {
textRows[tileRow][tileColumn + 1].requestFocus();
}
}
}
@Override
public void keyTyped(KeyEvent e) {
// empty placeholder
}
@Override
public void keyPressed(KeyEvent e) {
// empty placeholder
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
WordleGUI tilesGUI = new WordleGUI();
tilesGUI.setVisible(true);
});
}
}