-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgm9.pl
286 lines (241 loc) · 6.04 KB
/
pgm9.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
#!/usr/bin/perl
#
# pgm9.pl - Tommy Dickerson
# Program 9
# cit145
# Due 04/17/2008
#_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
#
# This Perl program uses a subroutine
# strategy and plays agianst an opponent
# in the game of Prisoner's Dilemma.
#_______________________________________________________
$mytotal = 0;
$optotal = 0;
print <<EOM;
[1] Enter moves manualy
[2] Play with moves from a file
[3] Play with a subroutine from this program
EOM
print "Enter your choice [1]: ";
while ($input = <>) {
chomp ($input);
# 1 chooses to enter moves manually #
if (($input =~ /^[ ]*[1][ ]*$/) or ($input =~ /^[ ]*$/) or ($input eq undef)) {
print "\nIs the opponent going to Testify or Hold? ";
while ($move = <>) {
chomp ($move);
if ($move =~ /^[tThH]$/) {
$move =~ tr/th/TH/;
&gamecore($move);
}
elsif ($move =~ /^[ ]*[qQ][ ]*$/) {
exit;
}
else {
print <<EOM;
[T] Testify
[H] Hold
[q] Quit
EOM
}
print "\nIs the opponent going to Testify or Hold? ";
}
}
# 2 choosed to load moves from a file, one per line #
elsif ($input =~ /^[ ]*[2][ ]*$/) {
print "\nPlease enter a file that contains your opponents moves: ";
$playfile = <>; chomp($playfile);
unless (-e $playfile) {
print "\nSorry the file '$playfile' does not exist.\nExiting...\n\n";
exit;
}
open MOVELIST, "$playfile" or die "\nCannot open file '$playfile': $!\n";
print "\nYour opponents moves have been loaded from file '$playfile'.\n\n";
print "Press [Enter] to continue.\n";
<>;
$line = 0;
while (<MOVELIST>) {
$moves[$line] = $_;
$line++;
}
close MOVELIST;
while ($opchoice = shift(@moves)) { &gamecore($opchoice); }
exit;
}
# 3 chooses to generate move from a subroutine #
elsif ($input =~ /^[ ]*[3][ ]*$/) {
print "\nHow many turns would you like to go? ";
while ($turns = <>) {
chomp ($turns);
if ($turns =~ /^\d+$/) {
while ($turns > 0) {
# Must add subroutine from end of program here #
$opchoice = &myprobsub(@prevMyBlysMoves); # EX. $opchoice = &SUBNAME(@prevMyMoves);
&gamecore($opchoice);
$turns--;
}
exit;
}
else {print "How many turns would you like to go? ";}
}
}
# tell the user if they have entered an invalid command #
else {
print <<EOM;
[1] Enter moves manualy
[2] Play with moves from a file
[3] Play with a subroutine from this program
EOM
print "Enter your choice [1]: ";
}
}
exit;
# SUBROUTINE gamecore: Run the game once and output the results to the user #
sub gamecore
{
my @argument = @_;
my $opmove = $argument[0]; # get opponents move
chomp($opmove);
my $mymove = &bullysub(@prevOpMoves); #get my move
$myhist .= $mymove; # create a history to display
$ophist .= $opmove; # create a history to display
push(@prevOpMoves, $opmove); # store the history of my opponents previous moves
($myears, $opyears) = &years($mymove, $opmove); # determine the num of years we each recieved
$mytotal += $myears; # add the number of years to a running total
$optotal += $opyears; # add the number of years to a running total
# Display the game results to the user #
print "====================\n\n";
print "My History:\n\t$myhist\nOpponents History:\n\t$ophist\n\n";
print "I $mymove and got $myears, opponent $opmove and got $opyears.\n";
print "My total $mytotal years, oppenents total $optotal years.\n\n";
}
# SUBROUTINE years: Determine the score of both players in the game for current moves #
sub years
{
my @arguments = @_;
my %table = (
'TT' => '4|4',
'TH' => '0|5',
'HH' => '2|2',
'HT' => '5|0',
);
my $result = $table{"$arguments[0]$arguments[1]"};
my($me, $op) = split(/\|/, $result, 2);
return($me, $op);
}
#
# NAME: Tommy Dickerson
# DESC: Try to get the lowest score without being screwed.
#
# STRATEGY: Find copycats and Hold to get lowest score. Find near copycats and mostly Hold. Anything else mostly Testify.
#
sub bullysub
{
my @opsmoves = @_;
my $lastmove = $opsmoves[$#opsmoves];
my $notherRand = rand();
my $size = @opsmoves;
if ($lastmove eq undef) {$myBlyschoice = "H";}
elsif ($size == 1) {$myBlyschoice = "T";}
else {
if ($lastmove eq $prevMyBlysMoves[($#prevMyBlysMoves - 1)]) {
$copycatBlys += 1;
}
else {$copycatBlys -= 1;}
my $copyprob = $copycatBlys / ($size - 1);
# Preamble
if ($size == 2) {$myBlyschoice = "H";}
elsif ($size == 3) {$myBlyschoice = "H";}
elsif ($size == 4) {$myBlyschoice = "T";}
elsif ($size == 5) {$myBlyschoice = "H";}
elsif ($size == 99) {$myBlyschoice = "T";}
elsif ($size == 149) {$myBlyschoice = "T";}
elsif ($size == 199) {$myBlyschoice = "T";}
else {
if ($copyprob == 1) {
$myBlyschoice = "H";
}
elsif ($copyprob > 0.88) {
if ($notherRand < 0.015) {
$myBlyschoice = "T";
}
else {$myBlyschoice = "H";}
}
else {
if ($notherRand < 0.015) {
$myBlyschoice = $lastmove;
}
else {
$myBlyschoice = "T";
}
}
}
}
push(@prevMyBlysMoves, $myBlyschoice);
return($myBlyschoice);
}
## END OF PROGRAM ##
# Add other subroutines below #
# # # # # # # # # # # # # # # # #
sub copycat
{
my @argument = @_;
$lastmove = $argument[$#argument];
unless ($lastmove) {
$lastmove = "T";
}
return($lastmove);
}
sub rand
{
my $random_number = rand();
if ($random_number < 0.5) {
$move = "T";
}
else { $move = "H"; }
return($move);
}
sub myprobsub
{
my @opponentsmoves = @_;
undef $numT;
undef $numH;
my $opplastmove = $opponentsmoves[$#opponentsmoves];
if ($#opponentsmoves == -1)
{
$mymove = 'H';
push(@MyMoveHist, $mymove);
return "$mymove";
}
else
{
foreach (@opponentsmoves) {
$element = $_;
if ($element eq 'T')
{
$numT += 1;
}
else
{
$numH += 1;
}
}
$oppProbT = $numT / ($#opponentsmoves + 1);
$oppProbH = $numH / ($#opponentsmoves + 1);
if (($oppProbT > 0.88) || ($oppProbH > 0.75))
{
$mymove = 'T';
}
else
{
my $random_number = rand();
if ($random_number < 0.88) {
$mymove = "T";
}
else { $mymove = "H"; }
}
push(@MyMoveHist, $mymove);
return ("$mymove");
}
}