File Coverage

blib/lib/Wx/App/Mastermind.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Wx::App::Mastermind;
2              
3             =head1 NAME
4              
5             Wx::App::Mastermind - a nontrivial example of wxPerl threads
6              
7             =head1 DESCRIPTION
8              
9             A simple Mastermind game whose main purpose is to demonstrate
10             the use of thread in wxPerl, in a task for which threads are
11             overkill anyway.
12              
13             =cut
14              
15 1     1   1395 use strict;
  1         2  
  1         31  
16 1     1   5 use warnings;
  1         2  
  1         29  
17 1     1   5 use base qw(Wx::Frame Class::Accessor::Fast);
  1         2  
  1         747  
18              
19             our $VERSION = '0.02';
20              
21             use Wx qw(:sizer);
22             use Wx::Event qw(EVT_MENU EVT_CLOSE);
23              
24             use Wx::App::Mastermind::Board;
25             use Wx::App::Mastermind::Player::Computer;
26             use Wx::App::Mastermind::Player::Human;
27              
28             __PACKAGE__->mk_accessors( qw(finished) );
29             __PACKAGE__->mk_ro_accessors( qw(human computer) );
30              
31             sub new {
32             my( $class ) = @_;
33             my $self = $class->SUPER::new( undef, -1, 'MasterMind' );
34              
35             $self->{computer} = Wx::App::Mastermind::Player::Computer->new( tries => 10 );
36             $self->{human} = Wx::App::Mastermind::Player::Human->new( tries => 10 );
37              
38             my $szGames = Wx::BoxSizer->new( wxHORIZONTAL );
39              
40             foreach my $player ( $self->human, $self->computer ) {
41             my $board = Wx::App::Mastermind::Board->new( $self, $player );
42              
43             $board->add_subscriber( 'turn_finished', $self, 'on_turn' );
44             $szGames->Add( $board, 1, wxGROW|wxALL, 5 );
45             }
46              
47             my $file = Wx::Menu->new;
48             EVT_MENU( $self, $file->Append( -1, "&New game" ), sub { $self->reset } );
49             $file->AppendSeparator;
50             EVT_MENU( $self, $file->Append( -1, "E&xit" ), sub { $self->on_exit } );
51             EVT_CLOSE( $self, sub { $self->on_close; $_[1]->Skip } );
52              
53             my $menubar = Wx::MenuBar->new;
54             $menubar->Append( $file, "&File" );
55              
56             $self->SetMenuBar( $menubar );
57              
58             $self->SetIcon( Wx::GetWxPerlIcon );
59             $self->SetSizerAndFit( $szGames );
60             $self->Show( 1 );
61              
62             return $self;
63             }
64              
65             sub reset {
66             my( $self ) = @_;
67              
68             $self->human->reset;
69             $self->computer->reset;
70             $self->human->start;
71             $self->computer->start;
72             }
73              
74             sub on_exit {
75             my( $self ) = @_;
76              
77             $self->Hide;
78             $self->Close;
79             $self->Destroy;
80             }
81              
82             sub on_close {
83             my( $self ) = @_;
84              
85             $self->human->terminate;
86             $self->computer->terminate;
87             }
88              
89             sub stop {
90             my( $self ) = @_;
91              
92             $self->human->stop;
93             $self->computer->stop;
94             }
95              
96             sub on_turn {
97             my( $self, $item, $event, %params ) = @_;
98              
99             my $other = $item == $self->human->board ? $self->computer : $self->human;
100              
101             if( $other == $self->human
102             && ( $self->computer->won || $self->human->won ) ) {
103             $self->human->board->show_code( 1 );
104             $self->stop;
105             } else {
106             $other->play;
107             }
108             }
109              
110             1;
111              
112             __END__