script/war.pl

filter: Statementstmt 88.5% 62 / 70 Branchbran 68.1% 15 / 22 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 78.8% 82 / 104 CC 1 SCAR 0.0
CC
1
Cov
80%
BEGIN
1.0
BEGIN
1.0
compare_last
1.0
CRAP 1.0
« blib/lib/Games/Cards/Tk.pm ↑ Summary
1 #!/usr/bin/perl -w
2  
3 # war.pl - watch two computer players play war
4 #
5 # Copyright 1999 Amir Karger (karger@post.harvard.edu)
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the same terms as Perl itself.
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Games::Cards;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Global variables for war
14 1 my $Cards_Per_Hand = 26;
15 # It would require just a bit of tinkering to make this game for 3 or more
16 # players. But why bother?
17 1 my $Number_Of_Players = 2;
18 1 my $Max_Turns = 500;
19 1 my $Long = 0; # set to 1 to print out more per turn
20 1 my $Interactive = 0; # wait for (useless) user input after every turn?
21  
22 # card values
23 1 my $valueref = {
24 "Ace" => 14,
25 2 => 2,
26 3 => 3,
27 4 => 4,
28 5 => 5,
29 6 => 6,
30 7 => 7,
31 8 => 8,
32 9 => 9,
33 10 => 10,
34 "Jack" => 11,
35 "Queen" => 12,
36 "King" => 13,
37 };
38  
39 1 srand(); # no seed nec. for perl > 5.004
40  
41 ######################################################################
42 # SETUP THE GAME
43 # Main variables
44 1 my $War; # the game
45 my $Deck; # the deck we're using in the game
46 1 my @Hands; # the players' hands
47 1 my @Table; # what the players put on the table
48  
49 # initialize the game. Suits are default; card values aren't
50 1 print "Welcome to war!\n";
51 1 ▶print "On each turn, hit RETURN to continue, CTRL-D to end\n\n" if $Interactive;
Branch: 1 branch
BranchTrueFalseTotal
if $Interactive 0 1 1
52 1 $War = new Games::Cards::Game {"cards_in_suit" => $valueref};
53  
54 # Create and shuffle the deck
55 1 print "Creating new deck.\n";
56 1 $Deck = new Games::Cards::Deck ($War, "Deck");
57 1 print "Shuffling the deck.\n";
58 1 $Deck->shuffle;
59  
60 # Deal out the hands
61 1 foreach my $i (1 .. $Number_Of_Players) {
62 2 print "Dealing hand $i\n";
63 2 my $hand = new Games::Cards::Queue ($War, "Player $i");
64 2 $Deck->give_cards($hand, $Cards_Per_Hand);
65 2 push @Hands, $hand;
66 }
67  
68 # Create CardSets for the cards each player puts on the table
69 1 foreach my $i (1 .. $Number_Of_Players) {
70 2 my $table_hand = new Games::Cards::Stack ($War, "Player $i showing");
71 2 push @Table, $table_hand;
72 }
73  
74 ######################################################################
75 # Now play
76 1 my @other = (1,0); # the other player
77 1 my $turns = 0;
78  
79 1 ▶while (++$turns < $Max_Turns) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$turns < $Max_Turns) 119 0 119
80 # print "Turn $turns: ",
81 # map ({" " . $_->name ." has " . $_->size . " cards."} @Hands), "\n";
82
83 119 ▶ if ($Long) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Long) { } 0 119 119
84 0 foreach (@Hands) { print $_->print("short"); }
85 } else {
86 119 my $i = 1;
87 119 foreach (@Hands) { print $i++ x $_->size, " " }
88 119 print "\n";
89 }
90 119 my $compare = 0;
91 119 ▶ while (!$compare) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($compare) 128 118 246
92 # Each player puts a card on the table
93 128 foreach (0 .. $#Hands) {
94 255 ▶ $Hands[$_]->give_cards($Table[$_], 1) || &win($other[$_], $turns);
Branch: 1 branch
BranchTrueFalseTotal
$Hands[$_]->give_cards($Table[$_], 1) or &win($other[$_], $turns) 254 1 255
95 }
96 0 ▶ if ($Long) {foreach (@Table) { print $_->print("short"); }}
Branch: 1 branch
BranchTrueFalseTotal
if ($Long) 0 127 127
97 127 $compare = compare_last($Table[0], $Table[1]);
98  
99 127 ▶ if (! $compare) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($compare) 9 118 127
100 # war
101 9 print "WAR!\n";
102 9 foreach (0 .. $#Hands) {
103 18 ▶ $Hands[$_]->give_cards($Table[$_], 3) ||
Branch: 1 branch
BranchTrueFalseTotal
$Hands[$_]->give_cards($Table[$_], 3) or &win($other[$_], $turns) 18 0 18
104 &win($other[$_], $turns);
105 }
106 }
107 } # end while !compare
108  
109 # Who won? Compare > 0 means player 1 won
110 # Winner gets the card (or cards if there was a war)
111 118 ▶ my $winner = ($compare > 0 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$compare > 0 ? : 62 56 118
112 118 my $loser = $other[$winner];
113 118 ▶ print $Hands[$winner]->{"name"}, " wins.\n" if $Long;
Branch: 1 branch
BranchTrueFalseTotal
if $Long 0 118 118
114 118 $Table[$winner]->give_cards($Hands[$winner], "all");
115 118 $Table[$loser]->give_cards($Hands[$winner], "all");
116  
117 # Make the game "interactive"
118 0 ▶ <STDIN> or do {print "Good. I'm bored too.\n"; exit;} if $Interactive;
Branch: 1 branch
BranchTrueFalseTotal
if $Interactive 0 118 118
Condition20%1/5
$Interactive && (readline STDIN || do { print "Good. I'm bored too.\n"; exit })
!ll
1180
readline STDIN or do { print "Good. I'm bored too.\n"; exit }
l!l&&r!l&&!r
000
MC/DC0%0/3
$Interactive && (readline STDIN || do { print "Good. I'm bored too.\n"; exit })
$Interactive readline STDIN do { print "Good. I'm bored too.\n"; exit }
Truth table0%0/4
A && B or C
A= $InteractiveB= readline STDINC= do { print "Good. I'm bored too.\n"; exit }
ABCresult
0XX0
1000
11X1
1011
119 } #end while (loop over turns)
120  
121 0 print "Too many turns. I give up.\n";
122 0 exit;
123  
124 ######################################################################
125 127 ▶sub compare_last {
Subroutine compare_last: called CC=1 SCAR=0.0
126 # compare last card in two card sets
127 # return positive value if second set wins, 0 for equality
128 127 my ($set1, $set2) = (shift, shift);
129 127 my $card1 = $set1->top_card;
130 127 my $card2 = $set2->top_card;
131  
132 127 return $card2->value - $card1->value;
133 }
134  
135 1 ▶sub win {
Subroutine win: called CC=1 SCAR=0.0
136 # player arg0 wins!
137 1 my ($winner, $turns) = (shift, shift);
138 1 print $Hands[$winner]->{"name"}, " wins after $turns turns!\n";
139 1 exit;
140 }
« blib/lib/Games/Cards/Tk.pm ↑ Summary