Skip to content

Instantly share code, notes, and snippets.

@bokutin
Created April 24, 2012 20:20
Show Gist options
  • Save bokutin/2483370 to your computer and use it in GitHub Desktop.
Save bokutin/2483370 to your computer and use it in GitHub Desktop.
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;
}
sub xrc {
my $self = shift;
$self->{xrc} ||= do {
require Wx::XRC;
my $xrc = Wx::XmlResource->new( &Wx::wxXRC_USE_LOCALE ) or die "cannot load XRC.";
$xrc->InitAllHandlers;
$xrc->Load( "janken.xrc" ) or die;
$xrc;
};
}
}
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;
Wx::wxTheApp->xrc->LoadOnFrame($self, undef, "MainFrame") or die;
Wx::Event::EVT_BUTTON( $self->FindWindow("stone_button"), -1, \&_button_handler );
Wx::Event::EVT_BUTTON( $self->FindWindow("scissors_button"), -1, \&_button_handler );
Wx::Event::EVT_BUTTON( $self->FindWindow("paper_button"), -1, \&_button_handler );
$self;
}
sub _button_handler {
my ($self, $event) = @_;
state $button_map = {
stone_button => "ぐー",
scissors_button => "ちょき",
paper_button => "ぱー",
};
my $your = $button_map->{ $self->GetName } or die;
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