Square

by snuke
ボードゲームです。

〜ルール〜
・初ターンだけは1手、以降は2手ずつ交代で石を置いて行きます。黒が先番です。
・36ヶ所全てが埋まった時点で終局となります。
・作った正方形の面積の合計がスコアになり、終局した時点でスコアが多い方が勝ちです。

ようするに「正方形を作るゲーム」です!

AS楽しいですね〜

↓android版もどうぞ^^
https://play.google.com/store/apps/details?id=jp.snuke.square
♥0 | Line 518 | Modified 2012-06-07 22:01:50 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.*;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.filters.BlurFilter;
    
    
    [SWF(width="500", height="500", backgroundColor="0xddddff", frameRate="24")]
    
    
    public class Square extends Sprite {
        public var spr:Sprite = new Sprite();
        
        public var sqs:int = X.sqs;
        public var datx:Array = X.datx;
        public var daty:Array = X.daty;
        public var datp:Array = X.datp;
        
        public var textF:TextFormat = new TextFormat();
        public var tf0:TextField;
        public var tf1:TextField;
        public var tfinit:TextField;
        public var tfundo:TextField;
        public var tfpl:Array = new Array(2);
        public var tfpl0:TextField;
        public var tfpl1:TextField;
        public var tfsc0:TextField;
        public var tfsc1:TextField;
        public var efe:Vector.<Effect> = new Vector.<Effect>(sqs);
        public var ten:Vector.<Ten> = new Vector.<Ten>(sqs);
        
        public var isi:Array = new Array(6);
        public var turn:int;
        public var score:Array = new Array(2);
        public var nowsc:Array = new Array(2);
        public var addsc:Array = new Array(2);
        public var nextc:int, nextw:int, wait:int = 5;
        public var nextx:Array = new Array(2);
        public var nexty:Array = new Array(2);
        public var pl0:int = 0, pl1:int = 1;
        
        public var se:Sound;
        
        
        public function Square(){
            this.addChild(spr);
            
            txt_init();
            
            var i:int;
            for(i = 0; i < 6; i++) isi[i] = new Array(6);
            for(i = 0; i < sqs; i++){
                efe[i] = new Effect();
                spr.addChild(efe[i]);
                ten[i] = new Ten(i);
                spr.addChild(ten[i]);
            }
            
            init();
            
            se = new Sound(new URLRequest("http://www29.atpages.jp/snuke/square/pati.mp3"));
            
            
            this.addEventListener(MouseEvent.MOUSE_DOWN, down);
            this.addEventListener(Event.ENTER_FRAME, main);
        }
        
        public function main(e:Event):void{
            if(!check_pl() && nextc >= 2) com();
            if(nextc < 2){
                if(nextw <= 0){
                    put(nextx[nextc],nexty[nextc]);
                    frame();
                    nextc++;
                    nextw = wait;
                } else {
                    nextw--;
                }
            }
            
            var i:int;
            for(i = 0; i < sqs; i++){
                if(efe[i].live){
                    if(efe[i].move()) addsc[efe[i].te] += datp[i];
                }
                if(ten[i].live) ten[i].move();
            }
            
            for(i = 0; i < 2; i++){
                if(addsc[i] > 0){
                    addsc[i]--;
                    nowsc[i]++;
                }
            }
            
            tfsc0.htmlText = "<font size='14'>"+nowsc[0]+"</font>";
            tfsc1.htmlText = "<font size='14'>"+nowsc[1]+"</font>";
            
            if(turn==36){
                if(score[0]==nowsc[0] && score[1]==nowsc[1]){
                    if(score[0]==score[1]){
                        tf0.htmlText = "<font color='#00aa00' size='35'>draw</font>";
                        tf1.htmlText = "<font color='#00aa00' size='35'>draw</font>";
                    } else if(score[0]>score[1]){
                        tf0.htmlText = "<font color='#ff0000' size='40'>win</font>";
                        tf1.htmlText = "<font color='#0000ff' size='30'>lose</font>";
                    } else {
                        tf0.htmlText = "<font color='#0000ff' size='30'>lose</font>";
                        tf1.htmlText = "<font color='#ff0000' size='40'>win</font>";
                    }
                }
            } else {
                tf0.text = "";
                tf1.text = "";
            }
            
            var r:int, g:int, b:int;
            g = 255-Math.abs(nowsc[1]-nowsc[0])*2;
            r = 255-Math.max(0,nowsc[1]-nowsc[0])*2;
            b = 255-Math.max(0,nowsc[0]-nowsc[1])*2;
            tf0.backgroundColor = (r<<16)+(g<<8)+b;
            tf1.backgroundColor = (b<<16)+(g<<8)+r;
        }
        
        
        public function init():void{
            var i:int, j:int;
            for(i = 0; i < 6; i++){
                for(j = 0; j < 6; j++){
                    isi[i][j] = 0;
                }
            }
            
            for(i = 0; i < sqs; i++){
                efe[i].kill();
                ten[i].kill();
            }
            
            turn = 0;
            score[0] = score[1] = 0;
            nowsc[0] = nowsc[1] = 0;
            addsc[0] = addsc[1] = 0;
            nextc = 2; nextw = 0;   
            
            frame();
            cal_score();
            
            tf0.text = tf1.text = "";
        }
        
        public function txt_init():void{
            textF.align = "center";
            
            tf0 = createTF(70,310,210,50);
            spr.addChild(tf0);
            tf1 = createTF(70,370,210,50);
            spr.addChild(tf1);
            
            tfinit = createButton(160,440,100,20);
            tfinit.text = "reset"; tfinit.backgroundColor = 0xccffcc;
            spr.addChild(tfinit);
            
            tfundo = createButton(30,440,100,20);
            tfundo.text = "undo"; tfundo.backgroundColor = 0xffcccc;
            spr.addChild(tfundo);
            
            tfsc0 = new TextField();
            tfsc0.x = 35; tfsc0.y = 328; tfsc0.width = 0;
            tfsc0.autoSize = TextFieldAutoSize.CENTER;
            tfsc0.textColor = 0xffffff; tfsc0.selectable = false;
            spr.addChild(tfsc0);
            tfsc1 = new TextField();
            tfsc1.x = 35; tfsc1.y = 388; tfsc1.width = 0;
            tfsc1.autoSize = TextFieldAutoSize.CENTER;
            tfsc1.selectable = false;
            spr.addChild(tfsc1);
            
            
            
            tfpl0 = createButton(360,20,80,20);
            tfpl0.border = false; tfpl0.backgroundColor = 0x000000;
            tfpl0.text = "Black player"; tfpl0.textColor = 0xffffff;
            spr.addChild(tfpl0);
            tfpl1 = createButton(360,220,80,20);
            tfpl1.border = false;
            tfpl1.text = "White player";
            spr.addChild(tfpl1);
            
            var i:int, j:int;
            for(i = 0; i < 2; i++){
                tfpl[i] = new Array(5);
                for(j = 0; j < 5; j++){
                    tfpl[i][j] = createButton(350,200*i+25*j+50,100,20);
                    spr.addChild(tfpl[i][j]);
                }
                tfpl[i][0].text = "Human";
                tfpl[i][1].text = "com Lv.0";
                tfpl[i][2].text = "com Lv.1";
                tfpl[i][3].text = "com Lv.2";
                tfpl[i][4].text = "com Lv.3";
            }
        }
        
        public function createButton(x:int,y:int,w:int,h:int):TextField{
            var tf:TextField = createTF(x,y,w,h);
            tf.selectable = false;
            tf.defaultTextFormat = textF;
            return tf;
        }
        
        public function createTF(x:int,y:int,w:int,h:int):TextField{
            var tf:TextField = new TextField();
            tf.border = true;
            tf.x = x; tf.y = y;
            tf.width = w; tf.height = h;
            tf.background = true;
            return tf;
        }

        
        
        public function frame():void{
            spr.graphics.clear();
            spr.graphics.beginFill(0xffd000);
            spr.graphics.drawRect(10,10,280,280);
            spr.graphics.endFill();
            spr.graphics.lineStyle(1,0x000000);
            
            var i:int, j:int;
            for(i = 0; i < 6; i++){
                spr.graphics.moveTo(i*40+50,50);
                spr.graphics.lineTo(i*40+50,251);
                spr.graphics.moveTo(50,i*40+50);
                spr.graphics.lineTo(250,i*40+50);
            }
            
            for(i = 0; i < 6; i++){
                for(j = 0; j < 6; j++){
                    if(isi[i][j] == 0) continue;
                    
                    if(cal_color(isi[i][j]) == 1){
                        spr.graphics.lineStyle();
                        spr.graphics.beginFill(0x000000);
                        spr.graphics.drawCircle(i*40+50,j*40+50,19);
                        spr.graphics.endFill();
                    } else {
                        spr.graphics.lineStyle(1,0x000000);
                        spr.graphics.beginFill(0xffffff);
                        spr.graphics.drawCircle(i*40+50,j*40+50,19);
                        spr.graphics.endFill();    
                    }
                    
                    if(isi[i][j] >= (turn>>1<<1)){
                        spr.graphics.lineStyle();
                        spr.graphics.beginFill(0xff0000);
                        spr.graphics.drawCircle(i*40+50,j*40+50,3);
                        spr.graphics.endFill();
                    }
                }
            }
            
            
            spr.graphics.lineStyle();
            spr.graphics.beginFill(0x000000);
            spr.graphics.drawCircle(35,335,25);
            spr.graphics.endFill();
            spr.graphics.lineStyle(1,0x000000);
            spr.graphics.beginFill(0xffffff);
            spr.graphics.drawCircle(35,395,25);
            spr.graphics.endFill();
            
            spr.graphics.lineStyle();
            spr.graphics.beginFill(0x000000);
            spr.graphics.drawCircle(335,pl0*25+60,8);
            spr.graphics.endFill();
            spr.graphics.lineStyle(1,0x000000);
            spr.graphics.beginFill(0xffffff);
            spr.graphics.drawCircle(335,pl1*25+260,8);
            spr.graphics.endFill();
        }
        
        
        public function cal_color(x:int):int{
            if(x<=0) return 0;
            return ((x>>1)&1)+1;
        }
        
        
        public function cal_score():void{
            var i:int, j:int, z:int;
            var c:Array = new Array(3);
            var bl:Boolean;
            score[0] = score[1] = 0;
            
            for(i = 0; i < sqs; i++){
                for(j = 0; j < 3; j++) c[j] = 0;
                bl = false;
                for(j = 0; j < 4; j++){
                    z = isi[datx[i][j]][daty[i][j]];
                    c[cal_color(z)]++;
                    if(z==turn) bl = true;
                }
                
                if(c[1] == 4){
                    score[0] += datp[i];
                    if(bl) play_efe(i,0);
                }
                if(c[2] == 4){
                    score[1] += datp[i];
                    if(bl) play_efe(i,1);
                }
            }
        }
        
        public function play_efe(n:int, teban:int):void{
            efe[n].init(n,teban);
            ten[n].init(n);
        }
        
        
        public function check_pl():Boolean{
            return (cal_color(turn+1)==1 && pl0==0) || (cal_color(turn+1)==2 && pl1==0);
        }
        
        
        public function put(x:int, y:int):void{
            if(isi[x][y] != 0) return;
            isi[x][y] = ++turn;
            se.play();
            cal_score();
        }
        
        public function pick():void{
            var i:int, j:int;
            while(turn > 0){
                for(i = 0; i < 6; i++)for(j = 0; j < 6; j++){
                    if(isi[i][j] == turn) isi[i][j] = 0;
                }
                turn--;
                
                if(cal_color(turn+1)==1 && pl0==0) break;
                if(cal_color(turn+1)==2 && pl1==0) break;
            }
            cal_score();
            
            nowsc[0] = score[0]; addsc[0] = 0;
            nowsc[1] = score[1]; addsc[1] = 0;
            for(i = 0; i < sqs; i++){
                efe[i].kill();
            }
        }
        
        
        public function text_hit(t:TextField, x:int, y:int):Boolean{
            return x>t.x && x<t.x+t.width && y>t.y && y<t.y+t.height;
        }
        
        public function down(e:MouseEvent):void{
            var x:int = spr.mouseX, y:int = spr.mouseY;
            if(x>30&&x<270&&y>30&&y<270){
                if(check_pl()) put((x-30)/40,(y-30)/40);
            }
            
            if(text_hit(tfinit,x,y)) init();
            if(text_hit(tfundo,x,y)) pick();
            
            var i:int, j:int;
            for(i = 0; i < 5; i++){
                if(text_hit(tfpl[0][i],x,y)) pl0 = i;
                if(text_hit(tfpl[1][i],x,y)) pl1 = i;
            }
            frame();
        }
        
        
        //以下com
        
        public var isic:Array = new Array(6);
        public var val:Array = new Array(2);
        public var per:Array = new Array(4);
        public var vs:int;
        public var vx:Array = new Array(40);
        public var vy:Array = new Array(40);
        
        public function cal_val():void{
            var i:int, j:int;
            var c:Array = new Array(3);
            val[0] = val[1] = 0.0;
            
            for(i = 0; i < sqs; i++){
                for(j = 0; j < 3; j++) c[j] = 0;
                for(j = 0; j < 4; j++){
                    c[cal_color(isic[datx[i][j]][daty[i][j]])]++;
                }
                
                if(c[1]>0&&c[2]>0) continue;
                if(c[1]>0){
                    val[0] += per[c[0]]*datp[i];
                }
                if(c[2]>0){
                    val[1] += per[c[0]]*datp[i];
                }
            }
        }
        
        
        public function com():void{
            var i:int, j:int, teban:int = cal_color(turn+1)-1, lv:int;
            var max1:int, max2:int, maxv:Number, nowv:Number;
            
            lv = (teban==0?pl0:pl1);
            
            per[0] = 1.0;
            switch(lv){
            case 1:
                per[1] = Math.random();
                per[2] = Math.random();
                per[3] = Math.random();
                break;
            case 3:
                per[1] = 0.8 + Math.random()*0.1;
                per[2] = 0.2 + Math.random()*0.1;
                per[3] = 0.1 + Math.random()*0.1;
                break;
            default:
                per[1] = 0.45 + Math.random()*0.1;
                per[2] = 0.2 + Math.random()*0.1;
                per[3] = 0.1 + Math.random()*0.05;
                break;
            }
            
            isic = isi;
            vs = 0;
            for(i = 0; i < 6; i++){
                for(j= 0; j < 6; j++){
                    if(isic[i][j] == 0){
                        vx[vs] = i;
                        vy[vs++] = j;
                    }
                }
            }
            
            if(turn == 0){
                nextc = 1; nextw = wait;
                nextx[1] = 1; nexty[1] = 1;
                return;
            } else if(turn == 35){
                nextc = 1; nextw = wait;
                nextx[1] = vx[0]; nexty[1] = vy[0];
                return;
            }
            
            max1 = 0;
            max2 = 1;
            maxv = -100000;
            for(i = 1; i < vs; i++){
                for(j= 0; j < i; j++){
                    isic[vx[i]][vy[i]] = turn+1; isic[vx[j]][vy[j]] = turn+2;
                    cal_val();
                    nowv = val[teban]-val[teban^1];
                    if(lv == 1) nowv *= Math.random();
                    if(lv == 2) nowv = val[teban];
                    isic[vx[i]][vy[i]] = 0; isic[vx[j]][vy[j]] = 0;
                    if(nowv > maxv){ max1 = i; max2 = j; maxv = nowv;}
                }
            }
            
            nextc = 0; nextw = wait;
            nextx[0] = vx[max1]; nexty[0] = vy[max1];
            nextx[1] = vx[max2]; nexty[1] = vy[max2];
        }
    }
    
    
}


