web上で動くじゃんけんプログラム
#!/usr/local/bin/perl
use strict;
use CGI::Carp qw(fatalsToBrowser);
use warnings;
print"Content-type: text/html; charset=Shift_JIS\n\n";
print <<"EOF";
<html>
<head>
<title>じゃんけん</title>
</head>
<body oncontextmenu="alert('右クリック禁止');return false">
0:グー[ 1:チョキ 2:パーのどれか入力<br>
<form method="POST"action="$ENV{'SCRIPT_NAME'}">
<input type="text" name="hand1">
<input type="hidden" name="mode" value="regist">
<input type="button" onclick="submit();" value="決定"></form>
EOF
# 宣言
my $alldata;
my %in;
# フォーム処理
if($ENV{'REQUEST_METHOD'} eq 'POST'){
read(STDIN,$alldata,$ENV{'CONTENT_LENGTH'});
}else{
$alldata = $ENV{'QUERY_STRING'};
}
foreach my $data(split(/&/,$alldata)){
my($key,$value) = split(/=/,$data);
$value =~ s/\+/ /g;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack('C',hex($1))/eg;
$value =~ s/\t//g;
$in{"$key"} = $value;
}
my $hand1 = $in{"hand1"};
if($in{mode} eq 'regist'){
print "userの手<br>";
if($hand1==0){
print "グー<br>";
} elsif($hand1==1){
print "チョキ<br>";
} elsif($hand1==2){
print "パー<br>";
} else {
print "入力が間違っています<br>";
}
print "computerの手<br>";
my $hand2 = int(rand(3));
if($hand2==0){
print "グー<br>";
} elsif($hand2==1){
print "チョキ<br>";
} elsif($hand2==2){
print "パー<br>";
} else {
print "入力が間違っています<br>";
}
my @result=(
["あいこ","勝ち","負け"],
["負け","あいこ","勝ち"],
["勝ち","負け","あいこ"],
);
print "$result[$hand1][$hand2]";
}