home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2003 January
/
ChipCD_1.03.iso
/
oddech
/
bechpack
/
bechpack.exe
/
scripts
/
frame_38
/
DoAction_2.as
next >
Wrap
Text File
|
2002-10-29
|
3KB
|
145 lines
function PieceX(col)
{
return xBase + col * xSpace;
}
function PieceY(row)
{
return yBase + row * ySpace;
}
function Initialize()
{
Congratulations.stop();
numRows = 4;
numCols = 4;
numCells = numRows * numCols;
xBase = p0._x;
yBase = p0._y;
xSpace = p0._width * 1.07;
ySpace = p0._height * 1.07;
var c = 1;
var r = 0;
var i = 1;
while(i < numCells - 1)
{
var name = "p" + i;
p0.duplicateMovieClip(name,i);
var newPiece = eval(name);
newPiece._x = PieceX(c);
newPiece._y = PieceY(r);
newPiece.PieceNumber = i + 1;
if(++c >= numCols)
{
c = 0;
r++;
}
i++;
}
posArray = [];
i = 0;
while(i < numCells - 1)
{
posArray[i] = i;
i++;
}
empty = numCells - 1;
}
function isWinner()
{
var i = 0;
while(i < numCells - 1)
{
if(posArray[i] != i)
{
return false;
}
i++;
}
return true;
}
function Click(clicked)
{
clicked--;
if(isWinner())
{
shuffle();
Congratulations.gotoAndStop(1);
}
else
{
pos = posArray[clicked];
emptyRow = Math.floor(empty / numCols);
emptyCol = empty - emptyRow * numCols;
clickedRow = Math.floor(pos / numCols);
clickedCol = pos - clickedRow * numCols;
rowDiff = Math.abs(clickedRow - emptyRow);
colDiff = Math.abs(clickedCol - emptyCol);
adjacent = rowDiff + colDiff == 1;
if(adjacent)
{
var piece = eval("/p" + clicked);
piece._x = PieceX(emptyCol);
piece._y = PieceY(emptyRow);
posArray[clicked] = empty;
empty = pos;
if(isWinner())
{
cz - 15 - gratulace.play();
}
}
}
}
function Shuffle()
{
var cf = function(x, y)
{
if(x[1] < y[1])
{
return -1;
}
if(y[1] < x[1])
{
return 1;
}
return 0;
};
var i;
var cell = [];
i = 0;
while(i < numCells)
{
cell.push([i,Math.random()]);
i++;
}
cell.sort(cf);
var r = 0;
var c = 0;
i = 0;
while(i < numCells)
{
var piece = cell[i][0];
if(piece == numCells - 1)
{
empty = i;
}
else
{
posArray[piece] = i;
var p = eval("/p" + piece);
p._x = PieceX(c);
p._y = PieceY(r);
}
if(++c >= numCols)
{
c = 0;
r++;
}
i++;
}
}
if(!initialized)
{
Initialize();
initialized = true;
}
Mouse.show();