Invert Tiles

by aont
♥2 | Line 121 | Modified 2010-04-06 18:01:07 | MIT License
play

Related images

ActionScript3 source code

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

<?xml version="1.0" encoding="utf-8"?>
<!--
タイルをクリックしていき、全てのタイルを裏返すのが目標です。
タイルをクリックするとそのタイルと上下左右隣のタイルが裏返ります。

テキストボックスの数字はパズルの大きさを表わしており、変更したい場合は書き換えた後にSetボタンを押してください。
Periodicにチェックをすると反対側のタイルが裏返るようになります。これも変更した後はSetボタンを押してください。
※ Periodicは自明解があります。

+ が付いたラベルは今までにクリックしたタイルを表しています
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="OnInitialize()">
	<mx:NumericStepper x="10" y="10" width="48" id="width_numstep" value="5" minimum="2" maximum="64"/>
	<mx:NumericStepper x="66" y="10" width="48" id="height_numstep" value="5" minimum="2" maximum="64"/>
	<mx:CheckBox x="122" y="10" label="Periodic" id="PeriodicCheckBox"/>
	<mx:Button x="198" y="10" label="Set" id="setvalue" width="50" click="SetButtonClick()"/>
	<mx:Script>
	<![CDATA[
		import mx.controls.Button;
		import mx.controls.Alert;
		private function OnInitialize():void
		{
			this.InitializePuzzle(int(this.width_numstep.value),int(this.height_numstep.value));
		}
		
		//[ArrayElementType("Button")]
		private var Tiles:Vector.<Button>;
		private var puzzle_width:int;
		private var puzzle_height:int;
		private function InitializePuzzle(width:int,height:int):void
		{
			[ArrayElementType("Button")]
			var Tiles:Vector.<Button> = this.Tiles = new Vector.<Button>(width*height);
			this.Periodic = this.PeriodicCheckBox.selected;
			this.puzzle_width = width;
			this.puzzle_height = height;
			
			for(var y:int=0;y<height;++y)
			{
				for(var x:int=0;x<width;++x)
				{
					Tiles[y*width+x] = CreatTile(x,y);
				}
			}
		}
		private function SetButtonClick():void
		{
			this.DestructPuzzle();
			this.InitializePuzzle(int(this.width_numstep.value),int(this.height_numstep.value));
		}

		private function CreatTile(x:int,y:int):Button
		{
			var tile:Button = new Button();
			tile.data = y*this.puzzle_width+x;
			tile.width = 32;
			tile.height = 32;
			tile.x = 10 + x*32;
			tile.y = 40 + y*32;
			
			tile.addEventListener(MouseEvent.CLICK,OnTileClick);
			this.addChild(tile);
			return tile;
		}
		private function DestructPuzzle():void
		{
			var width:int = this.puzzle_width;
			var height:int = this.puzzle_height;
			//[ArrayElementType("Button")]
			var Tiles:Vector.<Button> = this.Tiles;
			for(var y:int=0;y<height;++y)
			{
				for(var x:int=0;x<width;++x)
				{
					this.removeChild(Tiles[y*width+x]);
				}
			}
		}
		
		private var Periodic:Boolean = false;
		private function InvertXY(x:int,y:int):void
		{
			if(this.Periodic)
			{
				x = (x + this.puzzle_width) % this.puzzle_width;
				y = (y + this.puzzle_height) % this.puzzle_height;
			}
			else
			{
				if( x < 0 || x >= this.puzzle_width || y < 0 || y >= this.puzzle_height )
					return;
			}
			
			var tile:Button = this.Tiles[y*this.puzzle_width+x];
			tile.selected = !tile.selected;
		}
		private var ClickedTileString:String = "+";
		private function OnTileClick(event:MouseEvent):void 
		{
			var tile:Button = event.target as Button;
			tile.selected = !tile.selected;
			if(tile.label == ClickedTileString)
				tile.label = "";
			else
				tile.label = ClickedTileString;
			var data:int = int(tile.data);
			var x:int = data % this.puzzle_width;
			var y:int = data / this.puzzle_width;

			InvertXY(x+1,y);
			InvertXY(x-1,y);
			InvertXY(x,y-1);
			InvertXY(x,y+1);
			if(CompleteCheck())
				Alert.show("Complete!");
			
		}
		private function CompleteCheck():Boolean
		{
			var length:int = this.puzzle_width * this.puzzle_height;
			//[ArrayElementType("Button")]
			var Tiles:Vector.<Button> = this.Tiles;
			var tile:Button;
			for(var i:int=0;i<length;++i)
			{
				tile = Tiles[i];
				if(!tile.selected)
					return false;
			}
			return true;
		}
	]]>
	</mx:Script>
</mx:Application>