Created
April 24, 2012 20:49
-
-
Save bokutin/2483594 to your computer and use it in GitHub Desktop.
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
package Janken::Wx::App { | |
use strict; | |
use Wx; | |
use base "Wx::App"; | |
sub OnInit { | |
my $self = shift; | |
my $frame = Janken::Wx::Frame->new; | |
$frame->Fit; | |
$frame->Show(1); | |
return 1; | |
} | |
} | |
package Janken::Wx::Frame { | |
use utf8; | |
use strict; | |
use feature ":5.10"; | |
use Wx; | |
use base "Wx::Frame"; | |
sub new { | |
my $class = shift; | |
my $self = $class->SUPER::new(undef, -1, "じゃんけん"); | |
my $panel = do { | |
my $panel = Wx::Panel->new($self); | |
my $sizer = Wx::BoxSizer->new(&Wx::wxHORIZONTAL); | |
for (qw(ぐー ちょき ぱー)) { | |
my $button = Wx::Button->new($panel,-1,$_); | |
Wx::Event::EVT_BUTTON($button, -1, \&_button_handler); | |
$sizer->Add($button, 0, &Wx::wxALL, 10); | |
} | |
$panel->SetSizer($sizer); | |
$panel; | |
}; | |
{ | |
my $sizer = Wx::BoxSizer->new(&Wx::wxHORIZONTAL); | |
$sizer->Add($panel); | |
$self->SetSizer($sizer); | |
} | |
$self; | |
} | |
sub _button_handler { | |
my ($self, $event) = @_; | |
my $your = $self->GetLabel; | |
my $mech = qw(ぐー ちょき ぱー)[int(rand(3))]; | |
state $win_map = { | |
"ぐー,ちょき" => 1, | |
"ちょき,ぱー" => 1, | |
"ぱー,ぐー" => 1, | |
}; | |
my $judge = do { | |
my $key = join(",", $your, $mech); | |
if ($win_map->{$key}) { | |
"勝ち"; | |
} | |
elsif ( $your eq $mech ) { | |
"あいこ"; | |
} | |
else { | |
"負け"; | |
} | |
}; | |
Wx::MessageBox(sprintf("あなた: %s, ましん: %s, 勝敗: %s", $your, $mech, $judge), "ぽん"); | |
} | |
} | |
package main { | |
my $app = Janken::Wx::App->new; | |
$app->MainLoop; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment