High and Low

by daniwell
トランプで次にめくった数字が今の数字より大きい(high)か小さい(low)かを当てるというアレです。
♥2 | Line 117 | Modified 2012-02-11 22:10:38 | MIT License
play

ActionScript3 source code

/**
 * Copyright daniwell ( http://wonderfl.net/user/daniwell )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/mfpx
 */

package  
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	[SWF(backgroundColor = "0xffffff", frameRate = "30", width = "465", height = "465")]
	public class HighAndLow extends Sprite
	{
		private var now        :int;
		private var cardNums   :Array;
		private var resultText :CustomTextField;
		private var restText   :CustomTextField;
		
		public function HighAndLow() 
		{
			now = 0;
			cardNums = [];
			
			for (var i:int = 0; i < 13; i ++) for (var j:int = 0; j < 4; j ++) cardNums.push(i+1);
			
			// カードシャッフル
			for (i = 0; i < 52; i ++)
			{
				var tmp:int = cardNums[i];
				var r:int = int(Math.random() * 52);
				cardNums[i] = cardNums[r];
				cardNums[r] = tmp;
			}
			initUI();
			nextCard();
		}
		/* UI 初期化 */
		private function initUI():void
		{
			var btHigh :Button = new Button(20, 110, "↑high");
			btHigh.bool = true;
			btHigh.addEventListener(MouseEvent.CLICK, clickHandler);
			
			var btLow :Button = new Button(80, 110, "↓low");
			btLow.addEventListener(MouseEvent.CLICK, clickHandler);
			
			var tf :CustomTextField = new CustomTextField(20, 10, "次に出てくる数字が今の数字より大きいか小さいかを予想してください。\n(トランプ52枚。1が最少、13が最大とします)");
			resultText = new CustomTextField(20, 142);
			restText   = new CustomTextField(20, 175);
			
			addChild(btHigh); addChild(btLow); addChild(tf); addChild(resultText); addChild(restText);
			
			graphics.beginFill(0x000000); graphics.drawRect(0, 0, 465, 465);
			graphics.beginFill(0xffffff); graphics.drawRect(5, 5, 455, 192);
		}
		/* 次のカードへ */
		private function nextCard():void
		{
			var card :Card = new Card(now);
			card.x = 20 + now * 6;
			card.y = 50;
			card.setNumber(cardNums[now]);
			addChild(card);
			
			now ++;
			restText.text = "残り: " + (52 - now) + " 枚, 正解数: " + (resultText.text.split("○").length - 1) + " 枚";
		}
		/* High 又は Low をクリック */
		private function clickHandler(e:MouseEvent):void
		{
			if (now == 52) return;
			
			var bool:Boolean = (cardNums[now - 1] < cardNums[now]);
			if((e.target.bool == bool || cardNums[now - 1] == cardNums[now]) && cardNums[now - 1] != cardNums[now]) resultText.appendText("○");
			else resultText.appendText("×");
			
			if (now == 26) resultText.appendText("\n"); 
			
			nextCard();
		}
	}
}
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;

class Card extends Sprite
{
	private var _tf :TextField;
	
	public function Card(now :int)
	{
		if (now%2) graphics.beginFill(0x0099ff);
		else       graphics.beginFill(0xff3300);
		graphics.drawRoundRect(0, 0, 30, 40, 10, 10);	// 30 x 40
		
		_tf = new TextField();
		_tf.autoSize = "center";
		_tf.defaultTextFormat = new TextFormat("_sans", 13, 0xffffff);
		addChild(_tf);
	}
	public function setNumber(n:int):void
	{
		_tf.text = n.toString();
		_tf.x = 15 - _tf.width  / 2;
		_tf.y = 20 - _tf.height / 2;
	}
}
class Button extends Sprite
{
	public var bool:Boolean = false;
	
	public function Button(px:int = 0, py:int = 0, title:String = "", txCol:uint = 0xffffff, bgCol:uint = 0x000000)
	{
		x = px; y = py;
		graphics.beginFill(0);
		graphics.drawRect(0, 0, 50, 20);	// 50 x 20
		
		var tf :TextField = new TextField();
		tf.autoSize = "center";
		tf.defaultTextFormat = new TextFormat("_sans", 11, txCol);
		tf.text = title;
		tf.x = (50 - tf.width) / 2;
		tf.y = (20 - tf.height) / 2; 
		tf.mouseEnabled = false;
		addChild(tf);
		
		buttonMode = true;
		addEventListener(MouseEvent.MOUSE_OVER, function():void { alpha = 0.5 } );
		addEventListener(MouseEvent.MOUSE_OUT,  function():void { alpha = 1.0 } );
	}
}
class CustomTextField extends TextField
{
	public function CustomTextField(px:int = 0, py:int = 0, value:String = "")
	{
		x = px; y = py; autoSize = "left";
		var format :TextFormat = new TextFormat("_等幅", 11, 0x333333);
		format.leading = 3;
		defaultTextFormat = format;
		
		text = value;
	}
}