Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>オブジェクト指向に則ったじゃんけんプログラム in JS</h1>

<div class="field">
  <div class="con">
    <h3>プレイヤー紹介</h3>
    <table class="player-intro">
      <thead>
        <tr><th>名前</th><th>戦略</th></tr>
      </thead>
      <tbody></tbody>
    </table>
  </div>
  
  <div class="con janken-times">
    <h3>じゃんけん回数</h3>
    <p><span class="janken-times-span"></span>回</div></p>
  
  <div class="con match-history">
    <h3>戦歴</h3>
  </div>
  
  <div class="con final-result">
    <h3>最終結果</h3>
  </div>
</div>
              
            
!

CSS

              
                $base-color1: #eeeeee;
$base-color2: #393e46;
$base-color3: #222831;
$accent-color1: #00adb5;

body{
  margin: 0;
  font-family: sans-serif;
  color: $base-color3;
  background-color: $base-color2;
}

h1{
  margin: 0;
  padding: .5em 1em;
  font-size: 1.5em;
  color: $base-color1;
  font-weight: normal;
  text-align: center;
  background-color: $accent-color1;
}

.field{
  margin: 2em auto;
  width: 80%;
  
  .con{
    margin: 1em auto;
    background-color: $base-color1;
    
    h3{
      margin: 0;
      padding: 1em;
      color: $base-color1;
      font-weight: normal;
      text-align: center;
      background-color: $base-color3;
    }
  }
  
  .player-intro{
    width: 100%;
    margin: 1em auto;
    padding: 1em;
    
    th{
      padding: 1em;
      text-align: left;
      border-bottom: 2px dotted $base-color3;
    }
    td{
      padding: 1em;
    }
  }
  
  .janken-times, .final-result{
    p{
      margin: 0;
      padding: 1em;
      text-align: center;
    }
  }
  
  .match-history{
    .history{
      width: 80%;
      margin: 2em auto;
      padding: 2em 1em;
      background-color: #ddd;
      border-radius: 10px;
      .player1, .player2, .result{
        display: inline-block;
        width: 30%;
      }
      .player-name, .result{
        margin: 0 1em;
        padding: 1em 2em;
        color: $base-color1;
        text-align: center;
        background-color: $base-color2;
        border-radius: 10px;
      }
      .player-hand{
        width: 2em;
        height: 2em;
        margin: .5em auto;
        padding: 1em;
        font-size: 1.5em;
        text-align: center;
        background-color: $accent-color1;
        border-radius: 50%;
      }
      .result{
        width: 20%;
        height: 100%;
        background-color: $base-color3;
      }
    }
  }
}
              
            
!

JS

              
                // じゃんけんの手を定義
class JankenHand {
  constructor(char) {
    this.char = char;
  }

  toString() {
    return this.char;
  }

  // じゃんけんの手の文字とプログラム中の数値とをリンク
  static fromInt(index) {
    switch(index) {
      case 0: // 0=グー
        return JankenHandEnum.Rock;
        break;
      case 1: // 1=チョキ
        return JankenHandEnum.Scissors;
        break;
      case 2: // 2=パー
        return JankenHandEnum.Paper;
        break;
      default:
        throw new Error("indexが不正です。" + index);
    }
  }

  // 勝ったかどうかの判定
  winTo(hisHand) {
    switch(this) {
      case JankenHandEnum.Rock:
        return hisHand === JankenHandEnum.Scissors;
        break;
      case JankenHandEnum.Scissors:
        return hisHand === JankenHandEnum.Paper;
        break;
      case JankenHandEnum.Paper:
        return hisHand === JankenHandEnum.Rock;
        break;
      default:
        throw new Error("hisHandが不正です。" + hisHand);
    }
  }

  // 負けたかどうかの判定
  loseTo(hisHand) {
    return this !== hisHand && !this.winTo(hisHand);
  }
}

const JankenHandEnum = {};
JankenHandEnum.Rock     = new JankenHand("✊");
JankenHandEnum.Scissors = new JankenHand("✌️");
JankenHandEnum.Paper    = new JankenHand("✋");
// -----------------------------------

// じゃんけん戦略-テンプレート
class JankenStrategy {
  toString() {
  }
  
  nextHand() {
    // なにもしない
  }

  prevHands() {
    // なにもしない
  }
}

