-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrypWordBtnEvent.java
More file actions
94 lines (86 loc) · 2.69 KB
/
CrypWordBtnEvent.java
File metadata and controls
94 lines (86 loc) · 2.69 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
package crypSwing;
import java.awt.event.*;
import java.util.ArrayList;
/**
* This class contains the code for when a user hits a button in the Word-solving frame.
* @author Bitman
* @version 1.0 7/21/19
* @version 2.0 01/20/21
*/
public class CrypWordBtnEvent implements ActionListener {
CrypWordFrame gui;
/**
* The constructor gets a reference to the Word-solving frame.
* @param in
*/
public CrypWordBtnEvent(CrypWordFrame in) {
gui = in;
}
/**
* The code that gets triggered when a user hits a button.
*/
public void actionPerformed(ActionEvent btnEvent) {
int index;
String action = btnEvent.getActionCommand();
switch (action) {
case "CLEAR":
for (index=0; index < CrypWordFrame.MAX_WORD_LENGTH; index++) {
gui.ciphertext[index].setText("");
gui.plaintext[index].setText("");
}
gui.actualCiphertextWord.setText("");
gui.ciphertext[0].requestFocus();
break;
case "SOLVE":
solve();
}
}
/**
* Display all solutions for a word.
*/
private void solve() {
CrypKey ck = new CrypKey();
CrypKeyEntry cke = new CrypKeyEntry();
int index, total, resultTotal;
String temp;
ArrayList<String> output = new ArrayList<String>();
total = gui.actualCiphertextWord.getText().length();
if (total==0) {
output.add("Error: Ciphertext word not entered.");
}
else {
for (index = 0; index < total; index++) {
temp = gui.plaintext[index].getText();
if (!temp.isEmpty()) {
cke.setCrypKeyEntry(index, temp.charAt(0));
ck.addEntry(cke);
}
}
output = CrypCodeBreaker.breakWord(gui.actualCiphertextWord.getText(), gui.likeExclusion.isSelected(), ck);
if (gui.usePuzzle.isSelected()) {
String plaintextTaken = gui.puzzleFrame.usedPlaintext(gui.actualCiphertextWord.getText());
for (index = output.size() - 1; index >= 0; index--)
if (charsInCommon(output.get(index), plaintextTaken))
output.remove(index);
}
}
gui.results.setListData(output.toArray(new String[output.size()]));
resultTotal = output.size();
if (resultTotal < 2)
gui.resultsLabel.setText("Results: ");
else
gui.resultsLabel.setText(resultTotal + " Results: ");
}
/**
* Determine whether two strings have at least one character in common.
* @param string1 The first string
* @param string2 The second string
* @return boolean true if the strings have at least one common character, false otherwise.
*/
static boolean charsInCommon(String string1, String string2) {
for (char string1Char : string1.toCharArray())
for (char string2Char : string2.toCharArray())
if (string1Char == string2Char) return true;
return false;
}
}