Created
June 3, 2009 22:26
-
-
Save sbisbee/123288 to your computer and use it in GitHub Desktop.
A patch for the Debian scrabble package (v1.9) that has the computer play itself. Was refused by package maintainer for "usefulness" issues.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- scrabble 2005-10-18 16:14:08.599699208 -0400 | |
+++ scrabble-aipatch 2005-10-18 16:29:27.212048904 -0400 | |
@@ -3,11 +3,15 @@ | |
# | |
# Scrabble -- a crossword game | |
# | |
-# Version: 1.9 | |
+# Version: 1.9 (AI patch) | |
# | |
# Written by Brian White <bcwhite@pobox.com> | |
# Placed in the public domain (the only true "free") | |
# | |
+# | |
+# AI patch by Sam Bisbee <sbisbee@computervip.com> | |
+# Placed in the public domain (the only true "free") | |
+# | |
############################################################################### | |
@@ -101,6 +105,7 @@ | |
$Abort=0; | |
@MeWords; | |
@YuWords; | |
+$Comp; | |
@WordPrefixes = ("","un","mis","p?re"); | |
@@ -336,13 +341,15 @@ | |
print "|$y "; | |
if (hex($y) < 8) { | |
if ($y eq "0") { | |
- print "My Words:\n"; | |
+ print "My Words:\n" if(!$AI); | |
+ print "CompA's Words:\n" if($AI); | |
} else { | |
print " ".$MeWords[hex($y)-1],"\n"; | |
} | |
} else { | |
if ($y eq "8") { | |
- print "Your Words:\n"; | |
+ print "Your Words:\n" if(!$AI); | |
+ print "CompB's Words:\n" if($AI); | |
} else { | |
print " ".$YuWords[hex($y)-9],"\n"; | |
} | |
@@ -350,12 +357,19 @@ | |
} | |
print " - - - - - - - - - - - - - - -\n"; | |
print " 0 1 2 3 4 5 6 7 8 9 A B C D E\n\n"; | |
- printf " me:%-3d ",$CScore; | |
- foreach $c (split(//,$PLetters)) { | |
- print $c," "; | |
+ | |
+ if(!$AI) { | |
+ printf " me:%-3d ",$CScore; | |
+ foreach $c (split(//,$PLetters)) { | |
+ print $c," "; | |
+ } | |
+ | |
+ print " "x((7-length($PLetters))*2); | |
+ printf " you:%-3d\n\n",$PScore; | |
+ } else { | |
+ printf " CompA:%-3d\n",$CScore; | |
+ printf " CompB:%-3d\n\n",$PScore; | |
} | |
- print " "x((7-length($PLetters))*2); | |
- printf " you:%-3d\n\n",$PScore; | |
} | |
@@ -366,7 +380,7 @@ | |
$count = 7 - length($$rack); | |
while ($count--) { | |
- return if ($LRemain == 0); | |
+ last if ($LRemain == 0); | |
$rand = int(rand($LRemain)) + 1; | |
foreach $char (split(//,"ABCDEFGHIJKLMNOPQRSTUVWXYZ_*")) { | |
if ($char eq "*") { | |
@@ -591,7 +605,6 @@ | |
} | |
for ($line=0; length($$lines[$line+1]) != 0; ++$line) {}; | |
- | |
$extra = 0; | |
print "\n$who placed:\n"; | |
foreach $word (@list) { | |
@@ -607,7 +620,7 @@ | |
$print = "[$print" if ($word ne $main && $extra++ == 0); | |
$$lines[$line] .= " " if (length($$lines[$line]) != 0 && $extra < 2); | |
$$lines[$line] .= "," if (length($$lines[$line]) != 0 && $extra > 1); | |
- $line++ if (length($$lines[$line]) >= 42-length($print)); | |
+ $line++ if (length($$lines[$line]) >= 44-length($print)); | |
$$lines[$line] .= $print; | |
} | |
$$lines[$line] .= "]" if ($extra != 0); | |
@@ -876,8 +889,16 @@ | |
$think = CountWord($bestl,$bestm,\%words); | |
PlaceWord($bestl,$bestm,$letters); | |
- DisplayWords("I",$think,\%words,$bestm,\@MeWords); | |
+ if(!$AI) | |
+ { | |
+ DisplayWords("I",$think,\%words,$bestm,\@MeWords); | |
+ } | |
+ else | |
+ { | |
+ DisplayWords("$Comp",$think,\%words,$bestm,\@MeWords) if($Comp eq "CompA"); | |
+ DisplayWords("$Comp",$think,\%words,$bestm,\@YuWords) if($Comp eq "CompB"); | |
+ } | |
$$score += $think; | |
} | |
@@ -948,7 +969,7 @@ | |
while (1) { | |
print ": "; | |
$turn = lc(<STDIN>); | |
- $turn =~s/^\s+|\s+$//gs; | |
+ $turn =~s/^\s+|\s+$//gs; | |
# catch help | |
if ($turn eq "help") { | |
@@ -1184,13 +1205,13 @@ | |
} | |
} | |
} | |
- $index = 7 if ($index > 7); | |
- $index = 0 if ($index < 0); | |
if (!defined $index) { | |
print STDERR " -> XXX" if ($Debug); | |
$index = 8; | |
} | |
print STDERR " => ",$index+1,")\n" if ($Debug); | |
+ $index = 0 if ($index < 0); | |
+ $index = 8 if ($index > 8); | |
print STDERR $index+1 unless ($Debug); | |
$index = chr($index + ord('A')); | |
$counts{$index}++; | |
@@ -1212,79 +1233,166 @@ | |
exit(0); | |
} | |
+$Level = 0; | |
+$Inf = 0; | |
+$AI = 0; | |
+for($In = shift @ARGV; $In; $In = shift @ARGV) { | |
+ use Switch; | |
+ switch($In) { | |
+ case "--inf" { | |
+ $Inf = 1; | |
+ print "Infinity mode:\tON\n"; | |
+ } | |
+ case /^\d$/ { | |
+ $Level = $In; | |
-$Level = shift @ARGV; | |
-unless ($Level =~ m/^\d+$/) { | |
+ if($Level > 9) { | |
+ print "(level reduced to 9 [hardest]\n"; | |
+ $Level = 9; | |
+ } | |
+ elsif($Level < 1) { | |
+ print "(level raised to 1 [easiest]\n"; | |
+ $Level = 1; | |
+ } | |
+ } | |
+ | |
+ case "--ai" { | |
+ $AI = 1; | |
+ print "AI mode:\tON\n"; | |
+ } | |
+ } | |
+} | |
+ | |
+if(!$Level) { | |
$Level = 1; | |
print "Use: $0 <level> (where level is 1 to 9)\n"; | |
- print "Level \#1 (easiest) assumed...\n"; | |
- sleep(3); | |
-} | |
-if ($Level > 9) { | |
- print "(level reduced to 9 [hardest])\n"; | |
- $Level = 9; | |
-} | |
- | |
-push @WordFiles,$NewWords; | |
-ReadWords(@WordFiles); | |
-InitGame(); | |
-DrawLetters(\$PLetters); | |
-DrawLetters(\$CLetters); | |
-DisplayBoard(); | |
- | |
-$Board[hex("77")] = "-"; | |
-if (int(rand(2)) == 0) { | |
- print "I go first...\n"; | |
- ComputerTurn(\$CLetters,\$CScore); | |
- DrawLetters(\$CLetters); | |
- DisplayBoard(); | |
+ print "Level \#1 (easiest assumed)\n"; | |
} else { | |
- print "You go first...\n"; | |
+ print "Level set:\t$Level\n"; | |
} | |
+print "\n"; | |
-while ($CLetters ne $clast || $PLetters ne $plast) { | |
-# print STDERR "$LRemain letters remain: $CLetters $PLetters ($CScore vs $PScore)\n"; | |
- $clast = $CLetters; | |
- $plast = $PLetters; | |
- PlayerTurn(\$PLetters,\$PScore); | |
-# ComputerTurn(\$PLetters,\$PScore); | |
- DrawLetters(\$PLetters); | |
- DisplayBoard(); | |
- last unless $PLetters; | |
- last if $Abort; | |
+if(!$AI && $Inf) { | |
+ print "You may only use infinity mode if AI mode is turned on.\n"; | |
+ exit(1); | |
+} | |
- sleep(2); | |
- ComputerTurn(\$CLetters,\$CScore); | |
+sleep(3); | |
+ | |
+$Games = 1; | |
+for(;;) { | |
+ if($Inf) { | |
+ print "Game #$Games\n"; | |
+ } | |
+ | |
+ push @WordFiles,$NewWords; | |
+ ReadWords(@WordFiles); | |
+ InitGame(); | |
+ DrawLetters(\$PLetters); | |
DrawLetters(\$CLetters); | |
DisplayBoard(); | |
- last unless $CLetters; | |
-} | |
+ $Board[hex("77")] = "-"; | |
+ if (int(rand(2)) == 0) { | |
+ if($AI) { | |
+ $Comp = "CompA"; | |
+ print "CompA goes first...\n"; | |
+ } else { | |
+ print "I go first...\n"; | |
+ } | |
+ ComputerTurn(\$CLetters,\$CScore); | |
+ DrawLetters(\$CLetters); | |
+ DisplayBoard(); | |
+ } else { | |
+ if($AI) { | |
+ print "CompB goes first...\n"; | |
+ } else { | |
+ print "You go first...\n"; | |
+ } | |
+ } | |
-print "Game over: I have ", | |
- ($CLetters ? "\"$CLetters\"" : "no letters")," in my rack and you have ", | |
- ($PLetters ? "\"$PLetters\"" : "no letters")," in yours.\n"; | |
+ sleep(3) if(!$AI); | |
+ sleep(5) if($AI); | |
-if (!$Abort && $CLetters) { | |
- $amount = UnplayedLetters($CLetters); | |
- $CScore -= $amount; | |
- $PScore += $amount unless $PLetters; | |
- print "I have to ",($PLetters ? "remove" : "give you")," $amount point",($amount==1 ? "":"s")," from my score.\n"; | |
-} | |
-if (!$Abort && $PLetters) { | |
- $amount = UnplayedLetters($PLetters); | |
- $PScore -= $amount; | |
- $CScore += $amount unless $CLetters; | |
- print "You have to ",($CLetters ? "remove" : "give me")," $amount point",($amount==1 ? "":"s")," from your score.\n"; | |
-} | |
+ while ($CLetters ne $clast || $PLetters ne $plast) { | |
+# print STDERR "$LRemain letters remain: $CLetters $PLetters ($CScore vs $PScore)\n"; | |
+ $clast = $CLetters; | |
+ $plast = $PLetters; | |
+ | |
+ if($AI) { | |
+ $Comp = "CompB"; | |
+ | |
+ ComputerTurn(\$PLetters, \$PScore); | |
+ DrawLetters(\$PLetters); | |
+ DisplayBoard(); | |
+ last unless $PLetters; | |
+ | |
+ $Comp = "CompA"; | |
+ | |
+ ComputerTurn(\$CLetters, \$CScore); | |
+ DrawLetters(\$CLetters); | |
+ DisplayBoard(); | |
+ last unless $CLetters; | |
-print "\nFinal board:\n"; | |
-DisplayBoard(); | |
-SaveNewWords(); | |
+ sleep(5); | |
+ } else { | |
+ PlayerTurn(\$PLetters,\$PScore); | |
-if ($NewGames && open(STDOUT,">>$NewGames")) { | |
- print "-------------------------------------------------------------------------------\n"; | |
- print "level \#$Level; computer rack: \"$CLetters\"; player rack: \"$PLetters\"\n"; | |
+ DisplayBoard(\$PLetters); | |
+ DisplayBoard(); | |
+ last unless $PLetters; | |
+ last if $Abort; | |
+ | |
+ sleep(2); | |
+ | |
+ ComputerTurn(\$CLetters,\$CScore); | |
+ DrawLetters(\$CLetters); | |
+ DisplayBoard(); | |
+ last unless $CLetters; | |
+ } | |
+ } | |
+ | |
+ print "Game over: I have ", | |
+ ($CLetters ? "\"$CLetters\"" : "no letters")," in my rack and you have ", | |
+ ($PLetters ? "\"$PLetters\"" : "no letters")," in yours.\n" if(!$AI); | |
+ | |
+ print "Game over: CompA has ", | |
+ ($CLetters ? "\"$Cletters\"" : "no letters")," in her rack and CompB has ", | |
+ ($PLetters ? "\"$Pletters\"" : "no letters")," in hers.\n" if($AI); | |
+ | |
+ if (!$Abort && $CLetters) { | |
+ $amount = UnplayedLetters($CLetters); | |
+ $CScore -= $amount; | |
+ $PScore += $amount unless $PLetters; | |
+ print "I have to ",($PLetters ? "remove" : "give you")," $amount point",($amount==1 ? "":"s")," from my score.\n" if(!$AI); | |
+ print "CompA has to ",($PLetters ? "remove" : "give CompB")," $amount point",($amount==1 ? "":"s")," from her score\n" if($AI); | |
+ } | |
+ if (!$Abort && $PLetters) { | |
+ $amount = UnplayedLetters($PLetters); | |
+ $PScore -= $amount; | |
+ $CScore += $amount unless $CLetters; | |
+ print "You have to ",($CLetters ? "remove" : "give me")," $amount point",($amount==1 ? "":"s")," from your score.\n" if(!$AI); | |
+ print "CompB has to ",($CLetters ? "remove" : "give her")," $amount point",($amount==1 ? "":"s")," from her score.\n" if($AI); | |
+ } | |
+ | |
+ print "\nFinal board:\n"; | |
DisplayBoard(); | |
+ SaveNewWords(); | |
+ | |
+ if(!$AI) { | |
+ if ($NewGames && open(STDOUT,">>$NewGames")) { | |
+ print "-------------------------------------------------------------------------------\n"; | |
+ print "level \#$Level; computer rack: \"$CLetters\"; player rack: \"$PLetters\"\n"; | |
+ DisplayBoard(); | |
+ } | |
+ } | |
+ | |
+ last if(!$Inf); | |
+ | |
+ sleep(5); | |
+ $Games++; | |
+ | |
+ undef @YuWords; | |
+ undef @MeWords; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment