Created
August 3, 2011 12:08
-
-
Save ainame/1122486 to your computer and use it in GitHub Desktop.
じゃんけんをCUI上で行うだけの糞プログラム。Perl使って初めてプログラム書きました。
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
#! /usr/bin/env perl | |
use warnings; | |
use strict; | |
use 5.010; | |
sub prompt{ | |
do{ | |
my $hand; | |
say "グー[g], チョキ[c], パー[p]"; | |
print "入力してください:"; | |
chomp($hand = <STDIN>); | |
say ""; | |
if($hand eq "g" || $hand eq "c" || $hand eq "p"){ | |
return $hand; | |
}else{ | |
say "もう一度入力してください"; | |
} | |
}while(1); | |
} | |
sub judge { | |
my ($hum, $com) = @_; | |
# 勝ち負けルール 0=負け, 1=勝ち, 2=引き分け | |
my %rule = ( | |
"g" => [2,1,0], | |
"c" => [0,2,1], | |
"p" => [1,0,2] | |
); | |
my @num_to_hand = ("グー", "チョキ", "パー"); | |
my %str_to_hand = ( | |
"g" => $num_to_hand[0], | |
"c" => $num_to_hand[1], | |
"p" => $num_to_hand[2] | |
); | |
print "「・・・ポンッ!!」\n=> 人間:$str_to_hand{$hum} , com:$num_to_hand[$com] \n"; | |
return $rule{$hum}[$com]; | |
} | |
sub game{ | |
my $human_hand = prompt(); | |
#comの手は 0 = グー, 1 = チョキ, 2 = パーと対応 | |
my $com_hand = int rand 3; | |
given (judge($human_hand, $com_hand)){ | |
when(0){ say "あなたの負けorz"; return 0;} | |
when(1){ say "あなたの勝ち!!"; return 0;} | |
when(2){ say "引き分け\n\n「あいこでっ・・・」"; return 1; } | |
default{ say "Error!!"; exit(1);} | |
} | |
} | |
# main | |
say "「じゃんけん・・・」"; | |
while(game()){} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment