-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowmaze.pl
198 lines (187 loc) · 5.41 KB
/
showmaze.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
#!/usr/bin/perl
use warnings;
use strict;
use CGI qw(:standard);
{
# my ($doors, $rooms, $current_room, $door_choice, $offset) = @ARGV;
my $doors = param("doors");
my $rooms = param("rooms");
my $current_room = param("current_room");
my $door_choice = param("door_choice");
my $offset = param("users");
my @real_doors = decode_doors($offset);
if (!defined($offset)){
@real_doors = make_real_doors($rooms,$doors);
$offset = int(rand(100));
$current_room = 0;
$door_choice = -1;
show_page($rooms,$doors,$current_room,$door_choice,$offset,
"Choose a door:",@real_doors);
} elsif ($real_doors[$current_room]!=$door_choice){
show_page($rooms,$doors,$current_room,$door_choice,$offset,
"Wrong answer; choose another door:",@real_doors);
} else {
if ($current_room==$rooms - 1) {
show_end();
} else {
$current_room++;
$door_choice = -1;
show_page($rooms,$doors,$current_room,$door_choice,$offset,
"Right! You are in the next room. Choose another door:",
@real_doors);
}
}
}
#print(join("<br>",@real_doors));
#print(join("<br>",encode_doors(2,3,3,2,3)));
####################
# #
# decode_doors #
# Inputs: #
# the offset #
# Outputs: #
# the real doors #
# #
####################
sub decode_doors {
my ($offset) = @_;
if (defined($offset)) {
my $fake_doors = param("books");
my @real_doors = split(/,/,$fake_doors);
my $i = 0;
my $rd = 0;
foreach $rd (@real_doors){
$rd = $rd - 7 * $i - $offset;
$i++;
}
return @real_doors;
} else {
return (); # () is an empty array
# I returned an empty array
# because $fake_doors is empty
}
}
#########################
# #
# make_real_doors #
# Inputs: #
# the number of rooms #
# the number of doors #
# Outputs: #
# the real doors #
# #
#########################
sub make_real_doors {
my($rooms,$doors) = @_;
my @real_doors;
until ($rooms == -1) {
$real_doors[$rooms] = int(rand($doors));
$rooms--;
}
return @real_doors;
}
####################
# encode_doors #
# Inputs: #
# the offset and #
# the real doors #
# Outputs: #
# the fake doors #
####################
sub encode_doors {
my($offset,@real_doors) = @_;
my $rd = 0;
my $i = 0;
my @fake_doors = ();
foreach $rd (@real_doors) {
my $fd = $rd + 7 * $i + $offset;
$fake_doors[$i] = $fd;
$i++;
}
return @fake_doors;
}
##########################################################
# show_page #
# Inputs: #
# The number of rooms, #
# the number of doors, #
# the current room, #
# the number of the door the user chose last, #
# the offset #
# a message that tells if the user got the door right, #
# the real doors #
##########################################################
sub show_page {
my($rooms,$doors,$current_room,$door_choice,$offset,
$message,@real_doors) = @_;
start();
my $user_current_room = $current_room + 1;
print h1("The Maze"), p("you are in room ".$user_current_room
." of ".$rooms), p($message);
my $good_door = $real_doors[$current_room];
my $i = 0;
print "<table><tr>\n";
foreach $i (0..$doors - 1) {
print "<td>\n";
if ($i==$door_choice and $door_choice!=$good_door) {
# The door has been chosen and it is the wrong one
print img {src=>"/static/maze_img/blocked_door.jpg"}, p($i + 1);
} else {
# The door has not been chosen or it is the right one
print a({-href=>"showmaze.pl?".
"doors=$doors&".
"rooms=$rooms&".
"current_room=$current_room&".
"door_choice=$i&".
"users=$offset&".
"books=".
join(",",encode_doors($offset,@real_doors))
}),
img {src=>"/static/maze_img/door.jpg"}, p($i + 1);
}
print "</td>\n";
}
print "</tr></table>\n";
end();
}
##################################
# show_end #
# Inputs: #
# none #
# Outputs: #
# a web page with an open door #
##################################
sub show_end {
start();
print br, h2("The End");
print img {src=>"/static/maze_img/open_door.jpg"},"<br>";
print a({-href=>"/maze"},"Play again?");
print "<br>";
print a({-href=>"/"},"Quit");
end();
}
#################################
# start #
# Inputs: #
# none #
# Outputs: #
# the beginning of a web page #
#################################
sub start {
# print "Content-type: text/html\n\n";
# print `/usr/bin/php ../header.php Maze`
# print header(), start_html("The Maze");
# print header(), start_html(-title=>"The Maze", -style =>{-src=>["../stylesheet.css"],-media=> 'all'});
}
###########################
# end #
# Inputs: #
# none #
# Outputs: #
# the end of a web page #
###########################
sub end {
print h6("Made by Johan");
# print end_html();
# print `/usr/bin/php ../footer.php`
}