六角マインスイーパー
shift + click でチェックをつけれます。
参考:http://www40.atwiki.jp/spellbound/pages/863.html
/**
* Copyright WinField95 ( http://wonderfl.net/user/WinField95 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cFVe
*/
package
{
import flash.display.Sprite;
import flash.events.TextEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.MouseEvent;
/**
* ...
* @author Kyugo
*/
public class Main extends Sprite
{
var game:Game;
public function Main():void
{
var back:Sprite = new Sprite();
back.graphics.beginFill(0x000000);
back.graphics.drawRect(-500, -500, 4000, 4000);
back.graphics.endFill();
addChild(back);
game = new Game();
addChild(game);
}
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
class Game extends Sprite{
private var map:Array;
private var blocks:Array; // クリックするブロックを入れる二次元配列
private var HEIGHT:int = 20;
private var WIDTH:int = 8;
private var NUM_BOMB:int = 20;
private var X_RATE:Number = 0.49;
private var Y_RATE:Number = 1.7;
private var button1:SimpleButton;
//private const MASUSIZE_W:int = 30; // 横のマスサイズ
//private const MASUSIZE_H:int = 30; // 縦のマスサイズ
private const MASUSIZE_R:int = 30; // マスの半径
//private var dx:Array = [0,1,1,1,0,-1,-1,-1];
//private var dy:Array = [-1,-1,0,1,1,1,0,-1];
private var dx0:Array = [0,1,1,0,0,0];
private var dx1:Array = [0,0,0,0,-1,-1];
private var dy:Array = [-2,-1,1,2,1,-1];
private var state:Boolean;
private var guide:TextField;
private var cnt_block:int;
public function Game():void {
button1 = new Button('reset',130)
button1.x = 300;
button1.y = 400;
button1.addEventListener(MouseEvent.CLICK, reset);
addChild(button1);
init();
addEventListener(Event.ENTER_FRAME, onEnterForame);
}
public function reset(event:MouseEvent):void
{
removeChild(guide);
for (var i:int = 0; i < HEIGHT; i++){
for (var j:int = 0; j < WIDTH; j++){
var tb:Block = blocks[i][j];
removeChild(tb);
}
}
init();
}
public function init():void {
//爆弾の配置
var bombMap:Array = [];
var bombIndex:int = 0;
for (var i:int = 0 ; i < HEIGHT * WIDTH ; i++) {
if (bombIndex++ < NUM_BOMB) bombMap[i] = Status.BOMB;
else bombMap[i] = Status.FIELD;
}
shuffleArray(bombMap);
map = [];
for (var i:int = 0; i < HEIGHT; i++){
map[i] = [];
for (var j:int = 0; j < WIDTH; j++)
{
map[i][j] = bombMap[i * WIDTH + j];
}
}
//数字の配置
for (var i:int = 0 ; i < HEIGHT ; i++) {
for (var j:int = 0 ; j < WIDTH ; j++) {
if (map[i][j] == Status.BOMB) continue;
var cnt:int = 0;
for (var k:int = 0 ; k < 6 ; k++) {
var nx:int, ny:int;
if (i % 2) nx = j + dx1[k];
else nx = j + dx0[k];
ny = i + dy[k];
if (!(0 <= nx && nx < WIDTH) || !(0 <= ny && ny < HEIGHT)) continue;
if (map[ny][nx] != Status.BOMB) continue;
cnt++
}
map[i][j] = cnt;
}
}
//Blockの生成
blocks = [];
for (var i:int = 0 ; i < HEIGHT ; i++) {
blocks[i] = [];
var diff:int = i % 2;
for (var j:int = 0 ; j < WIDTH ; j++) {
//var block:Block = new Block(MASUSIZE_W, MASUSIZE_H);
//block.y = i * MASUSIZE_H;
//block.x = j * MASUSIZE_W;
var block:Block = new Block(MASUSIZE_R);
block.x = 100 + (j- diff*X_RATE) * (MASUSIZE_R *3.0/2.0);// * Math.sqrt(3);
//if (j % 2 == 1) block.x -= MASUSIZE_R;
block.y = 100 + i * (MASUSIZE_R/2.0 - Y_RATE);
//var c:String = "*"
//if (map[i][j] != Status.BOMB) c = map[i][j].toString();
//block.tf.text = c;
addChild(block);
blocks[i][j] = block;
}
}
// guideの初期化
guide = new TextField();
guide.defaultTextFormat = new TextFormat("Consolas,メイリオ,_typeWriter",20, 0xFFFFFF , true);
guide.autoSize = "left";
guide.selectable = false;
guide.text = 'ゲームスタート!!';
guide.x = 50;
guide.y = 400;
addChild(guide);
state = true;
cnt_block = 0;
}
//配列をランダムにソートしてシャッフル
private function shuffleArray(array:Array):void {
array.sort(function () {
return Math.floor(Math.random()*3)-1;
});
}
private function onEnterForame(event:Event):void {
// 選択されたブロックをさがす
var nx:int, ny:int;
nx = ny = -1;
for (var i:int = 0 ; i < HEIGHT ; i++) {
for (var j:int = 0 ; j < WIDTH ; j++) {
var tb:Block = blocks[i][j];
if (tb.checked) continue;
if (tb.select) {
ny = i;
nx = j;
break;
}
}
}
if (nx == -1 && ny == -1) return;
if (!state) return;
/*
var sp:Sprite = new Sprite();
sp.graphics.beginFill(0xF);
sp.graphics.drawCircle(1, 1, 100);
sp.graphics.endFill();
addChild(sp);
*/
if(map[ny][nx] == Status.BOMB){ // 地雷ならばゲームオーバー
var tmp:Block = blocks[ny][nx];
tmp.changeState(map[ny][nx]);
state = false;
guide.text = "ゲームオーバー"
for (var i:int = 0 ; i < HEIGHT ; i++) {
for (var j:int = 0 ; j < WIDTH ; j++) {
var tb:Block = blocks[i][j];
tb.changeState(map[i][j]);
}
}
return;
}
var queue:Array = new Array();
queue.push(new Point(nx, ny));
while(true){
var p:Point = queue.shift();
if (p == null) break;
var tmp:Block = blocks[p.y][p.x];
if (tmp.checked) continue;
tmp.changeState(map[p.y][p.x]);
cnt_block++;
if (map[p.y][p.x] != Status.FIELD) continue;
for (var i:int = 0 ; i < 6 ; i++){
if(p.y % 2)nx = p.x + dx1[i];
else nx = p.x + dx0[i];
ny = p.y + dy[i];
if (!(0 <= nx && nx < WIDTH) || !(0 <= ny && ny < HEIGHT)) continue;
queue.push(new Point(nx, ny));
}
}
if (cnt_block == (HEIGHT* WIDTH) - NUM_BOMB) {
state = false;
guide.text = "クリアー!! おめでとう!!";
}
}
}
import flash.display.*;
import flash.text.*;
class Button extends SimpleButton // ボタンのクラス
{
public function Button(label:String, width:int = 0):void
{
var up:Sprite = _buildImage(label, 0xCCCCCC, width);
var over:Sprite = _buildImage(label, 0x87CEFA, width);
var down:Sprite = _buildImage(label, 0x4682B4, width);
down.y = 1;
super(up, over, down, up);
}
public function _buildImage(label:String, color:int, width:int = 0):Sprite
{
var text:TextField = new TextField();
text.defaultTextFormat = new TextFormat('Verdana', 10, 0x000000, true,
null, null, null, null,
TextFormatAlign.CENTER);
text.autoSize = TextFieldAutoSize.CENTER;
text.selectable = false;
text.text = label;
text.x = (width - text.width) >> 1;
text.y = 5;
var base:Shape = new Shape();
var g:Graphics = base.graphics;
g.beginFill(color);
g.drawRect(0, 0, width, text.height + 3);
g.endFill();
var sp:Sprite = new Sprite();
sp.addChild(base);
sp.addChild(text);
return sp;
}
}
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
class Block extends Sprite
{
public var checked:Boolean = false;
public var flag:Boolean = false;
//public var w:Number;
//public var h:Number;
public var r:Number;
public var tf:TextField;
public var select:Boolean;
public var hexagon:Hexagon;
public function Block(r:Number)
{
//this.w = width;
//this.h = height;
this.r = r;
hexagon = new Hexagon(r, 0xFF9900);
addChild(hexagon);
setFlag(false);
tf = new TextField();
tf.defaultTextFormat = new TextFormat("Consolas,メイリオ,_typeWriter",20, 0x0 , true);
tf.autoSize = "left";
tf.selectable = false;
tf.x -= r / 2.0 - 9;
tf.y -= r / 2.0 - 2;
tf.text = '';
select = false;
addEventListener(MouseEvent.CLICK, onMouseClick);
}
public function setFlag(flag:Boolean):void
{
this.flag = flag;
graphics.clear();
if (flag)
{
/*
graphics.lineStyle(2.0);
graphics.beginFill(0xFF9900);
graphics.drawRect(0, 0, w, h);
graphics.endFill();
*/
removeChild(hexagon);
hexagon = new Hexagon(r, 0xFF9900);
addChild(hexagon);
}
else
{
/*
graphics.lineStyle(2.0);
graphics.beginFill(0x888888);
graphics.drawRect(0, 0, w, h);
graphics.endFill();
*/
removeChild(hexagon);
hexagon = new Hexagon(r, 0x888888);
addChild(hexagon);
}
}
public function changeState(num:int):void{
/*
graphics.clear();
graphics.lineStyle(2.0);
graphics.beginFill(0xFFFFFF);
graphics.drawRect(0, 0, w, h);
graphics.endFill();
*/
removeChild(hexagon);
hexagon = new Hexagon(r, 0xFFFFFF);
addChild(hexagon);
if (num != Status.BOMB && num != Status.FIELD) tf.text = num.toString();
else if(num == Status.BOMB) tf.text = "*"
checked = true;
addChild(tf);
}
private function onMouseClick(event:MouseEvent):void
{
if (event.shiftKey && !checked) {
setFlag(!flag);
return;
}
if(!flag) select = true;
}
}
import flash.display.*;
import flash.geom.Matrix;
import flash.geom.Point;
class Hexagon extends Sprite {
var stroke:GraphicsStroke;
var path:GraphicsPath;
var fill:GraphicsGradientFill;
var r:Number;
public function Hexagon(size:int,color:int) {
r=size/2;
setStroke();
setFill(color);
setPath();
drawGraphics();
}
//線のスタイル
private function setStroke():void {
stroke=new GraphicsStroke(2);
stroke.joints=JointStyle.MITER;
stroke.fill=new GraphicsSolidFill(0x000000);
}
//グラデーションでの塗り
private function setFill(color:int):void {
fill = new GraphicsGradientFill();
fill.colors = [color,color];
fill.matrix = new Matrix();
fill.matrix.createGradientBox(2*r,2*r,Math.PI/4,-r,-r);
}
//描画パス
public function setPath():void {
path=new GraphicsPath();
path.winding=GraphicsPathWinding.NON_ZERO;
for(var i:int=0;i<6;i++){
var pt:Point=Point.polar(r,2*Math.PI/6*i);
if(i==0){
path.moveTo(r,0);
}else{
path.lineTo(pt.x,pt.y);
}
}
path.lineTo(r,0);
}
//グラフィックスデータを使って図形を描く
private function drawGraphics():void {
var graphicsData:Vector.<IGraphicsData> = new Vector.<IGraphicsData>();
graphicsData.push(stroke, fill, path);
graphics.drawGraphicsData(graphicsData);
}
}
class Status {
public static const FIELD:int = 0;
public static const BOMB:int = -1;
}