// じゃんけん戦略-ランダムに手を出す
class RandomStrategy extends JankenStrategy {
  toString(){
    return "ランダム戦略";
  }
  
  nextHand() {
    // 3パターンなので[0−2]の範囲でランダムな値を返すようにする
    const index = Math.round(Math.random() * 2);
    return JankenHand.fromInt(index);
  }
}

// じゃんけん戦略-同じ手を出す
class FixedHandStrategy extends JankenStrategy {
  toString(){
    return "固定戦略";
  }

  constructor(hand) {
    super();
    this.hand = hand;
  }

  nextHand() {
    // いつも決まった手を返すようにする
    return this.hand;
  }
}

// じゃんけん戦略-前回の試合時の手を記憶
class ChottoKashikoiStrategy extends JankenStrategy {
  toString(){
    return "試合記憶戦略";
  }

  prevHands(myHand, hisHand) {
    this.myHand = myHand;
    this.hisHand = hisHand;
  }

  nextHand() {
    // 初回は前回の手がないためランダムとする
    if(!this.myHand || !this.hisHand) {
      return new RandomStrategy().nextHand();
    }

    if(this.myHand.winTo(this.hisHand)) {
      // 自分が勝った場合は、同じ手を使う
      return this.myHand;
    } else if(this.myHand.loseTo(this.hisHand)) {
      // あいてが勝った場合は、あいての手を使う
      return this.hisHand;
    } else {
      return new RandomStrategy().nextHand();
    }
  }
}

// -----------------------------------

// プレイヤーの雛形、デフォルトでランダム戦略をとる
class Player {
  constructor(name, strategy = new RandomStrategy(), win=0) {
    this.name = name;
    this.strategy = strategy;
  }

  getName() {
    return this.name;
  }
  getStrategy() {
    return this.strategy.toString();
  }
  getWin() {
    return this.win;
  }

  nextHand() {
    return this.strategy.nextHand();
  }

  prevHands(myHand, hisHand) {
    this.strategy.prevHands(myHand, hisHand);
  }
}

// -----------------------------------

const PlayerList = {};
PlayerList.p1 = new Player("Taro");
PlayerList.p2 = new Player("Hanako", new ChottoKashikoiStrategy());

let player1Win = 0;
let player2Win = 0;

for(p in PlayerList){
  $(".player-intro tbody").append("<tr><td>"+ PlayerList[p].getName() +"</td><td>"+ PlayerList[p].getStrategy() +"</td></tr>");
}

const jankenTimes = 10;
$(".janken-times-span").append(jankenTimes);
// 10回じゃんけんをする
for(let i = 1; i <= jankenTimes; i++) {
  $(".match-history").append('<div id="history'+i+'" class="history"></div');
  const hand1 = PlayerList.p1.nextHand();
  const hand2 = PlayerList.p2.nextHand();
  $("#history"+i).append('<div class="player1"><div class="player-name">'
                         + PlayerList.p1.getName()
                         +'</div><div class="player-hand">'
                         + hand1
                         +'</div></div><div class="player2"><div class="player-name">'
                         + PlayerList.p2.getName()
                         +'</div><div class="player-hand">'
                         + hand2
                         +'</div></div>');

  const result = hand1.winTo(hand2) ? "勝ち" : hand1.loseTo(hand2) ? "負け" : "あいこ";
  
  if(hand1.winTo(hand2)) {
    player1Win++;
    $("#history"+i).append('<div class="result">'
                               + PlayerList.p1.getName()
                               +'の勝ち!</div>');
  }
  else if(hand1.loseTo(hand2)) {
    player2Win++;
    $("#history"+i).append('<div class="result">'
                               + PlayerList.p2.getName()
                               +'の勝ち!</div>');
  }else{
    $("#history"+i).append('<div class="result">'
                               +'あいこです!</div>');
  }

  // 前の手を覚えておく
  PlayerList.p1.prevHands(hand1, hand2);
  PlayerList.p2.prevHands(hand2, hand1);

  // ここで結果を出力する
}

const finalResult = player1Win > player2Win ? PlayerList.p1.getName() :
                    player1Win < player2Win ? PlayerList.p2.getName() :
"even";

// ここで最終結果を出力する
if(finalResult == "even"){
  $(".final-result").append("<p>引き分けです!</p>");
}else{
  $(".final-result").append("<p>"+ finalResult +"の勝ちです!</p>");
}
              
            
!
999px

Console