/**
* Copyright Zahurdias.Calimero ( http://wonderfl.net/user/Zahurdias.Calimero )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/mvGg
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.ui.*;
import flash.utils.*;
public class Main extends Sprite
{
private var pt:TextField;
private var nbD:int = 45;
private var score:int = 0;
private var gTimer:Timer;
private var ii:int;
private var nbBt:TextField;
private var nbc:int;
private var tfb:TextFormat;
private var gridcopy:Array;
private var bc:int;
private var firstGame:Boolean = true;
private var grid:Array;
private var jj:int;
private var gOver:Boolean = false;
private var gDelay:int = 500;
private var st:TextField;
private var b:Shape;
private var nbDTmp:int = 0;
private var nbDScore:int = 0;
private var gWin:Boolean = false;
private var lc:Sprite;
private var isMoveable:Boolean = true;
public function Main()
{
nbc = Math.floor((1 + (Math.random() * 4)));
grid = new Array(11);
gridcopy = new Array(11);
lc = new Sprite();
gTimer = new Timer(gDelay);
pt = new TextField();
nbBt = new TextField();
st = new TextField();
tfb = new TextFormat("Arial", 14, 0, "bold", null, null, null, null, "center");
stage.scaleMode = StageScaleMode.NO_SCALE;
var border:Sprite = new Sprite();
border.graphics.lineStyle(1);
border.graphics.drawRect(0, 0, 350, 550);
stage.addChild(border);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addChild(pt);
pt.selectable = false;
pt.text = "COLORSTACK\n\n\nā, ā, ā to move\n\n4 tiles = GOOD\nmore tiles = BAD\n\n\npress [S] to start";
pt.width = 350;
pt.height = 300;
pt.setTextFormat(tfb);
pt.x = 0;
pt.y = 150;
}
private function setLabel(li:int, co:int, cr:int, c:int):void
{
gridcopy[li][co] = cr;
if ((((((li > 0)) && ((grid[(li - 1)][co] == c)))) && ((gridcopy[(li - 1)][co] == 0))))
setLabel((li - 1), co, cr, c);
if ((((((li < 10)) && ((grid[(li + 1)][co] == c)))) && ((gridcopy[(li + 1)][co] == 0))))
setLabel((li + 1), co, cr, c);
if ((((((co > 0)) && ((grid[li][(co - 1)] == c)))) && ((gridcopy[li][(co - 1)] == 0))))
setLabel(li, (co - 1), cr, c);
if ((((((co < 6)) && ((grid[li][(co + 1)] == c)))) && ((gridcopy[li][(co + 1)] == 0))))
setLabel(li, (co + 1), cr, c);
}
private function keyPressed(e:KeyboardEvent):void
{
var li:int, co:int, pB:int, cB:int;
if ((((e.keyCode == 83)) && (firstGame)))
{
firstGame = false;
startGame();
}
if ((((e.keyCode == Keyboard.LEFT)) && (isMoveable)))
{
if ((((jj > 0)) && ((grid[ii][(jj - 1)] == 0))))
{
jj--;
b.x = (jj * 50);
}
}
if ((((e.keyCode == Keyboard.RIGHT)) && (isMoveable)))
{
if ((((jj < 6)) && ((grid[ii][(jj + 1)] == 0))))
{
jj++;
b.x = (jj * 50);
}
}
if ((((e.keyCode == Keyboard.DOWN)) && (isMoveable)))
{
while ((((ii < 10)) && ((grid[(ii + 1)][jj] == 0))))
ii++;
b.y = (ii * 50);
grid[ii][jj] = bc;
}
if ((((e.keyCode == 83)) && (gOver)))
{
stage.removeChild(pt);
li = 0;
while (li < 11)
{
grid[li] = new Array(7);
gridcopy[li] = new Array(7);
co = 0;
while (co < 7)
{
if (li < 8)
{
grid[li][co] = 0;
}
else
{
cB = Math.floor((Math.random() * 4));
while (cB == pB)
{
cB = Math.floor((Math.random() * 4));
}
grid[li][co] = cB;
pB = cB;
}
gridcopy[li][co] = 0;
co++;
}
li++;
}
update();
draw();
stage.focus = this;
gOver = false;
gDelay = 500;
nbD = 45;
nbDTmp = 0;
nbBt.text = ("Blocks to remove\n" + nbD);
nbBt.setTextFormat(tfb);
score = 0;
st.text = ("Score\n" + score);
st.setTextFormat(tfb);
gTimer.delay = gDelay;
initBloc();
gTimer.start();
}
}
private function draw():void
{
var li:int, co:int;
var tmp:Shape;
while (lc.numChildren > 0)
{
lc.removeChildAt(0);
}
li = 0;
while (li < 11)
{
co = 0;
while (co < 7)
{
if (grid[li][co] != 0)
{
tmp = newBloc(grid[li][co]);
tmp.x = (co * 50);
tmp.y = (li * 50);
lc.addChild(tmp);
}
co++;
}
li++;
}
}
private function update():void
{
var co:int, li:int, tmp:int;
co = 0;
while (co < 7)
{
li = 9;
while (li >= 0)
{
if (grid[li][co] != 0)
{
tmp = li;
while ((((tmp < 10)) && ((grid[(tmp + 1)][co] == 0))))
{
grid[(tmp + 1)][co] = grid[tmp][co];
grid[tmp][co] = 0;
tmp++;
}
}
li--;
}
co++;
}
}
private function nextLevel():void
{
var li:int, co:int, cB:int, test:int;
var pB:int = 1;
nbDTmp = 0;
gDelay = (gDelay - (0.2 * gDelay));
gTimer.delay = gDelay;
co = 0;
while (co < 7)
{
test = (test + grid[0][co]);
co++;
}
if (test > 0) gOver = true;
li = 1;
while (li < 11)
{
co = 0;
while (co < 7)
{
grid[(li - 1)][co] = grid[li][co];
co++;
}
if (li == 10)
{
}
co = 0;
while (co < 7)
{
cB = Math.floor((1 + (Math.random() * 4)));
while ((((cB == pB)) || ((cB == grid[(li - 1)][co])))) cB = Math.floor((1 + (Math.random() * 4)));
grid[li][co] = cB;
pB = cB;
co++;
}
li++;
}
}
private function initBloc():void
{
var tmp:Shape;
nbBt.text = ("Blocks to remove\n" + nbD);
nbBt.setTextFormat(tfb);
if (nbDScore > 0)
{
score = (score + Math.pow(10, nbDScore));
nbDScore = 0;
st.text = ("Score\n" + score);
st.setTextFormat(tfb);
}
if (nbD <= 0)
{
gWin = true;
gOver = true;
gTimer.stop();
while (lc.numChildren > 0) lc.removeChildAt(0);
stage.addChild(pt);
pt.text = "You Won !\n\n[S] to try again";
pt.width = 350;
pt.setTextFormat(tfb);
pt.x = 0;
pt.y = 150;
}
if (((!(gOver)) && ((grid[0][3] == 0))))
{
bc = nbc;
nbc = Math.floor((1 + (Math.random() * 4)));
tmp = newBloc(nbc);
tmp.x = 400;
tmp.y = 50;
stage.addChild(tmp);
b = newBloc(bc);
lc.addChild(b);
ii = 0;
jj = 3;
b.x = (jj * 50);
b.y = (ii * 50);
isMoveable = true;
}
else
{
if (!gWin)
{
gOver = true;
gTimer.stop();
while (lc.numChildren > 0)
lc.removeChildAt(0);
stage.addChild(pt);
pt.text = "Game Over...\n\n[S] to try again";
pt.width = 350;
pt.setTextFormat(tfb);
pt.x = 0;
pt.y = 150;
}
}
}
private function newBloc(bc:Number):Shape
{
var tmp:Shape = new Shape();
var c:uint = 0xFF8000;
c = ((bc) == 1) ? 0xFF8000 : c;
c = ((bc) == 2) ? 0xFF : c;
c = ((bc) == 3) ? 0xFF0000 : c;
c = ((bc) == 4) ? 0xFFFF00 : c;
c = ((bc) == 10) ? 0xC8C8C8 : c;
tmp.graphics.lineStyle(2, 0);
tmp.graphics.beginFill(c);
tmp.graphics.drawRoundRect(0, 0, 50, 50, 20, 20);
tmp.graphics.endFill();
return (tmp);
}
private function analyse():Boolean
{
var li:int;
var co:int;
var c:int;
var nbr:int;
var nbpix:int;
var gridModif:Boolean;
var cr:int = 1;
li = 0;
while (li < 11)
{
co = 0;
while (co < 7)
{
gridcopy[li][co] = 0;
co++;
}
li++;
}
li = 0;
while (li < 11)
{
co = 0;
while (co < 7)
{
if ((((((grid[li][co] > 0)) && ((grid[li][co] < 5)))) && ((gridcopy[li][co] == 0))))
{
c = grid[li][co];
setLabel(li, co, cr, c);
cr++;
}
co++;
}
li++;
}
nbr = 1;
while (nbr <= cr)
{
nbpix = 0;
li = 0;
while (li < 11)
{
co = 0;
while (co < 7)
{
if (gridcopy[li][co] == nbr) nbpix = (nbpix + 1);
co++;
}
li++;
}
if (nbpix == 4)
{
co = 0;
while (co < 7)
{
li = 0;
while (li < 11)
{
if (gridcopy[li][co] == nbr) grid[li][co] = 0;
li++;
}
co++;
}
gridModif = true;
nbD--;
nbDTmp++;
nbDScore++;
if (nbDTmp == 5)
{
nextLevel();
}
}
if (nbpix > 4)
{
score = Math.floor((score / 2));
st.text = ("Score\n" + score);
st.setTextFormat(tfb);
li = 0;
while (li < 11)
{
co = 0;
while (co < 7)
{
if (gridcopy[li][co] == nbr) grid[li][co] = 10;
co++;
}
li++;
}
}
nbr++;
}
return (gridModif);
}
private function startGame():void
{
var li:int;
var co:int;
var pB:int;
var cB:int;
stage.removeChild(pt);
li = 0;
while (li < 11)
{
grid[li] = new Array(7);
gridcopy[li] = new Array(7);
co = 0;
while (co < 7)
{
if (li < 8)
{
grid[li][co] = 0;
}
else
{
cB = Math.floor((Math.random() * 4));
while (cB == pB)
{
cB = Math.floor((Math.random() * 4));
}
grid[li][co] = cB;
pB = cB;
}
gridcopy[li][co] = grid[li][co];
co++;
}
li++;
}
update();
while (analyse())
update();
draw();
nbD = 45;
nbDTmp = 0;
var nxtBt:TextField = new TextField();
nxtBt.x = 375;
nxtBt.y = 25;
nxtBt.text = "Next Tile";
nxtBt.setTextFormat(tfb);
stage.addChild(nxtBt);
nbBt.x = 350;
nbBt.y = 180;
nbBt.width = 150;
nbBt.text = ("Blocks to remove\n" + nbD);
nbBt.setTextFormat(tfb);
stage.addChild(nbBt);
st.x = 350;
st.y = 280;
st.width = 150;
st.text = ("Score\n" + score);
st.setTextFormat(tfb);
stage.addChild(st);
stage.addChild(lc);
initBloc();
stage.focus = this;
gTimer.addEventListener(TimerEvent.TIMER, mainLoop);
gTimer.start();
}
private function mainLoop(e:TimerEvent):void
{
if (isMoveable)
{
if ((((ii < 10)) && ((grid[(ii + 1)][jj] == 0))))
{
ii++;
b.y = (b.y + 50);
}
else
{
grid[ii][jj] = bc;
isMoveable = false;
}
}
else
{
if (analyse())
{
draw();
update();
}
else
{
draw();
initBloc();
}
}
}
}
}