2016年9月30日金曜日

開発環境

アルゴリズムパズル(Anany Levitin (著)、Maria Levitin (著)、黒川 洋 (翻訳)、松崎 公紀 (翻訳)、オライリージャパン)の中級パズル、58.(2色の帽子(Hats of Two Colors))をJavaScriptで。

コード(Emacs)

HTML5

<label for="black0">
  黒い帽子:
</label>
<input id="black0" type="number" min="1" max="11" step="1" value="6">人、
白い帽子: <span id="white0"></span><button id="start0">開始</button>
<div id="output0"></div>
<button id="next0">5分後</button>

<script src="sample59.js"></script>

JavaScript

{
    'use strict';
    Array.prototype.shuffle = function () {
        var shuffled = [],
            len = this.length,
            len0,
            i;

        for (len0 = len; len0 > 0; len0 -= 1) {
            i = Math.floor(Math.random() * len0);
            shuffled.push(this[i]);
            this.splice(i, 1);
        }
        for (i = 0; i < len; i += 1) {
            this[i] = shuffled[i];
        }
    };
    let div_output = document.querySelector('#output0'),
        input_black = document.querySelector('#black0'),
        span_white = document.querySelector('#white0'),
        button_start = document.querySelector('#start0'),
        button_next = document.querySelector('#next0'),
        nl = '<br>',
        t = new Date(),
        nums = 12,
        count,
        person = (num, color, others, count) => {
            return {
                num: num,
                color: color,
                others: others,
                count: count,
                toString: () => {
                    return `${num}${color}`;
                },
            };
        };

    let people;
    let start = () => {
        let black = parseInt(input_black.value),
            white = nums - black,
            colors = [];

        count = 0;
        people = [];
        for (let i = 0; i < nums; i += 1) {            
            let color = i < black ? '黒' : '白';
            colors[i] = color;
        }        
        colors.shuffle();
        for (let i = 0; i < nums; i += 1) {
            let color = colors[i],
                others = colors.slice(0, i).concat(colors.slice(i + 1)),
                c = 0;

            others.forEach((other) => {
                if (other === '黒') {
                    c += 1;
                }
            });
            people[i] = person(i, color, others, c);
        }
        t.setHours(12, 5);
        div_output.innerHTML =
            `${t.getHours()}:${t.getMinutes()}${nl}` + 
            people.join(' ') + nl;
    };
    let next = () => {
        t.setMinutes(t.getMinutes() + 5);
        count += 1;
        let people0 = [];
        for (let i = 0; i < nums; i += 1) {
            if (people[i] !== ' ' &&
                people[i].count === count - 1) {
                people0.push(people[i]);
                people[i] = ' ';
            }
        }
        div_output.innerHTML +=
            `${nl}${t.getHours()}:${t.getMinutes()}${nl}` +
            people.join(' ') + nl +
            people0.join(' ') + nl;
    };
    button_start.onclick = start;
    button_next.onclick = next;
    span_white.innerText = nums - parseInt(input_black.value, 10);
    input_black.onchange = 
        () => span_white.innerText = nums - parseInt(input_black.value, 10);
}
人、 白い帽子:

0 コメント:

コメントを投稿

Comments on Google+: