challenge バイナリクロック

 時刻を二進数相当の表現で出力する時計アプリケーションを書いてください。
 20:18の場合,例えば以下の様な出力をするイメージです。

出力例:
 ■□■□□
□■□□■□
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#! c:\ruby\bin\ruby.exe -Ks

String.class_eval do |string|
    def words
        self.split(//)
    end
    def fix_width(width, padding)
        (self.words.size > width) ? self : (padding * (width - self.words.size) + self)
    end
end

Fixnum.class_eval do |fixnum|
    alias :to_s_orig :to_s
    def to_s(base, width)
        binary = self.to_s_orig(base).fix_width(width, "0")
    end
end

class BinaryClock
    attr_accessor :now
    def initialize
        self.now = Time.now
    end
    def print
        output(self.now.hour.to_s(2, 5))
        output(self.now.min.to_s(2, 6))
    end
private
    def output(binary)
        puts binary.words.map { |f| f == "0" ? "□" : "■" }.join.fix_width(6, " ")
    end
end

BinaryClock.new.print

Posted feedbacks - ActionScript

まだなかったので、習作として作ってみました。

また、Wonderflにもあるので良かったら動作するのも見てみてください。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.geom.ColorTransform;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.utils.Timer;
    
    [SWF(width = 140, height = 80, backgroundColor = 0xCCCCCC, frameRate = 30)]
    
    public class BinaryClock extends Sprite 
    {
        private const UNIT_LENGTH:int = 20;
        
        private var hourPoints:Vector.<Sprite> = new Vector.<Sprite>();
        private var minutePoints:Vector.<Sprite> = new Vector.<Sprite>();
        private var secondPoints:Vector.<Sprite> = new Vector.<Sprite>();
        private var timeTexts:Vector.<TextField> = new Vector.<TextField>();
        
        public function BinaryClock():void
        {
            init();
        }
        
        private function init():void
        {
            (function(points:Vector.<Sprite>):void {
                for (var index:int = 0; index < 5; index++) {
                    var point:Sprite = createPoint();
                    point.x = index * UNIT_LENGTH + UNIT_LENGTH;
                    point.y = 0;
                    points.push(point);
                }
            })(hourPoints);
            var initPoints:Function = function(points:Vector.<Sprite>, y:int):void {
                for (var index:int = 0; index < 6; index++) {
                    var point:Sprite = createPoint();
                    point.x = index * UNIT_LENGTH;
                    point.y = y;
                    points.push(point);
                }
            }
            initPoints(minutePoints, UNIT_LENGTH);
            initPoints(secondPoints, UNIT_LENGTH * 2);
            
            (function(fields:Vector.<TextField>):void {
                for (var index:int = 0; index < 3; index++) {
                    var field:TextField = new TextField();
                    field.width = UNIT_LENGTH;
                    field.height = UNIT_LENGTH;
                    field.x = UNIT_LENGTH * 6;
                    field.y = index * UNIT_LENGTH;
                    field.autoSize = TextFieldAutoSize.CENTER;
                    timeTexts.push(field);
                }
            })(timeTexts);
            
            (function():Vector.<TextField> {
                var result:Vector.<TextField> = new Vector.<TextField>();
                for (var index:int = 0; index < 6; index++) {
                    var field:TextField = new TextField();
                    field.width = UNIT_LENGTH;
                    field.height = UNIT_LENGTH;
                    field.x = UNIT_LENGTH * (5 - index);
                    field.y = 3 * UNIT_LENGTH;
                    field.autoSize = TextFieldAutoSize.CENTER;
                    field.text = String(Math.pow(2, index));
                    result.push(field);
                }
                return result;
            })().map(function(field:TextField, i:int, a:*):void {
                addChild(field);
            });
            
            hourPoints = hourPoints.reverse();
            for each (var hp:Sprite in hourPoints) {
                addChild(hp);
            }
            minutePoints = minutePoints.reverse();
            for each (var mp:Sprite in minutePoints) {
                addChild(mp);
            }
            secondPoints = secondPoints.reverse();
            for each (var sp:Sprite in secondPoints) {
                addChild(sp);
            }
            for each (var field:TextField in timeTexts) {
                addChild(field);
            }
            
            var timer:Timer = new Timer(500);
            timer.addEventListener(TimerEvent.TIMER, setBinaryTime);
            timer.start();
        }

        private function createPoint():Sprite
        {
            var point:Sprite = new Sprite();
            point.graphics.beginFill(0xFFFFFF, 0.6);
            point.graphics.drawRoundRect(0, 0, UNIT_LENGTH, UNIT_LENGTH, UNIT_LENGTH / 2.0, UNIT_LENGTH / 2.0);
            point.graphics.endFill();
            return point;
        }
        
        private function setBinaryTime(event:TimerEvent):void
        {
            var now:Date = new Date();
            
            ([now.hours, now.minutes, now.seconds]).forEach(function(value:int, index:int, a:*):void {
                timeTexts[index].text = ((value < 10)? "0": "") + value;
            });

            var changeColorTime:Function = function(points:Vector.<Sprite>, value:int):void {
                for (var index:int = 0; index < points.length; index++) {
                    var mask:int = (1 << index);
                    changeColor(points[index], (~value & mask) >> index);
                }
            }
            changeColorTime(hourPoints, now.hours);
            changeColorTime(minutePoints, now.minutes);
            changeColorTime(secondPoints, now.seconds);
        }
        
        private function changeColor(s:Sprite, i:Number):void
        {
            s.transform.colorTransform = new ColorTransform(i, i, i, 1, 1, 1, 1, 0);
        }
    }
}

Index

Feed

Other

Link

Pathtraq

loading...