-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeCracker.java
More file actions
212 lines (191 loc) · 6.52 KB
/
CodeCracker.java
File metadata and controls
212 lines (191 loc) · 6.52 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* CodeCracker.java
* Authors: Cali Stenson and Clara Smith
* Date: December 6, 2013
* Course: Wellesley College CS230-Data Structures
* Semester: Fall 2013
* This class creates and operates the game itself using
* the ciphers package and the level class for construction.
*/
import java.util.*;
import javafoundations.*;
import Jama.*;
import ciphers.Cipher;
import ciphers.CaesarCipher;
import ciphers.VigenereCipher;
import ciphers.AffineCipher;
import ciphers.HillCipher;
public class CodeCracker {
/**
* Instance Variables
* levels AdjMatGraphPlus<level>
* codes HashMap<String, String>
* current Level
* levelAccomplished boolean
*/
private AdjMatGraphPlus<Level> levels;
private HashMap<String,String> codes = new HashMap();
private Level current;
private boolean levelAccomplished;
/**
* Constructor
* @param gameGraph AdjMatGraphPlus<Level>
*
*/
public CodeCracker(AdjMatGraphPlus<Level> gameGraph) {
levels = gameGraph;
current = levels.allSources().get(0);//with game graph the must be only one source to start with
codes = constructMap();//creates an easily accessible list of of messages and their encrypted values
levelAccomplished = false;//None of the levels have been played when the game is constructed
}
/**
* constructMap()
* method used to create a HashMap of the
* messages corresponding to their encrypted
* text. Convenient for lookup because it's
* faster than encrypting every message in
* the graph as needed. This way encryption
* only happens once.
*/
private HashMap constructMap(){
LinkedList<Level> listLevs = levels.BFS(current);
for(int i=0; i<listLevs.size(); i++){
Level curr = listLevs.get(i);
String message = curr.getMessage();
String encrypted = curr.getCipher().encrypt(curr.getMessage());
codes.put(message, encrypted);
}
return codes;
}
/**
* playLevel()
* @param decoded String
* @return boolean
* Checks to see if the string passed in matches
* the message associated with the current level
* returns true if the user plays correctly and
* false otherwise.
*/
public boolean playLevel(String decoded){
if(current.getMessage().equalsIgnoreCase(decoded)){
levelAccomplished = true;
System.out.println("Level " + current.getName() + " completed!");
return true;
}else{
System.out.println("Decoded message incorrect");
levelAccomplished = false;
return false;
}
}
/**
* chooseNewLevel()
* @param l Level
* @return boolean
* Attempts to choose new level for the game.
* Fails to choose a new level if the user picks one
* that doesn't connect to the current level or if the
* user tries to move on without accomplishing the
* current level.
*/
public boolean chooseNewLevel(Level l){
if(levels.isArc(current, l) && levelAccomplished){
current = l;
levelAccomplished = false;
return true;
}else if(!levelAccomplished){
System.out.println("Please complete current level");
return false;
}else{
System.out.println("Please choose appropriate level");
return false;
}
}
/**
* codedMessage()
* @param Level l
* @return String
* retrieves encrypted message from the codes HashMap.
*/
public String codedMessage(Level l){
String message = l.getMessage();
String coded = codes.get(message);
return coded;
}
/**
* getCurrent()
* @return Level
* Returns the current level the user is playing
*/
public Level getCurrent() {
return current;
}
/**
* reset()
* resets everything to initial state for new game
*/
public void reset() {
this.current = levels.allSources().get(0);
this.levelAccomplished = false;
}
/**
* main()
* for testing
*/
public static void main(String[] args) {
//creates the levels
Level rome = new Level(new CaesarCipher(3), "Content/Ancient_Rome.txt", "Ancient Rome", "Beware of the Ides of March");
VigenereCipher vc = new VigenereCipher("Wendy");
Level tuscany = new Level(vc, "Content/Tuscany.txt", "Tuscany", "The Medici family kills all who protest to their power.");
Level venice = new Level(vc, "Content/Venice.txt", "Venice", "The pirates attacked our ship! We need reinforcements.");
AffineCipher ac = new AffineCipher(9, 3, 3);
Level quad = new Level(ac, "Content/Academic_Quad_1940.txt", "Academic Quad 1940", "Storm the tower! First person to the top gets ice cream!");
Level tupelo = new Level(ac, "Content/Tupelo_Lane_May_1,_1940.txt", "Tupelo Lane May 1 1940", "Martha Attridge");
Level hoop = new Level(ac, "Content/Caf?_Hoop_1981.txt", "Caf? Hoop 1981", "Make Nachos.");
Level tunnels = new Level(ac, "Content/Wellesley_Tunnels_1990.txt", "Wellesley Tunnels 1990", "Watch out there is asbestos!");
//matrices for HillCipher
double[][] k = new double[2][2];
k[0][0] = 9;
k[0][1] = 6;
k[1][0] = 6;
k[1][1] = 5;
Matrix key = new Matrix(k);
double[][] shift = new double[2][1];
Matrix shiftVal = new Matrix(shift);
double[][] inv = new double[2][2];
inv[0][0] = 15;
inv[0][1] = 8;
inv[1][0] = 8;
inv[1][1] = 1;
Matrix keyInv = new Matrix(inv);
HillCipher hc = new HillCipher(key, shiftVal, keyInv);
Level clapp = new Level(hc, "Content/Clapp_Library.txt", "Clapp Library", "HI");
//creates graph of levels
AdjMatGraphPlus<Level> gameGraph = new AdjMatGraphPlus<Level>();
gameGraph.addVertex(rome);
gameGraph.addVertex(tuscany);
gameGraph.addVertex(venice);
gameGraph.addVertex(quad);
gameGraph.addVertex(tupelo);
gameGraph.addVertex(hoop);
gameGraph.addVertex(tunnels);
gameGraph.addVertex(clapp);
gameGraph.addArc(rome, tuscany);
gameGraph.addArc(rome, venice);
gameGraph.addArc(tuscany, quad);
gameGraph.addArc(tuscany, tupelo);
gameGraph.addArc(venice, hoop);
gameGraph.addArc(venice, tunnels);
gameGraph.addArc(quad, clapp);
gameGraph.addArc(tupelo, clapp);
gameGraph.addArc(hoop, clapp);
gameGraph.addArc(tunnels, clapp);
//creates the game.
CodeCracker game = new CodeCracker(gameGraph);
game.playLevel("Beware of the Ides of March");
game.chooseNewLevel(tuscany);
game.playLevel("The Medici family kills all who protest to their power.");
game.chooseNewLevel(quad);
game.playLevel("Storm the tower! First person to the top gets ice cream!");
game.playLevel("ladsfjsaldfjsdjf");
}//end main
}