PuttiCounter+NabeAzz (いまさら)
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
[SWF(width="200", height="400", backgroundColor="0x3a4044", frameRate="60")]
public class PuttiCounter extends Sprite
{
private const MAX_NUM:uint=100000;//いつまでやらせるか
private const CONST_ERROR:Number = 0.1;//プッチの間違え頻度調整
private const CHR_AHO:String = "1";//アホになる時の合い言葉
private const CHR_RET:String = "0";//アホから返る時の合い言葉
private var primeArray: Array = [];//素数格納
private var taskArray: Array = [undefined];//プッチの次のアクション
private var wordsArray: Array = [];//プッチの次の台詞
private var num: uint = 2;//カウントスタート
private var primeTimer: Timer = new Timer(1);
private var taskTimer: Timer = new Timer(400);
private var wordsTimer: Timer = new Timer(100);
private var puttiTextR: TextField = new TextField();
private var puttiTextC: TextField = new TextField();
private var puttiTextL: TextField = new TextField();
private var puttiFormat: TextFormat = new TextFormat();
private var talkingWords:Array = [];
private var shikaku: Sprite = new Sprite();
private var becomesAho:Boolean = false;
public function PuttiCounter():void
{
stage.align = "TL";
stage.scaleMode = "noScale";
puttiFormat.align = "center";
puttiFormat.size = 10;
puttiTextR.width = 10;
puttiTextR.height = 10;
puttiTextR.autoSize = "right";
puttiTextR.background = true;
puttiTextR.backgroundColor = 0xffffff;
puttiTextR.defaultTextFormat = puttiFormat;
puttiTextC.width = 10;
puttiTextC.height = 10;
puttiTextC.autoSize = "center";
puttiTextC.background = true;
puttiTextC.backgroundColor = 0xffffff;
puttiTextC.defaultTextFormat = puttiFormat;
puttiTextL.x = 0;
puttiTextL.width = 10;
puttiTextL.height = 10;
puttiTextL.autoSize = "left";
puttiTextL.background = true;
puttiTextL.backgroundColor = 0xffffff;
puttiTextL.defaultTextFormat = puttiFormat;
shikaku.x = 10;
shikaku.y = 10;
makeFukidashi();
addChildAt(shikaku,0);
shikaku.addChild(puttiTextR);
shikaku.addChild(puttiTextC);
shikaku.addChild(puttiTextL);
var dummEvt: TimerEvent = new TimerEvent("timer");
makeWords(dummEvt);
primeTimer.addEventListener("timer", makeArray);
primeTimer.start();
taskTimer.addEventListener("timer", makeWords);
taskTimer.start();
wordsTimer.addEventListener("timer", talkWords);
wordsTimer.start();
}
private function makeFukidashi():void
{
shikaku.graphics.clear();
var ww:Number = ((puttiTextL.width + puttiTextC.width + puttiTextR.width)+10)/8;
var hh:Number = (Math.max(puttiTextL.height,puttiTextC.height,puttiTextR.height)+10)/8;
shikaku.graphics.beginFill(0xffffff);
if (!becomesAho) shikaku.graphics.lineStyle(1.5, 0);
else if(becomesAho) shikaku.graphics.lineStyle(1.5, Math.floor(0x1000000*Math.random()));
shikaku.graphics.moveTo(-5, -5);
shikaku.graphics.lineTo(ww + 2 * ww * Math.random(), 2 * Math.random() - 6);
shikaku.graphics.lineTo(3 * ww + 2 * ww * Math.random(), 2 * Math.random() - 6);
shikaku.graphics.lineTo(5 * ww + 2 * ww * Math.random(), 2 * Math.random() - 6);
shikaku.graphics.lineTo(8 * ww, -5);
shikaku.graphics.lineTo(8 * ww+2 * Math.random() - 1, hh + 2 * hh * Math.random());
shikaku.graphics.lineTo(8 * ww+2 * Math.random() - 1, 3*hh + 2 * hh * Math.random());
shikaku.graphics.lineTo(8 * ww+2 * Math.random() - 1, 5*hh + 2 * hh * Math.random());
shikaku.graphics.lineTo(8 * ww, 8*hh);
shikaku.graphics.lineTo(5 * ww+ 2 * ww * Math.random(), 8*hh+2 * Math.random() - 1);
shikaku.graphics.lineTo(3 * ww+ 2 * ww * Math.random(), 8*hh+2 * Math.random() - 1);
shikaku.graphics.lineTo(ww + 2 * ww * Math.random(), 8 * hh+2 * Math.random() - 1);
shikaku.graphics.lineTo(2 * Math.random() - 6, 8*hh);
shikaku.graphics.lineTo(2 * Math.random() - 6, 5*hh + 2 * hh * Math.random());
shikaku.graphics.lineTo(2 * Math.random() - 6, 3*hh + 2 * hh * Math.random());
shikaku.graphics.lineTo(2 * Math.random() - 6, hh + 2 * hh * Math.random());
shikaku.graphics.lineTo(-5, -5);
shikaku.graphics.endFill();
}
//wordsArrayから1要素ずつ取り出し順に表示させる
private function talkWords(evt:TimerEvent):void
{
if (!(talkingWords[0] is String)) talkingWords = wordsArray.shift();
else {
var nextWord:String = talkingWords.shift();
if((nextWord === CHR_AHO) || (nextWord === CHR_RET))
{
if (nextWord === CHR_AHO) becomesAho = true;
else if (nextWord === CHR_RET) becomesAho = false;
nextWord = talkingWords.shift();
}
nextWord = nextWord +"\n";
if(puttiTextR.numLines<20)puttiTextR.appendText(nextWord);
else if(puttiTextC.numLines < 20) puttiTextC.appendText(nextWord);
else if (puttiTextL.numLines < 20) puttiTextL.appendText(nextWord);
else if (puttiTextL.numLines >= 20)
{
puttiTextR.text = puttiTextC.text;
puttiTextC.text = puttiTextL.text;
puttiTextL.text = nextWord;
}
makeFukidashi();
puttiTextR.x = puttiTextL.width + puttiTextC.width;
puttiTextC.x = puttiTextL.width;
}
}
//呼び出されるとある自然数が素数かどうか吟味し、taskArrayに次回実行する要素を一つ追加する
private function makeArray(evt:TimerEvent):void
{
var sqrtNum: uint = Math.round(Math.sqrt(num));
var frg_found:Boolean = true;
var pn: uint;
for each(pn in primeArray)
{
if(pn > sqrtNum)break;//現在まで見つかった素数でnumが割り切れなかった場合frg_foundは真のまま。
if( num % pn == 0 ){
frg_found = false;
if (Math.random() < (pn - 2) / sqrtNum * CONST_ERROR)taskArray.push([num, pn]);
//numについて見つかった素因数pnが大きいほど、numが素数かもしれない、と一瞬プッチが間違えやすくなる。
break;
}
}
if (frg_found)
{//素数発見時の処理
primeArray.push(num);
taskArray.push(num);
}
num = num + 1;
if (num == MAX_NUM)
{
//最初に戻る
taskArray.push(undefined);
primeArray = [];
num = 2;
}
return;
}
//呼び出されるとtaskArray先頭要素からwordsArray要素を作る
private function makeWords(evt:TimerEvent):void
{
var val :* = taskArray.shift();
var str :String = "";
var words0: String = "・・・";
var words1: String = "・・・いや違う・・・";
var words2: String = "は";
var words3: String = "の倍数だ・・・";
var words4: String = "落ち着け・・・3がつく素数の時だけアホになって落ち着くのだ・・・";
if (val is uint) {//取り出した要素が整数だったら、それは素数。
str = uint2String(val) + words0;
}
else if (val is Array) {//配列だったらプッチは間違えた
str = uint2String(val[0]) + words1 + uint2String(val[0]) + words2+ uint2String(val[1]) + words3;
}
else {//素数探索が間に合わなかった場合
str = words4;
}
wordsArray.push(str.replace(/(\d+|\D)/g, "$1\/").replace(/\/$/, "").split(/\//));
return;
}
//3がつく自然数と3の倍数の時にアホになったStringを返す関数
private function uint2String(n:uint):String
{
var digitStr: Array = [
["", "いちィィィ", "にィッ", "さァァん", "よォォォォん", "ゴゴゴゴゴ", "るぉォォク", "なナァァッ", "はちィィッ", "きゅゥゥウンッ"],
["", "じゅッ", "にィッじゅ", "さァんじゅ", "よォんじゅ", "ゴッじゅ", "るぉくじゅ", "なァなじゅ", "はァちじゅ", "きゅッじゅ"],
["", "ひゃァァく", "にィひゃッく", "サァんびゃッく", "よォんひゃッく", "ゴひゃっく", "るぉぴゃッく", "なァなひゃく", "はァッぴゃく", "きゅひゃッく"],
["", "せんッ", "にせんッ", "サァんぜんッ", "よォんせんッ", "ゴせんッ", "ろォくせんッ", "ななッせんッ", "ハッせんッ", "きゅゥせンッ"]
]
var digits4Str:Array = ["", "マンッ", "おォォくッ", "ちょォォォうッ", "ガァァァイ", "ジョッ", "ジョォォーッ"];
var str :String = n.toString(); trace(str);
if (!(/3/.test(str) || n % 3 == 0)) return str;
str = str.replace(/(\d)/g, "$1\/").replace(/\/$/, "").split(/\//).reverse().toString().replace(/,/g,"");
var strAry: Array = str.replace(/(\d{4})/g, "$1\/").replace(/\/$/, "").split(/\//);
for (var i: uint = 0; strAry[i] is String; i++ )
{
strAry[i] = strAry[i].replace(/(\d)/g, "$1\/").replace(/\/$/, "").split(/\//);
}
for (i = 0; (strAry[i] is Array); i++)
{
for (var j:uint = 0; (strAry[i][j] is String); j++)
{
strAry[i][j] = digitStr[j][uint(strAry[i][j])];
}
strAry[i][0] += digits4Str[i];
strAry[i].reverse();
}
strAry.reverse();
return CHR_AHO + strAry.toString().replace(/,/g,"") + CHR_RET;
}
}
}