class X{
    public static const sqs:int = 105;
    public static const datx:Array = [
    [0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0],[1,2,2,1],[1,2,2,1],[1,2,2,1],[1,2,2,1],[1,2,2,1],[2,3,3,2],[2,3,3,2],[2,3,3,2],[2,3,3,2],[2,3,3,2],[3,4,4,3],[3,4,4,3],[3,4,4,3],[3,4,4,3],[3,4,4,3],[4,5,5,4],[4,5,5,4],[4,5,5,4],[4,5,5,4],[4,5,5,4],
    [0,1,2,1],[0,1,2,1],[0,1,2,1],[0,1,2,1],[1,2,3,2],[1,2,3,2],[1,2,3,2],[1,2,3,2],[2,3,4,3],[2,3,4,3],[2,3,4,3],[2,3,4,3],[3,4,5,4],[3,4,5,4],[3,4,5,4],[3,4,5,4],
    [0,2,2,0],[0,2,2,0],[0,2,2,0],[0,2,2,0],[1,3,3,1],[1,3,3,1],[1,3,3,1],[1,3,3,1],[2,4,4,2],[2,4,4,2],[2,4,4,2],[2,4,4,2],[3,5,5,3],[3,5,5,3],[3,5,5,3],[3,5,5,3],
    [0,2,3,1],[0,2,3,1],[0,2,3,1],[1,3,4,2],[1,3,4,2],[1,3,4,2],[2,4,5,3],[2,4,5,3],[2,4,5,3],
    [0,1,3,2],[0,1,3,2],[0,1,3,2],[1,2,4,3],[1,2,4,3],[1,2,4,3],[2,3,5,4],[2,3,5,4],[2,3,5,4],
    [0,2,4,2],[0,2,4,2],[1,3,5,3],[1,3,5,3],
    [0,3,3,0],[0,3,3,0],[0,3,3,0],[1,4,4,1],[1,4,4,1],[1,4,4,1],[2,5,5,2],[2,5,5,2],[2,5,5,2],
    [0,3,4,1],[0,3,4,1],[1,4,5,2],[1,4,5,2],
    [0,1,4,3],[0,1,4,3],[1,2,5,4],[1,2,5,4],
    [0,3,5,2],[0,2,5,3],
    [0,4,4,0],[0,4,4,0],[1,5,5,1],[1,5,5,1],
    [0,4,5,1],[0,1,5,4],[0,5,5,0]];
    public static const daty:Array = [
    [0,0,1,1],[1,1,2,2],[2,2,3,3],[3,3,4,4],[4,4,5,5],[0,0,1,1],[1,1,2,2],[2,2,3,3],[3,3,4,4],[4,4,5,5],[0,0,1,1],[1,1,2,2],[2,2,3,3],[3,3,4,4],[4,4,5,5],[0,0,1,1],[1,1,2,2],[2,2,3,3],[3,3,4,4],[4,4,5,5],[0,0,1,1],[1,1,2,2],[2,2,3,3],[3,3,4,4],[4,4,5,5],
    [1,0,1,2],[2,1,2,3],[3,2,3,4],[4,3,4,5],[1,0,1,2],[2,1,2,3],[3,2,3,4],[4,3,4,5],[1,0,1,2],[2,1,2,3],[3,2,3,4],[4,3,4,5],[1,0,1,2],[2,1,2,3],[3,2,3,4],[4,3,4,5],
    [0,0,2,2],[1,1,3,3],[2,2,4,4],[3,3,5,5],[0,0,2,2],[1,1,3,3],[2,2,4,4],[3,3,5,5],[0,0,2,2],[1,1,3,3],[2,2,4,4],[3,3,5,5],[0,0,2,2],[1,1,3,3],[2,2,4,4],[3,3,5,5],
    [1,0,2,3],[2,1,3,4],[3,2,4,5],[1,0,2,3],[2,1,3,4],[3,2,4,5],[1,0,2,3],[2,1,3,4],[3,2,4,5],
    [2,0,1,3],[3,1,2,4],[4,2,3,5],[2,0,1,3],[3,1,2,4],[4,2,3,5],[2,0,1,3],[3,1,2,4],[4,2,3,5],
    [2,0,2,4],[3,1,3,5],[2,0,2,4],[3,1,3,5],
    [0,0,3,3],[1,1,4,4],[2,2,5,5],[0,0,3,3],[1,1,4,4],[2,2,5,5],[0,0,3,3],[1,1,4,4],[2,2,5,5],
    [1,0,3,4],[2,1,4,5],[1,0,3,4],[2,1,4,5],
    [3,0,1,4],[4,1,2,5],[3,0,1,4],[4,1,2,5],
    [2,0,3,5],[3,0,2,5],
    [0,0,4,4],[1,1,5,5],[0,0,4,4],[1,1,5,5],
    [1,0,4,5],[4,0,1,5],[0,0,5,5]];
    public static const datp:Array = [
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
    5,5,5,5,5,5,5,5,5,
    5,5,5,5,5,5,5,5,5,
    8,8,8,8,
    9,9,9,9,9,9,9,9,9,
    10,10,10,10,
    10,10,10,10,
    13,13,
    16,16,16,16,
    17,17,25];
    public static const col:Array = [,0x969696,0xc89600,,0x0000ff,0x00ff00,,,0xffff00,0x00ffff,0xff0000,
    ,,0xff00ff,,,0x96ff00,0x00ff96,,,,,,,,0xd2c8c8];
}



