無精・短気・傲慢

perlの事 いろいろ

じゃんけん判定

 じゃんけん勝敗判定アルゴリズム

  • 二人でじゃんけん
    • ($a - $b + 3) % 3
  • 複数人でじゃんけん

じゃんけんの勝敗なんて考えた事なかった。二人でじゃんけんを行った時の「グー」、「チョキ」、「パー」の9通りの組み合わせで、「勝ち」、「負け」、「引き分け」の3通りの結果が1行で判断出来るとは…「じゃんけん勝敗判定アルゴリズムの思い出」で衝撃を受けて早速じゃんけん判定ページを作ってみた。(やっぱりperlで)

これはもっとすごい($r |= 1 << $a)1行の繰り返しでじゃんけん判定が出来る【ネタばれ】大は小を兼ねるジャンケンプログラムのアルゴリズム(どんなプログラムでもアルゴリズムは使用可)【注意】

 ちゃぼ

WebSocketのちゃぼにも『じゃんけん機能』を追加したよ

  じゃんけんポン

<html>
  <head>
      <meta charset="utf-8"/>
      <script src="webperl.js"></script>
      <script src="https://code.jquery.com/jquery-3.3.1.min.js"
      integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
      crossorigin="anonymous"></script>
  </head>
  <body>
  <h1><a href="http://park15.wakwak.com/~k-lovely/cgi-bin/wiki/wiki.cgi?page=%A4%B8%A4%E3%A4%F3%A4%B1%A4%F3%C8%BD%C4%EA">じゃんけんぽん</a></h1>
  <button type="button" id="gu"><font size=50>&#x270A;</font></button>
  <button type="button" id="choki"><font size=50>&#x270C;</font></button>
  <button type="button" id="pa"><font size=50>&#x270B;</font></button>
  <hr>
  <div id="pc" style="font-size:50px;">
      &#x270A;
  </div>
  <div id="result">
  </div>
  <hr>
  <h2>5人でじゃんけんポン</h2>
  <div id="pc9" style="font-size:50px;">
      &#x270A;
  </div>
  <div id="result2">
  </div>
      <script type="text/perl">
           use utf8;
           my $item = {qw(0 &#x270A; 1 &#x270C; 2 &#x270B;)};
           my $jq = js('jQuery');
           sub pon{
               my $you = shift;
               my $pc = int(rand()*3);
               my @pc = map {int(rand()*3)} (1 .. 3);                                                                                  
               $jq->('#pc')->html($item->{$pc});
               $jq->('#pc9')->html(join('|',map{$item->{$_}} ($pc,@pc)));
               $jq->('#result2')->html(judge($you,$pc,@pc));
               return qw(DRAW Your_lost Your_win)[judg($you,$pc)];
           }
           sub judg{
               return (shift() - shift() + 3)%3;
           }
           sub judge{
               my $r=0;
               $r |= 1<<$_ for(@_);
               return qw(不明 引き分け 引き分け グーの勝ち 引き分け パーの勝ち チョキの勝ち 引き分け)[$r];
           }

           $jq->('#gu')->on('click',sub{ $jq->('#result')->html(pon(0)); });
           $jq->('#choki')->on('click',sub{ $jq->('#result')->html(pon(1)); });
           $jq->('#pa')->on('click',sub{ $jq->('#result')->html(pon(2)); });

      </script>
  </body>
</html>

 ターミナルでじゃんけんぽん

#!/usr/bin/perl
use strict;
use warnings;

my $item = {qw(0 グー 1 チョキ 2 パー)};
my $finish = 'e';

play();
sub play{
    while((my $input = prompt('> ')) ne $finish){
        my $computer = int(rand()*3);
        print "あなた-> $item->{$input} --- $item->{$computer} <-コンピュータ \n\n";
        print "@{[qw(あいこ あなたの負け あなたの勝ち)[judg($input,$computer)]]}です\n\n";
    }
}
sub judg{
    my ($you,$computer) = @_;
    return ($you - $computer + 3) % 3;
}

sub prompt{
    my $ps1 = shift;
    print join("、",message()),"\n";
    print $ps1;
    while(<>){
        chomp();
        return $finish if(/^(e|q|exit|quit|999)$/i);
        return $_ if(exists $item->{$_});
        print $ps1;
    }
    return $finish;
}
sub message{
    map {"$_ :$item->{$_}"} sort keys(%$item);
}

 ERROR

ccess to script at 'https://webperlcdn.zero-g.net/v0.07-beta/webperl.js' from origin 'http://www21051ue.sakura.ne.jp' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.