#!/usr/bin/perl
use CGI;
$q = new CGI;
print $q->header, $q->start_html;
$message = $q->param('message');
$url = "http://www.cs.indiana.edu/classes/a348/spr2007/notes/cards";
if ($message) {
@a = split(' ', $message);
$first = splice(@a, 0, 1);
$message = join(' ', @a);
$compHand = $q->param('compHand');
@compHand = split(' ', $compHand);
$userHand = $q->param('userHand');
@userHand = split(' ', $userHand);
$compScore = $q->param('compScore');
$userScore = $q->param('userScore');
if ($#compHand < $#userHand) {
splice(@compHand, $#compHand + 1, 0, $first);
$cdc = &calculate(@compHand);
$cdu = &calculate(@userHand);
if ($#compHand == 4) {
if ($cdu < $cdc) {
$compScore += 1;
} elsif ($cdc < $cdu) {
$userScore += 1;
}
}
} elsif ($#compHand == 4) {
# grade, reset, update score
# why do we have to repeat this data structure so many times? how can we fix that?
@a = ("2c", "2d", "2h", "2s",
"3c", "4c", "5c", "6c", "7c", "8c", "9c", "tc", "ac", "jc", "qc", "kc",
"3d", "4c", "5c", "6c", "7c", "8c", "9c", "tc", "ac", "jc", "qc", "kc",
"3h", "4h", "5h", "6h", "7h", "8h", "9h", "th", "ah", "jh", "qh", "kh",
"3s", "4s", "5s", "6s", "7s", "8s", "9s", "ts", "as", "js", "qs", "ks");
@b = ();
while ($#a >= 0) {
$i = int(rand($#a+1)); # randomly choose one element in @a
splice(@b, 0, 0, $a[$i]); # put it on the left of @b
splice(@a, $i, 1); # ... and take it out from @a
}
$anotherMessage = "The game has just (re)started. The deck is shuffled.";
$message = join(' ', @b);
@userHand = ();
@compHand = ();
$cdu = 0;
$cdc = 0;
} else {
splice(@userHand, $#userHand + 1, 0, $first);
$cdc = &calculate(@compHand);
$cdu = &calculate(@userHand);
}
$user = " $userScore ($cdu)
";
foreach $i (@userHand) {
$user .= " | ";
}
$comp = " $compScore ($cdc) | ";
foreach $i (@compHand) {
$comp .= " | ";
}
$showHands = "";
$userHand = join(' ', @userHand);
$compHand = join(' ', @compHand);
} else {
@a = ("2c", "2d", "2h", "2s",
"3c", "4c", "5c", "6c", "7c", "8c", "9c", "tc", "ac", "jc", "qc", "kc",
"3d", "4c", "5c", "6c", "7c", "8c", "9c", "tc", "ac", "jc", "qc", "kc",
"3h", "4h", "5h", "6h", "7h", "8h", "9h", "th", "ah", "jh", "qh", "kh",
"3s", "4s", "5s", "6s", "7s", "8s", "9s", "ts", "as", "js", "qs", "ks");
@b = ();
while ($#a >= 0) {
$i = int(rand($#a+1)); # randomly choose one element in @a
splice(@b, 0, 0, $a[$i]); # put it on the left of @b
splice(@a, $i, 1); # ... and take it out from @a
}
$anotherMessage = "The game has just (re)started. The deck is shuffled.";
$message = join(' ', @b);
$compHand = "";
$userHand = "";
$compScore = 0;
$userScore = 0;
$showHands = "";
$user = " ";
$comp = " ";
$showHands = "";
}
print qq{
};
print $q->end_html;
sub calculate {
local (@m) = @_;
if ($#m <= 0) { return 0; }
%values = ( # what is wrong with this data structure? can you fix that?
"2c", 2,
"2d", 2,
"2h", 2,
"2s", 2,
"3c", 3, "4c", 4, "5c", 5, "6c", 6, "7c", 7, "8c", 8, "9c", 9, "tc", 10, "ac", 1, "jc", 12, "qc", 13, "kc", 14,
"3d", 3, "4c", 4, "5c", 5, "6c", 6, "7c", 7, "8c", 8, "9c", 9, "tc", 10, "ac", 1, "jc", 12, "qc", 13, "kc", 14,
"3h", 3, "4h", 4, "5h", 5, "6h", 6, "7h", 7, "8h", 8, "9h", 9, "th", 10, "ah", 1, "jh", 12, "qh", 13, "kh", 14,
"3s", 3, "4s", 4, "5s", 5, "6s", 6, "7s", 7, "8s", 8, "9s", 9, "ts", 10, "as", 1, "js", 12, "qs", 13, "ks", 14
);
$max = $values{$m[0]};
$min = $values{$m[0]};
foreach $m (@m) {
if ($values{$m} > $max) {
$max = $values{$m};
}
if ($values{$m} < $min) {
$min = $values{$m};
}
}
return $max - $min;
}
|