-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto.pl
executable file
·350 lines (320 loc) · 9.89 KB
/
crypto.pl
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/perl -w
use strict;
use Gtk2 '-init';
#use Gtk2::SimpleList;
use Gtk2::SimpleMenu;
use Text::Wrap;
sub TRUE{1} sub FALSE{0}
my $min_fortune=80;
my $max_fortune=130;
my $fortune_command = "fortune -n $min_fortune -l";
my $numColumns = 40;
my $font_size = 15;
my $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
my $fortune;
my $key;
my $victorious = FALSE;
my %encrypt;
my %decrypt;
my %guesses; #win when enough of %guesses is like %decrypt. (not all letters are used.)
my %guessLabels; #lists of (empty at first) labels in fortuneview
my @guessEntries; #list of 26 entries in guesstable
my %letterCount; #unencrypted
my %enc_letterCount;
my $victory_message =
"Congratulations.\n You took 5 points from Jack Bauer. \nRun.";
my $labelfont = Gtk2::Pango::FontDescription->from_string("Andale Mono Bold $font_size");
my $entryfont = Gtk2::Pango::FontDescription->from_string("Andale Mono $font_size");
my $win = Gtk2::Window->new();
$win->signal_connect("delete_event", sub {Gtk2->main_quit} );
my $fortuneView;
my $guessTable;
my $vbox = Gtk2::VBox->new(FALSE,0);
$win->add($vbox);
my $menu_tree = [
_File => {
item_type => '<Branch>',
children => [
_New => {
item_type => '<StockItem>',
callback => \&new_puzzle,
callback_action => 0,
accelerator => '<ctrl>N',
extra_data => 'gtk-new',
},
_Cheat => {
callback => \&cheat,
callback_action => 1,
callback_data => 'per entry cbdata',
accelerator => '<ctrl>C',
},
_Quit => {
item_type => '<StockItem>',
callback => sub{Gtk2->main_quit},
callback_action => 2,
accelerator => '<ctrl>Q',
extra_data => 'gtk-quit',
},
]
},
];
my $menu = Gtk2::SimpleMenu->new (
menu_tree => $menu_tree,
default_callback => sub {print "unimplemented\n"},
user_data => 'user_data',
);
$vbox->pack_start($menu->{widget}, TRUE, FALSE, 0);
$win->add_accel_group($menu->{accel_group});
new_puzzle();
Gtk2->main();
sub new_puzzle{
$victorious = FALSE;
%guesses = ();
$fortune = uc get_fortune($min_fortune, $max_fortune);
gen_random_key();
count_letters();
reload_crypto_tables();
}
sub reload_crypto_tables{
$fortuneView->destroy if defined $fortuneView;
$guessTable->destroy if defined $guessTable;
$fortuneView = Gtk2::Table->new(6, $numColumns);
$guessTable = Gtk2::Table->new(2, 26, TRUE);
$vbox->pack_start($fortuneView, TRUE, FALSE, 0);
$vbox->pack_start($guessTable, TRUE, FALSE, 0);
reloadFortuneView();
reloadGuessTable();
$win->show_all;
}
sub count_letters{
%letterCount = ();
%enc_letterCount = ();
for my $char (split //, $fortune){
$letterCount{$char}++ if $char =~ /[A-Z]/;
$char = encrypt($char);
$enc_letterCount{$char}++ if $char =~ /[A-Z]/;
$guesses{$char} = '' if $char =~ /[A-Z]/;
}
}
sub get_fortune{
my ($min,$max) = @_;
while(1){
my $fortune = `$fortune_command`;
next if length ($fortune) < $min;
next if length ($fortune) > $max;
$fortune =~ s/\t/ /g; #tabs to (3) spaces
#newlines to 1 space, unless there's space after it. (as in quotes)
$fortune =~ s/\n(\S)/ $1/g;
return $fortune;
}
}
#modified fisher yates shuffle
sub gen_random_key{
my @array = split (//, $alpha);
my @alpha = @array;
for (my $i = @array; --$i; ) {
my $j = int rand ($i+1);
@array[$i, $j] = @array[$j, $i];
}
for (0..$#alpha){
$decrypt{$alpha[$_]} = $array[$_];
$encrypt{$array[$_]} = $alpha[$_];
}
#ensure a derangement
for (0..$#alpha){
if ($alpha[$_] eq $array[$_]) {
gen_random_key();
#print "key not deranged, setting another..\n";
return;
}
}
}
sub getGuess{
my $char = shift;
return $char if $char !~ /[A-Z]/; #space, num, or punctuation
return $guesses{$char} if $guesses{$char};
return '_';
}
sub encrypt{
my $char = shift;
return $char unless defined $encrypt{$char};
return $encrypt{$char};
}
sub decrypt{
my $char = shift;
return $char unless defined $encrypt{$char};
return $decrypt{$char};
}
#is for fortuneview:
sub insert_char_label{
my ($lbl, $col, $row) = @_;
$fortuneView->attach_defaults ($lbl, $col,$col+1, $row,$row+1);
}
#split into lines and then split each line into chars
sub get_fortune_chars{
$Text::Wrap::columns = $numColumns;
my @splitFortune = split ("\n", wrap('','',$fortune));
@splitFortune = map { [split(//,$_)] } @splitFortune;
return @splitFortune;
}
#set data in the table
sub reloadFortuneView{
%guessLabels = ();
my @splitFortune = get_fortune_chars();
for (my $rownum=0 ; $rownum<@splitFortune ; $rownum++){
my @line=@{$splitFortune[$rownum]};
for (my $colnum=0 ; $colnum<@line ; $colnum++){
my $char=$line[$colnum];
my $label1 = Gtk2::Label->new (getGuess ($encrypt{$char} or $char));
useMyLabelFont($label1);
insert_char_label ($label1, $colnum, 3*$rownum);
push @{$guessLabels{$char}}, $label1;
my $label2 = Gtk2::Label->new (encrypt($char));
useMyLabelFont($label2);
insert_char_label ($label2, $colnum, 3*$rownum+1);
}
insert_char_label(Gtk2::Label->new(' '), 0, 3*$rownum+2);
}
}
sub reloadGuessTable{
my @alpha = split //, $alpha; #all uc letters
for (0..$#alpha){
my $char = $alpha[$_];
my $guessEntry = Gtk2::Entry->new_with_max_length (1);
useMyEntryFont ($guessEntry);
$guessEntry->set_size_request ($font_size+6, $font_size*2);
my $guess = '';
$guessEntry->set_text ($guess);
$guessTable->attach_defaults ($guessEntry, $_,$_+1, 0,1);
$guessEntry->signal_connect("changed", \&make_guess, $char);
$guessEntries[$_] = $guessEntry;
my $lbl = Gtk2::Label->new ($char);
useMyLabelFont($lbl);
$guessTable->attach_defaults ($lbl, $_,$_+1, 1,2);
}
}
sub setGuessBgColor{
my ($char, $color) = @_;
my $widget = $guessEntries[ord($char) - ord('A')];
$color = Gtk2::Gdk::Color->parse ($color);
$widget->modify_base('normal', $color);
$widget->show_all();
}
#letters never represent themselves. Blue when people guess otherwise.
#letter representations are one-to-one. Yellow when that is violated.
my %blues;
my %yellows;
sub set_entry_conflicts{
#key and guess should be different
my %sameAsGuess;
for (keys %guesses){
if ($_ eq $guesses{$_}){
$sameAsGuess{$_}=1;
unless ($blues{$_}){
setGuessBgColor($_, 'lightblue');
$blues{$_} = 1;
}
}
}
for (keys %blues){
unless ($sameAsGuess{$_}){
setGuessBgColor($_, 'white');
setGuessBgColor($_, 'yellow') if $yellows{$_};
delete $blues{$_};
}
}
#only one-to-one guesses
my %multiple_guesses;
for (values %guesses){
next unless defined $_;
$multiple_guesses{$_}++;
#print $_;
}
for my $key (keys %guesses){
my $guess = $guesses{$key};
next unless $guess;
next unless defined $multiple_guesses{$guess};
next unless ($multiple_guesses{$guess} > 1);
next if $yellows{$key};
#warn "$key $guess $multiple_guesses{$guess}";
setGuessBgColor($key, 'yellow');
$yellows{$key} = 1;
}
#unyellow:
for (keys %yellows){
next if $guesses{$_} and $multiple_guesses{$guesses{$_}} > 1;
setGuessBgColor($_, 'white');
setGuessBgColor($_, 'lightblue') if $blues{$_};
delete $yellows{$_};
}
}
sub make_guess{
my ($entry, $char) = @_;
my $guess = $entry->get_text;
if ($guess =~ /[A-Za-z]/){
$guesses{$char} = uc $guess;
$entry->set_text(uc $guess);
}
else{
$entry->set_text('');
delete $guesses{$char}
}
#adjust fortuneview to new guess
for my $lbl ( @{ $guessLabels {decrypt($char)} } ){
my $text = $guesses{$char} ? $guesses{$char} : '_';
$lbl->set_text($text)
}
set_entry_conflicts();
if (detectVictory()){
doVictory()
}
}
sub detectVictory{
return 0 if $victorious;
my $lettersCorrect = 0;
for my $char (keys %enc_letterCount){
return 0 unless defined $guesses{$char};
return 0 if $guesses{$char} ne $decrypt{$char}
}
return 1;
}
sub useMyLabelFont{
my $label = shift;
$label->modify_font($labelfont);
}
sub useMyEntryFont{
my $label = shift;
$label->modify_font($entryfont);
}
#display victory window
sub doVictory{
$victorious = TRUE;
my $victWin = Gtk2::Window->new();
my $label = Gtk2::Label->new($victory_message);
useMyLabelFont ($label);
my $okbutton = Gtk2::Button->new("ok");
$okbutton->signal_connect("clicked", sub {$victWin->destroy} );
my $vb = Gtk2::VBox->new(FALSE,0);
$vb->pack_start($label, TRUE, FALSE, 0);
$vb->pack_start($okbutton, TRUE, FALSE, 0);
$victWin->add($vb);
$victWin->show_all;
}
sub cheat{
my $cheatWin = Gtk2::Window->new();
$Text::Wrap::columns = $numColumns;
my $wrappedFortune = wrap('','',$fortune);
my $label = Gtk2::Label->new($wrappedFortune);
useMyLabelFont ($label);
my $okbutton = Gtk2::Button->new("ok");
$okbutton->signal_connect("clicked", sub {$cheatWin->destroy} );
my $vb = Gtk2::VBox->new(FALSE,0);
$vb->pack_start($label, TRUE, FALSE, 0);
$vb->pack_start($okbutton, TRUE, FALSE, 0);
$cheatWin->add($vb);
$cheatWin->show_all;
}
BEGIN{
# if I don't do this, Text::Wrap will turn spaces into tabs
$Text::Wrap::unexpand = 0;
}