import flash.display.Sprite;
import flash.filters.GlowFilter;
import flash.text.*;

class Effect extends Sprite {
    public var te:int;
    public var tx:Number, ty:Number, ax:Number, ay:Number, dx:Number, dy:Number, a:Number;
    public var live:Boolean;
    
    public function Effect(){
    }
    
    public function init(n:int,teban:int):void{
        te = teban;
        graphics.lineStyle(2, X.col[X.datp[n]]);
        live = true;
        
        x = (X.datx[n][0]+X.datx[n][2])*20+50; y = (X.daty[n][0]+X.daty[n][2])*20+50;
        tx = 35; ty = teban*60+335;
        dx = tx-x; dy = ty-y; a = Math.sqrt(dx*dx+dy*dy);
        ax = dx/a; ay = dy/a; a = -6;
        scaleX = scaleY = 1; rotation = 0; alpha = 0;
        
        var i:int = 1;
        graphics.moveTo((X.datx[n][0]*40+50)-x,(X.daty[n][0]*40+50)-y);
        while(1){
            graphics.lineTo((X.datx[n][i]*40+50)-x,(X.daty[n][i]*40+50)-y);
            i = (i+1)%4;
            if(i == 1) break;
        }
        
        filters = [new GlowFilter(X.col[X.datp[n]],1,10,10)];
    }
    
    public function move():Boolean{
        a += 1;
        if(a < 0){
            alpha += 0.2;
        } else {
            x += ax*a;
            y += ay*a;
            rotation += a;
            scaleX = scaleY = (tx-x)/dx;
            if(y > ty){ kill(); return true;}
        }
        return false;
    }
    
    public function kill():void{
        live = false;
        graphics.clear();
    }
}


class Ten extends Sprite {
    public var live:Boolean, a:int;
    public var tf:TextField;
    
    public function Ten(n:int){
        tf = new TextField();
        tf.autoSize = TextFieldAutoSize.CENTER;
        tf.x = 0; tf.y = -12; tf.width = 0;
        tf.textColor = X.col[X.datp[n]];
        tf.htmlText = "<font size='24'>"+X.datp[n]+"</font>";
        tf.selectable = false;
        addChild(tf);
    }
    
    public function init(n:int):void{
        live = true;
        x = (X.datx[n][0]+X.datx[n][2])*20+50; y = (X.daty[n][0]+X.daty[n][2])*20+50;
        scaleX = scaleY = 1;
        alpha = 0;
        a = -6;
    }
    
    public function move():void{
        a += 1;
        if(a>0){
            alpha -= 0.05;
            scaleX -= 0.03; scaleY -= 0.03;
            y -= 5;
            if(scaleX < 0) kill();
        } else {
            alpha += 0.2;
            y += 1;
        }
    }
    
    public function kill():void{
        live = false;
        scaleX = scaleY = 0;
    }
}